summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaolo Bonzini <pbonzini@redhat.com>2025-09-12 13:27:01 +0200
committerXavier Claessens <xclaesse@gmail.com>2025-10-27 21:14:30 +0100
commitf08a8f94e7c9ed3d4d8cb08c1a23153b6f122819 (patch)
tree82e50a9c69943a2a81b39ca2d6547af673b7cc73
parent271dbd81c3821c7535056470b52ddbd6f5ada703 (diff)
downloadmeson-f08a8f94e7c9ed3d4d8cb08c1a23153b6f122819.tar.gz
rust: drop +SUFFIX from crate name
Allow multiple targets for the same crate name, which is useful when the same crate is used for both the host and the build machine. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
-rw-r--r--docs/markdown/Rust.md8
-rw-r--r--mesonbuild/backend/ninjabackend.py3
-rw-r--r--test cases/rust/30 both machines/lib.rs3
-rw-r--r--test cases/rust/30 both machines/meson.build36
-rw-r--r--test cases/rust/30 both machines/test.rs5
5 files changed, 54 insertions, 1 deletions
diff --git a/docs/markdown/Rust.md b/docs/markdown/Rust.md
index 452dad6e6..67bbdec5b 100644
--- a/docs/markdown/Rust.md
+++ b/docs/markdown/Rust.md
@@ -98,3 +98,11 @@ target is a proc macro or dylib, or it depends on a dylib, in which case [`-C
prefer-dynamic`](https://doc.rust-lang.org/rustc/codegen-options/index.html#prefer-dynamic)
will be passed to the Rust compiler, and the standard libraries will be
dynamically linked.
+
+## Multiple targets for the same crate name
+
+For library targets that have `rust_abi: 'rust'`, the crate name is derived from the
+target name. First, dashes, spaces and dots are replaced with underscores. Second,
+*since 1.10.0* anything after the first `+` is dropped. This allows creating multiple
+targets for the same crate name, for example when the same crate is built multiple
+times with different features, or for both the build and the host machine.
diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py
index 5486b089b..13f4cdc81 100644
--- a/mesonbuild/backend/ninjabackend.py
+++ b/mesonbuild/backend/ninjabackend.py
@@ -1950,8 +1950,9 @@ class NinjaBackend(backends.Backend):
@staticmethod
def _get_rust_crate_name(target_name: str) -> str:
# Rustc replaces - with _. spaces or dots are not allowed, so we replace them with underscores
+ # Also +SUFFIX is dropped, which can be used to distinguish host from build crates
crate_name = target_name.replace('-', '_').replace(' ', '_').replace('.', '_')
- return crate_name
+ return crate_name.split('+', 1)[0]
@staticmethod
def _get_rust_dependency_name(target: build.BuildTarget, dependency: LibTypes) -> str:
diff --git a/test cases/rust/30 both machines/lib.rs b/test cases/rust/30 both machines/lib.rs
new file mode 100644
index 000000000..432593368
--- /dev/null
+++ b/test cases/rust/30 both machines/lib.rs
@@ -0,0 +1,3 @@
+pub fn answer() -> u32 {
+ 42
+}
diff --git a/test cases/rust/30 both machines/meson.build b/test cases/rust/30 both machines/meson.build
new file mode 100644
index 000000000..511518a7b
--- /dev/null
+++ b/test cases/rust/30 both machines/meson.build
@@ -0,0 +1,36 @@
+project(
+ 'testproj',
+ 'rust',
+ version : '0.1',
+ meson_version : '>= 1.9.0',
+ default_options : ['rust_std=2021'],
+)
+
+lib_host = static_library(
+ 'lib',
+ 'lib.rs',
+ rust_abi: 'rust'
+)
+
+lib_build = static_library(
+ 'lib+build',
+ 'lib.rs',
+ rust_abi: 'rust',
+ native: true,
+)
+
+exe_host = executable(
+ 'test-host',
+ 'test.rs',
+ link_with: lib_host,
+)
+
+exe_build = executable(
+ 'test-build',
+ 'test.rs',
+ link_with: lib_build,
+ native: true,
+)
+
+test('host', exe_host)
+test('build', exe_build)
diff --git a/test cases/rust/30 both machines/test.rs b/test cases/rust/30 both machines/test.rs
new file mode 100644
index 000000000..c9679d452
--- /dev/null
+++ b/test cases/rust/30 both machines/test.rs
@@ -0,0 +1,5 @@
+use lib::answer;
+
+fn main() {
+ println!("The answer is {}.\n", answer());
+}