From ddb09e4f176cd8660767f4be04be59d03de108bc Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Wed, 18 Jan 2023 11:52:56 +0000 Subject: [PATCH 1/5] c_build_helper.py: Move compile to helper Move compilation to a separate helper function in c_build_helper.py to allow more generic use. Signed-off-by: David Horstmann --- scripts/mbedtls_dev/c_build_helper.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/scripts/mbedtls_dev/c_build_helper.py b/scripts/mbedtls_dev/c_build_helper.py index 5c587a16bb..b035dff8fc 100644 --- a/scripts/mbedtls_dev/c_build_helper.py +++ b/scripts/mbedtls_dev/c_build_helper.py @@ -89,6 +89,27 @@ int main(void) } ''') +def compile_c_file(c_filename, exe_filename, include_dirs): + cc = os.getenv('CC', 'cc') + cmd = [cc] + + proc = subprocess.Popen(cmd, + stdout=subprocess.DEVNULL, + stderr=subprocess.PIPE, + universal_newlines=True) + cc_is_msvc = 'Microsoft (R) C/C++' in proc.communicate()[1] + + cmd += ['-I' + dir for dir in include_dirs] + if cc_is_msvc: + # MSVC has deprecated using -o to specify the output file, + # and produces an object file in the working directory by default. + obj_filename = exe_filename[:-4] + '.obj' + cmd += ['-Fe' + exe_filename, '-Fo' + obj_filename] + else: + cmd += ['-o' + exe_filename] + + subprocess.check_call(cmd + [c_filename]) + def get_c_expression_values( cast_to, printf_format, expressions, @@ -123,10 +144,8 @@ def get_c_expression_values( expressions) ) c_file.close() - cc = os.getenv('CC', 'cc') - subprocess.check_call([cc] + - ['-I' + dir for dir in include_path] + - ['-o', exe_name, c_name]) + + compile_c_file(c_name, exe_name, include_path) if keep_c: sys.stderr.write('List of {} tests kept at {}\n' .format(caller, c_name)) From 0b1b97badf1d2396169982146359fdb6c526a6be Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Thu, 26 Jan 2023 19:11:57 +0000 Subject: [PATCH 2/5] Make c_build_helper module respect HOSTCC If HOSTCC is set, use that to generate files, otherwise use CC. This should make cross-compilation with generated files slightly easier. Signed-off-by: David Horstmann --- scripts/mbedtls_dev/c_build_helper.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/scripts/mbedtls_dev/c_build_helper.py b/scripts/mbedtls_dev/c_build_helper.py index b035dff8fc..805b8e5f06 100644 --- a/scripts/mbedtls_dev/c_build_helper.py +++ b/scripts/mbedtls_dev/c_build_helper.py @@ -90,7 +90,10 @@ int main(void) ''') def compile_c_file(c_filename, exe_filename, include_dirs): - cc = os.getenv('CC', 'cc') + # Respect $HOSTCC if it is set + cc = os.getenv('HOSTCC', None) + if cc is None: + cc = os.getenv('CC', 'cc') cmd = [cc] proc = subprocess.Popen(cmd, From 41fbc3ad3aec06a126be8d1d5d71ffa27184ef08 Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Fri, 27 Jan 2023 19:53:49 +0000 Subject: [PATCH 3/5] Add ChangeLog for c_build_helper improvements Signed-off-by: David Horstmann --- ChangeLog.d/c-build-helper-hostcc.txt | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 ChangeLog.d/c-build-helper-hostcc.txt diff --git a/ChangeLog.d/c-build-helper-hostcc.txt b/ChangeLog.d/c-build-helper-hostcc.txt new file mode 100644 index 0000000000..ebf77b3eb6 --- /dev/null +++ b/ChangeLog.d/c-build-helper-hostcc.txt @@ -0,0 +1,4 @@ +Features + * Use HOSTCC (if it is set) when compiling C code during generation of the + generated files. This allows them to be generated when CC is set for + cross compilation. From e28f2ee68548fa9055c62ba4c4a5a1ca67135348 Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Mon, 30 Jan 2023 09:50:59 +0000 Subject: [PATCH 4/5] Add docstring for new compile function. Signed-off-by: David Horstmann --- scripts/mbedtls_dev/c_build_helper.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/scripts/mbedtls_dev/c_build_helper.py b/scripts/mbedtls_dev/c_build_helper.py index 805b8e5f06..d76b746060 100644 --- a/scripts/mbedtls_dev/c_build_helper.py +++ b/scripts/mbedtls_dev/c_build_helper.py @@ -90,6 +90,13 @@ int main(void) ''') def compile_c_file(c_filename, exe_filename, include_dirs): + """Compile a C source file with the host compiler. + + * ``c_filename``: the name of the source file to compile. + * ``exe_filename``: the name for the executable to be created. + * ``include_dirs``: a list of paths to include directories to be passed + with the -I switch. + """ # Respect $HOSTCC if it is set cc = os.getenv('HOSTCC', None) if cc is None: From 5e29196561b90c2932e7c3df04bbe3220cbc48b0 Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Wed, 1 Feb 2023 13:39:57 +0000 Subject: [PATCH 5/5] Fix near-tautological repetition in ChangeLog Signed-off-by: David Horstmann --- ChangeLog.d/c-build-helper-hostcc.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ChangeLog.d/c-build-helper-hostcc.txt b/ChangeLog.d/c-build-helper-hostcc.txt index ebf77b3eb6..86182c3be5 100644 --- a/ChangeLog.d/c-build-helper-hostcc.txt +++ b/ChangeLog.d/c-build-helper-hostcc.txt @@ -1,4 +1,4 @@ Features * Use HOSTCC (if it is set) when compiling C code during generation of the - generated files. This allows them to be generated when CC is set for - cross compilation. + configuration-independent files. This allows them to be generated when + CC is set for cross compilation.