|
| 1 | +use std::path::Path; |
| 2 | +use util::{CargoResult, CargoResultExt, Config}; |
| 3 | + |
| 4 | +/// Configuration information for a rustc build. |
| 5 | +#[derive(Debug)] |
| 6 | +pub struct BuildConfig { |
| 7 | + /// The target arch triple, defaults to host arch |
| 8 | + pub requested_target: Option<String>, |
| 9 | + /// How many rustc jobs to run in parallel |
| 10 | + pub jobs: u32, |
| 11 | + /// Whether we are building for release |
| 12 | + pub release: bool, |
| 13 | + /// In what mode we are compiling |
| 14 | + pub mode: CompileMode, |
| 15 | + /// Whether to print std output in json format (for machine reading) |
| 16 | + pub message_format: MessageFormat, |
| 17 | +} |
| 18 | + |
| 19 | +impl BuildConfig { |
| 20 | + /// Parse all config files to learn about build configuration. Currently |
| 21 | + /// configured options are: |
| 22 | + /// |
| 23 | + /// * build.jobs |
| 24 | + /// * build.target |
| 25 | + /// * target.$target.ar |
| 26 | + /// * target.$target.linker |
| 27 | + /// * target.$target.libfoo.metadata |
| 28 | + pub fn new( |
| 29 | + config: &Config, |
| 30 | + jobs: Option<u32>, |
| 31 | + requested_target: &Option<String>, |
| 32 | + mode: CompileMode, |
| 33 | + ) -> CargoResult<BuildConfig> { |
| 34 | + let requested_target = match requested_target { |
| 35 | + &Some(ref target) if target.ends_with(".json") => { |
| 36 | + let path = Path::new(target) |
| 37 | + .canonicalize() |
| 38 | + .chain_err(|| format_err!("Target path {:?} is not a valid file", target))?; |
| 39 | + Some(path.into_os_string() |
| 40 | + .into_string() |
| 41 | + .map_err(|_| format_err!("Target path is not valid unicode"))?) |
| 42 | + } |
| 43 | + other => other.clone(), |
| 44 | + }; |
| 45 | + if let Some(ref s) = requested_target { |
| 46 | + if s.trim().is_empty() { |
| 47 | + bail!("target was empty") |
| 48 | + } |
| 49 | + } |
| 50 | + let cfg_target = config.get_string("build.target")?.map(|s| s.val); |
| 51 | + let target = requested_target.clone().or(cfg_target); |
| 52 | + |
| 53 | + if jobs == Some(0) { |
| 54 | + bail!("jobs must be at least 1") |
| 55 | + } |
| 56 | + if jobs.is_some() && config.jobserver_from_env().is_some() { |
| 57 | + config.shell().warn( |
| 58 | + "a `-j` argument was passed to Cargo but Cargo is \ |
| 59 | + also configured with an external jobserver in \ |
| 60 | + its environment, ignoring the `-j` parameter", |
| 61 | + )?; |
| 62 | + } |
| 63 | + let cfg_jobs = match config.get_i64("build.jobs")? { |
| 64 | + Some(v) => { |
| 65 | + if v.val <= 0 { |
| 66 | + bail!( |
| 67 | + "build.jobs must be positive, but found {} in {}", |
| 68 | + v.val, |
| 69 | + v.definition |
| 70 | + ) |
| 71 | + } else if v.val >= i64::from(u32::max_value()) { |
| 72 | + bail!( |
| 73 | + "build.jobs is too large: found {} in {}", |
| 74 | + v.val, |
| 75 | + v.definition |
| 76 | + ) |
| 77 | + } else { |
| 78 | + Some(v.val as u32) |
| 79 | + } |
| 80 | + } |
| 81 | + None => None, |
| 82 | + }; |
| 83 | + let jobs = jobs.or(cfg_jobs).unwrap_or(::num_cpus::get() as u32); |
| 84 | + Ok(BuildConfig { |
| 85 | + requested_target: target, |
| 86 | + jobs, |
| 87 | + release: false, |
| 88 | + mode, |
| 89 | + message_format: MessageFormat::Human, |
| 90 | + }) |
| 91 | + } |
| 92 | + |
| 93 | + pub fn json_messages(&self) -> bool { |
| 94 | + self.message_format == MessageFormat::Json |
| 95 | + } |
| 96 | + |
| 97 | + pub fn test(&self) -> bool { |
| 98 | + self.mode == CompileMode::Test || self.mode == CompileMode::Bench |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 103 | +pub enum MessageFormat { |
| 104 | + Human, |
| 105 | + Json, |
| 106 | +} |
| 107 | + |
| 108 | +/// The general "mode" of what to do. |
| 109 | +/// This is used for two purposes. The commands themselves pass this in to |
| 110 | +/// `compile_ws` to tell it the general execution strategy. This influences |
| 111 | +/// the default targets selected. The other use is in the `Unit` struct |
| 112 | +/// to indicate what is being done with a specific target. |
| 113 | +#[derive(Clone, Copy, PartialEq, Debug, Eq, Hash)] |
| 114 | +pub enum CompileMode { |
| 115 | + /// A target being built for a test. |
| 116 | + Test, |
| 117 | + /// Building a target with `rustc` (lib or bin). |
| 118 | + Build, |
| 119 | + /// Building a target with `rustc` to emit `rmeta` metadata only. If |
| 120 | + /// `test` is true, then it is also compiled with `--test` to check it like |
| 121 | + /// a test. |
| 122 | + Check { test: bool }, |
| 123 | + /// Used to indicate benchmarks should be built. This is not used in |
| 124 | + /// `Target` because it is essentially the same as `Test` (indicating |
| 125 | + /// `--test` should be passed to rustc) and by using `Test` instead it |
| 126 | + /// allows some de-duping of Units to occur. |
| 127 | + Bench, |
| 128 | + /// A target that will be documented with `rustdoc`. |
| 129 | + /// If `deps` is true, then it will also document all dependencies. |
| 130 | + Doc { deps: bool }, |
| 131 | + /// A target that will be tested with `rustdoc`. |
| 132 | + Doctest, |
| 133 | + /// A marker for Units that represent the execution of a `build.rs` |
| 134 | + /// script. |
| 135 | + RunCustomBuild, |
| 136 | +} |
| 137 | + |
| 138 | +impl CompileMode { |
| 139 | + /// Returns true if the unit is being checked. |
| 140 | + pub fn is_check(&self) -> bool { |
| 141 | + match *self { |
| 142 | + CompileMode::Check { .. } => true, |
| 143 | + _ => false, |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + /// Returns true if this is a doc or doctest. Be careful using this. |
| 148 | + /// Although both run rustdoc, the dependencies for those two modes are |
| 149 | + /// very different. |
| 150 | + pub fn is_doc(&self) -> bool { |
| 151 | + match *self { |
| 152 | + CompileMode::Doc { .. } | CompileMode::Doctest => true, |
| 153 | + _ => false, |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + /// Returns true if this is any type of test (test, benchmark, doctest, or |
| 158 | + /// check-test). |
| 159 | + pub fn is_any_test(&self) -> bool { |
| 160 | + match *self { |
| 161 | + CompileMode::Test |
| 162 | + | CompileMode::Bench |
| 163 | + | CompileMode::Check { test: true } |
| 164 | + | CompileMode::Doctest => true, |
| 165 | + _ => false, |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + /// Returns true if this is the *execution* of a `build.rs` script. |
| 170 | + pub fn is_run_custom_build(&self) -> bool { |
| 171 | + *self == CompileMode::RunCustomBuild |
| 172 | + } |
| 173 | + |
| 174 | + /// List of all modes (currently used by `cargo clean -p` for computing |
| 175 | + /// all possible outputs). |
| 176 | + pub fn all_modes() -> &'static [CompileMode] { |
| 177 | + static ALL: [CompileMode; 9] = [ |
| 178 | + CompileMode::Test, |
| 179 | + CompileMode::Build, |
| 180 | + CompileMode::Check { test: true }, |
| 181 | + CompileMode::Check { test: false }, |
| 182 | + CompileMode::Bench, |
| 183 | + CompileMode::Doc { deps: true }, |
| 184 | + CompileMode::Doc { deps: false }, |
| 185 | + CompileMode::Doctest, |
| 186 | + CompileMode::RunCustomBuild, |
| 187 | + ]; |
| 188 | + &ALL |
| 189 | + } |
| 190 | +} |
0 commit comments