Supply units to the context at the point of creation
authorAleksey Kladov <aleksey.kladov@gmail.com>
Tue, 27 Mar 2018 16:03:22 +0000 (19:03 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Tue, 27 Mar 2018 16:03:22 +0000 (19:03 +0300)
src/cargo/ops/cargo_clean.rs
src/cargo/ops/cargo_rustc/context/mod.rs
src/cargo/ops/cargo_rustc/mod.rs

index 7f7d96225daa5076c8b23fd4518cba321134bade..bc0bc51f8ef730bef2e0a0690d09de627e7129fb 100644 (file)
@@ -37,20 +37,6 @@ pub fn clean(ws: &Workspace, opts: &CleanOptions) -> CargoResult<()> {
 
     let profiles = ws.profiles();
     let host_triple = opts.config.rustc()?.host.clone();
-    let mut cx = Context::new(
-        ws,
-        &resolve,
-        &packages,
-        opts.config,
-        BuildConfig {
-            host_triple,
-            requested_target: opts.target.clone(),
-            release: opts.release,
-            jobs: 1,
-            ..BuildConfig::default()
-        },
-        profiles,
-    )?;
     let mut units = Vec::new();
 
     for spec in opts.spec.iter() {
@@ -99,8 +85,21 @@ pub fn clean(ws: &Workspace, opts: &CleanOptions) -> CargoResult<()> {
         }
     }
 
-    cx.probe_target_info()?;
-    cx.build_unit_dependencies(&units)?;
+    let mut cx = Context::new(
+        ws,
+        &resolve,
+        &packages,
+        opts.config,
+        BuildConfig {
+            host_triple,
+            requested_target: opts.target.clone(),
+            release: opts.release,
+            jobs: 1,
+            ..BuildConfig::default()
+        },
+        profiles,
+        &units,
+    )?;
 
     for unit in units.iter() {
         rm_rf(&cx.fingerprint_dir(unit), config)?;
index 9fd597223e8daa02e3f9ec0d8dd745021f4be99d..7bb9b088f4fa375c47b247479b1f0eb6df7e836a 100644 (file)
@@ -104,7 +104,7 @@ pub struct Context<'a, 'cfg: 'a> {
     profiles: &'a Profiles,
     incremental_env: Option<bool>,
 
-    unit_dependencies: Option<HashMap<Unit<'a>, Vec<Unit<'a>>>>,
+    unit_dependencies: HashMap<Unit<'a>, Vec<Unit<'a>>>,
     /// For each Unit, a list all files produced as a triple of
     ///
     ///  - File name that will be produced by the build process (in `deps`)
@@ -154,6 +154,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
         config: &'cfg Config,
         build_config: BuildConfig,
         profiles: &'a Profiles,
+        units: &[Unit<'a>],
     ) -> CargoResult<Context<'a, 'cfg>> {
         let dest = if build_config.release {
             "release"
@@ -184,8 +185,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
             None => Client::new(build_config.jobs as usize - 1)
                 .chain_err(|| "failed to create jobserver")?,
         };
-
-        Ok(Context {
+        let mut cx = Context {
             ws,
             host: host_layout,
             target: target_layout,
@@ -208,11 +208,17 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
             jobserver,
             build_script_overridden: HashSet::new(),
 
-            unit_dependencies: None,
+            unit_dependencies: HashMap::new(),
             // TODO: Pre-Calculate these with a topo-sort, rather than lazy-calculating
             target_filenames: HashMap::new(),
             target_metadatas: HashMap::new(),
-        })
+        };
+
+        cx.probe_target_info()?;
+        let deps = build_unit_dependencies(units, &cx)?;
+        cx.unit_dependencies = deps;
+
+        Ok(cx)
     }
 
     /// Prepare this context, ensuring that all filesystem directories are in
@@ -237,15 +243,9 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
         Ok(())
     }
 
-    pub fn build_unit_dependencies(&mut self, units: &[Unit<'a>]) -> CargoResult<()> {
-        assert!(self.unit_dependencies.is_none());
-        self.unit_dependencies = Some(build_unit_dependencies(units, self)?);
-        Ok(())
-    }
-
     /// Ensure that we've collected all target-specific information to compile
     /// all the units mentioned in `units`.
-    pub fn probe_target_info(&mut self) -> CargoResult<()> {
+    fn probe_target_info(&mut self) -> CargoResult<()> {
         debug!("probe_target_info");
         let host_target_same = match self.requested_target() {
             Some(s) if s != self.config.rustc()?.host => false,
@@ -809,7 +809,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
                 return Vec::new();
             }
         }
-        self.unit_dependencies.as_ref().unwrap()[unit].clone()
+        self.unit_dependencies[unit].clone()
     }
 
     fn dep_platform_activated(&self, dep: &Dependency, kind: Kind) -> bool {
index 695b882c3e67a15645de11c8b667a07d29476eca..950a829bec2307492b4503affc890599a3ef2293 100644 (file)
@@ -164,13 +164,19 @@ pub fn compile_targets<'a, 'cfg: 'a>(
         })
         .collect::<Vec<_>>();
 
-    let mut cx = Context::new(ws, resolve, packages, config, build_config, profiles)?;
+    let mut cx = Context::new(
+        ws,
+        resolve,
+        packages,
+        config,
+        build_config,
+        profiles,
+        &units,
+    )?;
 
     let mut queue = JobQueue::new(&cx);
 
     cx.prepare()?;
-    cx.probe_target_info()?;
-    cx.build_unit_dependencies(&units)?;
     cx.build_used_in_plugin_map(&units)?;
     custom_build::build_map(&mut cx, &units)?;