Associated Rustc with a build config
authorAleksey Kladov <aleksey.kladov@gmail.com>
Sat, 14 Apr 2018 08:49:13 +0000 (11:49 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Sat, 14 Apr 2018 08:49:13 +0000 (11:49 +0300)
We want to create `rustc` only when we already have workspace, so that
we could use the `target` dir to store cached info about the compiler.

src/bin/cli.rs
src/cargo/core/compiler/compilation.rs
src/cargo/core/compiler/context/compilation_files.rs
src/cargo/core/compiler/context/mod.rs
src/cargo/core/compiler/context/target_info.rs
src/cargo/core/compiler/custom_build.rs
src/cargo/core/compiler/fingerprint.rs
src/cargo/core/compiler/mod.rs
src/cargo/ops/cargo_test.rs
src/cargo/util/config.rs

index 548557395474b02d417a0909a4cf3d06fa7d7e4c..768012a44454825d1bd416c1329c5883149790e3 100644 (file)
@@ -47,7 +47,7 @@ Run with 'cargo -Z [FLAG] [SUBCOMMAND]'"
     }
 
     if let Some(ref code) = args.value_of("explain") {
-        let mut procss = config.rustc()?.process();
+        let mut procss = config.new_rustc()?.process();
         procss.arg("--explain").arg(code).exec()?;
         return Ok(());
     }
index b8dcdb5ec1c386a75fea2965e593194144f2c061..71bddf20faee015fe79e8fac49a8b63984c4b2ad 100644 (file)
@@ -57,15 +57,17 @@ pub struct Compilation<'cfg> {
     /// Flags to pass to rustdoc when invoked from cargo test, per package.
     pub rustdocflags: HashMap<PackageId, Vec<String>>,
 
+    pub host: String,
     pub target: String,
 
     config: &'cfg Config,
+    rustc_process: ProcessBuilder,
 
     target_runner: LazyCell<Option<(PathBuf, Vec<String>)>>,
 }
 
 impl<'cfg> Compilation<'cfg> {
-    pub fn new(config: &'cfg Config) -> Compilation<'cfg> {
+    pub fn new(config: &'cfg Config, rustc_process: ProcessBuilder) -> Compilation<'cfg> {
         Compilation {
             libraries: HashMap::new(),
             native_dirs: BTreeSet::new(), // TODO: deprecated, remove
@@ -81,6 +83,8 @@ impl<'cfg> Compilation<'cfg> {
             cfgs: HashMap::new(),
             rustdocflags: HashMap::new(),
             config,
+            rustc_process,
+            host: String::new(),
             target: String::new(),
             target_runner: LazyCell::new(),
         }
@@ -88,7 +92,7 @@ impl<'cfg> Compilation<'cfg> {
 
     /// See `process`.
     pub fn rustc_process(&self, pkg: &Package) -> CargoResult<ProcessBuilder> {
-        self.fill_env(self.config.rustc()?.process(), pkg, true)
+        self.fill_env(self.rustc_process.clone(), pkg, true)
     }
 
     /// See `process`.
index da49868c3de3f8b011fb21c0c3aadeec4947414f..88aa1cfc761f546290dca17f74698f7c23a6a56a 100644 (file)
@@ -434,9 +434,7 @@ fn compute_metadata<'a, 'cfg>(
     unit.target.name().hash(&mut hasher);
     unit.target.kind().hash(&mut hasher);
 
-    if let Ok(rustc) = cx.config.rustc() {
-        rustc.verbose_version.hash(&mut hasher);
-    }
+    cx.build_config.rustc.verbose_version.hash(&mut hasher);
 
     // Seed the contents of __CARGO_DEFAULT_LIB_METADATA to the hasher if present.
     // This should be the release channel, to get a different hash for each channel.
index e6fab8ed0d6efd1c289c2bd5bb40a634e6036855..f5f279a2d94a44039bcd9fe39be95c255827d7ed 100644 (file)
@@ -130,7 +130,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
             let _p = profile::start("Context::probe_target_info");
             debug!("probe_target_info");
             let host_target_same = match build_config.requested_target {
-                Some(ref s) if s != &config.rustc()?.host => false,
+                Some(ref s) if s != &build_config.host_triple => false,
                 _ => true,
             };
 
@@ -150,7 +150,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
             config,
             target_info,
             host_info,
-            compilation: Compilation::new(config),
+            compilation: Compilation::new(config, build_config.rustc.process()),
             build_state: Arc::new(BuildState::new(&build_config)),
             build_config,
             fingerprints: HashMap::new(),
@@ -302,6 +302,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
                 self.compilation.native_dirs.insert(dir.clone());
             }
         }
+        self.compilation.host = self.build_config.host_triple.clone();
         self.compilation.target = self.build_config.target_triple().to_string();
         Ok(self.compilation)
     }
index 40a840103757a22326713caffd827682992a570b..78a2220ca53be0e7b396a8a4569e86af077427af 100644 (file)
@@ -53,7 +53,7 @@ impl FileType {
 impl TargetInfo {
     pub fn new(config: &Config, build_config: &BuildConfig, kind: Kind) -> CargoResult<TargetInfo> {
         let rustflags = env_args(config, build_config, None, kind, "RUSTFLAGS")?;
-        let mut process = config.rustc()?.process();
+        let mut process = build_config.rustc.process();
         process
             .arg("-")
             .arg("--crate-name")
index 3c94e1e002853382ab013570a558194877b6334c..578e2312cb4dba736c6fe04bddb89e582f837982 100644 (file)
@@ -142,7 +142,7 @@ fn build_work<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoRes
             },
         )
         .env("HOST", &cx.build_config.host_triple)
-        .env("RUSTC", &cx.config.rustc()?.path)
+        .env("RUSTC", &cx.build_config.rustc.path)
         .env("RUSTDOC", &*cx.config.rustdoc()?)
         .inherit_jobserver(&cx.jobserver);
 
index 5e734f1ae3691f944fec369262ed1981a5381ea2..8387fd2fdef726fe3ea67c5f7ced77c88c897787 100644 (file)
@@ -455,7 +455,7 @@ fn calculate<'a, 'cfg>(
         cx.rustflags_args(unit)?
     };
     let fingerprint = Arc::new(Fingerprint {
-        rustc: util::hash_u64(&cx.config.rustc()?.verbose_version),
+        rustc: util::hash_u64(&cx.build_config.rustc.verbose_version),
         target: util::hash_u64(&unit.target),
         profile: util::hash_u64(&(&unit.profile, cx.incremental_args(unit)?)),
         // Note that .0 is hashed here, not .1 which is the cwd. That doesn't
index 127e85e92d947b582d8fa84520ca0e7cc4a57ee8..f626637c74ec6938645dadae3a021c8ab66b7383 100644 (file)
@@ -12,11 +12,10 @@ use serde_json;
 use core::{Feature, PackageId, Profile, Target};
 use core::manifest::Lto;
 use core::shell::ColorChoice;
-use util::{self, machine_message, Config, ProcessBuilder};
+use util::{self, machine_message, Config, ProcessBuilder, Rustc, Freshness};
 use util::{internal, join_paths, profile};
 use util::paths;
 use util::errors::{CargoResult, CargoResultExt, Internal};
-use util::Freshness;
 
 use self::job::{Job, Work};
 use self::job_queue::JobQueue;
@@ -48,8 +47,8 @@ pub enum Kind {
 }
 
 /// Configuration information for a rustc build.
-#[derive(Default, Clone)]
 pub struct BuildConfig {
+    pub rustc: Rustc,
     /// The host arch triple
     ///
     /// e.g. x86_64-unknown-linux-gnu, would be
@@ -125,20 +124,24 @@ impl BuildConfig {
             None => None,
         };
         let jobs = jobs.or(cfg_jobs).unwrap_or(::num_cpus::get() as u32);
-
-        let host_triple = config.rustc()?.host.clone();
+        let rustc = config.new_rustc()?;
+        let host_triple = 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 {
+            rustc,
             host_triple,
             requested_target: target,
             jobs,
             host: host_config,
             target: target_config,
-            ..Default::default()
+            release: false,
+            test: false,
+            doc_all: false,
+            json_messages: false,
         })
     }
 
index de0bb07b6267f95f86f1b06362e39cc185cc5f65..c65cc1970d47fc75eb4336348f753e4fa44d26c5 100644 (file)
@@ -150,7 +150,7 @@ fn run_doc_tests(
     let config = options.compile_opts.config;
 
     // We don't build/rust doctests if target != host
-    if config.rustc()?.host != compilation.target {
+    if compilation.host != compilation.target {
         return Ok((Test::Doc, errors));
     }
 
index 281165ec209a49424a8098f7971d5259f9d8741e..a95c56c7e0ace339cf17f46c9cd681784dffd836 100644 (file)
@@ -158,13 +158,11 @@ impl Config {
     }
 
     /// Get the path to the `rustc` executable
-    pub fn rustc(&self) -> CargoResult<&Rustc> {
-        self.rustc.try_borrow_with(|| {
-            Rustc::new(
-                self.get_tool("rustc")?,
-                self.maybe_get_tool("rustc_wrapper")?,
-            )
-        })
+    pub fn new_rustc(&self) -> CargoResult<Rustc> {
+        Rustc::new(
+            self.get_tool("rustc")?,
+            self.maybe_get_tool("rustc_wrapper")?,
+        )
     }
 
     /// Get the path to the `cargo` executable