Skip to content

Commit daf6d4a

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 ba7ae7c commit daf6d4a

File tree

6 files changed

+38
-4
lines changed

6 files changed

+38
-4
lines changed

docs/markdown/Builtin-options.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ or compiler being used:
290290
| cpp_thread_count | 4 | integer value ≥ 0 | Number of threads to use with emcc when using threads |
291291
| cpp_winlibs | see below | free-form comma-separated list | Standard Windows libs to link against |
292292
| fortran_std | none | [none, legacy, f95, f2003, f2008, f2018] | Fortran language standard to use |
293+
| rust_dynamic_std | false | true, false | Whether to link dynamically to the Rust standard library *(Added in 1.8.0)* |
293294
| cuda_ccbindir | | filesystem path | CUDA non-default toolchain directory to use (-ccbin) *(Added in 0.57.1)* |
294295

295296
The default values of `c_winlibs` and `cpp_winlibs` are in
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/compilers/rust.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,11 @@ def get_options(self) -> MutableKeyedOptionDictType:
238238
self.form_compileropt_key('std'),
239239
'Rust edition to use',
240240
['none', '2015', '2018', '2021', '2024'],
241-
'none'),))
241+
'none'),
242+
self.create_option(options.UserBooleanOption,
243+
self.form_compileropt_key('dynamic_std'),
244+
'Whether to link Rust programs to a dynamic libstd',
245+
False)))
242246

243247
def get_dependency_compile_args(self, dep: 'Dependency') -> T.List[str]:
244248
# Rust doesn't have dependency compile arguments so simply return

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

test cases/rust/1 basic/test.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
{"type": "exe", "file": "usr/bin/rust-program"},
44
{"type": "pdb", "file": "usr/bin/rust-program"},
55
{"type": "exe", "file": "usr/bin/rust-program2"},
6-
{"type": "pdb", "file": "usr/bin/rust-program2"}
6+
{"type": "pdb", "file": "usr/bin/rust-program2"},
7+
{"type": "exe", "file": "usr/bin/rust-dynamic"},
8+
{"type": "pdb", "file": "usr/bin/rust-dynamic"}
79
]
810
}

0 commit comments

Comments
 (0)