Skip to content

Commit 1ce138d

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 e133f1d commit 1ce138d

File tree

6 files changed

+41
-5
lines changed

6 files changed

+41
-5
lines changed

docs/markdown/Builtin-options.md

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

304305
The default values of `c_winlibs` and `cpp_winlibs` are in
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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, `staticlib` crates cannot be produced if `rust_dynamic_std` is
7+
true, but this may change in the future.

mesonbuild/backend/ninjabackend.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2119,13 +2119,27 @@ def _link_library(libname: str, static: bool, bundle: bool = False):
21192119
and dep.rust_crate_type == 'dylib'
21202120
for dep in target_deps)
21212121

2122-
if target.rust_crate_type in {'dylib', 'proc-macro'} or has_rust_shared_deps:
2123-
# add prefer-dynamic if any of the Rust libraries we link
2122+
if target.rust_crate_type in {'dylib', 'proc-macro'}:
2123+
# also add prefer-dynamic if any of the Rust libraries we link
21242124
# against are dynamic or this is a dynamic library itself,
21252125
# otherwise we'll end up with multiple implementations of libstd.
2126+
has_rust_shared_deps = True
2127+
elif self.get_target_option(target, 'rust_dynamic_std'):
2128+
if target.rust_crate_type == 'staticlib':
2129+
# staticlib crates always include a copy of the Rust libstd,
2130+
# therefore it is not possible to also link it dynamically.
2131+
# The options to avoid this (-Z staticlib-allow-rdylib-deps and
2132+
# -Z staticlib-prefer-dynamic) are not yet stable; alternatively,
2133+
# one could use "--emit obj" (implemented in the pull request at
2134+
# https://github.com/mesonbuild/meson/pull/11213) or "--emit rlib"
2135+
# (officially not recommended for linking with C programs).
2136+
raise MesonException('rust_dynamic_std does not support staticlib crates yet')
2137+
# want libstd as a shared dep
2138+
has_rust_shared_deps = True
2139+
2140+
if has_rust_shared_deps:
21262141
args += ['-C', 'prefer-dynamic']
2127-
2128-
if isinstance(target, build.SharedLibrary) or has_shared_deps:
2142+
if has_shared_deps or has_rust_shared_deps:
21292143
args += self.get_build_rpath_args(target, rustc)
21302144

21312145
return deps, fortran_order_deps, project_deps, args

mesonbuild/compilers/rust.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,12 @@ def get_options(self) -> MutableKeyedOptionDictType:
237237
'none',
238238
choices=['none', '2015', '2018', '2021', '2024'])
239239

240+
key = self.form_compileropt_key('dynamic_std')
241+
opts[key] = options.UserBooleanOption(
242+
self.make_option_name(key),
243+
'Whether to link Rust build targets to a dynamic libstd',
244+
False)
245+
240246
return opts
241247

242248
def get_dependency_compile_args(self, dep: 'Dependency') -> T.List[str]:

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)