From 1624354f33bf0a33f0e715ba1ca391ae0154ad19 Mon Sep 17 00:00:00 2001 From: Tristan Partin Date: Wed, 12 Jul 2023 15:12:36 -0500 Subject: Use CompileCheckMode enum There were a ton of naked strings with TODOs telling us to use the enum. --- mesonbuild/compilers/mixins/clike.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'mesonbuild/compilers/mixins/clike.py') diff --git a/mesonbuild/compilers/mixins/clike.py b/mesonbuild/compilers/mixins/clike.py index cec6b75..fe39ef1 100644 --- a/mesonbuild/compilers/mixins/clike.py +++ b/mesonbuild/compilers/mixins/clike.py @@ -370,7 +370,7 @@ class CLikeCompiler(Compiler): #include <{hname}> #endif''' return self.compiles(code, env, extra_args=extra_args, - dependencies=dependencies, mode='preprocess', disable_cache=disable_cache) + dependencies=dependencies, mode=CompileCheckMode.PREPROCESS, disable_cache=disable_cache) def has_header_symbol(self, hname: str, symbol: str, prefix: str, env: 'Environment', *, @@ -475,7 +475,7 @@ class CLikeCompiler(Compiler): need_exe_wrapper = env.need_exe_wrapper(self.for_machine) if need_exe_wrapper and self.exe_wrapper is None: raise compilers.CrossNoRunException('Can not run test applications in this cross environment.') - with self._build_wrapper(code, env, extra_args, dependencies, mode='link', want_output=True) as p: + with self._build_wrapper(code, env, extra_args, dependencies, mode=CompileCheckMode.LINK, want_output=True) as p: if p.returncode != 0: mlog.debug(f'Could not compile test file {p.input_name}: {p.returncode}\n') return compilers.RunResult(False) @@ -678,9 +678,9 @@ class CLikeCompiler(Compiler): {delim}\n{dname}''' args = self.build_wrapper_args(env, extra_args, dependencies, mode=CompileCheckMode.PREPROCESS).to_native() - func = functools.partial(self.cached_compile, code, env.coredata, extra_args=args, mode='preprocess') + func = functools.partial(self.cached_compile, code, env.coredata, extra_args=args, mode=CompileCheckMode.PREPROCESS) if disable_cache: - func = functools.partial(self.compile, code, extra_args=args, mode='preprocess', temp_dir=env.scratch_dir) + func = functools.partial(self.compile, code, extra_args=args, mode=CompileCheckMode.PREPROCESS, temp_dir=env.scratch_dir) with func() as p: cached = p.cached if p.returncode != 0: @@ -919,7 +919,7 @@ class CLikeCompiler(Compiler): ''' args = self.get_compiler_check_args(CompileCheckMode.COMPILE) n = '_symbols_have_underscore_prefix_searchbin' - with self._build_wrapper(code, env, extra_args=args, mode='compile', want_output=True, temp_dir=env.scratch_dir) as p: + with self._build_wrapper(code, env, extra_args=args, mode=CompileCheckMode.COMPILE, want_output=True, temp_dir=env.scratch_dir) as p: if p.returncode != 0: raise RuntimeError(f'BUG: Unable to compile {n!r} check: {p.stderr}') if not os.path.isfile(p.output_name): @@ -954,7 +954,7 @@ class CLikeCompiler(Compiler): #endif {delim}MESON_UNDERSCORE_PREFIX ''' - with self._build_wrapper(code, env, mode='preprocess', want_output=False, temp_dir=env.scratch_dir) as p: + with self._build_wrapper(code, env, mode=CompileCheckMode.PREPROCESS, want_output=False, temp_dir=env.scratch_dir) as p: if p.returncode != 0: raise RuntimeError(f'BUG: Unable to preprocess _symbols_have_underscore_prefix_define check: {p.stdout}') symbol_prefix = p.stdout.partition(delim)[-1].rstrip() @@ -1269,7 +1269,7 @@ class CLikeCompiler(Compiler): return args.copy() def has_arguments(self, args: T.List[str], env: 'Environment', code: str, - mode: str) -> T.Tuple[bool, bool]: + mode: CompileCheckMode) -> T.Tuple[bool, bool]: return self.compiles(code, env, extra_args=args, mode=mode) def _has_multi_arguments(self, args: T.List[str], env: 'Environment', code: str) -> T.Tuple[bool, bool]: @@ -1289,7 +1289,7 @@ class CLikeCompiler(Compiler): 'the compiler you are using. has_link_argument or ' 'other similar method can be used instead.') new_args.append(arg) - return self.has_arguments(new_args, env, code, mode='compile') + return self.has_arguments(new_args, env, code, mode=CompileCheckMode.COMPILE) def has_multi_arguments(self, args: T.List[str], env: 'Environment') -> T.Tuple[bool, bool]: return self._has_multi_arguments(args, env, 'extern int i;\nint i;\n') @@ -1300,7 +1300,7 @@ class CLikeCompiler(Compiler): # false positive. args = self.linker.fatal_warnings() + args args = self.linker_to_compiler_args(args) - return self.has_arguments(args, env, code, mode='link') + return self.has_arguments(args, env, code, mode=CompileCheckMode.LINK) def has_multi_link_arguments(self, args: T.List[str], env: 'Environment') -> T.Tuple[bool, bool]: return self._has_multi_link_arguments(args, env, 'int main(void) { return 0; }\n') @@ -1340,7 +1340,7 @@ class CLikeCompiler(Compiler): @functools.lru_cache(maxsize=None) def can_compile(self, src: 'mesonlib.FileOrString') -> bool: # Files we preprocess can be anything, e.g. .in - if self.mode == 'PREPROCESSOR': + if self.mode == CompileCheckMode.PREPROCESS: return True return super().can_compile(src) @@ -1348,6 +1348,6 @@ class CLikeCompiler(Compiler): if not self.preprocessor: self.preprocessor = copy.copy(self) self.preprocessor.exelist = self.exelist + self.get_preprocess_to_file_args() - self.preprocessor.mode = 'PREPROCESSOR' + self.preprocessor.mode = CompileCheckMode.PREPROCESS self.modes.append(self.preprocessor) return self.preprocessor -- cgit v1.1 From 921c2370a722cbaa42bd256c699fae3185084939 Mon Sep 17 00:00:00 2001 From: Tristan Partin Date: Wed, 12 Jul 2023 17:36:25 -0500 Subject: Replace some type comments with annotations --- mesonbuild/compilers/mixins/clike.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'mesonbuild/compilers/mixins/clike.py') diff --git a/mesonbuild/compilers/mixins/clike.py b/mesonbuild/compilers/mixins/clike.py index fe39ef1..d3e1008 100644 --- a/mesonbuild/compilers/mixins/clike.py +++ b/mesonbuild/compilers/mixins/clike.py @@ -103,7 +103,7 @@ class CLikeCompilerArgs(arglist.CompilerArgs): default_dirs = self.compiler.get_default_include_dirs() if default_dirs: real_default_dirs = [self._cached_realpath(i) for i in default_dirs] - bad_idx_list = [] # type: T.List[int] + bad_idx_list: T.List[int] = [] for i, each in enumerate(new): if not each.startswith('-isystem'): continue @@ -136,11 +136,11 @@ class CLikeCompiler(Compiler): """Shared bits for the C and CPP Compilers.""" if T.TYPE_CHECKING: - warn_args = {} # type: T.Dict[str, T.List[str]] + warn_args: T.Dict[str, T.List[str]] = {} # TODO: Replace this manual cache with functools.lru_cache - find_library_cache = {} # type: T.Dict[T.Tuple[T.Tuple[str, ...], str, T.Tuple[str, ...], str, LibType], T.Optional[T.List[str]]] - find_framework_cache = {} # type: T.Dict[T.Tuple[T.Tuple[str, ...], str, T.Tuple[str, ...], bool], T.Optional[T.List[str]]] + find_library_cache: T.Dict[T.Tuple[T.Tuple[str, ...], str, T.Tuple[str, ...], str, LibType], T.Optional[T.List[str]]] = {} + find_framework_cache: T.Dict[T.Tuple[T.Tuple[str, ...], str, T.Tuple[str, ...], bool], T.Optional[T.List[str]]] = {} internal_libs = arglist.UNIXY_COMPILER_INTERNAL_LIBS def __init__(self, exe_wrapper: T.Optional['ExternalProgram'] = None): @@ -389,8 +389,8 @@ class CLikeCompiler(Compiler): dependencies=dependencies) def _get_basic_compiler_args(self, env: 'Environment', mode: CompileCheckMode) -> T.Tuple[T.List[str], T.List[str]]: - cargs = [] # type: T.List[str] - largs = [] # type: T.List[str] + cargs: T.List[str] = [] + largs: T.List[str] = [] if mode is CompileCheckMode.LINK: # Sometimes we need to manually select the CRT to use with MSVC. # One example is when trying to do a compiler check that involves @@ -446,8 +446,8 @@ class CLikeCompiler(Compiler): # TODO: we want to ensure the front end does the listifing here dependencies = [dependencies] # Collect compiler arguments - cargs = self.compiler_args() # type: arglist.CompilerArgs - largs = [] # type: T.List[str] + cargs: arglist.CompilerArgs = self.compiler_args() + largs: T.List[str] = [] for d in dependencies: # Add compile flags needed by dependencies cargs += d.get_compile_args() @@ -805,7 +805,7 @@ class CLikeCompiler(Compiler): # # class StrProto(typing.Protocol): # def __str__(self) -> str: ... - fargs = {'prefix': prefix, 'func': funcname} # type: T.Dict[str, T.Union[str, bool, int]] + fargs: T.Dict[str, T.Union[str, bool, int]] = {'prefix': prefix, 'func': funcname} # glibc defines functions that are not available on Linux as stubs that # fail with ENOSYS (such as e.g. lchmod). In this case we want to fail @@ -1002,7 +1002,7 @@ class CLikeCompiler(Compiler): return self._symbols_have_underscore_prefix_searchbin(env) def _get_patterns(self, env: 'Environment', prefixes: T.List[str], suffixes: T.List[str], shared: bool = False) -> T.List[str]: - patterns = [] # type: T.List[str] + patterns: T.List[str] = [] for p in prefixes: for s in suffixes: patterns.append(p + '{}.' + s) @@ -1066,7 +1066,7 @@ class CLikeCompiler(Compiler): @staticmethod def _sort_shlibs_openbsd(libs: T.List[str]) -> T.List[str]: - filtered = [] # type: T.List[str] + filtered: T.List[str] = [] for lib in libs: # Validate file as a shared library of type libfoo.so.X.Y ret = lib.rsplit('.so.', maxsplit=1) @@ -1205,7 +1205,7 @@ class CLikeCompiler(Compiler): os_env = os.environ.copy() os_env['LC_ALL'] = 'C' _, _, stde = mesonlib.Popen_safe(commands, env=os_env, stdin=subprocess.PIPE) - paths = [] # T.List[str] + paths: T.List[str] = [] for line in stde.split('\n'): if '(framework directory)' not in line: continue @@ -1273,7 +1273,7 @@ class CLikeCompiler(Compiler): return self.compiles(code, env, extra_args=args, mode=mode) def _has_multi_arguments(self, args: T.List[str], env: 'Environment', code: str) -> T.Tuple[bool, bool]: - new_args = [] # type: T.List[str] + new_args: T.List[str] = [] for arg in args: # some compilers, e.g. GCC, don't warn for unsupported warning-disable # flags, so when we are testing a flag like "-Wno-forgotten-towel", also -- cgit v1.1 From d4bcf05c39e650d9651b5f2c60e7c12d59367e9c Mon Sep 17 00:00:00 2001 From: Tristan Partin Date: Wed, 12 Jul 2023 17:47:12 -0500 Subject: Annotate naked fundamental Python types Although mypy wasn't complaining, pyright was. --- mesonbuild/compilers/mixins/clike.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'mesonbuild/compilers/mixins/clike.py') diff --git a/mesonbuild/compilers/mixins/clike.py b/mesonbuild/compilers/mixins/clike.py index d3e1008..978c058 100644 --- a/mesonbuild/compilers/mixins/clike.py +++ b/mesonbuild/compilers/mixins/clike.py @@ -226,7 +226,7 @@ class CLikeCompiler(Compiler): # system directories aren't mixed, we only need to check one file for each # directory and go by that. If we can't check the file for some reason, assume # the compiler knows what it's doing, and accept the directory anyway. - retval = [] + retval: T.List[str] = [] for d in dirs: files = [f for f in os.listdir(d) if f.endswith('.so') and os.path.isfile(os.path.join(d, f))] # if no files, accept directory and move on @@ -1216,7 +1216,7 @@ class CLikeCompiler(Compiler): def _find_framework_real(self, name: str, env: 'Environment', extra_dirs: T.List[str], allow_system: bool) -> T.Optional[T.List[str]]: code = 'int main(void) { return 0; }' - link_args = [] + link_args: T.List[str] = [] for d in extra_dirs: link_args += ['-F' + d] # We can pass -Z to disable searching in the system frameworks, but -- cgit v1.1 From c1863f781bf56190ff00f081733c2cb1f2f73884 Mon Sep 17 00:00:00 2001 From: Tristan Partin Date: Wed, 12 Jul 2023 17:58:35 -0500 Subject: Remove Compiler._build_wrapper(temp_dir:) The function wasn't using the keyword argument and all the callers were using env.scratch_dir anyway. --- mesonbuild/compilers/mixins/clike.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'mesonbuild/compilers/mixins/clike.py') diff --git a/mesonbuild/compilers/mixins/clike.py b/mesonbuild/compilers/mixins/clike.py index 978c058..da36185 100644 --- a/mesonbuild/compilers/mixins/clike.py +++ b/mesonbuild/compilers/mixins/clike.py @@ -680,7 +680,7 @@ class CLikeCompiler(Compiler): mode=CompileCheckMode.PREPROCESS).to_native() func = functools.partial(self.cached_compile, code, env.coredata, extra_args=args, mode=CompileCheckMode.PREPROCESS) if disable_cache: - func = functools.partial(self.compile, code, extra_args=args, mode=CompileCheckMode.PREPROCESS, temp_dir=env.scratch_dir) + func = functools.partial(self.compile, code, extra_args=args, mode=CompileCheckMode.PREPROCESS) with func() as p: cached = p.cached if p.returncode != 0: @@ -919,7 +919,7 @@ class CLikeCompiler(Compiler): ''' args = self.get_compiler_check_args(CompileCheckMode.COMPILE) n = '_symbols_have_underscore_prefix_searchbin' - with self._build_wrapper(code, env, extra_args=args, mode=CompileCheckMode.COMPILE, want_output=True, temp_dir=env.scratch_dir) as p: + with self._build_wrapper(code, env, extra_args=args, mode=CompileCheckMode.COMPILE, want_output=True) as p: if p.returncode != 0: raise RuntimeError(f'BUG: Unable to compile {n!r} check: {p.stderr}') if not os.path.isfile(p.output_name): @@ -954,7 +954,7 @@ class CLikeCompiler(Compiler): #endif {delim}MESON_UNDERSCORE_PREFIX ''' - with self._build_wrapper(code, env, mode=CompileCheckMode.PREPROCESS, want_output=False, temp_dir=env.scratch_dir) as p: + with self._build_wrapper(code, env, mode=CompileCheckMode.PREPROCESS, want_output=False) as p: if p.returncode != 0: raise RuntimeError(f'BUG: Unable to preprocess _symbols_have_underscore_prefix_define check: {p.stdout}') symbol_prefix = p.stdout.partition(delim)[-1].rstrip() -- cgit v1.1