cargo: Add library API version into its name

It is required to have unique library names for rust_dependency_map to
work. In the case we have foo crate in multiple versions, we have to
make sure their .rlib file have a different name.
This commit is contained in:
Xavier Claessens
2025-10-27 16:42:21 +00:00
committed by Xavier Claessens
parent 1c620b2b78
commit 44ce04537d
11 changed files with 54 additions and 3 deletions

View File

@@ -43,6 +43,15 @@ def _dependency_varname(dep: Dependency) -> str:
return f'{fixup_meson_varname(dep.package)}_{(dep.api.replace(".", "_"))}_dep'
def _library_name(name: str, api: str, lib_type: Literal['rust', 'c', 'proc-macro'] = 'rust') -> str:
# Add the API version to the library name to avoid conflicts when multiple
# versions of the same crate are used. The Ninja backend removed everything
# after the + to form the crate name.
if lib_type == 'c':
return name
return f'{name}+{api.replace(".", "_")}'
def _extra_args_varname() -> str:
return 'extra_args'
@@ -652,8 +661,8 @@ class Interpreter:
dependencies.append(build.identifier(_dependency_varname(dep)))
if name != dep.package:
dep_pkg = self._dep_package(pkg, dep)
dep_lib_name = dep_pkg.manifest.lib.name
dependency_map[build.string(fixup_meson_varname(dep_lib_name))] = build.string(name)
dep_lib_name = _library_name(dep_pkg.manifest.lib.name, dep_pkg.manifest.package.api)
dependency_map[build.string(dep_lib_name)] = build.string(name)
for name, sys_dep in pkg.manifest.system_dependencies.items():
if sys_dep.enabled(pkg.features):
dependencies.append(build.identifier(f'{fixup_meson_varname(name)}_system_dep'))
@@ -673,7 +682,7 @@ class Interpreter:
}
posargs: T.List[mparser.BaseNode] = [
build.string(fixup_meson_varname(pkg.manifest.lib.name)),
build.string(_library_name(pkg.manifest.lib.name, pkg.manifest.package.api, lib_type)),
build.string(pkg.manifest.lib.path),
]

View File

@@ -0,0 +1,3 @@
project('cargo multiple crate versions')
subproject('main')

View File

@@ -0,0 +1,2 @@
[wrap-file]
method=cargo

View File

@@ -0,0 +1,6 @@
[package]
name = "foo"
version = "1.0"
[lib]
path = "lib.rs"

View File

@@ -0,0 +1,3 @@
pub fn foo1() -> i32 {
1
}

View File

@@ -0,0 +1,2 @@
[wrap-file]
method=cargo

View File

@@ -0,0 +1,6 @@
[package]
name = "foo"
version = "2.0"
[lib]
path = "lib.rs"

View File

@@ -0,0 +1,3 @@
pub fn foo2() -> i32 {
1
}

View File

@@ -0,0 +1,2 @@
[wrap-file]
method=cargo

View File

@@ -0,0 +1,9 @@
[package]
name = "main"
[dependencies]
foo1 = { package="foo", version="1" }
foo2 = { package="foo", version="2" }
[lib]
path = "lib.rs"

View File

@@ -0,0 +1,6 @@
extern crate foo1;
extern crate foo2;
pub fn func() -> i32 {
foo1::foo1() + foo2::foo2()
}