From 8c0de5ad2c9c855e069e41f753d70390082d2896 Mon Sep 17 00:00:00 2001 From: Axel Ricard Date: Tue, 21 May 2024 14:22:40 +0200 Subject: [PATCH 1/2] fix sanity check for d cross-compilation Cherry-picked from https://github.com/mesonbuild/meson/commit/8d7ffe6e863834f0190eb6ae9dfe0891c9083c31 --- mesonbuild/compilers/d.py | 14 ++++++++++++-- mesonbuild/compilers/detect.py | 3 ++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/mesonbuild/compilers/d.py b/mesonbuild/compilers/d.py index de344c05781a..46cffdd0fc43 100644 --- a/mesonbuild/compilers/d.py +++ b/mesonbuild/compilers/d.py @@ -443,10 +443,18 @@ def sanity_check(self, work_dir: str, environment: 'Environment') -> None: output_name = os.path.join(work_dir, 'dtest') with open(source_name, 'w', encoding='utf-8') as ofile: ofile.write('''void main() { }''') - pc = subprocess.Popen(self.exelist + self.get_output_args(output_name) + self._get_target_arch_args() + [source_name], cwd=work_dir) + + compile_cmdlist = self.exelist + self.get_output_args(output_name) + self._get_target_arch_args() + [source_name] + + # If cross-compiling, we can't run the sanity check, only compile it. + if environment.need_exe_wrapper(self.for_machine) and not environment.has_exe_wrapper(): + compile_cmdlist += self.get_compile_only_args() + + pc = subprocess.Popen(compile_cmdlist, cwd=work_dir) pc.wait() if pc.returncode != 0: raise EnvironmentException('D compiler %s cannot compile programs.' % self.name_string()) + if environment.need_exe_wrapper(self.for_machine): if not environment.has_exe_wrapper(): # Can't check if the binaries run so we have to assume they do @@ -545,7 +553,9 @@ def _get_target_arch_args(self) -> T.List[str]: # LDC2 on Windows targets to current OS architecture, but # it should follow the target specified by the MSVC toolchain. if self.info.is_windows(): - if self.arch == 'x86_64': + if self.is_cross: + return [f'-mtriple={self.arch}-windows-msvc'] + elif self.arch == 'x86_64': return ['-m64'] return ['-m32'] return [] diff --git a/mesonbuild/compilers/detect.py b/mesonbuild/compilers/detect.py index 90a3ac597ebb..62187b9c2448 100644 --- a/mesonbuild/compilers/detect.py +++ b/mesonbuild/compilers/detect.py @@ -1161,7 +1161,8 @@ def detect_d_compiler(env: 'Environment', for_machine: MachineChoice) -> Compile return cls( exelist, version, for_machine, info, arch, - full_version=full_version, linker=linker, version_output=out) + full_version=full_version, linker=linker, + is_cross=is_cross, version_output=out) elif 'gdc' in out: cls = d.GnuDCompiler linker = guess_nix_linker(env, exelist, cls, version, for_machine) From b92996d9b63988d81b539e69255f116f947c72c9 Mon Sep 17 00:00:00 2001 From: Christoph Reiter Date: Fri, 12 Jul 2024 07:17:12 +0200 Subject: [PATCH 2/2] Make sure machine_info_can_run() isn't called on incomplete MachineInfo If need_exe_wrapper() is called while figuring out the language compiler, the MachineInfo isn't complete yet, so machine_info_can_run() would return False despite not cross compiling. Make sure this fails loudly. Squash of the following cherry-picked commits: - https://github.com/mesonbuild/meson/commit/df833b0512cd4e59c14205f6d9ff0d0642b3dd4f - https://github.com/mesonbuild/meson/commit/c108d5b2588501b462695d4d10f2aa2fcd91394e - https://github.com/mesonbuild/meson/commit/eba9e7efcf189306f5d8a7ab6406b05438a35988 --- mesonbuild/backend/backends.py | 3 ++- mesonbuild/compilers/cuda.py | 4 ++-- mesonbuild/compilers/d.py | 4 ++-- mesonbuild/compilers/mixins/clike.py | 4 ++-- mesonbuild/compilers/rust.py | 2 +- mesonbuild/environment.py | 1 + 6 files changed, 10 insertions(+), 8 deletions(-) diff --git a/mesonbuild/backend/backends.py b/mesonbuild/backend/backends.py index 740f349e4433..bea8c7e854a1 100644 --- a/mesonbuild/backend/backends.py +++ b/mesonbuild/backend/backends.py @@ -568,7 +568,8 @@ def get_executable_serialisation( else: extra_paths = [] - if self.environment.need_exe_wrapper(exe_for_machine): + is_cross_built = not self.environment.machines.matches_build_machine(exe_for_machine) + if is_cross_built and self.environment.need_exe_wrapper(): if not self.environment.has_exe_wrapper(): msg = 'An exe_wrapper is needed but was not found. Please define one ' \ 'in cross file and check the command and/or add it to PATH.' diff --git a/mesonbuild/compilers/cuda.py b/mesonbuild/compilers/cuda.py index 3761019b9945..eaea5c8462bd 100644 --- a/mesonbuild/compilers/cuda.py +++ b/mesonbuild/compilers/cuda.py @@ -551,7 +551,7 @@ def sanity_check(self, work_dir: str, env: 'Environment') -> None: flags += self.get_ccbin_args(env.coredata.options) # If cross-compiling, we can't run the sanity check, only compile it. - if env.need_exe_wrapper(self.for_machine) and not env.has_exe_wrapper(): + if self.is_cross and not env.has_exe_wrapper(): # Linking cross built apps is painful. You can't really # tell if you should use -nostdlib or not and for example # on OSX the compiler binary is the same but you need @@ -573,7 +573,7 @@ def sanity_check(self, work_dir: str, env: 'Environment') -> None: raise EnvironmentException(f'Compiler {self.name_string()} cannot compile programs.') # Run sanity check (if possible) - if env.need_exe_wrapper(self.for_machine): + if self.is_cross: if not env.has_exe_wrapper(): return else: diff --git a/mesonbuild/compilers/d.py b/mesonbuild/compilers/d.py index 46cffdd0fc43..c478c040ba9c 100644 --- a/mesonbuild/compilers/d.py +++ b/mesonbuild/compilers/d.py @@ -447,7 +447,7 @@ def sanity_check(self, work_dir: str, environment: 'Environment') -> None: compile_cmdlist = self.exelist + self.get_output_args(output_name) + self._get_target_arch_args() + [source_name] # If cross-compiling, we can't run the sanity check, only compile it. - if environment.need_exe_wrapper(self.for_machine) and not environment.has_exe_wrapper(): + if self.is_cross and not environment.has_exe_wrapper(): compile_cmdlist += self.get_compile_only_args() pc = subprocess.Popen(compile_cmdlist, cwd=work_dir) @@ -455,7 +455,7 @@ def sanity_check(self, work_dir: str, environment: 'Environment') -> None: if pc.returncode != 0: raise EnvironmentException('D compiler %s cannot compile programs.' % self.name_string()) - if environment.need_exe_wrapper(self.for_machine): + if self.is_cross: if not environment.has_exe_wrapper(): # Can't check if the binaries run so we have to assume they do return diff --git a/mesonbuild/compilers/mixins/clike.py b/mesonbuild/compilers/mixins/clike.py index d273015bcae7..434ec9fe7f37 100644 --- a/mesonbuild/compilers/mixins/clike.py +++ b/mesonbuild/compilers/mixins/clike.py @@ -278,7 +278,7 @@ def _sanity_check_impl(self, work_dir: str, environment: 'Environment', mode = CompileCheckMode.LINK if self.is_cross: binname += '_cross' - if environment.need_exe_wrapper(self.for_machine) and not environment.has_exe_wrapper(): + if not environment.has_exe_wrapper(): # Linking cross built C/C++ apps is painful. You can't really # tell if you should use -nostdlib or not and for example # on OSX the compiler binary is the same but you need @@ -308,7 +308,7 @@ def _sanity_check_impl(self, work_dir: str, environment: 'Environment', if pc.returncode != 0: raise mesonlib.EnvironmentException(f'Compiler {self.name_string()} cannot compile programs.') # Run sanity check - if environment.need_exe_wrapper(self.for_machine): + if self.is_cross: if not environment.has_exe_wrapper(): # Can't check if the binaries run so we have to assume they do return diff --git a/mesonbuild/compilers/rust.py b/mesonbuild/compilers/rust.py index ce1079190d98..f89d83fd944e 100644 --- a/mesonbuild/compilers/rust.py +++ b/mesonbuild/compilers/rust.py @@ -86,7 +86,7 @@ def sanity_check(self, work_dir: str, environment: 'Environment') -> None: if pc.returncode != 0: raise EnvironmentException(f'Rust compiler {self.name_string()} cannot compile programs.') self._native_static_libs(work_dir, source_name) - if environment.need_exe_wrapper(self.for_machine): + if self.is_cross: if not environment.has_exe_wrapper(): # Can't check if the binaries run so we have to assume they do return diff --git a/mesonbuild/environment.py b/mesonbuild/environment.py index 19b9e81b53b5..a86f47b7e971 100644 --- a/mesonbuild/environment.py +++ b/mesonbuild/environment.py @@ -505,6 +505,7 @@ def machine_info_can_run(machine_info: MachineInfo): if machine_info.system != detect_system(): return False true_build_cpu_family = detect_cpu_family({}) + assert machine_info.cpu_family is not None, 'called on incomplete machine_info' return \ (machine_info.cpu_family == true_build_cpu_family) or \ ((true_build_cpu_family == 'x86_64') and (machine_info.cpu_family == 'x86')) or \