From 47d85425e8cd5275c2874cff840a1d32ad12b73d Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Wed, 6 Nov 2024 18:25:03 +0100 Subject: [PATCH] compilers: avoid one or more version_compare per target version_compare can take a few milliseconds. If you have a thousand object files or a multiple thereof, it adds up. Signed-off-by: Paolo Bonzini --- mesonbuild/backend/ninjabackend.py | 5 +++-- mesonbuild/compilers/mixins/gnu.py | 9 ++++++--- mesonbuild/compilers/vala.py | 3 ++- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py index 495d7ce500a3..658a19271229 100644 --- a/mesonbuild/backend/ninjabackend.py +++ b/mesonbuild/backend/ninjabackend.py @@ -623,6 +623,7 @@ def generate(self, capture: bool = False, vslite_ctx: T.Optional[T.Dict] = None) if ninja is None: raise MesonException('Could not detect Ninja v1.8.2 or newer') (self.ninja_command, self.ninja_version) = ninja + self.ninja_has_dyndeps = mesonlib.version_compare(self.ninja_version, '>=1.10.0') outfilename = os.path.join(self.environment.get_build_dir(), self.ninja_filename) tempfilename = outfilename + '~' with open(tempfilename, 'w', encoding='utf-8') as outfile: @@ -1089,7 +1090,7 @@ def generate_target(self, target) -> None: self.add_build(elem) def should_use_dyndeps_for_target(self, target: 'build.BuildTarget') -> bool: - if mesonlib.version_compare(self.ninja_version, '<1.10.0'): + if not self.ninja_has_dyndeps: return False if 'fortran' in target.compilers: return True @@ -2451,7 +2452,7 @@ def use_dyndeps_for_fortran(self) -> bool: '''Use the new Ninja feature for scanning dependencies during build, rather than up front. Remove this and all old scanning code once Ninja minimum version is bumped to 1.10.''' - return mesonlib.version_compare(self.ninja_version, '>=1.10.0') + return self.ninja_has_dyndeps def generate_fortran_dep_hack(self, crstr: str) -> None: if self.use_dyndeps_for_fortran(): diff --git a/mesonbuild/compilers/mixins/gnu.py b/mesonbuild/compilers/mixins/gnu.py index 62f55543a0a7..21a57b44fef1 100644 --- a/mesonbuild/compilers/mixins/gnu.py +++ b/mesonbuild/compilers/mixins/gnu.py @@ -550,16 +550,19 @@ def __init__(self, defines: T.Optional[T.Dict[str, str]]): super().__init__() self.defines = defines or {} self.base_options.update({OptionKey('b_colorout'), OptionKey('b_lto_threads')}) + self._has_color_support = mesonlib.version_compare(self.version, '>=4.9.0') + self._has_wpedantic_support = mesonlib.version_compare(self.version, '>=4.8.0') + self._has_lto_auto_support = mesonlib.version_compare(self.version, '>=10.0') def get_colorout_args(self, colortype: str) -> T.List[str]: - if mesonlib.version_compare(self.version, '>=4.9.0'): + if self._has_color_support: return gnu_color_args[colortype][:] return [] def get_warn_args(self, level: str) -> T.List[str]: # Mypy doesn't understand cooperative inheritance args = super().get_warn_args(level) - if mesonlib.version_compare(self.version, '<4.8.0') and '-Wpedantic' in args: + if not self._has_wpedantic_support and '-Wpedantic' in args: # -Wpedantic was added in 4.8.0 # https://gcc.gnu.org/gcc-4.8/changes.html args[args.index('-Wpedantic')] = '-pedantic' @@ -612,7 +615,7 @@ def get_prelink_args(self, prelink_name: str, obj_list: T.List[str]) -> T.List[s def get_lto_compile_args(self, *, threads: int = 0, mode: str = 'default') -> T.List[str]: if threads == 0: - if mesonlib.version_compare(self.version, '>= 10.0'): + if self._has_lto_auto_support: return ['-flto=auto'] # This matches clang's behavior of using the number of cpus return [f'-flto={multiprocessing.cpu_count()}'] diff --git a/mesonbuild/compilers/vala.py b/mesonbuild/compilers/vala.py index a1d57b38cb8e..430b1598a9b1 100644 --- a/mesonbuild/compilers/vala.py +++ b/mesonbuild/compilers/vala.py @@ -31,6 +31,7 @@ def __init__(self, exelist: T.List[str], version: str, for_machine: MachineChoic self.version = version self.base_options = {OptionKey('b_colorout')} self.force_link = False + self._has_color_support = mesonlib.version_compare(self.version, '>=0.37.1') def needs_static_linker(self) -> bool: return False # Because compiles into C. @@ -80,7 +81,7 @@ def get_werror_args(self) -> T.List[str]: return ['--fatal-warnings'] def get_colorout_args(self, colortype: str) -> T.List[str]: - if version_compare(self.version, '>=0.37.1'): + if self._has_color_support: return ['--color=' + colortype] return []