Rollup merge of #129781 - Veykril:lw-x-py-compiler-features, r=albertlarsan68

Make `./x.py <cmd> compiler/<crate>` aware of the crate's features

Does not fix https://github.com/rust-lang/rust/issues/129727 on its own as the way the parallel-compiler cfg and feature flags are setup being generally incompatible with `resolver = 2` but it progresses on the issue. But this should in theory allow compiler crates to work that do not depend on the parallel compiler stuff (so some leaf crates).
This commit is contained in:
Matthias Krüger 2024-09-06 07:33:57 +02:00 committed by GitHub
commit b09f316058
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 30 additions and 12 deletions

View File

@ -228,7 +228,7 @@ fn run(self, builder: &Builder<'_>) {
self.override_build_kind.unwrap_or(builder.kind), self.override_build_kind.unwrap_or(builder.kind),
); );
rustc_cargo(builder, &mut cargo, target, &compiler); rustc_cargo(builder, &mut cargo, target, &compiler, &self.crates);
// For ./x.py clippy, don't run with --all-targets because // For ./x.py clippy, don't run with --all-targets because
// linting tests and benchmarks can produce very noisy results // linting tests and benchmarks can produce very noisy results

View File

@ -197,7 +197,7 @@ fn run(self, builder: &Builder<'_>) {
Kind::Clippy, Kind::Clippy,
); );
rustc_cargo(builder, &mut cargo, target, &compiler); rustc_cargo(builder, &mut cargo, target, &compiler, &self.crates);
// Explicitly pass -p for all compiler crates -- this will force cargo // Explicitly pass -p for all compiler crates -- this will force cargo
// to also lint the tests/benches/examples for these crates, rather // to also lint the tests/benches/examples for these crates, rather

View File

@ -988,7 +988,7 @@ fn run(self, builder: &Builder<'_>) -> u32 {
Kind::Build, Kind::Build,
); );
rustc_cargo(builder, &mut cargo, target, &compiler); rustc_cargo(builder, &mut cargo, target, &compiler, &self.crates);
// NB: all RUSTFLAGS should be added to `rustc_cargo()` so they will be // NB: all RUSTFLAGS should be added to `rustc_cargo()` so they will be
// consistently applied by check/doc/test modes too. // consistently applied by check/doc/test modes too.
@ -1047,10 +1047,11 @@ pub fn rustc_cargo(
cargo: &mut Cargo, cargo: &mut Cargo,
target: TargetSelection, target: TargetSelection,
compiler: &Compiler, compiler: &Compiler,
crates: &[String],
) { ) {
cargo cargo
.arg("--features") .arg("--features")
.arg(builder.rustc_features(builder.kind, target)) .arg(builder.rustc_features(builder.kind, target, crates))
.arg("--manifest-path") .arg("--manifest-path")
.arg(builder.src.join("compiler/rustc/Cargo.toml")); .arg(builder.src.join("compiler/rustc/Cargo.toml"));

View File

@ -826,7 +826,7 @@ fn run(self, builder: &Builder<'_>) {
// see https://github.com/rust-lang/rust/pull/122066#issuecomment-1983049222 // see https://github.com/rust-lang/rust/pull/122066#issuecomment-1983049222
// cargo.rustdocflag("--generate-link-to-definition"); // cargo.rustdocflag("--generate-link-to-definition");
compile::rustc_cargo(builder, &mut cargo, target, &compiler); compile::rustc_cargo(builder, &mut cargo, target, &compiler, &self.crates);
cargo.arg("-Zskip-rustdoc-fingerprint"); cargo.arg("-Zskip-rustdoc-fingerprint");
// Only include compiler crates, no dependencies of those, such as `libc`. // Only include compiler crates, no dependencies of those, such as `libc`.

View File

@ -2690,7 +2690,7 @@ fn run(self, builder: &Builder<'_>) {
} }
} }
Mode::Rustc => { Mode::Rustc => {
compile::rustc_cargo(builder, &mut cargo, target, &compiler); compile::rustc_cargo(builder, &mut cargo, target, &compiler, &self.crates);
} }
_ => panic!("can only test libraries"), _ => panic!("can only test libraries"),
}; };

View File

@ -1,3 +1,4 @@
use std::collections::BTreeMap;
use std::path::PathBuf; use std::path::PathBuf;
use serde_derive::Deserialize; use serde_derive::Deserialize;
@ -21,6 +22,7 @@ struct Package {
manifest_path: String, manifest_path: String,
dependencies: Vec<Dependency>, dependencies: Vec<Dependency>,
targets: Vec<Target>, targets: Vec<Target>,
features: BTreeMap<String, Vec<String>>,
} }
/// For more information, see the output of /// For more information, see the output of
@ -51,7 +53,13 @@ pub fn build(build: &mut Build) {
.map(|dep| dep.name) .map(|dep| dep.name)
.collect(); .collect();
let has_lib = package.targets.iter().any(|t| t.kind.iter().any(|k| k == "lib")); let has_lib = package.targets.iter().any(|t| t.kind.iter().any(|k| k == "lib"));
let krate = Crate { name: name.clone(), deps, path, has_lib }; let krate = Crate {
name: name.clone(),
deps,
path,
has_lib,
features: package.features.keys().cloned().collect(),
};
let relative_path = krate.local_path(build); let relative_path = krate.local_path(build);
build.crates.insert(name.clone(), krate); build.crates.insert(name.clone(), krate);
let existing_path = build.crate_paths.insert(relative_path, name); let existing_path = build.crate_paths.insert(relative_path, name);

View File

@ -183,6 +183,7 @@ struct Crate {
deps: HashSet<String>, deps: HashSet<String>,
path: PathBuf, path: PathBuf,
has_lib: bool, has_lib: bool,
features: Vec<String>,
} }
impl Crate { impl Crate {
@ -672,16 +673,24 @@ fn std_features(&self, target: TargetSelection) -> String {
} }
/// Gets the space-separated set of activated features for the compiler. /// Gets the space-separated set of activated features for the compiler.
fn rustc_features(&self, kind: Kind, target: TargetSelection) -> String { fn rustc_features(&self, kind: Kind, target: TargetSelection, crates: &[String]) -> String {
let possible_features_by_crates: HashSet<_> = crates
.iter()
.flat_map(|krate| &self.crates[krate].features)
.map(std::ops::Deref::deref)
.collect();
let check = |feature: &str| -> bool {
crates.is_empty() || possible_features_by_crates.contains(feature)
};
let mut features = vec![]; let mut features = vec![];
if self.config.jemalloc { if self.config.jemalloc && check("jemalloc") {
features.push("jemalloc"); features.push("jemalloc");
} }
if self.config.llvm_enabled(target) || kind == Kind::Check { if (self.config.llvm_enabled(target) || kind == Kind::Check) && check("llvm") {
features.push("llvm"); features.push("llvm");
} }
// keep in sync with `bootstrap/compile.rs:rustc_cargo_env` // keep in sync with `bootstrap/compile.rs:rustc_cargo_env`
if self.config.rustc_parallel { if self.config.rustc_parallel && check("rustc_use_parallel_compiler") {
features.push("rustc_use_parallel_compiler"); features.push("rustc_use_parallel_compiler");
} }
if self.config.rust_randomize_layout { if self.config.rust_randomize_layout {
@ -693,7 +702,7 @@ fn rustc_features(&self, kind: Kind, target: TargetSelection) -> String {
// which is everything (including debug/trace/etc.) // which is everything (including debug/trace/etc.)
// if its unset, if debug_assertions is on, then debug_logging will also be on // if its unset, if debug_assertions is on, then debug_logging will also be on
// as well as tracing *ignoring* this feature when debug_assertions is on // as well as tracing *ignoring* this feature when debug_assertions is on
if !self.config.rust_debug_logging { if !self.config.rust_debug_logging && check("max_level_info") {
features.push("max_level_info"); features.push("max_level_info");
} }