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.
  • Loading branch information
sp1ritCS committed Nov 19, 2024
1 parent 6f67b10 commit ef74545
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions mesonbuild/compilers/mixins/clike.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,17 @@ 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 expresseion and evaluate it on the build machines compiler
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[mesonlib.MachineChoice.BUILD]['c'].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)

# If no bounds are given, compute them in the limit of int32
maxint = 0x7fffffff
minint = -0x80000000
Expand Down

0 comments on commit ef74545

Please sign in to comment.