Skip to content

Commit 8f8aade

Browse files
bonzinixclaesse
authored andcommitted
cargo: use both_libraries when appropriate
Fixes: #15028
1 parent b695f2d commit 8f8aade

File tree

6 files changed

+33
-21
lines changed

6 files changed

+33
-21
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,9 @@ Cargo subprojects automatically call `override_dependency` with the name
331331
* `0.0.x` -> '0'
332332
It allows to make different dependencies for incompatible versions of the same
333333
crate.
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.
334+
- the suffix is `-rs` for `rlib` and `dylib` crate types. The suffix is added to
335+
distinguish Rust crates from C-ABI dependencies; for example `gstreamer-1.0`
336+
is a system pkg-config dependency and `gstreamer-0.22-rs` is a Cargo dependency.
338337

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

mesonbuild/cargo/interpreter.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
from ..interpreterbase import SubProject
3333
from ..compilers.rust import RustCompiler
3434

35+
from typing_extensions import Literal
36+
3537
def _dependency_name(package_name: str, api: str, suffix: str = '-rs') -> str:
36-
basename = package_name[:-len(suffix)] if package_name.endswith(suffix) else package_name
38+
basename = package_name[:-len(suffix)] if suffix and package_name.endswith(suffix) else package_name
3739
return f'{basename}-{api}{suffix}'
3840

3941

@@ -139,8 +141,19 @@ def _create_package(self, pkg: PackageState, build: builder.Builder, subdir: str
139141
ast += self._create_meson_subdir(build)
140142

141143
if pkg.manifest.lib:
142-
for crate_type in pkg.manifest.lib.crate_type:
143-
ast.extend(self._create_lib(pkg, build, crate_type))
144+
crate_type = pkg.manifest.lib.crate_type
145+
if 'dylib' in crate_type and 'cdylib' in crate_type:
146+
raise MesonException('Cannot build both dylib and cdylib due to file name conflict')
147+
if 'proc-macro' in crate_type:
148+
ast.extend(self._create_lib(pkg, build, 'proc-macro', shared=True))
149+
if any(x in crate_type for x in ['lib', 'rlib', 'dylib']):
150+
ast.extend(self._create_lib(pkg, build, 'rust',
151+
static=('lib' in crate_type or 'rlib' in crate_type),
152+
shared='dylib' in crate_type))
153+
if any(x in crate_type for x in ['staticlib', 'cdylib']):
154+
ast.extend(self._create_lib(pkg, build, 'c',
155+
static='staticlib' in crate_type,
156+
shared='cdylib' in crate_type))
144157

145158
return ast
146159

@@ -560,7 +573,9 @@ def _create_meson_subdir(self, build: builder.Builder) -> T.List[mparser.BaseNod
560573
build.block([build.function('subdir', [build.string('meson')])]))
561574
]
562575

563-
def _create_lib(self, pkg: PackageState, build: builder.Builder, crate_type: raw.CRATE_TYPE) -> T.List[mparser.BaseNode]:
576+
def _create_lib(self, pkg: PackageState, build: builder.Builder,
577+
lib_type: Literal['rust', 'c', 'proc-macro'],
578+
static: bool = False, shared: bool = False) -> T.List[mparser.BaseNode]:
564579
dependencies: T.List[mparser.BaseNode] = []
565580
dependency_map: T.Dict[mparser.BaseNode, mparser.BaseNode] = {}
566581
for name in pkg.required_deps:
@@ -593,21 +608,19 @@ def _create_lib(self, pkg: PackageState, build: builder.Builder, crate_type: raw
593608
'rust_args': build.array(rust_args),
594609
}
595610

596-
depname_suffix = '-rs' if crate_type in {'lib', 'rlib', 'proc-macro'} else f'-{crate_type}'
611+
depname_suffix = '' if lib_type == 'c' else '-rs'
597612
depname = _dependency_name(pkg.manifest.package.name, pkg.manifest.package.api, depname_suffix)
598613

599614
lib: mparser.BaseNode
600-
if crate_type == 'proc-macro':
615+
if lib_type == 'proc-macro':
601616
lib = build.method('proc_macro', build.identifier('rust'), posargs, kwargs)
602617
else:
603-
if crate_type in {'lib', 'rlib', 'staticlib'}:
604-
target_type = 'static_library'
605-
elif crate_type in {'dylib', 'cdylib'}:
606-
target_type = 'shared_library'
618+
if static and shared:
619+
target_type = 'both_libraries'
607620
else:
608-
raise MesonException(f'Unsupported crate type {crate_type}')
609-
if crate_type in {'staticlib', 'cdylib'}:
610-
kwargs['rust_abi'] = build.string('c')
621+
target_type = 'shared_library' if shared else 'static_library'
622+
623+
kwargs['rust_abi'] = build.string(lib_type)
611624
lib = build.function(target_type, posargs, kwargs)
612625

613626
features_args: T.List[mparser.BaseNode] = []

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-cdylib')
3+
foo_dep = dependency('foo-0')
44
exe = executable('app', 'main.c',
55
dependencies: foo_dep,
66
)

test cases/rust/22 cargo subproject/subprojects/foo-0-rs.wrap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
method = cargo
33

44
[provide]
5-
dependency_names = foo-0-cdylib
5+
dependency_names = foo-0

test cases/rust/22 cargo subproject/subprojects/foo-0-rs/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "0.0.1"
44
edition = "2021"
55

66
[lib]
7-
crate-type = ["cdylib"]
7+
crate-type = ["staticlib", "cdylib"]
88
path = "lib.rs"
99

1010
# This dependency does not exist, verify optional works.

test cases/rust/30 cargo workspace/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ e = executable('test-foo-1-rs', 'test_foo_1.rs',
66
)
77
test('test-foo-1-rs', e)
88

9-
foo_cdylib = dependency('foo-1-cdylib')
9+
foo_cdylib = dependency('foo-1')
1010
e = executable('test-foo-1-cdylib', 'test_foo_1.c',
1111
dependencies: [foo_cdylib],
1212
)

0 commit comments

Comments
 (0)