Skip to content

Commit b483c4b

Browse files
committed
cargo: Add support for target specific dependencies
1 parent 3699158 commit b483c4b

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
@@ -19,9 +19,8 @@
1919
import urllib.parse
2020
import typing as T
2121

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

@@ -34,6 +33,7 @@
3433
from .. import mparser
3534
from ..environment import Environment
3635
from ..interpreterbase import SubProject
36+
from ..compilers.rust import RustCompiler
3737

3838
# Copied from typeshed. Blarg that they don't expose this
3939
class DataclassInstance(Protocol):
@@ -474,10 +474,13 @@ class PackageKey:
474474
class Interpreter:
475475
def __init__(self, env: Environment) -> None:
476476
self.environment = env
477+
self.host_rustc = T.cast('RustCompiler', self.environment.coredata.compilers[MachineChoice.HOST]['rust'])
477478
# Map Cargo.toml's subdir to loaded manifest.
478479
self.manifests: T.Dict[str, Manifest] = {}
479480
# Map of cargo package (name + api) to its state
480481
self.packages: T.Dict[PackageKey, PackageState] = {}
482+
# Rustc's config
483+
self.cfgs = self._get_cfgs()
481484

482485
def interpret(self, subdir: str) -> mparser.CodeBlockNode:
483486
manifest = self._load_manifest(subdir)
@@ -521,6 +524,10 @@ def _fetch_package(self, package_name: str, api: str) -> T.Tuple[PackageState, b
521524
manifest = self._load_manifest(subdir)
522525
pkg = PackageState(manifest)
523526
self.packages[key] = pkg
527+
# Merge target specific dependencies that are enabled
528+
for condition, dependencies in manifest.target.items():
529+
if cfg.eval_cfg(condition, self.cfgs):
530+
manifest.dependencies.update(dependencies)
524531
# Fetch required dependencies recursively.
525532
for depname, dep in manifest.dependencies.items():
526533
if not dep.optional:
@@ -588,6 +595,23 @@ def _enable_feature(self, pkg: PackageState, feature: str) -> None:
588595
else:
589596
self._enable_feature(pkg, f)
590597

598+
def _get_cfgs(self) -> T.Dict[str, str]:
599+
cfgs = self.host_rustc.get_cfgs().copy()
600+
rustflags = self.environment.coredata.get_external_args(MachineChoice.HOST, 'rust')
601+
rustflags_i = iter(rustflags)
602+
for i in rustflags_i:
603+
if i == '--cfg':
604+
cfgs.append(next(rustflags_i))
605+
return dict(self._split_cfg(i) for i in cfgs)
606+
607+
@staticmethod
608+
def _split_cfg(cfg: str) -> T.Tuple[str, str]:
609+
pair = cfg.split('=', maxsplit=1)
610+
value = pair[1] if len(pair) > 1 else ''
611+
if value and value[0] == '"':
612+
value = value[1:-1]
613+
return pair[0], value
614+
591615
def _create_project(self, pkg: PackageState, build: builder.Builder) -> T.List[mparser.BaseNode]:
592616
"""Create the project() function call
593617

mesonbuild/compilers/rust.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,20 @@ def _native_static_libs(self, work_dir: str, source_name: str) -> None:
142142
def get_dependency_gen_args(self, outtarget: str, outfile: str) -> T.List[str]:
143143
return ['--dep-info', outfile]
144144

145+
@functools.lru_cache(maxsize=None)
145146
def get_sysroot(self) -> str:
146147
cmd = self.get_exelist(ccache=False) + ['--print', 'sysroot']
147148
p, stdo, stde = Popen_safe_logged(cmd)
148149
return stdo.split('\n', maxsplit=1)[0]
149150

150151
@functools.lru_cache(maxsize=None)
151-
def get_crt_static(self) -> bool:
152+
def get_cfgs(self) -> T.List[str]:
152153
cmd = self.get_exelist(ccache=False) + ['--print', 'cfg']
153154
p, stdo, stde = Popen_safe_logged(cmd)
154-
return bool(re.search('^target_feature="crt-static"$', stdo, re.MULTILINE))
155+
return stdo.splitlines()
156+
157+
def get_crt_static(self) -> bool:
158+
return 'target_feature="crt-static"' in self.get_cfgs()
155159

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

mesonbuild/interpreter/interpreter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,7 @@ def _do_subproject_cargo(self, subp_name: str, subdir: str,
10361036
mlog.warning('Cargo subproject is an experimental feature and has no backwards compatibility guarantees.',
10371037
once=True, location=self.current_node)
10381038
if self.environment.cargo is None:
1039+
self.add_languages(['rust'], True, MachineChoice.HOST)
10391040
self.environment.cargo = cargo.Interpreter(self.environment)
10401041
with mlog.nested(subp_name):
10411042
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)