Skip to content

Commit bdaffa7

Browse files
committed
rust: add rust_dynamic_std option
As an initial implementation, simply adding "-C prefer-dynamic" works for binary crates (as well as dylib and proc-macro that already used it). In the future this could be extended to other crate types. For more information see the comment in the changed file, as well as mesonbuild#8828 and mesonbuild#14215. Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 06374f3 commit bdaffa7

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## New experimental option `rust_dynamic_std`
2+
3+
A new option `rust_dynamic_std` can be used to link Rust programs so
4+
that they use a dynamic library for the Rust `libstd`.
5+
6+
Right now, C ABI crates (corresponding to Rust crate types `cdylib` and
7+
`staticlib`) cannot be produced if `rust_dynamic_std` is true, but this
8+
may change in the future.

mesonbuild/backend/ninjabackend.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,13 +2115,26 @@ def _link_library(libname: str, static: bool, bundle: bool = False):
21152115
and dep.rust_crate_type == 'dylib'
21162116
for dep in target_deps)
21172117

2118-
if target.rust_crate_type in {'dylib', 'proc-macro'} or has_rust_shared_deps:
2118+
if target.rust_crate_type in {'cdylib', 'staticlib'} \
2119+
and target.get_option(OptionKey('rust_dynamic_std')):
2120+
# cdylib and staticlib crates always include a copy of the Rust
2121+
# libstd, therefore it is not possible to also link it dynamically.
2122+
# The options to avoid this (-Z staticlib-allow-rdylib-deps and
2123+
# -Z staticlib-prefer-dynamic) are not yet stable; alternatively,
2124+
# one could use "--emit obj" (implemented in the pull request at
2125+
# https://github.com/mesonbuild/meson/pull/11213) or "--emit rlib"
2126+
# (officially not recommended for linking with C programs).
2127+
raise MesonException('rust_dynamic_std does not support cdylib and staticlib crates yet')
2128+
2129+
if target.rust_crate_type in {'dylib', 'proc-macro'} or has_rust_shared_deps \
2130+
or target.get_option(OptionKey('rust_dynamic_std')):
21192131
# add prefer-dynamic if any of the Rust libraries we link
21202132
# against are dynamic or this is a dynamic library itself,
21212133
# otherwise we'll end up with multiple implementations of libstd.
21222134
args += ['-C', 'prefer-dynamic']
21232135

2124-
if isinstance(target, build.SharedLibrary) or has_shared_deps:
2136+
if isinstance(target, build.SharedLibrary) or has_shared_deps \
2137+
or target.get_option(OptionKey('rust_dynamic_std')):
21252138
args += self.get_build_rpath_args(target, rustc)
21262139

21272140
proc_macro_dylib_path = None

mesonbuild/options.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ def add_to_argparse(self, name: str, parser: argparse.ArgumentParser, help_suffi
638638
(OptionKey('layout'), BuiltinOption(UserComboOption, 'Build directory layout', 'mirror', choices=['mirror', 'flat'])),
639639
(OptionKey('optimization'), BuiltinOption(UserComboOption, 'Optimization level', '0', choices=['plain', '0', 'g', '1', '2', '3', 's'])),
640640
(OptionKey('prefer_static'), BuiltinOption(UserBooleanOption, 'Whether to try static linking before shared linking', False)),
641+
(OptionKey('rust_dynamic_std'), BuiltinOption(UserBooleanOption, 'Whether to link Rust programs to a dynamic libstd', False)),
641642
(OptionKey('stdsplit'), BuiltinOption(UserBooleanOption, 'Split stdout and stderr in test logs', True)),
642643
(OptionKey('strip'), BuiltinOption(UserBooleanOption, 'Strip targets on install', False)),
643644
(OptionKey('unity'), BuiltinOption(UserComboOption, 'Unity build', 'off', choices=['on', 'off', 'subprojects'])),

test cases/rust/1 basic/meson.build

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ e = executable('rust-program', 'prog.rs',
66
)
77
test('rusttest', e)
88

9+
e = executable('rust-dynamic', 'prog.rs',
10+
override_options: {'rust_dynamic_std': true},
11+
install : true
12+
)
13+
test('rusttest-dynamic', e)
14+
915
subdir('subdir')
1016

1117
# this should fail due to debug_assert

0 commit comments

Comments
 (0)