Skip to content

Commit 3144125

Browse files
committed
Add rustc/rustdoc config keys to Cargo config
In addition to global RUSTC/RUSTDOC env vars, this commit recognizes `build.rustc` and `build.rustdoc` as configuration keys for Cargo to instruct what tools should be used instead of the default. Closes rust-lang#967
1 parent 2fe0bf8 commit 3144125

16 files changed

Lines changed: 133 additions & 103 deletions

src/cargo/ops/cargo_compile.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ pub enum CompileFilter<'a> {
8282
}
8383
}
8484

85-
pub fn compile(manifest_path: &Path,
86-
options: &CompileOptions)
87-
-> CargoResult<ops::Compilation> {
85+
pub fn compile<'a>(manifest_path: &Path,
86+
options: &CompileOptions<'a>)
87+
-> CargoResult<ops::Compilation<'a>> {
8888
debug!("compile; manifest-path={}", manifest_path.display());
8989

9090
let mut source = try!(PathSource::for_path(manifest_path.parent().unwrap(),
@@ -101,8 +101,9 @@ pub fn compile(manifest_path: &Path,
101101
compile_pkg(&package, options)
102102
}
103103

104-
pub fn compile_pkg(package: &Package, options: &CompileOptions)
105-
-> CargoResult<ops::Compilation> {
104+
pub fn compile_pkg<'a>(package: &Package,
105+
options: &CompileOptions<'a>)
106+
-> CargoResult<ops::Compilation<'a>> {
106107
let CompileOptions { config, jobs, target, spec, features,
107108
no_default_features, release, mode,
108109
ref filter, ref exec_engine,
@@ -174,10 +175,12 @@ pub fn compile_pkg(package: &Package, options: &CompileOptions)
174175
profile.rustc_args = Some(args.to_vec());
175176
Some((target, profile))
176177
}
177-
Some(_) =>
178-
return Err(human("extra arguments to `rustc` can only be passed to one target, \
179-
consider filtering\nthe package by passing e.g. `--lib` or \
180-
`--bin NAME` to specify a single target")),
178+
Some(_) => {
179+
return Err(human("extra arguments to `rustc` can only be passed to \
180+
one target, consider filtering\nthe package by \
181+
passing e.g. `--lib` or `--bin NAME` to specify \
182+
a single target"))
183+
}
181184
None => None,
182185
};
183186

@@ -195,7 +198,8 @@ pub fn compile_pkg(package: &Package, options: &CompileOptions)
195198

196199
try!(ops::compile_targets(&targets, to_build,
197200
&PackageSet::new(&packages),
198-
&resolve_with_overrides, &sources,
201+
&resolve_with_overrides,
202+
&sources,
199203
config,
200204
build_config,
201205
to_build.manifest().profiles()))

src/cargo/ops/cargo_rustc/compilation.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ use std::path::PathBuf;
44
use semver::Version;
55

66
use core::{PackageId, Package, Target};
7-
use util::{self, CargoResult};
7+
use util::{self, CargoResult, Config};
88

99
use super::{CommandType, CommandPrototype};
1010

1111
/// A structure returning the result of a compilation.
12-
pub struct Compilation {
12+
pub struct Compilation<'cfg> {
1313
/// All libraries which were built for a package.
1414
///
1515
/// This is currently used for passing --extern flags to rustdoc tests later
@@ -44,10 +44,12 @@ pub struct Compilation {
4444

4545
/// Features enabled during this compilation.
4646
pub features: HashSet<String>,
47+
48+
config: &'cfg Config,
4749
}
4850

49-
impl Compilation {
50-
pub fn new(pkg: &Package) -> Compilation {
51+
impl<'cfg> Compilation<'cfg> {
52+
pub fn new(pkg: &Package, config: &'cfg Config) -> Compilation<'cfg> {
5153
Compilation {
5254
libraries: HashMap::new(),
5355
native_dirs: HashMap::new(), // TODO: deprecated, remove
@@ -58,6 +60,7 @@ impl Compilation {
5860
extra_env: HashMap::new(),
5961
package: pkg.clone(),
6062
features: HashSet::new(),
63+
config: config,
6164
}
6265
}
6366

@@ -98,7 +101,7 @@ impl Compilation {
98101
search_path.push(self.deps_output.clone());
99102
let search_path = try!(util::join_paths(&search_path,
100103
util::dylib_path_envvar()));
101-
let mut cmd = try!(CommandPrototype::new(cmd));
104+
let mut cmd = try!(CommandPrototype::new(cmd, self.config));
102105
cmd.env(util::dylib_path_envvar(), &search_path);
103106
for (k, v) in self.extra_env.iter() {
104107
cmd.env(k, v);

src/cargo/ops/cargo_rustc/context.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ pub enum Platform {
2525
PluginAndTarget,
2626
}
2727

28-
pub struct Context<'a> {
29-
pub config: &'a Config,
28+
pub struct Context<'a, 'cfg: 'a> {
29+
pub config: &'cfg Config,
3030
pub resolve: &'a Resolve,
31-
pub sources: &'a SourceMap<'a>,
32-
pub compilation: Compilation,
31+
pub sources: &'a SourceMap<'cfg>,
32+
pub compilation: Compilation<'cfg>,
3333
pub build_state: Arc<BuildState>,
3434
pub exec_engine: Arc<Box<ExecEngine>>,
3535
pub fingerprints: HashMap<(&'a PackageId, &'a Target, &'a Profile, Kind),
@@ -49,23 +49,24 @@ pub struct Context<'a> {
4949
profiles: &'a Profiles,
5050
}
5151

52-
impl<'a> Context<'a> {
52+
impl<'a, 'cfg> Context<'a, 'cfg> {
5353
pub fn new(resolve: &'a Resolve,
54-
sources: &'a SourceMap<'a>,
54+
sources: &'a SourceMap<'cfg>,
5555
deps: &'a PackageSet,
56-
config: &'a Config,
56+
config: &'cfg Config,
5757
host: Layout,
5858
target_layout: Option<Layout>,
5959
root_pkg: &Package,
6060
build_config: BuildConfig,
61-
profiles: &'a Profiles) -> CargoResult<Context<'a>> {
61+
profiles: &'a Profiles) -> CargoResult<Context<'a, 'cfg>> {
6262
let target = build_config.requested_target.clone();
6363
let target = target.as_ref().map(|s| &s[..]);
64-
let (target_dylib, target_exe) = try!(Context::filename_parts(target));
64+
let (target_dylib, target_exe) = try!(Context::filename_parts(target,
65+
config));
6566
let (host_dylib, host_exe) = if build_config.requested_target.is_none() {
6667
(target_dylib.clone(), target_exe.clone())
6768
} else {
68-
try!(Context::filename_parts(None))
69+
try!(Context::filename_parts(None, config))
6970
};
7071
let target_triple = target.unwrap_or(config.rustc_host()).to_string();
7172
let engine = build_config.exec_engine.as_ref().cloned().unwrap_or({
@@ -84,7 +85,7 @@ impl<'a> Context<'a> {
8485
host_dylib: host_dylib,
8586
host_exe: host_exe,
8687
requirements: HashMap::new(),
87-
compilation: Compilation::new(root_pkg),
88+
compilation: Compilation::new(root_pkg, config),
8889
build_state: Arc::new(BuildState::new(&build_config, deps)),
8990
build_config: build_config,
9091
exec_engine: engine,
@@ -96,9 +97,9 @@ impl<'a> Context<'a> {
9697

9798
/// Run `rustc` to discover the dylib prefix/suffix for the target
9899
/// specified as well as the exe suffix
99-
fn filename_parts(target: Option<&str>)
100+
fn filename_parts(target: Option<&str>, cfg: &Config)
100101
-> CargoResult<(Option<(String, String)>, String)> {
101-
let mut process = try!(util::process(util::rustc()));
102+
let mut process = try!(util::process(cfg.rustc()));
102103
process.arg("-")
103104
.arg("--crate-name").arg("_")
104105
.arg("--crate-type").arg("dylib")

src/cargo/ops/cargo_rustc/engine.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use std::fmt;
44
use std::path::Path;
55
use std::process::Output;
66

7-
use util::{self, CargoResult, ProcessError, ProcessBuilder, process};
7+
use util::{CargoResult, ProcessError, ProcessBuilder, process};
8+
use util::Config;
89

910
/// Trait for objects that can execute commands.
1011
pub trait ExecEngine: Send + Sync {
@@ -35,11 +36,12 @@ pub struct CommandPrototype {
3536
}
3637

3738
impl CommandPrototype {
38-
pub fn new(ty: CommandType) -> CargoResult<CommandPrototype> {
39+
pub fn new(ty: CommandType, config: &Config)
40+
-> CargoResult<CommandPrototype> {
3941
Ok(CommandPrototype {
4042
builder: try!(match ty {
41-
CommandType::Rustc => process(util::rustc()),
42-
CommandType::Rustdoc => process(util::rustdoc()),
43+
CommandType::Rustc => process(config.rustc()),
44+
CommandType::Rustdoc => process(config.rustdoc()),
4345
CommandType::Target(ref s) |
4446
CommandType::Host(ref s) => process(s),
4547
}),

src/cargo/ops/cargo_rustc/fingerprint.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ pub type Preparation = (Freshness, Work, Work);
3939
/// This function will calculate the fingerprint for a target and prepare the
4040
/// work necessary to either write the fingerprint or copy over all fresh files
4141
/// from the old directories to their new locations.
42-
pub fn prepare_target<'a>(cx: &mut Context<'a>,
43-
pkg: &'a Package,
44-
target: &'a Target,
45-
profile: &'a Profile,
46-
kind: Kind) -> CargoResult<Preparation> {
42+
pub fn prepare_target<'a, 'cfg>(cx: &mut Context<'a, 'cfg>,
43+
pkg: &'a Package,
44+
target: &'a Target,
45+
profile: &'a Profile,
46+
kind: Kind) -> CargoResult<Preparation> {
4747
let _p = profile::start(format!("fingerprint: {} / {}",
4848
pkg.package_id(), target.name()));
4949
let new = dir(cx, pkg, kind);
@@ -131,12 +131,12 @@ impl Fingerprint {
131131
///
132132
/// Information like file modification time is only calculated for path
133133
/// dependencies and is calculated in `calculate_target_fresh`.
134-
fn calculate<'a>(cx: &mut Context<'a>,
135-
pkg: &'a Package,
136-
target: &'a Target,
137-
profile: &'a Profile,
138-
kind: Kind)
139-
-> CargoResult<Fingerprint> {
134+
fn calculate<'a, 'cfg>(cx: &mut Context<'a, 'cfg>,
135+
pkg: &'a Package,
136+
target: &'a Target,
137+
profile: &'a Profile,
138+
kind: Kind)
139+
-> CargoResult<Fingerprint> {
140140
let key = (pkg.package_id(), target, profile, kind);
141141
match cx.fingerprints.get(&key) {
142142
Some(s) => return Ok(s.clone()),

src/cargo/ops/cargo_rustc/mod.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::env;
33
use std::ffi::OsString;
44
use std::fs;
55
use std::io::prelude::*;
6-
use std::path::{self, PathBuf};
6+
use std::path::{self, Path, PathBuf};
77
use std::sync::Arc;
88

99
use core::{SourceMap, Package, PackageId, PackageSet, Target, Resolve};
@@ -56,8 +56,8 @@ pub struct TargetConfig {
5656
///
5757
/// The second element of the tuple returned is the target triple that rustc
5858
/// is a host for.
59-
pub fn rustc_version() -> CargoResult<(String, String)> {
60-
let output = try!(try!(util::process(util::rustc()))
59+
pub fn rustc_version<P: AsRef<Path>>(rustc: P) -> CargoResult<(String, String)> {
60+
let output = try!(try!(util::process(rustc.as_ref()))
6161
.arg("-vV")
6262
.exec_with_output());
6363
let output = try!(String::from_utf8(output.stdout).map_err(|_| {
@@ -77,17 +77,17 @@ pub fn rustc_version() -> CargoResult<(String, String)> {
7777

7878
// Returns a mapping of the root package plus its immediate dependencies to
7979
// where the compiled libraries are all located.
80-
pub fn compile_targets<'a>(targets: &[(&'a Target, &'a Profile)],
81-
pkg: &'a Package,
82-
deps: &PackageSet,
83-
resolve: &'a Resolve,
84-
sources: &'a SourceMap<'a>,
85-
config: &'a Config,
86-
build_config: BuildConfig,
87-
profiles: &'a Profiles)
88-
-> CargoResult<Compilation> {
80+
pub fn compile_targets<'a, 'cfg: 'a>(targets: &[(&'a Target, &'a Profile)],
81+
pkg: &'a Package,
82+
deps: &'a PackageSet,
83+
resolve: &'a Resolve,
84+
sources: &'a SourceMap<'cfg>,
85+
config: &'cfg Config,
86+
build_config: BuildConfig,
87+
profiles: &'a Profiles)
88+
-> CargoResult<Compilation<'cfg>> {
8989
if targets.is_empty() {
90-
return Ok(Compilation::new(pkg))
90+
return Ok(Compilation::new(pkg, config))
9191
}
9292

9393
debug!("compile_targets: {}", pkg);
@@ -181,10 +181,10 @@ pub fn compile_targets<'a>(targets: &[(&'a Target, &'a Profile)],
181181
Ok(cx.compilation)
182182
}
183183

184-
fn compile<'a>(targets: &[(&'a Target, &'a Profile)],
185-
pkg: &'a Package,
186-
cx: &mut Context<'a>,
187-
jobs: &mut JobQueue<'a>) -> CargoResult<()> {
184+
fn compile<'a, 'cfg>(targets: &[(&'a Target, &'a Profile)],
185+
pkg: &'a Package,
186+
cx: &mut Context<'a, 'cfg>,
187+
jobs: &mut JobQueue<'a>) -> CargoResult<()> {
188188
debug!("compile_pkg; pkg={}", pkg);
189189
let profiling_marker = profile::start(format!("preparing: {}", pkg));
190190

@@ -292,10 +292,10 @@ fn compile<'a>(targets: &[(&'a Target, &'a Profile)],
292292
Ok(())
293293
}
294294

295-
fn prepare_init<'a>(cx: &mut Context<'a>,
296-
pkg: &'a Package,
297-
jobs: &mut JobQueue<'a>,
298-
visited: &mut HashSet<&'a PackageId>) {
295+
fn prepare_init<'a, 'cfg>(cx: &mut Context<'a, 'cfg>,
296+
pkg: &'a Package,
297+
jobs: &mut JobQueue<'a>,
298+
visited: &mut HashSet<&'a PackageId>) {
299299
if !visited.insert(pkg.package_id()) { return }
300300

301301
// Set up all dependencies

src/cargo/ops/cargo_test.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ pub fn run_benches(manifest_path: &Path,
101101
Ok(try!(build_and_run(manifest_path, options, &args)).err())
102102
}
103103

104-
fn build_and_run(manifest_path: &Path,
105-
options: &TestOptions,
106-
test_args: &[String])
107-
-> CargoResult<Result<Compilation, ProcessError>> {
104+
fn build_and_run<'a>(manifest_path: &Path,
105+
options: &TestOptions<'a>,
106+
test_args: &[String])
107+
-> CargoResult<Result<Compilation<'a>, ProcessError>> {
108108
let config = options.compile_opts.config;
109109
let mut source = try!(PathSource::for_path(&manifest_path.parent().unwrap(),
110110
config));

0 commit comments

Comments
 (0)