From 9d61c0f5dd02136f1b31baef425a192b6aa4e2f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Thu, 27 Apr 2023 10:08:09 +0300 Subject: [PATCH] rust: Require `native: true` for `proc-macro` targets and allow linking into targets for other platforms proc-macros are basically compiler extensions and must be compiled for the build machine so that the compiler can load and execute it at runtime. Without this, cross-compilation of targets that use proc-macro dependencies is going to fail. Fixes https://github.com/mesonbuild/meson/issues/11702 --- mesonbuild/build.py | 6 ++++-- test cases/rust/18 proc-macro/meson.build | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/mesonbuild/build.py b/mesonbuild/build.py index 0c4326a8762a..88ee88ed4ace 100644 --- a/mesonbuild/build.py +++ b/mesonbuild/build.py @@ -1408,7 +1408,7 @@ def link(self, target): msg = f"Can't link non-PIC static library {t.name!r} into shared library {self.name!r}. " msg += "Use the 'pic' option to static_library to build with PIC." raise InvalidArguments(msg) - if self.for_machine is not t.for_machine: + if self.for_machine is not t.for_machine and (not t.uses_rust() or t.rust_crate_type != 'proc-macro'): msg = f'Tried to mix libraries for machines {self.for_machine} and {t.for_machine} in target {self.name!r}' if self.environment.is_cross_build(): raise InvalidArguments(msg + ' This is not possible in a cross build.') @@ -1432,7 +1432,7 @@ def link_whole(self, target): msg = f"Can't link non-PIC static library {t.name!r} into shared library {self.name!r}. " msg += "Use the 'pic' option to static_library to build with PIC." raise InvalidArguments(msg) - if self.for_machine is not t.for_machine: + if self.for_machine is not t.for_machine and (not t.uses_rust() or t.rust_crate_type != 'proc-macro'): msg = f'Tried to mix libraries for machines {self.for_machine} and {t.for_machine} in target {self.name!r}' if self.environment.is_cross_build(): raise InvalidArguments(msg + ' This is not possible in a cross build.') @@ -2328,6 +2328,8 @@ def process_kwargs(self, kwargs): else: raise InvalidArguments(f'Invalid rust_crate_type "{rust_crate_type}": must be a string.') if rust_crate_type == 'proc-macro': + if not kwargs.get('native', False): + mlog.warning('Rust "proc-macro" crate type requires "native: true". This will fail in a cross-build.') FeatureNew.single_use('Rust crate type "proc-macro"', '0.62.0', self.subproject) def get_import_filename(self) -> T.Optional[str]: diff --git a/test cases/rust/18 proc-macro/meson.build b/test cases/rust/18 proc-macro/meson.build index 01c4cbe17569..09cde8314515 100644 --- a/test cases/rust/18 proc-macro/meson.build +++ b/test cases/rust/18 proc-macro/meson.build @@ -8,6 +8,7 @@ pm = shared_library( 'proc_macro_examples', 'proc.rs', rust_crate_type : 'proc-macro', + native: true, ) main = executable(