Skip to content

Commit 4a8a38c

Browse files
committed
Auto merge of #2398 - alexcrichton:config-env-var, r=brson
This commit adds a more principled system to rationalize what ends up being a configuration value versus an environment variable. This problem is solved by just saying that they're one and the same! Similar to Bundler, this commit supports overriding the `foo.bar` configuration value with the `CARGO_FOO_BAR` environment variable. Currently this is used as part of the `get_string` and `get_i64` methods on `Config`. This means, for example, that the following environment variables can now be used to configure Cargo: * CARGO_BUILD_JOBS * CARGO_HTTP_TIMEOUT * CARGO_HTTP_PROXY Currently it's not supported to encode a list in an environment variable, so for example `CARGO_PATHS` would not be read when reading the global `paths` configuration value. cc #2362 cc #2395 -- intended to close this in tandem with #2397
2 parents ff4bda2 + a40440c commit 4a8a38c

File tree

9 files changed

+255
-133
lines changed

9 files changed

+255
-133
lines changed

src/cargo/ops/cargo_compile.rs

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use core::{Source, SourceId, SourceMap, PackageSet, Package, Target};
3232
use core::{Profile, TargetKind, Profiles};
3333
use core::resolver::{Method, Resolve};
3434
use ops::{self, BuildOutput, ExecEngine};
35-
use util::config::{ConfigValue, Config};
35+
use util::config::Config;
3636
use util::{CargoResult, internal, ChainError, profile};
3737

3838
/// Contains information about how a package should be compiled.
@@ -421,19 +421,21 @@ fn scrape_build_config(config: &Config,
421421
target: Option<String>)
422422
-> CargoResult<ops::BuildConfig> {
423423
let cfg_jobs = match try!(config.get_i64("build.jobs")) {
424-
Some((n, p)) => {
425-
if n <= 0 {
426-
bail!("build.jobs must be positive, but found {} in {:?}", n, p)
427-
} else if n >= u32::max_value() as i64 {
428-
bail!("build.jobs is too large: found {} in {:?}", n, p)
424+
Some(v) => {
425+
if v.val <= 0 {
426+
bail!("build.jobs must be positive, but found {} in {}",
427+
v.val, v.definition)
428+
} else if v.val >= u32::max_value() as i64 {
429+
bail!("build.jobs is too large: found {} in {}", v.val,
430+
v.definition)
429431
} else {
430-
Some(n as u32)
432+
Some(v.val as u32)
431433
}
432434
}
433435
None => None,
434436
};
435437
let jobs = jobs.or(cfg_jobs).unwrap_or(::num_cpus::get() as u32);
436-
let cfg_target = try!(config.get_string("build.target")).map(|s| s.0);
438+
let cfg_target = try!(config.get_string("build.target")).map(|s| s.val);
437439
let target = target.or(cfg_target);
438440
let mut base = ops::BuildConfig {
439441
jobs: jobs,
@@ -453,16 +455,18 @@ fn scrape_target_config(config: &Config, triple: &str)
453455

454456
let key = format!("target.{}", triple);
455457
let mut ret = ops::TargetConfig {
456-
ar: try!(config.get_path(&format!("{}.ar", key))),
457-
linker: try!(config.get_path(&format!("{}.linker", key))),
458+
ar: try!(config.get_path(&format!("{}.ar", key))).map(|v| v.val),
459+
linker: try!(config.get_path(&format!("{}.linker", key))).map(|v| v.val),
458460
overrides: HashMap::new(),
459461
};
460462
let table = match try!(config.get_table(&key)) {
461-
Some((table, _)) => table,
463+
Some(table) => table.val,
462464
None => return Ok(ret),
463465
};
464466
for (lib_name, _) in table.into_iter() {
465-
if lib_name == "ar" || lib_name == "linker" { continue }
467+
if lib_name == "ar" || lib_name == "linker" {
468+
continue
469+
}
466470

467471
let mut output = BuildOutput {
468472
library_paths: Vec::new(),
@@ -472,40 +476,39 @@ fn scrape_target_config(config: &Config, triple: &str)
472476
rerun_if_changed: Vec::new(),
473477
};
474478
let key = format!("{}.{}", key, lib_name);
475-
let table = try!(config.get_table(&key)).unwrap().0;
479+
let table = try!(config.get_table(&key)).unwrap().val;
476480
for (k, _) in table.into_iter() {
477481
let key = format!("{}.{}", key, k);
478-
match try!(config.get(&key)).unwrap() {
479-
ConfigValue::String(v, path) => {
480-
if k == "rustc-flags" {
481-
let whence = format!("in `{}` (in {})", key,
482-
path.display());
483-
let (paths, links) = try!(
484-
BuildOutput::parse_rustc_flags(&v, &whence)
485-
);
486-
output.library_paths.extend(paths.into_iter());
487-
output.library_links.extend(links.into_iter());
488-
} else {
489-
output.metadata.push((k, v));
490-
}
491-
},
492-
ConfigValue::List(a, p) => {
493-
if k == "rustc-link-lib" {
494-
output.library_links.extend(a.into_iter().map(|v| v.0));
495-
} else if k == "rustc-link-search" {
496-
output.library_paths.extend(a.into_iter().map(|v| {
497-
PathBuf::from(&v.0)
498-
}));
499-
} else if k == "rustc-cfg" {
500-
output.cfgs.extend(a.into_iter().map(|v| v.0));
501-
} else {
502-
try!(config.expected("string", &k,
503-
ConfigValue::List(a, p)));
504-
}
505-
},
506-
// technically could be a list too, but that's the exception to
507-
// the rule...
508-
cv => { try!(config.expected("string", &k, cv)); }
482+
match &k[..] {
483+
"rustc-flags" => {
484+
let flags = try!(config.get_string(&key)).unwrap();
485+
let whence = format!("in `{}` (in {})", key,
486+
flags.definition);
487+
let (paths, links) = try!(
488+
BuildOutput::parse_rustc_flags(&flags.val, &whence)
489+
);
490+
output.library_paths.extend(paths.into_iter());
491+
output.library_links.extend(links.into_iter());
492+
}
493+
"rustc-link-lib" => {
494+
let list = try!(config.get_list(&key)).unwrap();
495+
output.library_links.extend(list.val.into_iter()
496+
.map(|v| v.0));
497+
}
498+
"rustc-link-search" => {
499+
let list = try!(config.get_list(&key)).unwrap();
500+
output.library_paths.extend(list.val.into_iter().map(|v| {
501+
PathBuf::from(&v.0)
502+
}));
503+
}
504+
"rustc-cfg" => {
505+
let list = try!(config.get_list(&key)).unwrap();
506+
output.cfgs.extend(list.val.into_iter().map(|v| v.0));
507+
}
508+
_ => {
509+
let val = try!(config.get_string(&key)).unwrap();
510+
output.metadata.push((k, val.val));
511+
}
509512
}
510513
}
511514
ret.overrides.insert(lib_name, output);

src/cargo/ops/cargo_install.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,11 +347,11 @@ pub fn uninstall(root: Option<&str>,
347347
}
348348

349349
fn resolve_root(flag: Option<&str>, config: &Config) -> CargoResult<PathBuf> {
350-
let config_root = try!(config.get_string("install.root"));
350+
let config_root = try!(config.get_path("install.root"));
351351
Ok(flag.map(PathBuf::from).or_else(|| {
352352
env::var_os("CARGO_INSTALL_ROOT").map(PathBuf::from)
353-
}).or_else(|| {
354-
config_root.clone().map(|(v, _)| PathBuf::from(v))
353+
}).or_else(move || {
354+
config_root.map(|v| v.val)
355355
}).unwrap_or_else(|| {
356356
config.home().to_owned()
357357
}))

0 commit comments

Comments
 (0)