Move BuildConfig initialization into new() method
authorDirkjan Ochtman <dirkjan@ochtman.nl>
Thu, 12 Apr 2018 14:12:14 +0000 (16:12 +0200)
committerDirkjan Ochtman <dirkjan@ochtman.nl>
Thu, 12 Apr 2018 14:12:14 +0000 (16:12 +0200)
src/cargo/core/compiler/mod.rs
src/cargo/ops/cargo_clean.rs
src/cargo/ops/cargo_compile.rs

index cc527bc3c379131f21b764845591964139d60851..0c650083a19b28fe28f3f63516f7420bd95bccba 100644 (file)
@@ -76,16 +76,68 @@ pub struct BuildConfig {
 }
 
 impl BuildConfig {
-    pub fn new(host_triple: &str, requested_target: &Option<String>) -> CargoResult<BuildConfig> {
-        if let Some(ref s) = *requested_target {
+    /// Parse all config files to learn about build configuration. Currently
+    /// configured options are:
+    ///
+    /// * build.jobs
+    /// * build.target
+    /// * target.$target.ar
+    /// * target.$target.linker
+    /// * target.$target.libfoo.metadata
+    pub fn new(
+        config: &Config,
+        jobs: Option<u32>,
+        requested_target: &Option<String>,
+    ) -> CargoResult<BuildConfig> {
+        if let &Some(ref s) = requested_target {
             if s.trim().is_empty() {
                 bail!("target was empty")
             }
         }
+        let cfg_target = config.get_string("build.target")?.map(|s| s.val);
+        let target = requested_target.clone().or(cfg_target);
+
+        if jobs.is_some() && config.jobserver_from_env().is_some() {
+            config.shell().warn(
+                "a `-j` argument was passed to Cargo but Cargo is \
+                 also configured with an external jobserver in \
+                 its environment, ignoring the `-j` parameter",
+            )?;
+        }
+        let cfg_jobs = match config.get_i64("build.jobs")? {
+            Some(v) => {
+                if v.val <= 0 {
+                    bail!(
+                        "build.jobs must be positive, but found {} in {}",
+                        v.val,
+                        v.definition
+                    )
+                } else if v.val >= i64::from(u32::max_value()) {
+                    bail!(
+                        "build.jobs is too large: found {} in {}",
+                        v.val,
+                        v.definition
+                    )
+                } else {
+                    Some(v.val as u32)
+                }
+            }
+            None => None,
+        };
+        let jobs = jobs.or(cfg_jobs).unwrap_or(::num_cpus::get() as u32);
+
+        let host_triple = config.rustc()?.host.clone();
+        let host_config = TargetConfig::new(config, &host_triple)?;
+        let target_config = match target.as_ref() {
+            Some(triple) => TargetConfig::new(config, triple)?,
+            None => host_config.clone(),
+        };
         Ok(BuildConfig {
-            host_triple: host_triple.to_string(),
-            requested_target: (*requested_target).clone(),
-            jobs: 1,
+            host_triple,
+            requested_target: target,
+            jobs,
+            host: host_config,
+            target: target_config,
             ..Default::default()
         })
     }
index 0cab1d9728c8effcdd77a8e8f9aebe8256de45b3..43a89ea64230ef0b8d355482e45c6181b5104343 100644 (file)
@@ -84,7 +84,7 @@ pub fn clean(ws: &Workspace, opts: &CleanOptions) -> CargoResult<()> {
         }
     }
 
-    let mut build_config = BuildConfig::new(&opts.config.rustc()?.host, &opts.target)?;
+    let mut build_config = BuildConfig::new(config, Some(1), &opts.target)?;
     build_config.release = opts.release;
     let mut cx = Context::new(ws, &resolve, &packages, opts.config, build_config, profiles)?;
     cx.prepare_units(None, &units)?;
index 6745fa490f965aa1615bd1bec195e2d20d98eba4..e734c4cb8010756fedba77826c603b5af1695165 100644 (file)
@@ -29,7 +29,7 @@ use std::sync::Arc;
 use core::{Package, Source, Target};
 use core::{PackageId, PackageIdSpec, Profile, Profiles, TargetKind, Workspace};
 use core::compiler::{BuildConfig, Compilation, Context, DefaultExecutor, Executor};
-use core::compiler::{Kind, TargetConfig, Unit};
+use core::compiler::{Kind, Unit};
 use core::resolver::{Method, Resolve};
 use ops;
 use util::config::Config;
@@ -260,7 +260,7 @@ pub fn compile_ws<'a>(
         bail!("jobs must be at least 1")
     }
 
-    let mut build_config = scrape_build_config(config, jobs, target)?;
+    let mut build_config = BuildConfig::new(config, jobs, &target)?;
     build_config.release = release;
     build_config.test = mode == CompileMode::Test || mode == CompileMode::Bench;
     build_config.json_messages = message_format == MessageFormat::Json;
@@ -833,56 +833,3 @@ fn generate_targets<'a>(
 
     filter_compatible_targets(targets, features)
 }
-
-/// Parse all config files to learn about build configuration. Currently
-/// configured options are:
-///
-/// * build.jobs
-/// * build.target
-/// * target.$target.ar
-/// * target.$target.linker
-/// * target.$target.libfoo.metadata
-fn scrape_build_config(
-    config: &Config,
-    jobs: Option<u32>,
-    target: Option<String>,
-) -> CargoResult<BuildConfig> {
-    if jobs.is_some() && config.jobserver_from_env().is_some() {
-        config.shell().warn(
-            "a `-j` argument was passed to Cargo but Cargo is \
-             also configured with an external jobserver in \
-             its environment, ignoring the `-j` parameter",
-        )?;
-    }
-    let cfg_jobs = match config.get_i64("build.jobs")? {
-        Some(v) => {
-            if v.val <= 0 {
-                bail!(
-                    "build.jobs must be positive, but found {} in {}",
-                    v.val,
-                    v.definition
-                )
-            } else if v.val >= i64::from(u32::max_value()) {
-                bail!(
-                    "build.jobs is too large: found {} in {}",
-                    v.val,
-                    v.definition
-                )
-            } else {
-                Some(v.val as u32)
-            }
-        }
-        None => None,
-    };
-    let jobs = jobs.or(cfg_jobs).unwrap_or(::num_cpus::get() as u32);
-    let cfg_target = config.get_string("build.target")?.map(|s| s.val);
-    let target = target.or(cfg_target);
-    let mut base = BuildConfig::new(&config.rustc()?.host, &target)?;
-    base.jobs = jobs;
-    base.host = TargetConfig::new(config, &base.host_triple)?;
-    base.target = match target.as_ref() {
-        Some(triple) => TargetConfig::new(config, triple)?,
-        None => base.host.clone(),
-    };
-    Ok(base)
-}