Skip to content

Commit ceef92d

Browse files
committed
Auto merge of #8177 - ehuss:build-std-incremental, r=alexcrichton
build-std: Don't treat std like a "local" package. This changes it so that build-std will not treat the std crates like a "local" package. This has the following changes: - std does not build with incremental. This generally shouldn't be needed, and incremental has various bugs with std crates. - Cargo's dep-info fingerprint tracking will not track the std crate sources, these are tracked via other means. - Cargo's `.d` dep-info file does not include std crate sources. - Lints are capped to "allow" for std crates, and warnings are not shown by default. Closes rust-lang/wg-cargo-std-aware#44 Closes rust-lang/wg-cargo-std-aware#55
2 parents ade4c7b + b6a4b07 commit ceef92d

12 files changed

+81
-24
lines changed

src/cargo/core/compiler/build_context/mod.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::core::compiler::unit_graph::UnitGraph;
22
use crate::core::compiler::{BuildConfig, CompileKind, Unit};
33
use crate::core::profiles::Profiles;
4+
use crate::core::PackageSet;
45
use crate::core::{InternedString, Workspace};
5-
use crate::core::{PackageId, PackageSet};
66
use crate::util::config::Config;
77
use crate::util::errors::CargoResult;
88
use crate::util::Rustc;
@@ -99,10 +99,6 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
9999
&self.target_data.info(unit.kind).rustdocflags
100100
}
101101

102-
pub fn show_warnings(&self, pkg: PackageId) -> bool {
103-
pkg.source_id().is_path() || self.config.extra_verbose()
104-
}
105-
106102
pub fn extra_args_for(&self, unit: &Unit) -> Option<&Vec<String>> {
107103
self.extra_compiler_args.get(unit)
108104
}

src/cargo/core/compiler/job_queue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ impl<'cfg> DrainState<'cfg> {
889889
artifact: Artifact,
890890
cx: &mut Context<'_, '_>,
891891
) -> CargoResult<()> {
892-
if unit.mode.is_run_custom_build() && cx.bcx.show_warnings(unit.pkg.package_id()) {
892+
if unit.mode.is_run_custom_build() && unit.show_warnings(cx.bcx.config) {
893893
self.emit_warnings(None, unit, cx)?;
894894
}
895895
let unlocked = self.queue.finish(unit, &artifact);

src/cargo/core/compiler/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ fn compile<'cfg>(
136136
};
137137
work.then(link_targets(cx, unit, false)?)
138138
} else {
139-
let work = if cx.bcx.show_warnings(unit.pkg.package_id()) {
139+
let work = if unit.show_warnings(bcx.config) {
140140
replay_output_cache(
141141
unit.pkg.package_id(),
142142
&unit.target,
@@ -223,6 +223,7 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> Car
223223
.to_path_buf();
224224
let fingerprint_dir = cx.files().fingerprint_dir(unit);
225225
let script_metadata = cx.find_build_script_metadata(unit.clone());
226+
let is_local = unit.is_local();
226227

227228
return Ok(Work::new(move |state| {
228229
// Only at runtime have we discovered what the extra -L and -l
@@ -312,7 +313,7 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> Car
312313
&pkg_root,
313314
&target_dir,
314315
// Do not track source files in the fingerprint for registry dependencies.
315-
current_id.source_id().is_path(),
316+
is_local,
316317
)
317318
.chain_err(|| {
318319
internal(format!(
@@ -687,12 +688,12 @@ fn add_path_args(bcx: &BuildContext<'_, '_>, unit: &Unit, cmd: &mut ProcessBuild
687688
fn add_cap_lints(bcx: &BuildContext<'_, '_>, unit: &Unit, cmd: &mut ProcessBuilder) {
688689
// If this is an upstream dep we don't want warnings from, turn off all
689690
// lints.
690-
if !bcx.show_warnings(unit.pkg.package_id()) {
691+
if !unit.show_warnings(bcx.config) {
691692
cmd.arg("--cap-lints").arg("allow");
692693

693694
// If this is an upstream dep but we *do* want warnings, make sure that they
694695
// don't fail compilation.
695-
} else if !unit.pkg.package_id().source_id().is_path() {
696+
} else if !unit.is_local() {
696697
cmd.arg("--cap-lints").arg("warn");
697698
}
698699
}

src/cargo/core/compiler/output_depinfo.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ fn add_deps_for_unit(
9696
// Recursively traverse all transitive dependencies
9797
let unit_deps = Vec::from(cx.unit_deps(unit)); // Create vec due to mutable borrow.
9898
for dep in unit_deps {
99-
let source_id = dep.unit.pkg.package_id().source_id();
100-
if source_id.is_path() {
99+
if unit.is_local() {
101100
add_deps_for_unit(deps, cx, &dep.unit, visited)?;
102101
}
103102
}

src/cargo/core/compiler/standard_lib.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,13 @@ pub fn generate_std_roots(
152152
// in time is minimal, and the difference in caching is
153153
// significant.
154154
let mode = CompileMode::Build;
155-
let profile =
156-
profiles.get_profile(pkg.package_id(), /*is_member*/ false, unit_for, mode);
155+
let profile = profiles.get_profile(
156+
pkg.package_id(),
157+
/*is_member*/ false,
158+
/*is_local*/ false,
159+
unit_for,
160+
mode,
161+
);
157162
let features =
158163
std_features.activated_features(pkg.package_id(), FeaturesFor::NormalOrDev);
159164
Ok(interner.intern(

src/cargo/core/compiler/unit.rs

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::core::compiler::{CompileKind, CompileMode};
22
use crate::core::manifest::{LibKind, Target, TargetKind};
33
use crate::core::{profiles::Profile, InternedString, Package};
44
use crate::util::hex::short_hash;
5+
use crate::util::Config;
56
use std::cell::RefCell;
67
use std::collections::HashSet;
78
use std::fmt;
@@ -67,6 +68,19 @@ impl UnitInner {
6768
pub fn requires_upstream_objects(&self) -> bool {
6869
self.mode.is_any_test() || self.target.kind().requires_upstream_objects()
6970
}
71+
72+
/// Returns whether or not this is a "local" package.
73+
///
74+
/// A "local" package is one that the user can likely edit, or otherwise
75+
/// wants warnings, etc.
76+
pub fn is_local(&self) -> bool {
77+
self.pkg.package_id().source_id().is_path() && !self.is_std
78+
}
79+
80+
/// Returns whether or not warnings should be displayed for this unit.
81+
pub fn show_warnings(&self, config: &Config) -> bool {
82+
self.is_local() || config.extra_verbose()
83+
}
7084
}
7185

7286
impl Unit {

src/cargo/core/compiler/unit_dependencies.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -574,10 +574,14 @@ fn new_unit_dep(
574574
kind: CompileKind,
575575
mode: CompileMode,
576576
) -> CargoResult<UnitDep> {
577-
let profile =
578-
state
579-
.profiles
580-
.get_profile(pkg.package_id(), state.ws.is_member(pkg), unit_for, mode);
577+
let is_local = pkg.package_id().source_id().is_path() && !state.is_std;
578+
let profile = state.profiles.get_profile(
579+
pkg.package_id(),
580+
state.ws.is_member(pkg),
581+
is_local,
582+
unit_for,
583+
mode,
584+
);
581585
new_unit_dep_with_profile(state, parent, pkg, target, unit_for, kind, mode, profile)
582586
}
583587

src/cargo/core/profiles.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ impl Profiles {
287287
&self,
288288
pkg_id: PackageId,
289289
is_member: bool,
290+
is_local: bool,
290291
unit_for: UnitFor,
291292
mode: CompileMode,
292293
) -> Profile {
@@ -360,7 +361,7 @@ impl Profiles {
360361
// itself (aka crates.io / git dependencies)
361362
//
362363
// (see also https://github.com/rust-lang/cargo/issues/3972)
363-
if !pkg_id.source_id().is_path() {
364+
if !is_local {
364365
profile.incremental = false;
365366
}
366367
profile.name = profile_name;

src/cargo/ops/cargo_clean.rs

+2
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,15 @@ pub fn clean(ws: &Workspace<'_>, opts: &CleanOptions<'_>) -> CargoResult<()> {
109109
profiles.get_profile_run_custom_build(&profiles.get_profile(
110110
pkg.package_id(),
111111
ws.is_member(pkg),
112+
/*is_local*/ true,
112113
*unit_for,
113114
CompileMode::Build,
114115
))
115116
} else {
116117
profiles.get_profile(
117118
pkg.package_id(),
118119
ws.is_member(pkg),
120+
/*is_local*/ true,
119121
*unit_for,
120122
*mode,
121123
)

src/cargo/ops/cargo_compile.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -760,8 +760,14 @@ fn generate_targets(
760760
_ => target_mode,
761761
};
762762
let kind = default_arch_kind.for_target(target);
763-
let profile =
764-
profiles.get_profile(pkg.package_id(), ws.is_member(pkg), unit_for, target_mode);
763+
let is_local = pkg.package_id().source_id().is_path();
764+
let profile = profiles.get_profile(
765+
pkg.package_id(),
766+
ws.is_member(pkg),
767+
is_local,
768+
unit_for,
769+
target_mode,
770+
);
765771

766772
// No need to worry about build-dependencies, roots are never build dependencies.
767773
let features_for = FeaturesFor::from_for_host(target.proc_macro());

tests/testsuite/profile_config.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,8 @@ fn named_config_profile() {
405405
let dep_pkg = PackageId::new("dep", "0.1.0", crates_io).unwrap();
406406

407407
// normal package
408-
let p = profiles.get_profile(a_pkg, true, UnitFor::new_normal(), CompileMode::Build);
408+
let mode = CompileMode::Build;
409+
let p = profiles.get_profile(a_pkg, true, true, UnitFor::new_normal(), mode);
409410
assert_eq!(p.name, "foo");
410411
assert_eq!(p.codegen_units, Some(2)); // "foo" from config
411412
assert_eq!(p.opt_level, "1"); // "middle" from manifest
@@ -414,7 +415,7 @@ fn named_config_profile() {
414415
assert_eq!(p.overflow_checks, true); // "dev" built-in (ignore package override)
415416

416417
// build-override
417-
let bo = profiles.get_profile(a_pkg, true, UnitFor::new_host(false), CompileMode::Build);
418+
let bo = profiles.get_profile(a_pkg, true, true, UnitFor::new_host(false), mode);
418419
assert_eq!(bo.name, "foo");
419420
assert_eq!(bo.codegen_units, Some(6)); // "foo" build override from config
420421
assert_eq!(bo.opt_level, "1"); // SAME as normal
@@ -423,7 +424,7 @@ fn named_config_profile() {
423424
assert_eq!(bo.overflow_checks, true); // SAME as normal
424425

425426
// package overrides
426-
let po = profiles.get_profile(dep_pkg, false, UnitFor::new_normal(), CompileMode::Build);
427+
let po = profiles.get_profile(dep_pkg, false, true, UnitFor::new_normal(), mode);
427428
assert_eq!(po.name, "foo");
428429
assert_eq!(po.codegen_units, Some(7)); // "foo" package override from config
429430
assert_eq!(po.opt_level, "1"); // SAME as normal

tests/testsuite/standard_lib.rs

+28
Original file line numberDiff line numberDiff line change
@@ -576,3 +576,31 @@ fn macro_expanded_shadow() {
576576

577577
p.cargo("build -v").build_std(&setup).target_host().run();
578578
}
579+
580+
#[cargo_test]
581+
fn ignores_incremental() {
582+
// Incremental is not really needed for std, make sure it is disabled.
583+
// Incremental also tends to have bugs that affect std libraries more than
584+
// any other crate.
585+
let setup = match setup() {
586+
Some(s) => s,
587+
None => return,
588+
};
589+
let p = project().file("src/lib.rs", "").build();
590+
p.cargo("build")
591+
.env("CARGO_INCREMENTAL", "1")
592+
.build_std(&setup)
593+
.target_host()
594+
.run();
595+
let incremental: Vec<_> = p
596+
.glob(format!("target/{}/debug/incremental/*", rustc_host()))
597+
.map(|e| e.unwrap())
598+
.collect();
599+
assert_eq!(incremental.len(), 1);
600+
assert!(incremental[0]
601+
.file_name()
602+
.unwrap()
603+
.to_str()
604+
.unwrap()
605+
.starts_with("foo-"));
606+
}

0 commit comments

Comments
 (0)