From d525027d14445a6a03ac6b7c9f3749cc3b9cf494 Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Tue, 1 Oct 2024 09:26:41 -0700 Subject: [PATCH 1/3] compilers: annotate lang_suffixes --- mesonbuild/compilers/compilers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mesonbuild/compilers/compilers.py b/mesonbuild/compilers/compilers.py index cb047be0c14c..35a0b2be768a 100644 --- a/mesonbuild/compilers/compilers.py +++ b/mesonbuild/compilers/compilers.py @@ -51,7 +51,7 @@ # Mapping of language to suffixes of files that should always be in that language # This means we can't include .h headers here since they could be C, C++, ObjC, etc. # First suffix is the language's default. -lang_suffixes = { +lang_suffixes: T.Mapping[str, T.Tuple[str, ...]] = { 'c': ('c',), 'cpp': ('cpp', 'cppm', 'cc', 'cxx', 'c++', 'hh', 'hpp', 'ipp', 'hxx', 'ino', 'ixx', 'C', 'H'), 'cuda': ('cu',), From 20baa1695dec8aa21017a1ec53778fd21f4f930c Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Tue, 1 Oct 2024 09:28:24 -0700 Subject: [PATCH 2/3] compilers: remove hasattr for `file_suffixes` This is used in exactly two cases, and it's just not worth it. Those two cases can override the default set of extensions, and in the process allow a nice bit of code cleanup, especially toward type checking. --- mesonbuild/compilers/compilers.py | 3 +-- mesonbuild/compilers/fortran.py | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/mesonbuild/compilers/compilers.py b/mesonbuild/compilers/compilers.py index 35a0b2be768a..dbe80273924d 100644 --- a/mesonbuild/compilers/compilers.py +++ b/mesonbuild/compilers/compilers.py @@ -452,8 +452,7 @@ def __init__(self, ccache: T.List[str], exelist: T.List[str], version: str, self.exelist = ccache + exelist self.exelist_no_ccache = exelist # In case it's been overridden by a child class already - if not hasattr(self, 'file_suffixes'): - self.file_suffixes = lang_suffixes[self.language] + self.file_suffixes = lang_suffixes[self.language] if not hasattr(self, 'can_compile_suffixes'): self.can_compile_suffixes: T.Set[str] = set(self.file_suffixes) self.default_suffix = self.file_suffixes[0] diff --git a/mesonbuild/compilers/fortran.py b/mesonbuild/compilers/fortran.py index a99a95ce37ba..5012fba074a0 100644 --- a/mesonbuild/compilers/fortran.py +++ b/mesonbuild/compilers/fortran.py @@ -262,7 +262,6 @@ def openmp_flags(self, env: Environment) -> T.List[str]: class IntelFortranCompiler(IntelGnuLikeCompiler, FortranCompiler): - file_suffixes = ('f90', 'f', 'for', 'ftn', 'fpp', ) id = 'intel' def __init__(self, exelist: T.List[str], version: str, for_machine: MachineChoice, is_cross: bool, @@ -275,6 +274,7 @@ def __init__(self, exelist: T.List[str], version: str, for_machine: MachineChoic # FIXME: Add support for OS X and Windows in detect_fortran_compiler so # we are sent the type of compiler IntelGnuLikeCompiler.__init__(self) + self.file_suffixes = ('f90', 'f', 'for', 'ftn', 'fpp', ) default_warn_args = ['-warn', 'general', '-warn', 'truncated_source'] self.warn_args = {'0': [], '1': default_warn_args, @@ -318,7 +318,6 @@ class IntelLLVMFortranCompiler(IntelFortranCompiler): class IntelClFortranCompiler(IntelVisualStudioLikeCompiler, FortranCompiler): - file_suffixes = ('f90', 'f', 'for', 'ftn', 'fpp', ) always_args = ['/nologo'] def __init__(self, exelist: T.List[str], version: str, for_machine: MachineChoice, @@ -329,6 +328,7 @@ def __init__(self, exelist: T.List[str], version: str, for_machine: MachineChoic is_cross, info, linker=linker, full_version=full_version) IntelVisualStudioLikeCompiler.__init__(self, target) + self.file_suffixes = ('f90', 'f', 'for', 'ftn', 'fpp', ) default_warn_args = ['/warn:general', '/warn:truncated_source'] self.warn_args = {'0': [], From aa312d6becd5729746ed08ab4268fe9a393207a1 Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Tue, 1 Oct 2024 09:31:22 -0700 Subject: [PATCH 3/3] compilers: remove hasattr from `can_compile_suffixes` This is never set outside the `Compiler.__init__`, only added to. As such there's no reason to have this `hasattr` check. It's wasting time *and* confusing our static checkers. --- mesonbuild/compilers/compilers.py | 4 +--- mesonbuild/compilers/mixins/ccrx.py | 1 - 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/mesonbuild/compilers/compilers.py b/mesonbuild/compilers/compilers.py index dbe80273924d..3ec50c9583c1 100644 --- a/mesonbuild/compilers/compilers.py +++ b/mesonbuild/compilers/compilers.py @@ -451,10 +451,8 @@ def __init__(self, ccache: T.List[str], exelist: T.List[str], version: str, full_version: T.Optional[str] = None, is_cross: bool = False): self.exelist = ccache + exelist self.exelist_no_ccache = exelist - # In case it's been overridden by a child class already self.file_suffixes = lang_suffixes[self.language] - if not hasattr(self, 'can_compile_suffixes'): - self.can_compile_suffixes: T.Set[str] = set(self.file_suffixes) + self.can_compile_suffixes = set(self.file_suffixes) self.default_suffix = self.file_suffixes[0] self.version = version self.full_version = full_version diff --git a/mesonbuild/compilers/mixins/ccrx.py b/mesonbuild/compilers/mixins/ccrx.py index 63270726bc4c..d1badaa1c7eb 100644 --- a/mesonbuild/compilers/mixins/ccrx.py +++ b/mesonbuild/compilers/mixins/ccrx.py @@ -40,7 +40,6 @@ class CcrxCompiler(Compiler): if T.TYPE_CHECKING: is_cross = True - can_compile_suffixes: T.Set[str] = set() id = 'ccrx'