Rust: Prevent linking Rust ABI with C library/executable

pull/12276/head
Xavier Claessens 1 year ago committed by Xavier Claessens
parent bdf1f3c0e2
commit 7592003484
  1. 28
      mesonbuild/build.py
  2. 5
      test cases/rust/4 polyglot/meson.build

@ -1408,12 +1408,7 @@ class BuildTarget(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:
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.')
else:
mlog.warning(msg + ' This will fail in cross build.')
self.check_can_link_together(t)
self.link_targets.append(t)
def link_whole(self, targets, promoted: bool = False):
@ -1429,12 +1424,7 @@ class BuildTarget(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:
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.')
else:
mlog.warning(msg + ' This will fail in cross build.')
self.check_can_link_together(t)
if isinstance(self, StaticLibrary) and not self.uses_rust():
# When we're a static library and we link_whole: to another static
# library, we need to add that target's objects to ourselves.
@ -1480,6 +1470,17 @@ class BuildTarget(Target):
f' and thus has to include objects from {t.name!r} to be usable.')
raise InvalidArguments(m)
def check_can_link_together(self, t: BuildTargetTypes) -> None:
links_with_rust_abi = isinstance(t, BuildTarget) and t.uses_rust_abi()
if not self.uses_rust() and links_with_rust_abi:
raise InvalidArguments(f'Try to link Rust ABI library {t.name!r} with a non-Rust target {self.name!r}')
if self.for_machine is not t.for_machine:
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.')
else:
mlog.warning(msg + ' This will fail in cross build.')
def add_pch(self, language: str, pchlist: T.List[str]) -> None:
if not pchlist:
return
@ -1645,6 +1646,9 @@ class BuildTarget(Target):
def uses_rust(self) -> bool:
return 'rust' in self.compilers
def uses_rust_abi(self) -> bool:
return self.uses_rust() and self.rust_crate_type in {'dylib', 'rlib', 'proc-macro'}
def uses_fortran(self) -> bool:
return 'fortran' in self.compilers

@ -47,8 +47,9 @@ foreach crate_type : ['lib', 'rlib', 'dylib', 'cdylib', 'staticlib', 'proc-macro
install: true)
test(f'polyglottest-@name@', e)
else
# FIXME: Verify that linking Rust ABI with C ABI executable raises an error.
# Currently it only fails at build time.
testcase expect_error('Try to link Rust ABI library .*', how: 're')
executable(f'prog-@name@', 'prog.c', link_with: l)
endtestcase
endif
endforeach
endforeach

Loading…
Cancel
Save