Skip to content

Commit

Permalink
cargo: Add support for system-deps dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
thiblahute authored and xclaesse committed Feb 28, 2024
1 parent 8fe7d86 commit 7ab8559
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 1 deletion.
38 changes: 37 additions & 1 deletion mesonbuild/cargo/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,24 @@ class Package:
autobenches: bool = True


@dataclasses.dataclass
class SystemDependency:
name: T.Optional[str]
version: T.Optional[T.List[str]] = None
features: T.Dict[str, T.Dict[str, str]] = dataclasses.field(default_factory=dict)

@classmethod
def from_raw(cls, k: str, raw: manifest.DependencyV) -> Dependency:
"""Create a dependency from a raw cargo dictionary"""
fixed = _fixup_raw_mappings(raw)
fixed = {
'name': fixed.get('name', k),
'version': fixed.get('version'),
'features': {k: v for k, v in fixed.items() if k not in ['name', 'version']}
}

return cls(**fixed)

@dataclasses.dataclass
class Dependency:

Expand Down Expand Up @@ -284,6 +302,7 @@ class Manifest:
dependencies: T.Dict[str, Dependency]
dev_dependencies: T.Dict[str, Dependency]
build_dependencies: T.Dict[str, Dependency]
system_dependencies: T.Dict[str, SystemDependency]
lib: Library
bin: T.List[Binary]
test: T.List[Test]
Expand Down Expand Up @@ -314,6 +333,7 @@ def _convert_manifest(raw_manifest: manifest.Manifest, subdir: str, path: str =
{k: Dependency.from_raw(v, k) for k, v in raw_manifest.get('dependencies', {}).items()},
{k: Dependency.from_raw(v, k) for k, v in raw_manifest.get('dev-dependencies', {}).items()},
{k: Dependency.from_raw(v, k) for k, v in raw_manifest.get('build-dependencies', {}).items()},
{k: SystemDependency.from_raw(k, d) for k, d in raw_manifest['package'].get('metadata', {}).get('system-deps', {}).items()},
Library(**lib),
[Binary(**_fixup_raw_mappings(b)) for b in raw_manifest.get('bin', {})],
[Test(**_fixup_raw_mappings(b)) for b in raw_manifest.get('test', {})],
Expand Down Expand Up @@ -537,7 +557,7 @@ def _create_cfg(cargo: Manifest, build: builder.Builder) -> T.List[mparser.BaseN
has_build_deps_message = None
if cargo.build_dependencies:
has_build_deps_message = build.block([
build.function('error', [
build.function('warning', [
build.string('Cannot use build.rs with build-dependencies. It should be ported manually in meson/meson.build'),
]),
])
Expand Down Expand Up @@ -682,6 +702,20 @@ def _create_dependencies(cargo: Manifest, build: builder.Builder) -> T.List[mpar
ast.append(build.if_(cfg_to_meson(condition, build), build.block(ifblock), build.block(elseblock)))
for name, dep in cargo.dependencies.items():
ast += _create_dependency(name, dep, build)
for name, dep in cargo.system_dependencies.items():
kw = {}
if dep.version is not None:
kw['version'] = build.array([build.string(s) for s in dep.version])
ast.extend([
build.assign(
build.function(
'dependency',
[build.string(dep.name)],
kw,
),
f'{name}_system_dep',
),
])
return ast


Expand All @@ -694,6 +728,8 @@ def _create_lib(cargo: Manifest, build: builder.Builder, crate_type: manifest.CR
dependencies.append(build.identifier(_dependency_varname(dep.package)))
if name != dep.package:
dependency_map[build.string(fixup_meson_varname(dep.package))] = build.string(name)
for name, dep in cargo.system_dependencies.items():
dependencies.append(build.identifier(f'{name}_system_dep'))

rust_args: T.List[mparser.BaseNode] = [
build.identifier('features_args'),
Expand Down
5 changes: 5 additions & 0 deletions test cases/cargo/06 system deps link/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extern crate sub;

pub fn main() {
std::process::exit(sub::func());
}
10 changes: 10 additions & 0 deletions test cases/cargo/06 system deps link/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
project('cargo system-deps', 'rust')

rust = import('unstable-rust')

sub = rust.cargo('sub')

exe = executable('main', 'main.rs', dependencies : sub.get_variable('dep'))

test('main', exe)

14 changes: 14 additions & 0 deletions test cases/cargo/06 system deps link/subprojects/sub/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = 'sub'
version = '0.1.1'
edition = '2021'
description = 'a project with a system dependency'

[build-dependencies]
system-deps = "6"

[lib]
name = "sub"

[package.metadata.system-deps.system]
version = "0.1"
12 changes: 12 additions & 0 deletions test cases/cargo/06 system deps link/subprojects/sub/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
mod ffi {
extern "C" {
pub fn func() -> i32;
}
}

pub fn func() -> i32 {
unsafe {
ffi::func()
}
}

5 changes: 5 additions & 0 deletions test cases/cargo/06 system deps link/subprojects/system.wrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[wrap-file]
path = 'system'

[provide]
system = system_dep
5 changes: 5 additions & 0 deletions test cases/cargo/06 system deps link/subprojects/system/lib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <stdio.h>

int func() {
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
project('system', 'c', version: '0.2')


system_dep = declare_dependency(link_with: library('system', 'lib.c'))
meson.override_dependency('system', system_dep)

0 comments on commit 7ab8559

Please sign in to comment.