Skip to content

Commit a65f461

Browse files
committed
cargo: Add support for target specific dependencies
1 parent bf2075e commit a65f461

File tree

8 files changed

+59
-5
lines changed

8 files changed

+59
-5
lines changed

mesonbuild/cargo/interpreter.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
import itertools
2121
import typing as T
2222

23-
from . import builder
24-
from . import version
25-
from ..mesonlib import MesonException, Popen_safe
23+
from . import builder, version, cfg
24+
from ..mesonlib import MesonException, Popen_safe, MachineChoice
2625
from .. import coredata, mlog
2726
from ..wrap.wrap import PackageDefinition
2827

@@ -35,6 +34,7 @@
3534
from .. import mparser
3635
from ..environment import Environment
3736
from ..interpreterbase import SubProject
37+
from ..compilers.rust import RustCompiler
3838

3939
# Copied from typeshed. Blarg that they don't expose this
4040
class DataclassInstance(Protocol):
@@ -476,10 +476,13 @@ class PackageKey:
476476
class Interpreter:
477477
def __init__(self, env: Environment) -> None:
478478
self.environment = env
479+
self.host_rustc = T.cast('RustCompiler', self.environment.coredata.compilers[MachineChoice.HOST]['rust'])
479480
# Map Cargo.toml's subdir to loaded manifest.
480481
self.manifests: T.Dict[str, Manifest] = {}
481482
# Map of cargo package (name + api) to its state
482483
self.packages: T.Dict[PackageKey, PackageState] = {}
484+
# Rustc's config
485+
self.cfgs = self._get_cfgs()
483486

484487
def interpret(self, subdir: str) -> mparser.CodeBlockNode:
485488
manifest = self._load_manifest(subdir)
@@ -526,6 +529,10 @@ def _fetch_package(self, package_name: str, api: str) -> T.Tuple[PackageState, b
526529
self.environment.wrap_resolver.wraps[meson_depname].type is not None
527530
pkg = PackageState(manifest, downloaded)
528531
self.packages[key] = pkg
532+
# Merge target specific dependencies that are enabled
533+
for condition, dependencies in manifest.target.items():
534+
if cfg.eval_cfg(condition, self.cfgs):
535+
manifest.dependencies.update(dependencies)
529536
# Fetch required dependencies recursively.
530537
for depname, dep in manifest.dependencies.items():
531538
if not dep.optional:
@@ -599,6 +606,23 @@ def _enable_feature(self, pkg: PackageState, feature: str) -> None:
599606
else:
600607
self._enable_feature(pkg, f)
601608

609+
def _get_cfgs(self) -> T.Dict[str, str]:
610+
cfgs = self.host_rustc.get_cfgs().copy()
611+
rustflags = self.environment.coredata.get_external_args(MachineChoice.HOST, 'rust')
612+
rustflags_i = iter(rustflags)
613+
for i in rustflags_i:
614+
if i == '--cfg':
615+
cfgs.append(next(rustflags_i))
616+
return dict(self._split_cfg(i) for i in cfgs)
617+
618+
@staticmethod
619+
def _split_cfg(cfg: str) -> T.Tuple[str, str]:
620+
pair = cfg.split('=', maxsplit=1)
621+
value = pair[1] if len(pair) > 1 else ''
622+
if value and value[0] == '"':
623+
value = value[1:-1]
624+
return pair[0], value
625+
602626
def _create_project(self, pkg: PackageState, build: builder.Builder) -> T.List[mparser.BaseNode]:
603627
"""Create the project() function call
604628

mesonbuild/compilers/rust.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,14 @@ def get_target_libdir(self) -> str:
182182
return stdo.split('\n', maxsplit=1)[0]
183183

184184
@functools.lru_cache(maxsize=None)
185-
def get_crt_static(self) -> bool:
185+
def get_cfgs(self) -> T.List[str]:
186186
cmd = self.get_exelist(ccache=False) + ['--print', 'cfg']
187187
p, stdo, stde = Popen_safe_logged(cmd)
188-
return bool(re.search('^target_feature="crt-static"$', stdo, re.MULTILINE))
188+
return stdo.splitlines()
189+
190+
@functools.lru_cache(maxsize=None)
191+
def get_crt_static(self) -> bool:
192+
return 'target_feature="crt-static"' in self.get_cfgs()
189193

190194
def get_debug_args(self, is_debug: bool) -> T.List[str]:
191195
return clike_debug_args[is_debug]

mesonbuild/interpreter/interpreter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,7 @@ def _do_subproject_cargo(self, subp_name: str, subdir: str,
10501050
mlog.warning('Cargo subproject is an experimental feature and has no backwards compatibility guarantees.',
10511051
once=True, location=self.current_node)
10521052
if self.environment.cargo is None:
1053+
self.add_languages(['rust'], True, MachineChoice.HOST)
10531054
self.environment.cargo = cargo.Interpreter(self.environment)
10541055
with mlog.nested(subp_name):
10551056
ast = self.environment.cargo.interpret(subdir)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ features = ["f1"]
2727
[dependencies.libname]
2828
version = "1"
2929

30+
[target."cfg(unix)".dependencies.unixdep]
31+
version = "0.1"
32+
3033
[features]
3134
default = ["f1"]
3235
f1 = ["f2", "f3"]

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ extern "C" {
88
#[cfg(feature = "foo")]
99
#[no_mangle]
1010
pub extern "C" fn rust_func() -> i32 {
11+
#[cfg(unix)]
12+
{
13+
extern crate unixdep;
14+
assert!(unixdep::only_on_unix() == 0);
15+
}
1116
assert!(common::common_func() == 0);
1217
assert!(libothername::stuff() == 42);
1318
let v: i32;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[wrap-file]
2+
method = cargo
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "unixdep"
3+
version = "0.1"
4+
edition = "2021"
5+
6+
[lib]
7+
path = "lib.rs"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
pub fn only_on_unix() -> i32 {
2+
0
3+
}
4+
5+
#[cfg(not(unix))]
6+
pub fn broken() -> i32 {
7+
plop
8+
}

0 commit comments

Comments
 (0)