Store compile mode as enum in BuildConfig
authorDirkjan Ochtman <dirkjan@ochtman.nl>
Sat, 14 Apr 2018 08:49:15 +0000 (10:49 +0200)
committerDirkjan Ochtman <dirkjan@ochtman.nl>
Thu, 3 May 2018 19:54:48 +0000 (21:54 +0200)
src/bin/cargo/command_prelude.rs
src/cargo/core/compiler/context/mod.rs
src/cargo/core/compiler/context/unit_dependencies.rs
src/cargo/core/compiler/job_queue.rs
src/cargo/core/compiler/mod.rs
src/cargo/core/profiles.rs
src/cargo/ops/cargo_clean.rs
src/cargo/ops/cargo_compile.rs
src/cargo/ops/cargo_fetch.rs
src/cargo/ops/cargo_package.rs
src/cargo/ops/mod.rs

index 3f98f9ea482971b4debdfda40abc3f127b7daea7..fb1b907a4216b246df0a25c3d6b1b70b02ef8825 100644 (file)
@@ -11,7 +11,7 @@ use cargo::util::important_paths::find_root_manifest_for_wd;
 
 pub use clap::{AppSettings, Arg, ArgMatches};
 pub use cargo::{CliError, CliResult, Config};
-pub use cargo::ops::CompileMode;
+pub use cargo::core::compiler::CompileMode;
 
 pub type App = clap::App<'static, 'static>;
 
index a0e166860eac1be727989ef0ef9e2e22035a7b28..02b6f11112096582945ca21cbe1a87568b9c50df 100644 (file)
@@ -8,7 +8,6 @@ use jobserver::Client;
 
 use core::{Package, PackageId, Resolve, Target};
 use core::profiles::Profile;
-use ops::CompileMode;
 use util::errors::{CargoResult, CargoResultExt};
 use util::{internal, profile, Config};
 
@@ -16,7 +15,7 @@ use super::custom_build::{self, BuildDeps, BuildScripts, BuildState};
 use super::fingerprint::Fingerprint;
 use super::job_queue::JobQueue;
 use super::layout::Layout;
-use super::{BuildContext, Compilation, Executor, FileFlavor, Kind};
+use super::{BuildContext, Compilation, CompileMode, Executor, FileFlavor, Kind};
 
 mod unit_dependencies;
 use self::unit_dependencies::build_unit_dependencies;
index ee66f62d97026831abe82461eb1b66e68e03b50a..e53c3da374d9b538bb4658c41edc027a2583c354 100644 (file)
 //! (for example, with and without tests), so we actually build a dependency
 //! graph of `Unit`s, which capture these properties.
 
-use super::{BuildContext, Kind, Unit};
+use super::{BuildContext, CompileMode, Kind, Unit};
 use core::dependency::Kind as DepKind;
 use core::profiles::ProfileFor;
 use core::{Package, Target};
-use ops::CompileMode;
 use std::collections::HashMap;
 use CargoResult;
 
@@ -35,7 +34,7 @@ pub fn build_unit_dependencies<'a, 'cfg>(
         // cleared, and avoid building the lib thrice (once with `panic`, once
         // without, once for --test).  In particular, the lib included for
         // doctests and examples are `Build` mode here.
-        let profile_for = if unit.mode.is_any_test() || bcx.build_config.test {
+        let profile_for = if unit.mode.is_any_test() || bcx.build_config.test() {
             ProfileFor::TestDependency
         } else {
             ProfileFor::Any
index f167b49de64b66d700ed23cd6e5968ec10f1d1fd..235dccc93e6fbb93013ec2a34ab29a1a7142d685 100644 (file)
@@ -11,12 +11,11 @@ use jobserver::{Acquired, HelperThread};
 use core::profiles::Profile;
 use core::{PackageId, Target};
 use handle_error;
-use ops::CompileMode;
 use util::{internal, profile, CargoResult, CargoResultExt, ProcessBuilder};
 use util::{Config, DependencyQueue, Dirty, Fresh, Freshness};
 
 use super::job::Job;
-use super::{BuildContext, Context, Kind, Unit};
+use super::{BuildContext, CompileMode, Context, Kind, Unit};
 
 /// A management structure of the entire dependency graph to compile.
 ///
index 82cbbc43de440178fee33118dfd50c24fe222b0e..031321faf2d881de879498ac6e7510f0756e0841 100644 (file)
@@ -12,7 +12,6 @@ use serde_json;
 use core::profiles::{Lto, Profile};
 use core::shell::ColorChoice;
 use core::{Feature, PackageId, Target};
-use ops::CompileMode;
 use util::errors::{CargoResult, CargoResultExt, Internal};
 use util::paths;
 use util::{self, machine_message, Config, Freshness, ProcessBuilder, Rustc};
@@ -61,8 +60,8 @@ pub struct BuildConfig {
     pub jobs: u32,
     /// Whether we are building for release
     pub release: bool,
-    /// Whether we are running tests
-    pub test: bool,
+    /// In what mode we are compiling
+    pub mode: CompileMode,
     /// Whether to print std output in json format (for machine reading)
     pub message_format: MessageFormat,
 }
@@ -81,6 +80,7 @@ impl BuildConfig {
         jobs: Option<u32>,
         requested_target: &Option<String>,
         rustc_info_cache: Option<PathBuf>,
+        mode: CompileMode,
     ) -> CargoResult<BuildConfig> {
         if let &Some(ref s) = requested_target {
             if s.trim().is_empty() {
@@ -131,7 +131,7 @@ impl BuildConfig {
             host: host_config,
             target: target_config,
             release: false,
-            test: false,
+            mode,
             message_format: MessageFormat::Human,
         })
     }
@@ -156,6 +156,10 @@ impl BuildConfig {
     pub fn json_messages(&self) -> bool {
         self.message_format == MessageFormat::Json
     }
+
+    pub fn test(&self) -> bool {
+        self.mode == CompileMode::Test || self.mode == CompileMode::Bench
+    }
 }
 
 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -164,6 +168,90 @@ pub enum MessageFormat {
     Json,
 }
 
+/// The general "mode" of what to do.
+/// This is used for two purposes.  The commands themselves pass this in to
+/// `compile_ws` to tell it the general execution strategy.  This influences
+/// the default targets selected.  The other use is in the `Unit` struct
+/// to indicate what is being done with a specific target.
+#[derive(Clone, Copy, PartialEq, Debug, Eq, Hash)]
+pub enum CompileMode {
+    /// A target being built for a test.
+    Test,
+    /// Building a target with `rustc` (lib or bin).
+    Build,
+    /// Building a target with `rustc` to emit `rmeta` metadata only. If
+    /// `test` is true, then it is also compiled with `--test` to check it like
+    /// a test.
+    Check { test: bool },
+    /// Used to indicate benchmarks should be built.  This is not used in
+    /// `Target` because it is essentially the same as `Test` (indicating
+    /// `--test` should be passed to rustc) and by using `Test` instead it
+    /// allows some de-duping of Units to occur.
+    Bench,
+    /// A target that will be documented with `rustdoc`.
+    /// If `deps` is true, then it will also document all dependencies.
+    Doc { deps: bool },
+    /// A target that will be tested with `rustdoc`.
+    Doctest,
+    /// A marker for Units that represent the execution of a `build.rs`
+    /// script.
+    RunCustomBuild,
+}
+
+impl CompileMode {
+    /// Returns true if the unit is being checked.
+    pub fn is_check(&self) -> bool {
+        match *self {
+            CompileMode::Check { .. } => true,
+            _ => false,
+        }
+    }
+
+    /// Returns true if this is a doc or doctest. Be careful using this.
+    /// Although both run rustdoc, the dependencies for those two modes are
+    /// very different.
+    pub fn is_doc(&self) -> bool {
+        match *self {
+            CompileMode::Doc { .. } | CompileMode::Doctest => true,
+            _ => false,
+        }
+    }
+
+    /// Returns true if this is any type of test (test, benchmark, doctest, or
+    /// check-test).
+    pub fn is_any_test(&self) -> bool {
+        match *self {
+            CompileMode::Test
+            | CompileMode::Bench
+            | CompileMode::Check { test: true }
+            | CompileMode::Doctest => true,
+            _ => false,
+        }
+    }
+
+    /// Returns true if this is the *execution* of a `build.rs` script.
+    pub fn is_run_custom_build(&self) -> bool {
+        *self == CompileMode::RunCustomBuild
+    }
+
+    /// List of all modes (currently used by `cargo clean -p` for computing
+    /// all possible outputs).
+    pub fn all_modes() -> &'static [CompileMode] {
+        static ALL: [CompileMode; 9] = [
+            CompileMode::Test,
+            CompileMode::Build,
+            CompileMode::Check { test: true },
+            CompileMode::Check { test: false },
+            CompileMode::Bench,
+            CompileMode::Doc { deps: true },
+            CompileMode::Doc { deps: false },
+            CompileMode::Doctest,
+            CompileMode::RunCustomBuild,
+        ];
+        &ALL
+    }
+}
+
 /// Information required to build for a target
 #[derive(Clone, Default)]
 pub struct TargetConfig {
index fbca991d68f53354b7d4f56e15025e8bbff319e2..8ad40abd8f6f4454cfc5f58ee991942e49b0040f 100644 (file)
@@ -1,9 +1,9 @@
 use std::collections::HashSet;
 use std::{cmp, fmt, hash};
 
+use core::compiler::CompileMode;
 use core::interning::InternedString;
 use core::Shell;
-use ops::CompileMode;
 use util::lev_distance::lev_distance;
 use util::toml::{StringOrBool, TomlProfile, U32OrBool};
 use util::CargoResult;
index f9f032760a7320c4543f4d8398b8f41205fe694e..6d5a2b7d9fffd5c08598055053533d720a6e872f 100644 (file)
@@ -1,10 +1,10 @@
 use std::fs;
 use std::path::Path;
 
-use core::compiler::{BuildConfig, BuildContext, Context, Kind, Unit};
+use core::compiler::{BuildConfig, BuildContext, CompileMode, Context, Kind, Unit};
 use core::profiles::ProfileFor;
 use core::Workspace;
-use ops::{self, CompileMode};
+use ops;
 use util::errors::{CargoResult, CargoResultExt};
 use util::paths;
 use util::Config;
@@ -88,7 +88,8 @@ pub fn clean(ws: &Workspace, opts: &CleanOptions) -> CargoResult<()> {
         }
     }
 
-    let mut build_config = BuildConfig::new(config, Some(1), &opts.target, None)?;
+    let mut build_config =
+        BuildConfig::new(config, Some(1), &opts.target, None, CompileMode::Build)?;
     build_config.release = opts.release;
     let bcx = BuildContext::new(
         ws,
index 87c37b0febce66dbb37b6bbcb64cd9f8e0131ac4..0abad64f07f1b0b245dd41958065c62f9b8b5efa 100644 (file)
@@ -27,7 +27,7 @@ use std::path::{Path, PathBuf};
 use std::sync::Arc;
 
 use core::compiler::{BuildConfig, BuildContext, Compilation, Context, DefaultExecutor, Executor};
-use core::compiler::{Kind, MessageFormat, Unit};
+use core::compiler::{CompileMode, Kind, MessageFormat, Unit};
 use core::profiles::{ProfileFor, Profiles};
 use core::resolver::{Method, Resolve};
 use core::{Package, Source, Target};
@@ -97,90 +97,6 @@ impl<'a> CompileOptions<'a> {
     }
 }
 
-/// The general "mode" of what to do.
-/// This is used for two purposes.  The commands themselves pass this in to
-/// `compile_ws` to tell it the general execution strategy.  This influences
-/// the default targets selected.  The other use is in the `Unit` struct
-/// to indicate what is being done with a specific target.
-#[derive(Clone, Copy, PartialEq, Debug, Eq, Hash)]
-pub enum CompileMode {
-    /// A target being built for a test.
-    Test,
-    /// Building a target with `rustc` (lib or bin).
-    Build,
-    /// Building a target with `rustc` to emit `rmeta` metadata only. If
-    /// `test` is true, then it is also compiled with `--test` to check it like
-    /// a test.
-    Check { test: bool },
-    /// Used to indicate benchmarks should be built.  This is not used in
-    /// `Target` because it is essentially the same as `Test` (indicating
-    /// `--test` should be passed to rustc) and by using `Test` instead it
-    /// allows some de-duping of Units to occur.
-    Bench,
-    /// A target that will be documented with `rustdoc`.
-    /// If `deps` is true, then it will also document all dependencies.
-    Doc { deps: bool },
-    /// A target that will be tested with `rustdoc`.
-    Doctest,
-    /// A marker for Units that represent the execution of a `build.rs`
-    /// script.
-    RunCustomBuild,
-}
-
-impl CompileMode {
-    /// Returns true if the unit is being checked.
-    pub fn is_check(&self) -> bool {
-        match *self {
-            CompileMode::Check { .. } => true,
-            _ => false,
-        }
-    }
-
-    /// Returns true if this is a doc or doctest. Be careful using this.
-    /// Although both run rustdoc, the dependencies for those two modes are
-    /// very different.
-    pub fn is_doc(&self) -> bool {
-        match *self {
-            CompileMode::Doc { .. } | CompileMode::Doctest => true,
-            _ => false,
-        }
-    }
-
-    /// Returns true if this is any type of test (test, benchmark, doctest, or
-    /// check-test).
-    pub fn is_any_test(&self) -> bool {
-        match *self {
-            CompileMode::Test
-            | CompileMode::Bench
-            | CompileMode::Check { test: true }
-            | CompileMode::Doctest => true,
-            _ => false,
-        }
-    }
-
-    /// Returns true if this is the *execution* of a `build.rs` script.
-    pub fn is_run_custom_build(&self) -> bool {
-        *self == CompileMode::RunCustomBuild
-    }
-
-    /// List of all modes (currently used by `cargo clean -p` for computing
-    /// all possible outputs).
-    pub fn all_modes() -> &'static [CompileMode] {
-        static ALL: [CompileMode; 9] = [
-            CompileMode::Test,
-            CompileMode::Build,
-            CompileMode::Check { test: true },
-            CompileMode::Check { test: false },
-            CompileMode::Bench,
-            CompileMode::Doc { deps: true },
-            CompileMode::Doc { deps: false },
-            CompileMode::Doctest,
-            CompileMode::RunCustomBuild,
-        ];
-        &ALL
-    }
-}
-
 #[derive(Clone, PartialEq, Eq, Debug)]
 pub enum Packages {
     Default,
@@ -332,9 +248,8 @@ pub fn compile_ws<'a>(
     let rustc_info_cache = ws.target_dir()
         .join(".rustc_info.json")
         .into_path_unlocked();
-    let mut build_config = BuildConfig::new(config, jobs, &target, Some(rustc_info_cache))?;
+    let mut build_config = BuildConfig::new(config, jobs, &target, Some(rustc_info_cache), mode)?;
     build_config.release = release;
-    build_config.test = mode == CompileMode::Test || mode == CompileMode::Bench;
     build_config.message_format = message_format;
     let default_arch_kind = if build_config.requested_target.is_some() {
         Kind::Target
index db400fb4de4450fd50e961697770bd4fc1a7f81b..0bf64ba19e4b39979f0c65f552ac482943a395ff 100644 (file)
@@ -1,4 +1,4 @@
-use core::compiler::{BuildConfig, Kind, TargetInfo};
+use core::compiler::{BuildConfig, CompileMode, Kind, TargetInfo};
 use core::{PackageSet, Resolve, Workspace};
 use ops;
 use std::collections::HashSet;
@@ -19,7 +19,8 @@ pub fn fetch<'a>(
     let (packages, resolve) = ops::resolve_ws(ws)?;
 
     let jobs = Some(1);
-    let build_config = BuildConfig::new(ws.config(), jobs, &options.target, None)?;
+    let build_config =
+        BuildConfig::new(ws.config(), jobs, &options.target, None, CompileMode::Build)?;
     let target_info = TargetInfo::new(ws.config(), &build_config, Kind::Target)?;
     {
         let mut fetched_packages = HashSet::new();
index 1cb3f24fbef7480787ffe2c0c025054f1faa7816..cf898b30c5d7939be1373cc86aa381e353f96e65 100644 (file)
@@ -10,7 +10,7 @@ use git2;
 use tar::{Archive, Builder, EntryType, Header};
 
 use core::{Package, Source, SourceId, Workspace};
-use core::compiler::{DefaultExecutor, MessageFormat};
+use core::compiler::{CompileMode, DefaultExecutor, MessageFormat};
 use sources::PathSource;
 use util::{self, internal, Config, FileLock};
 use util::paths;
@@ -348,7 +348,7 @@ fn run_verify(ws: &Workspace, tar: &FileLock, opts: &PackageOpts) -> CargoResult
             },
             release: false,
             message_format: MessageFormat::Human,
-            mode: ops::CompileMode::Build,
+            mode: CompileMode::Build,
             target_rustdoc_args: None,
             target_rustc_args: None,
             export_dir: None,
index 08e65ba54898c2ed5ac93d6d84d7a8c40f49afa3..1d061936131c03f32f40cacc6169e6df5f36fe10 100644 (file)
@@ -1,6 +1,6 @@
 pub use self::cargo_clean::{clean, CleanOptions};
 pub use self::cargo_compile::{compile, compile_with_exec, compile_ws, CompileOptions};
-pub use self::cargo_compile::{CompileFilter, CompileMode, FilterRule, Packages};
+pub use self::cargo_compile::{CompileFilter, FilterRule, Packages};
 pub use self::cargo_read_manifest::{read_package, read_packages};
 pub use self::cargo_run::run;
 pub use self::cargo_install::{install, install_list, uninstall};