Skip to content

Commit c2d9805

Browse files
Run cc instead of cpp
Run a C compiler, not just the preprocessor. This allows capturing static assertions (`MBEDTLS_STATIC_ASSERT`). Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
1 parent ba4429d commit c2d9805

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

scripts/mbedtls_framework/unittest_config_checks.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
This tests the output of ``generate_config_checks.py``.
44
This can also let us verify what we enforce in the manually written
5-
checks in ``<PROJECT>_check_config.h``.
5+
checks in ``<PROJECT>_check_config.h`` and ``<PROJECT>_config.c``.
66
"""
77

88
## Copyright The Mbed TLS Contributors
@@ -29,27 +29,27 @@ class TestConfigChecks(unittest.TestCase):
2929
PROJECT_SPECIFIC_INCLUDE_DIRECTORIES = [] #type: List[str]
3030

3131
# Increase the length of strings that assertion failures are willing to
32-
# print. This is useful for failures where the preprocessor has a lot
32+
# print. This is useful for failures where the compiler has a lot
3333
# to say.
3434
maxDiff = 9999
3535

3636
def setUp(self) -> None:
37-
self.cpp_output = None #type: Optional[str]
37+
self.cc_output = None #type: Optional[str]
3838

3939
def tearDown(self) -> None:
40-
"""Log the preprocessor output to a file, if available and desired.
40+
"""Log the compiler output to a file, if available and desired.
4141
4242
This is intended for debugging. It only happens if the environment
4343
variable UNITTEST_CONFIG_CHECKS_DEBUG is non-empty.
4444
"""
4545
if os.getenv('UNITTEST_CONFIG_CHECKS_DEBUG'):
46-
# We set self.cpp_output to the preprocessor output before
46+
# We set self.cc_output to the compiler output before
4747
# asserting, and set it to None if all the assertions pass.
48-
if self.cpp_output is not None:
48+
if self.cc_output is not None:
4949
basename = os.path.splitext(os.path.basename(sys.argv[0]))[0]
5050
filename = f'{basename}.{self._testMethodName}.out.txt'
5151
with open(filename, 'w') as out:
52-
out.write(self.cpp_output)
52+
out.write(self.cc_output)
5353

5454
def user_config_file_name(self, variant: str) -> str:
5555
"""Construct a unique temporary file name for a user config header."""
@@ -81,12 +81,12 @@ def run_with_config_files(self,
8181
mbedtls_user_config_file: Optional[str],
8282
extra_options: List[str],
8383
) -> subprocess.CompletedProcess:
84-
"""Run cpp with the given user configuration files.
84+
"""Run cc with the given user configuration files.
8585
8686
Return the CompletedProcess object capturing the return code,
8787
stdout and stderr.
8888
"""
89-
cmd = ['cpp']
89+
cmd = [os.getenv('CC', 'cc')]
9090
if os.getenv('UNITTEST_CONFIG_CHECKS_DEBUG'):
9191
cmd += ['-dD']
9292
if crypto_user_config_file is not None:
@@ -99,7 +99,7 @@ def run_with_config_files(self,
9999
cmd += ['-Iinclude',
100100
'-I.',
101101
'-I' + os.path.dirname(self.PROJECT_CONFIG_C)]
102-
cmd.append(self.PROJECT_CONFIG_C)
102+
cmd += ['-o', os.devnull, '-c', self.PROJECT_CONFIG_C]
103103
return subprocess.run(cmd,
104104
check=False,
105105
encoding='utf-8',
@@ -111,7 +111,7 @@ def run_with_config(self,
111111
mbedtls_user_config: Optional[str] = None,
112112
extra_options: Optional[List[str]] = None,
113113
) -> subprocess.CompletedProcess:
114-
"""Run cpp with the given content for user configuration files.
114+
"""Run cc with the given content for user configuration files.
115115
116116
Return the CompletedProcess object capturing the return code,
117117
stdout and stderr.
@@ -149,41 +149,41 @@ def good_case(self,
149149
mbedtls_user_config: Optional[str] = None,
150150
extra_options: Optional[List[str]] = None,
151151
) -> None:
152-
"""Run cpp with the given user config(s). Expect no error.
152+
"""Run cc with the given user config(s). Expect no error.
153153
154-
Pass extra_options on the command line of cpp.
154+
Pass extra_options on the command line of cc.
155155
"""
156156
cp = self.run_with_config(crypto_user_config, mbedtls_user_config,
157157
extra_options=extra_options)
158158
# Assert the error text before the status. That way, if it fails,
159159
# we see the unexpected error messages in the test log.
160-
self.cpp_output = cp.stdout
160+
self.cc_output = cp.stdout
161161
self.assertEqual(cp.stderr, '')
162162
self.assertEqual(cp.returncode, 0)
163-
self.cpp_output = None
163+
self.cc_output = None
164164

165165
def bad_case(self,
166166
crypto_user_config: Optional[str],
167167
mbedtls_user_config: Optional[str] = None,
168168
error: Optional[Union[str, Pattern]] = None,
169169
extra_options: Optional[List[str]] = None,
170170
) -> None:
171-
"""Run cpp with the given user config(s). Expect errors.
171+
"""Run cc with the given user config(s). Expect errors.
172172
173-
Pass extra_options on the command line of cpp.
173+
Pass extra_options on the command line of cc.
174174
175-
If error is given, the standard error from cpp must match this regex.
175+
If error is given, the standard error from cc must match this regex.
176176
"""
177177
cp = self.run_with_config(crypto_user_config, mbedtls_user_config,
178178
extra_options=extra_options)
179-
self.cpp_output = cp.stdout
179+
self.cc_output = cp.stdout
180180
if error is not None:
181181
# Assert the error text before the status. That way, if it fails,
182182
# we see the unexpected error messages in the test log.
183183
self.assertRegex(cp.stderr, error)
184184
self.assertGreater(cp.returncode, 0)
185185
self.assertLess(cp.returncode, 126)
186-
self.cpp_output = None
186+
self.cc_output = None
187187

188188
# Nominal case, run first
189189
def test_01_nominal(self) -> None:

0 commit comments

Comments
 (0)