Skip to content

Commit

Permalink
compilers/clike: Speedup cross_compute_int
Browse files Browse the repository at this point in the history
Expand the expression passed into cross_compute_int using the
preprocessor first and then try to evaluate the expanded expression
using the host machine compiler and test if the result is valid.

Co-authored-by: Charles Brunet <[email protected]>
  • Loading branch information
sp1ritCS and bruchar1 committed Jan 13, 2025
1 parent a86476c commit 0834eb0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
15 changes: 15 additions & 0 deletions mesonbuild/compilers/mixins/clike.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,21 @@ def cross_compute_int(self, expression: str, low: T.Optional[int], high: T.Optio
if self._compile_int(f'{expression} == {guess}', prefix, env, extra_args, dependencies):
return guess

# Try to expand the expression and evaluate it on the build machines compiler
if self.language in env.coredata.compilers.build:
try:
expanded, _ = self.get_define(expression, prefix, env, extra_args, dependencies, False)
evaluate_expanded = f'''
#include <stdio.h>
#include <stdint.h>
int main(void) {{ int expression = {expanded}; printf("%d", expression); return 0; }}'''
run = env.coredata.compilers.build[self.language].run(evaluate_expanded, env)
if run and run.compiled and run.returncode == 0:
if self._compile_int(f'{expression} == {run.stdout}', prefix, env, extra_args, dependencies):
return int(run.stdout)
except mesonlib.EnvironmentException:
pass

# If no bounds are given, compute them in the limit of int32
maxint = 0x7fffffff
minint = -0x80000000
Expand Down
9 changes: 9 additions & 0 deletions unittests/linuxcrosstests.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ def test_cflags_cross_environment_pollution(self):
compdb = self.get_compdb()
self.assertNotIn('-DBUILD_ENVIRONMENT_ONLY', compdb[0]['command'])

def test_cross_compute_int(self):
'''
Test that compute int works even in environments that that do not have
exe_wrapper to run built executables.
'''
testdir = os.path.join(self.common_test_dir, '134 compute int')
self.init(testdir)
self.build()

def test_cross_file_overrides_always_args(self):
'''
Test that $lang_args in cross files always override get_always_args().
Expand Down

0 comments on commit 0834eb0

Please sign in to comment.