Skip to content

Commit 33a6d99

Browse files
committed
deduplicate logic shared with build_context::env_args
1 parent 6a12aa5 commit 33a6d99

File tree

3 files changed

+25
-27
lines changed

3 files changed

+25
-27
lines changed

src/cargo/core/compiler/build_context/mod.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::HashMap;
22
use std::env;
33
use std::path::{Path, PathBuf};
4-
use std::str::{self, FromStr};
4+
use std::str;
55

66
use core::profiles::Profiles;
77
use core::{Dependency, Workspace};
@@ -393,16 +393,9 @@ fn env_args(
393393
// ...including target.'cfg(...)'.rustflags
394394
if let Some(target_cfg) = target_cfg {
395395
if let Some(table) = config.get_table("target")? {
396-
let cfgs = table.val.keys().filter_map(|t| {
397-
if t.starts_with("cfg(") && t.ends_with(')') {
398-
let cfg = &t[4..t.len() - 1];
399-
CfgExpr::from_str(cfg).ok().and_then(|c| {
400-
if c.matches(target_cfg) {
401-
Some(t)
402-
} else {
403-
None
404-
}
405-
})
396+
let cfgs = table.val.keys().filter_map(|key| {
397+
if CfgExpr::matches_key(key, target_cfg) {
398+
Some(key)
406399
} else {
407400
None
408401
}

src/cargo/core/compiler/compilation.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::collections::{BTreeSet, HashMap, HashSet};
22
use std::env;
33
use std::ffi::OsStr;
44
use std::path::PathBuf;
5-
use std::str::FromStr;
65

76
use semver::Version;
87

@@ -275,22 +274,17 @@ fn target_runner(bcx: &BuildContext) -> CargoResult<Option<(PathBuf, Vec<String>
275274
if let Some(table) = bcx.config.get_table("target")? {
276275
let mut matching_runner = None;
277276

278-
for t in table.val.keys() {
279-
if t.starts_with("cfg(") && t.ends_with(')') {
280-
let cfg = &t[4..t.len() - 1];
281-
if let Ok(c) = CfgExpr::from_str(cfg) {
282-
if c.matches(target_cfg) {
283-
let key = format!("target.{}.runner", t);
284-
if let Some(runner) = bcx.config.get_path_and_args(&key)? {
285-
// more than one match, error out
286-
if matching_runner.is_some() {
287-
bail!("several matching instances of `target.'cfg(..)'.runner` \
288-
in `.cargo/config`")
289-
}
290-
291-
matching_runner = Some(runner.val);
292-
}
277+
for key in table.val.keys() {
278+
if CfgExpr::matches_key(key, target_cfg) {
279+
let key = format!("target.{}.runner", key);
280+
if let Some(runner) = bcx.config.get_path_and_args(&key)? {
281+
// more than one match, error out
282+
if matching_runner.is_some() {
283+
bail!("several matching instances of `target.'cfg(..)'.runner` \
284+
in `.cargo/config`")
293285
}
286+
287+
matching_runner = Some(runner.val);
294288
}
295289
}
296290
}

src/cargo/util/cfg.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ impl fmt::Display for Cfg {
6060
}
6161

6262
impl CfgExpr {
63+
/// Utility function to check if the key, "cfg(..)" matches the `target_cfg`
64+
pub fn matches_key(key: &str, target_cfg: &[Cfg]) -> bool {
65+
if key.starts_with("cfg(") && key.ends_with(')') {
66+
let cfg = &key[4..key.len() - 1 ];
67+
68+
CfgExpr::from_str(cfg).ok().map(|ce| ce.matches(target_cfg)).unwrap_or(false)
69+
} else {
70+
false
71+
}
72+
}
73+
6374
pub fn matches(&self, cfg: &[Cfg]) -> bool {
6475
match *self {
6576
CfgExpr::Not(ref e) => !e.matches(cfg),

0 commit comments

Comments
 (0)