Skip to content

Commit d12cc03

Browse files
committed
Auto merge of #3527 - nikomatsakis:master, r=alexcrichton
check for `CARGO_INCREMENTAL` and pass `-Zincremental` if present Per the discussion on IRC, this adds a very simple way for cargo users to opt into incremental compilation by setting the `CARGO_INCREMENTAL` environment variable (i.e., `CARGO_INCREMENTAL=1 cargo build`). This will result in incremental data being stored into the `target/incremental` directory. Since it supplies `-Z`, this option is only intended for use on nightly compilers, though cargo makes no effort to check. The plan is to keep incremental compilation optional until we are feeling more confident it's not going to cause problems for people. At that point, it should become part of the compilation profile. It will be the default when building in debug builds, and opt-in for release builds.
2 parents 6dd4ff0 + 30e91b5 commit d12cc03

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

src/cargo/ops/cargo_rustc/context.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub struct Context<'a, 'cfg: 'a> {
4848
target_info: TargetInfo,
4949
host_info: TargetInfo,
5050
profiles: &'a Profiles,
51+
incremental_enabled: bool,
5152
}
5253

5354
#[derive(Clone, Default)]
@@ -74,6 +75,11 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
7475
None => None,
7576
};
7677

78+
// Enable incremental builds if the user opts in. For now,
79+
// this is an environment variable until things stabilize a
80+
// bit more.
81+
let incremental_enabled = env::var("CARGO_INCREMENTAL").is_ok();
82+
7783
Ok(Context {
7884
ws: ws,
7985
host: host_layout,
@@ -93,6 +99,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
9399
build_explicit_deps: HashMap::new(),
94100
links: Links::new(),
95101
used_in_plugin: HashSet::new(),
102+
incremental_enabled: incremental_enabled,
96103
})
97104
}
98105

@@ -843,6 +850,14 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
843850
self.lib_profile()
844851
}
845852

853+
pub fn incremental_args(&self, unit: &Unit) -> CargoResult<Vec<String>> {
854+
if self.incremental_enabled {
855+
Ok(vec![format!("-Zincremental={}", self.layout(unit.kind).incremental().display())])
856+
} else {
857+
Ok(vec![])
858+
}
859+
}
860+
846861
pub fn rustflags_args(&self, unit: &Unit) -> CargoResult<Vec<String>> {
847862
env_args(self.config, &self.build_config, unit.kind, "RUSTFLAGS")
848863
}

src/cargo/ops/cargo_rustc/layout.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
//! $pkg2/
4141
//! $pkg3/
4242
//!
43+
//! # Directory used to store incremental data for the compiler (when
44+
//! # incremental is enabled.
45+
//! incremental/
46+
//!
4347
//! # Hidden directory that holds all of the fingerprint files for all
4448
//! # packages
4549
//! .fingerprint/
@@ -57,6 +61,7 @@ pub struct Layout {
5761
deps: PathBuf,
5862
native: PathBuf,
5963
build: PathBuf,
64+
incremental: PathBuf,
6065
fingerprint: PathBuf,
6166
examples: PathBuf,
6267
_lock: FileLock,
@@ -88,6 +93,7 @@ impl Layout {
8893
deps: root.join("deps"),
8994
native: root.join("native"),
9095
build: root.join("build"),
96+
incremental: root.join("incremental"),
9197
fingerprint: root.join(".fingerprint"),
9298
examples: root.join("examples"),
9399
root: root,
@@ -102,6 +108,7 @@ impl Layout {
102108

103109
mkdir(&self.deps)?;
104110
mkdir(&self.native)?;
111+
mkdir(&self.incremental)?;
105112
mkdir(&self.fingerprint)?;
106113
mkdir(&self.examples)?;
107114
mkdir(&self.build)?;
@@ -120,6 +127,7 @@ impl Layout {
120127
pub fn deps(&self) -> &Path { &self.deps }
121128
pub fn examples(&self) -> &Path { &self.examples }
122129
pub fn root(&self) -> &Path { &self.root }
130+
pub fn incremental(&self) -> &Path { &self.incremental }
123131
pub fn fingerprint(&self) -> &Path { &self.fingerprint }
124132
pub fn build(&self) -> &Path { &self.build }
125133
}

src/cargo/ops/cargo_rustc/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ fn rustc(cx: &mut Context, unit: &Unit, exec: Arc<Executor>) -> CargoResult<Work
278278
let dep_info_loc = fingerprint::dep_info_loc(cx, unit);
279279
let cwd = cx.config.cwd().to_path_buf();
280280

281+
rustc.args(&cx.incremental_args(unit)?);
281282
rustc.args(&cx.rustflags_args(unit)?);
282283
let json_messages = cx.build_config.json_messages;
283284
let package_id = unit.pkg.package_id().clone();

tests/build.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,31 @@ fn cargo_compile_simple() {
2929
execs().with_status(0).with_stdout("i am foo\n"));
3030
}
3131

32+
/// Check that the `CARGO_INCREMENTAL` environment variable results in
33+
/// `rustc` getting `-Zincremental` passed to it.
34+
#[test]
35+
fn cargo_compile_incremental() {
36+
if !is_nightly() {
37+
return
38+
}
39+
40+
let p = project("foo")
41+
.file("Cargo.toml", &basic_bin_manifest("foo"))
42+
.file("src/foo.rs", &main_file(r#""i am foo""#, &[]));
43+
44+
assert_that(
45+
p.cargo_process("build").arg("-v").env("CARGO_INCREMENTAL", "1"),
46+
execs().with_stderr_contains(
47+
"[RUNNING] `rustc [..] -Zincremental=[..][/]target[/]debug[/]incremental`\n")
48+
.with_status(0));
49+
50+
assert_that(
51+
p.cargo_process("test").arg("-v").env("CARGO_INCREMENTAL", "1"),
52+
execs().with_stderr_contains(
53+
"[RUNNING] `rustc [..] -Zincremental=[..][/]target[/]debug[/]incremental`\n")
54+
.with_status(0));
55+
}
56+
3257
#[test]
3358
fn cargo_compile_manifest_path() {
3459
let p = project("foo")

0 commit comments

Comments
 (0)