From 32e827dcdc451e1c5dde952cf08e4b654eac7057 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Wed, 8 Aug 2018 12:27:04 -0400 Subject: Use `env.machines.*` to avoid some `is_cross` This is a small example of the `is_cross` removal the that abstraction enables. --- mesonbuild/backend/backends.py | 4 +-- mesonbuild/backend/ninjabackend.py | 4 +-- mesonbuild/build.py | 20 ++++++------- mesonbuild/compilers/clike.py | 21 ++++++------- mesonbuild/dependencies/boost.py | 16 +++++----- mesonbuild/environment.py | 19 ++++++------ mesonbuild/mesonlib.py | 60 +++++++++++++------------------------- 7 files changed, 63 insertions(+), 81 deletions(-) (limited to 'mesonbuild') diff --git a/mesonbuild/backend/backends.py b/mesonbuild/backend/backends.py index a044ab5..6e75974 100644 --- a/mesonbuild/backend/backends.py +++ b/mesonbuild/backend/backends.py @@ -743,8 +743,8 @@ class Backend: exe_wrapper = self.environment.get_exe_wrapper() else: exe_wrapper = None - if mesonlib.for_windows(is_cross, self.environment) or \ - mesonlib.for_cygwin(is_cross, self.environment): + if mesonlib.for_windows(self.environment) or \ + mesonlib.for_cygwin(self.environment): extra_bdeps = [] if isinstance(exe, build.CustomTarget): extra_bdeps = exe.get_transitive_build_target_deps() diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py index 54fb117..c919d8f 100644 --- a/mesonbuild/backend/ninjabackend.py +++ b/mesonbuild/backend/ninjabackend.py @@ -665,8 +665,8 @@ int dummy; # CustomTarget command needs extra paths first. is_cross = self.environment.is_cross_build() and \ self.environment.need_exe_wrapper() - if mesonlib.for_windows(is_cross, self.environment) or \ - mesonlib.for_cygwin(is_cross, self.environment): + if mesonlib.for_windows(self.environment) or \ + mesonlib.for_cygwin(self.environment): extra_bdeps = target.get_transitive_build_target_deps() extra_paths = self.determine_windows_extra_paths(target.command[0], extra_bdeps, is_cross) diff --git a/mesonbuild/build.py b/mesonbuild/build.py index 65b2c20..3394459 100644 --- a/mesonbuild/build.py +++ b/mesonbuild/build.py @@ -928,7 +928,7 @@ This will become a hard error in a future Meson release.''') # You can't disable PIC on OS X. The compiler ignores -fno-PIC. # PIC is always on for Windows (all code is position-independent # since library loading is done differently) - if for_darwin(self.is_cross, self.environment) or for_windows(self.is_cross, self.environment): + if for_darwin(self.environment) or for_windows(self.environment): self.pic = True else: self.pic = self._extract_pic_pie(kwargs, 'pic') @@ -1253,7 +1253,7 @@ You probably should put it in link_with instead.''') ''' for link_target in self.link_targets: if isinstance(link_target, SharedModule): - if for_darwin(self.is_cross, self.environment): + if for_darwin(self.environment): raise MesonException('''target links against shared modules. This is not permitted on OSX''') else: @@ -1436,8 +1436,8 @@ class Executable(BuildTarget): self.prefix = '' if not hasattr(self, 'suffix'): # Executable for Windows or C#/Mono - if (for_windows(is_cross, environment) or - for_cygwin(is_cross, environment) or 'cs' in self.compilers): + if (for_windows(environment) or + for_cygwin(environment) or 'cs' in self.compilers): self.suffix = 'exe' elif ('c' in self.compilers and self.compilers['c'].get_id().startswith('arm') or 'cpp' in self.compilers and self.compilers['cpp'].get_id().startswith('arm')): @@ -1475,7 +1475,7 @@ class Executable(BuildTarget): implib_basename = self.name + '.exe' if not isinstance(kwargs.get('implib', False), bool): implib_basename = kwargs['implib'] - if for_windows(is_cross, environment) or for_cygwin(is_cross, environment): + if for_windows(environment) or for_cygwin(environment): self.vs_import_filename = '{0}.lib'.format(implib_basename) self.gcc_import_filename = 'lib{0}.a'.format(implib_basename) if self.get_using_msvc(): @@ -1652,7 +1652,7 @@ class SharedLibrary(BuildTarget): # C, C++, Swift, Vala # Only Windows uses a separate import library for linking # For all other targets/platforms import_filename stays None - elif for_windows(is_cross, env): + elif for_windows(env): suffix = 'dll' self.vs_import_filename = '{0}{1}.lib'.format(self.prefix if self.prefix is not None else '', self.name) self.gcc_import_filename = '{0}{1}.dll.a'.format(self.prefix if self.prefix is not None else 'lib', self.name) @@ -1677,7 +1677,7 @@ class SharedLibrary(BuildTarget): self.filename_tpl = '{0.prefix}{0.name}-{0.soversion}.{0.suffix}' else: self.filename_tpl = '{0.prefix}{0.name}.{0.suffix}' - elif for_cygwin(is_cross, env): + elif for_cygwin(env): suffix = 'dll' self.gcc_import_filename = '{0}{1}.dll.a'.format(self.prefix if self.prefix is not None else 'lib', self.name) # Shared library is of the form cygfoo.dll @@ -1689,7 +1689,7 @@ class SharedLibrary(BuildTarget): self.filename_tpl = '{0.prefix}{0.name}-{0.soversion}.{0.suffix}' else: self.filename_tpl = '{0.prefix}{0.name}.{0.suffix}' - elif for_darwin(is_cross, env): + elif for_darwin(env): prefix = 'lib' suffix = 'dylib' # On macOS, the filename can only contain the major version @@ -1699,7 +1699,7 @@ class SharedLibrary(BuildTarget): else: # libfoo.dylib self.filename_tpl = '{0.prefix}{0.name}.{0.suffix}' - elif for_android(is_cross, env): + elif for_android(env): prefix = 'lib' suffix = 'so' # Android doesn't support shared_library versioning @@ -1764,7 +1764,7 @@ class SharedLibrary(BuildTarget): def process_kwargs(self, kwargs, environment): super().process_kwargs(kwargs, environment) - if not for_android(self.is_cross, self.environment): + if not for_android(self.environment): supports_versioning = True else: supports_versioning = False diff --git a/mesonbuild/compilers/clike.py b/mesonbuild/compilers/clike.py index 3665f1f..968685b 100644 --- a/mesonbuild/compilers/clike.py +++ b/mesonbuild/compilers/clike.py @@ -274,9 +274,10 @@ class CLikeCompiler: return [] def gen_export_dynamic_link_args(self, env): - if mesonlib.for_windows(env.is_cross_build(), env) or mesonlib.for_cygwin(env.is_cross_build(), env): + m = env.machines[self.for_machine] + if m.is_windows() or m.is_cygwin(): return ['-Wl,--export-all-symbols'] - elif mesonlib.for_darwin(env.is_cross_build(), env): + elif mesonlib.for_darwin(env): return [] else: return ['-Wl,-export-dynamic'] @@ -884,7 +885,7 @@ class CLikeCompiler: for p in prefixes: for s in suffixes: patterns.append(p + '{}.' + s) - if shared and mesonlib.for_openbsd(self.is_cross, env): + if shared and mesonlib.for_openbsd(env): # Shared libraries on OpenBSD can be named libfoo.so.X.Y: # https://www.openbsd.org/faq/ports/specialtopics.html#SharedLibs # @@ -911,9 +912,9 @@ class CLikeCompiler: else: prefixes = ['lib', ''] # Library suffixes and prefixes - if mesonlib.for_darwin(env.is_cross_build(), env): + if mesonlib.for_darwin(env): shlibext = ['dylib', 'so'] - elif mesonlib.for_windows(env.is_cross_build(), env): + elif mesonlib.for_windows(env): # FIXME: .lib files can be import or static so we should read the # file, figure out which one it is, and reject the wrong kind. if isinstance(self, compilers.VisualStudioLikeCompiler): @@ -922,7 +923,7 @@ class CLikeCompiler: shlibext = ['dll.a', 'lib', 'dll'] # Yep, static libraries can also be foo.lib stlibext += ['lib'] - elif mesonlib.for_cygwin(env.is_cross_build(), env): + elif mesonlib.for_cygwin(env): shlibext = ['dll', 'dll.a'] prefixes = ['cyg'] + prefixes else: @@ -1126,12 +1127,12 @@ class CLikeCompiler: return self.find_framework_impl(name, env, extra_dirs, allow_system) def thread_flags(self, env): - if mesonlib.for_haiku(self.is_cross, env) or mesonlib.for_darwin(self.is_cross, env): + if mesonlib.for_haiku(env) or mesonlib.for_darwin(env): return [] return ['-pthread'] def thread_link_flags(self, env): - if mesonlib.for_haiku(self.is_cross, env) or mesonlib.for_darwin(self.is_cross, env): + if mesonlib.for_haiku(env) or mesonlib.for_darwin(env): return [] return ['-pthread'] @@ -1189,8 +1190,8 @@ class CLikeCompiler: def has_func_attribute(self, name, env): # Just assume that if we're not on windows that dllimport and dllexport # don't work - if not (mesonlib.for_windows(env.is_cross_build(), env) or - mesonlib.for_cygwin(env.is_cross_build(), env)): + if not (mesonlib.for_windows(env) or + mesonlib.for_cygwin(env)): if name in ['dllimport', 'dllexport']: return False, False diff --git a/mesonbuild/dependencies/boost.py b/mesonbuild/dependencies/boost.py index 0de1372..a955b6a 100644 --- a/mesonbuild/dependencies/boost.py +++ b/mesonbuild/dependencies/boost.py @@ -123,13 +123,13 @@ class BoostDependency(ExternalDependency): self.libdir = os.environ['BOOST_LIBRARYDIR'] if self.boost_root is None: - if mesonlib.for_windows(self.want_cross, self.env): + if mesonlib.for_windows(self.env): self.boost_roots = self.detect_win_roots() else: self.boost_roots = self.detect_nix_roots() if self.incdir is None: - if mesonlib.for_windows(self.want_cross, self.env): + if mesonlib.for_windows(self.env): self.incdir = self.detect_win_incdir() else: self.incdir = self.detect_nix_incdir() @@ -268,7 +268,7 @@ class BoostDependency(ExternalDependency): pass # 2. Fall back to the old method else: - if mesonlib.for_windows(self.want_cross, self.env): + if mesonlib.for_windows(self.env): self.detect_lib_modules_win() else: self.detect_lib_modules_nix() @@ -290,7 +290,7 @@ class BoostDependency(ExternalDependency): def compiler_tag(self): tag = None compiler = self.env.detect_cpp_compiler(self.want_cross) - if mesonlib.for_windows(self.want_cross, self.env): + if mesonlib.for_windows(self.env): if compiler.get_id() in ['msvc', 'clang-cl']: comp_ts_version = compiler.get_toolset_version() compiler_ts = comp_ts_version.split('.') @@ -304,10 +304,10 @@ class BoostDependency(ExternalDependency): if not self.is_multithreading: return '' - if mesonlib.for_darwin(self.want_cross, self.env): + if mesonlib.for_darwin(self.env): # - Mac: requires -mt for multithreading, so should not fall back to non-mt libraries. return '-mt' - elif mesonlib.for_windows(self.want_cross, self.env): + elif mesonlib.for_windows(self.env): # - Windows: requires -mt for multithreading, so should not fall back to non-mt libraries. return '-mt' else: @@ -340,7 +340,7 @@ class BoostDependency(ExternalDependency): # FIXME - how to handle different distributions, e.g. for Mac? Currently we handle homebrew and macports, but not fink. def abi_tags(self): - if mesonlib.for_windows(self.want_cross, self.env): + if mesonlib.for_windows(self.env): return [self.versioned_abi_tag(), self.threading_tag()] else: return [self.threading_tag()] @@ -437,7 +437,7 @@ class BoostDependency(ExternalDependency): def detect_lib_modules_nix(self): if self.static: libsuffix = 'a' - elif mesonlib.for_darwin(self.want_cross, self.env): + elif mesonlib.for_darwin(self.env): libsuffix = 'dylib' else: libsuffix = 'so' diff --git a/mesonbuild/environment.py b/mesonbuild/environment.py index ff4a6b1..f8603f6 100644 --- a/mesonbuild/environment.py +++ b/mesonbuild/environment.py @@ -513,12 +513,11 @@ class Environment: # Various prefixes and suffixes for import libraries, shared libraries, # static libraries, and executables. # Versioning is added to these names in the backends as-needed. - cross = self.is_cross_build() - if mesonlib.for_windows(cross, self): + if mesonlib.for_windows(self): self.exe_suffix = 'exe' self.object_suffix = 'obj' self.win_libdir_layout = True - elif mesonlib.for_cygwin(cross, self): + elif mesonlib.for_cygwin(self): self.exe_suffix = 'exe' self.object_suffix = 'o' self.win_libdir_layout = True @@ -774,9 +773,9 @@ class Environment: cls = ClangClCCompiler if lang == 'c' else ClangClCPPCompiler return cls(compiler, version, is_cross, exe_wrap, target) if 'clang' in out: - if 'Apple' in out or mesonlib.for_darwin(want_cross, self): + if 'Apple' in out or mesonlib.for_darwin(self): compiler_type = CompilerType.CLANG_OSX - elif 'windows' in out or mesonlib.for_windows(want_cross, self): + elif 'windows' in out or mesonlib.for_windows(self): compiler_type = CompilerType.CLANG_MINGW else: compiler_type = CompilerType.CLANG_STANDARD @@ -808,16 +807,16 @@ class Environment: return cls(compiler, version, is_cross, exe_wrap, target) if 'PGI Compilers' in out: - if mesonlib.for_darwin(want_cross, self): + if self.machines[for_machine].is_darwin(): compiler_type = CompilerType.PGI_OSX - elif mesonlib.for_windows(want_cross, self): + elif self.machines[for_machine].is_windows(): compiler_type = CompilerType.PGI_WIN else: compiler_type = CompilerType.PGI_STANDARD cls = PGICCompiler if lang == 'c' else PGICPPCompiler return cls(ccache + compiler, version, compiler_type, is_cross, exe_wrap) if '(ICC)' in out: - if mesonlib.for_darwin(want_cross, self): + if self.machine[for_macine].is_darwin(): compiler_type = CompilerType.ICC_OSX elif mesonlib.for_windows(want_cross, self): # TODO: fix ICC on Windows @@ -932,9 +931,9 @@ class Environment: return PathScaleFortranCompiler(compiler, version, is_cross, exe_wrap, full_version=full_version) if 'PGI Compilers' in out: - if mesonlib.for_darwin(want_cross, self): + if self.machine[for_macine].is_darwin(): compiler_type = CompilerType.PGI_OSX - elif mesonlib.for_windows(want_cross, self): + elif self.machines[for_machine].is_windows(): compiler_type = CompilerType.PGI_WIN else: compiler_type = CompilerType.PGI_STANDARD diff --git a/mesonbuild/mesonlib.py b/mesonbuild/mesonlib.py index e3ddf28..1b07d85 100644 --- a/mesonbuild/mesonlib.py +++ b/mesonbuild/mesonlib.py @@ -463,93 +463,75 @@ def is_dragonflybsd() -> bool: def is_freebsd() -> bool: return platform.system().lower() == 'freebsd' -def _get_machine_is_cross(env, is_cross): - """ - This is not morally correct, but works for now. For cross builds the build - and host machines differ. `is_cross == true` means the host machine, while - `is_cross == false` means the build machine. Both are used in practice, - even though the documentation refers to the host machine implying we should - hard-code it. For non-cross builds `is_cross == false` is passed but the - host and build machines are identical so it doesn't matter. - - Users for `for_*` should instead specify up front which machine they want - and query that like: - - env.machines[MachineChoice.HOST].is_haiku() - - """ - for_machine = MachineChoice.HOST if is_cross else MachineChoice.BUILD - return env.machines[for_machine] - -def for_windows(is_cross, env): +def for_windows(env): """ Host machine is windows? - Deprecated: Please use `env.machines[for_machine].is_windows()`. + Deprecated: Please use `env.machines.host.is_windows()`. Note: 'host' is the machine on which compiled binaries will run """ - return _get_machine_is_cross(env, is_cross).is_windows() + return env.machines.host.is_windows() -def for_cygwin(is_cross, env): +def for_cygwin(env): """ Host machine is cygwin? - Deprecated: Please use `env.machines[for_machine].is_cygwin()`. + Deprecated: Please use `env.machines.host.is_cygwin()`. Note: 'host' is the machine on which compiled binaries will run """ - return _get_machine_is_cross(env, is_cross).is_cygwin() + return env.machines.host.is_cygwin() -def for_linux(is_cross, env): +def for_linux(env): """ Host machine is linux? - Deprecated: Please use `env.machines[for_machine].is_linux()`. + Deprecated: Please use `env.machines.host.is_linux()`. Note: 'host' is the machine on which compiled binaries will run """ - return _get_machine_is_cross(env, is_cross).is_linux() + return env.machines.host.is_linux() -def for_darwin(is_cross, env): +def for_darwin(env): """ Host machine is Darwin (iOS/OS X)? - Deprecated: Please use `env.machines[for_machine].is_darwin()`. + Deprecated: Please use `env.machines.host.is_darwin()`. Note: 'host' is the machine on which compiled binaries will run """ - return _get_machine_is_cross(env, is_cross).is_darwin() + return env.machines.host.is_darwin() -def for_android(is_cross, env): +def for_android(env): """ Host machine is Android? - Deprecated: Please use `env.machines[for_machine].is_android()`. + Deprecated: Please use `env.machines.host.is_android()`. Note: 'host' is the machine on which compiled binaries will run """ - return _get_machine_is_cross(env, is_cross).is_android() + return env.machines.host.is_android() -def for_haiku(is_cross, env): +def for_haiku(env): """ Host machine is Haiku? - Deprecated: Please use `env.machines[for_machine].is_haiku()`. + Deprecated: Please use `env.machines.host.is_haiku()`. Note: 'host' is the machine on which compiled binaries will run """ - return _get_machine_is_cross(env, is_cross).is_haiku() + return env.machines.host.is_haiku() -def for_openbsd(is_cross, env): +def for_openbsd(env): """ Host machine is OpenBSD? - Deprecated: Please use `env.machines[for_machine].is_openbsd()`. + Deprecated: Please use `env.machines.host.is_openbsd()`. Note: 'host' is the machine on which compiled binaries will run """ - return _get_machine_is_cross(env, is_cross).is_openbsd() + return env.machines.host.is_openbsd() def exe_exists(arglist: List[str]) -> bool: try: -- cgit v1.1