Skip to content

Commit 0cb70e4

Browse files
xclaessedcbaker
authored andcommitted
cargo: Use -rs suffix only for rust ABI
A cargo package can build multiple crate types for the same library. Using the same name in meson.override_dependency() fails. [pbonzini: adjust documentation]
1 parent 56e84d3 commit 0cb70e4

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

docs/markdown/Wrap-dependency-system-manual.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,18 +322,19 @@ foo-bar-1.0 = foo_bar_dep
322322
**Note**: This is experimental and has no backwards or forwards compatibility guarantees.
323323
See [Meson's rules on mixing build systems](Mixing-build-systems.md).
324324

325-
Cargo subprojects automatically override the `<package_name>-<version>-rs` dependency
326-
name:
325+
Cargo subprojects automatically call `override_dependency` with the name
326+
`<package_name>-<version>-<suffix>`, where every part is defeined as follows:
327327
- `package_name` is defined in `[package] name = ...` section of the `Cargo.toml`.
328328
- `version` is the API version deduced from `[package] version = ...` as follow:
329329
* `x.y.z` -> 'x'
330330
* `0.x.y` -> '0.x'
331331
* `0.0.x` -> '0'
332332
It allows to make different dependencies for incompatible versions of the same
333333
crate.
334-
- `-rs` suffix is added to distinguish from regular system dependencies, for
335-
example `gstreamer-1.0` is a system pkg-config dependency and `gstreamer-0.22-rs`
336-
is a Cargo dependency.
334+
- the suffix is `-rs` for `rlib` and `dylib` crate types, otherwise it is the
335+
crate type (e.g. `staticlib` or `cdylib`). The suffix is added to distinguish
336+
Rust crates from regular system dependencies; for example `gstreamer-1.0` is a
337+
system pkg-config dependency and `gstreamer-0.22-rs` is a Cargo dependency.
337338

338339
That means the `.wrap` file should have `dependency_names = foo-1-rs` in their
339340
`[provide]` section when `Cargo.toml` has package name `foo` and version `1.2`.

mesonbuild/cargo/interpreter.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -441,9 +441,9 @@ def _version_to_api(version: str) -> str:
441441
return '0'
442442

443443

444-
def _dependency_name(package_name: str, api: str) -> str:
445-
basename = package_name[:-3] if package_name.endswith('-rs') else package_name
446-
return f'{basename}-{api}-rs'
444+
def _dependency_name(package_name: str, api: str, suffix: str = '-rs') -> str:
445+
basename = package_name[:-len(suffix)] if package_name.endswith(suffix) else package_name
446+
return f'{basename}-{api}{suffix}'
447447

448448

449449
def _dependency_varname(package_name: str) -> str:
@@ -805,6 +805,9 @@ def _create_lib(self, pkg: PackageState, build: builder.Builder, crate_type: man
805805
'rust_args': build.array(rust_args),
806806
}
807807

808+
depname_suffix = '-rs' if crate_type in {'lib', 'rlib', 'proc-macro'} else f'-{crate_type}'
809+
depname = _dependency_name(pkg.manifest.package.name, pkg.manifest.package.api, depname_suffix)
810+
808811
lib: mparser.BaseNode
809812
if pkg.manifest.lib.proc_macro or crate_type == 'proc-macro':
810813
lib = build.method('proc_macro', build.identifier('rust'), posargs, kwargs)
@@ -837,7 +840,8 @@ def _create_lib(self, pkg: PackageState, build: builder.Builder, crate_type: man
837840
'link_with': build.identifier('lib'),
838841
'variables': build.dict({
839842
build.string('features'): build.string(','.join(pkg.features)),
840-
})
843+
}),
844+
'version': build.string(pkg.manifest.package.version),
841845
},
842846
),
843847
'dep'
@@ -846,7 +850,7 @@ def _create_lib(self, pkg: PackageState, build: builder.Builder, crate_type: man
846850
'override_dependency',
847851
build.identifier('meson'),
848852
[
849-
build.string(_dependency_name(pkg.manifest.package.name, pkg.manifest.package.api)),
853+
build.string(depname),
850854
build.identifier('dep'),
851855
],
852856
),

test cases/rust/22 cargo subproject/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
project('cargo subproject', 'c')
22

3-
foo_dep = dependency('foo-0-rs')
3+
foo_dep = dependency('foo-0-cdylib')
44
exe = executable('app', 'main.c',
55
dependencies: foo_dep,
66
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
[wrap-file]
22
method = cargo
3+
4+
[provide]
5+
dependency_names = foo-0-cdylib

0 commit comments

Comments
 (0)