Skip to content

Commit 5f3fe39

Browse files
committed
Rust: Prevent linking Rust ABI with C library/executable
1 parent ffd7caf commit 5f3fe39

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

mesonbuild/build.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,12 +1401,7 @@ def link(self, targets):
14011401
msg = f"Can't link non-PIC static library {t.name!r} into shared library {self.name!r}. "
14021402
msg += "Use the 'pic' option to static_library to build with PIC."
14031403
raise InvalidArguments(msg)
1404-
if self.for_machine is not t.for_machine:
1405-
msg = f'Tried to mix libraries for machines {self.for_machine} and {t.for_machine} in target {self.name!r}'
1406-
if self.environment.is_cross_build():
1407-
raise InvalidArguments(msg + ' This is not possible in a cross build.')
1408-
else:
1409-
mlog.warning(msg + ' This will fail in cross build.')
1404+
self.check_can_link_together(t)
14101405
self.link_targets.append(t)
14111406

14121407
def link_whole(self, targets, promoted: bool = False):
@@ -1422,12 +1417,7 @@ def link_whole(self, targets, promoted: bool = False):
14221417
msg = f"Can't link non-PIC static library {t.name!r} into shared library {self.name!r}. "
14231418
msg += "Use the 'pic' option to static_library to build with PIC."
14241419
raise InvalidArguments(msg)
1425-
if self.for_machine is not t.for_machine:
1426-
msg = f'Tried to mix libraries for machines {self.for_machine} and {t.for_machine} in target {self.name!r}'
1427-
if self.environment.is_cross_build():
1428-
raise InvalidArguments(msg + ' This is not possible in a cross build.')
1429-
else:
1430-
mlog.warning(msg + ' This will fail in cross build.')
1420+
self.check_can_link_together(t)
14311421
if isinstance(self, StaticLibrary) and not self.uses_rust():
14321422
# When we're a static library and we link_whole: to another static
14331423
# library, we need to add that target's objects to ourselves.
@@ -1473,6 +1463,16 @@ def check_can_extract_objects(self, t: T.Union[Target, CustomTargetIndex], origi
14731463
f' and thus has to include objects from {t.name!r} to be usable.')
14741464
raise InvalidArguments(m)
14751465

1466+
def check_can_link_together(self, t: BuildTargetTypes) -> None:
1467+
if self.for_machine is not t.for_machine:
1468+
msg = f'Tried to mix libraries for machines {self.for_machine} and {t.for_machine} in target {self.name!r}'
1469+
if self.environment.is_cross_build():
1470+
raise InvalidArguments(msg + ' This is not possible in a cross build.')
1471+
else:
1472+
mlog.warning(msg + ' This will fail in cross build.')
1473+
if not self.uses_rust() and isinstance(t, BuildTarget) and t.uses_rust_abi():
1474+
raise InvalidArguments(f'Try to link Rust ABI library {t.name!r} with a non-Rust target {self.name!r}')
1475+
14761476
def add_pch(self, language: str, pchlist: T.List[str]) -> None:
14771477
if not pchlist:
14781478
return
@@ -1638,6 +1638,9 @@ def get_used_stdlib_args(self, link_language: str) -> T.List[str]:
16381638
def uses_rust(self) -> bool:
16391639
return 'rust' in self.compilers
16401640

1641+
def uses_rust_abi(self) -> bool:
1642+
return self.uses_rust() and self.rust_crate_type in {'dylib', 'rlib', 'proc-macro'}
1643+
16411644
def uses_fortran(self) -> bool:
16421645
return 'fortran' in self.compilers
16431646

test cases/rust/4 polyglot/meson.build

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ foreach crate_type : ['lib', 'rlib', 'dylib', 'cdylib', 'staticlib', 'proc-macro
4747
install: true)
4848
test(f'polyglottest-@name@', e)
4949
else
50-
# FIXME: Verify that linking Rust ABI with C ABI executable raises an error.
51-
# Currently it only fails at build time.
50+
testcase expect_error('Try to link Rust ABI library .*', how: 're')
51+
executable(f'prog-@name@', 'prog.c', link_with: l)
52+
endtestcase
5253
endif
5354
endforeach
5455
endforeach

0 commit comments

Comments
 (0)