]> git.proxmox.com Git - cargo.git/commitdiff
deduplicate logic shared with `build_context::env_args`
authorJorge Aparicio <jorge@japaric.io>
Sat, 8 Sep 2018 12:50:35 +0000 (14:50 +0200)
committerJorge Aparicio <jorge@japaric.io>
Sat, 8 Sep 2018 12:50:35 +0000 (14:50 +0200)
src/cargo/core/compiler/build_context/mod.rs
src/cargo/core/compiler/compilation.rs
src/cargo/util/cfg.rs

index ed65d6fc3e7893eee3529209e837e2c44d8c88e6..094b61bb28a429dee3e0695657844a5de80bada6 100644 (file)
@@ -1,7 +1,7 @@
 use std::collections::HashMap;
 use std::env;
 use std::path::{Path, PathBuf};
-use std::str::{self, FromStr};
+use std::str;
 
 use core::profiles::Profiles;
 use core::{Dependency, Workspace};
@@ -393,16 +393,9 @@ fn env_args(
     // ...including target.'cfg(...)'.rustflags
     if let Some(target_cfg) = target_cfg {
         if let Some(table) = config.get_table("target")? {
-            let cfgs = table.val.keys().filter_map(|t| {
-                if t.starts_with("cfg(") && t.ends_with(')') {
-                    let cfg = &t[4..t.len() - 1];
-                    CfgExpr::from_str(cfg).ok().and_then(|c| {
-                        if c.matches(target_cfg) {
-                            Some(t)
-                        } else {
-                            None
-                        }
-                    })
+            let cfgs = table.val.keys().filter_map(|key| {
+                if CfgExpr::matches_key(key, target_cfg) {
+                    Some(key)
                 } else {
                     None
                 }
index ea65df0ac775342e6720870c9f79f62479a9a446..d479a6141255012a584048ee994e1846926a2ff6 100644 (file)
@@ -2,7 +2,6 @@ use std::collections::{BTreeSet, HashMap, HashSet};
 use std::env;
 use std::ffi::OsStr;
 use std::path::PathBuf;
-use std::str::FromStr;
 
 use semver::Version;
 
@@ -275,22 +274,17 @@ fn target_runner(bcx: &BuildContext) -> CargoResult<Option<(PathBuf, Vec<String>
         if let Some(table) = bcx.config.get_table("target")? {
             let mut matching_runner = None;
 
-            for t in table.val.keys() {
-                if t.starts_with("cfg(") && t.ends_with(')') {
-                    let cfg = &t[4..t.len() - 1];
-                    if let Ok(c) = CfgExpr::from_str(cfg) {
-                        if c.matches(target_cfg) {
-                            let key = format!("target.{}.runner", t);
-                            if let Some(runner) = bcx.config.get_path_and_args(&key)? {
-                                // more than one match, error out
-                                if matching_runner.is_some() {
-                                    bail!("several matching instances of `target.'cfg(..)'.runner` \
-                                           in `.cargo/config`")
-                                }
-
-                                matching_runner = Some(runner.val);
-                            }
+            for key in table.val.keys() {
+                if CfgExpr::matches_key(key, target_cfg) {
+                    let key = format!("target.{}.runner", key);
+                    if let Some(runner) = bcx.config.get_path_and_args(&key)? {
+                        // more than one match, error out
+                        if matching_runner.is_some() {
+                            bail!("several matching instances of `target.'cfg(..)'.runner` \
+                                   in `.cargo/config`")
                         }
+
+                        matching_runner = Some(runner.val);
                     }
                 }
             }
index a0bfef6f348906c1e0470e0da3ded7632cbb0760..877452c8ff02a84a8fbb7856afb3bcf77dda03c9 100644 (file)
@@ -60,6 +60,17 @@ impl fmt::Display for Cfg {
 }
 
 impl CfgExpr {
+    /// Utility function to check if the key, "cfg(..)" matches the `target_cfg`
+    pub fn matches_key(key: &str, target_cfg: &[Cfg]) -> bool {
+        if key.starts_with("cfg(") && key.ends_with(')') {
+            let cfg = &key[4..key.len() - 1 ];
+
+            CfgExpr::from_str(cfg).ok().map(|ce| ce.matches(target_cfg)).unwrap_or(false)
+        } else {
+            false
+        }
+    }
+
     pub fn matches(&self, cfg: &[Cfg]) -> bool {
         match *self {
             CfgExpr::Not(ref e) => !e.matches(cfg),