diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py index 3143dff28..292980271 100644 --- a/mesonbuild/backend/ninjabackend.py +++ b/mesonbuild/backend/ninjabackend.py @@ -1998,7 +1998,7 @@ class NinjaBackend(backends.Backend): # "-l" argument and does not rely on platform specific dynamic linker. lib = self.get_target_filename_for_linking(d) link_whole = d in target.link_whole_targets - if isinstance(target, build.StaticLibrary): + if isinstance(target, build.StaticLibrary) or (isinstance(target, build.Executable) and rustc.get_crt_static()): static = isinstance(d, build.StaticLibrary) libname = os.path.basename(lib) if verbatim else d.name _link_library(libname, static, bundle=link_whole) diff --git a/mesonbuild/compilers/rust.py b/mesonbuild/compilers/rust.py index e10cb4cf3..89b913a58 100644 --- a/mesonbuild/compilers/rust.py +++ b/mesonbuild/compilers/rust.py @@ -3,6 +3,7 @@ from __future__ import annotations +import functools import subprocess, os.path import textwrap import re @@ -118,6 +119,12 @@ class RustCompiler(Compiler): p, stdo, stde = Popen_safe_logged(cmd) return stdo.split('\n', maxsplit=1)[0] + @functools.lru_cache(maxsize=None) + def get_crt_static(self) -> bool: + cmd = self.get_exelist(ccache=False) + ['--print', 'cfg'] + p, stdo, stde = Popen_safe_logged(cmd) + return bool(re.search('^target_feature="crt-static"$', stdo, re.MULTILINE)) + def get_debug_args(self, is_debug: bool) -> T.List[str]: return clike_debug_args[is_debug] diff --git a/test cases/rust/23 crt-static/lib.c b/test cases/rust/23 crt-static/lib.c new file mode 100644 index 000000000..f18e504a3 --- /dev/null +++ b/test cases/rust/23 crt-static/lib.c @@ -0,0 +1,6 @@ +#include + +void test_function(void) +{ + puts("Hello, world!"); +} diff --git a/test cases/rust/23 crt-static/main.rs b/test cases/rust/23 crt-static/main.rs new file mode 100644 index 000000000..d527ed970 --- /dev/null +++ b/test cases/rust/23 crt-static/main.rs @@ -0,0 +1,9 @@ +extern "C" { + fn test_function(); +} + +pub fn main() { + unsafe { + test_function(); + } +} diff --git a/test cases/rust/23 crt-static/meson.build b/test cases/rust/23 crt-static/meson.build new file mode 100644 index 000000000..dcdfc36e2 --- /dev/null +++ b/test cases/rust/23 crt-static/meson.build @@ -0,0 +1,9 @@ +project('rustprog', 'c') + +if not add_languages('rust', required : false) + error('MESON_SKIP_TEST crt-static doesn\'t work') +endif + +c_lib = static_library('lib', 'lib.c') + +executable('main', 'main.rs', link_with : c_lib) diff --git a/test cases/rust/23 crt-static/test.json b/test cases/rust/23 crt-static/test.json new file mode 100644 index 000000000..2d9470985 --- /dev/null +++ b/test cases/rust/23 crt-static/test.json @@ -0,0 +1,5 @@ +{ + "env": { + "RUSTC": "rustc -C target-feature=+crt-static" + } +}