From e8c3fb1d11aff9970a0ba9b8806f4adc2d7b0144 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20=22sp1rit=22=E2=80=8B?= Date: Tue, 19 Nov 2024 12:49:17 +0100 Subject: [PATCH] compilers/clike: Speedup cross_compute_int 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 --- mesonbuild/compilers/mixins/clike.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/mesonbuild/compilers/mixins/clike.py b/mesonbuild/compilers/mixins/clike.py index a75cd2a81bf0..19a6bb4875af 100644 --- a/mesonbuild/compilers/mixins/clike.py +++ b/mesonbuild/compilers/mixins/clike.py @@ -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 + #include + 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