Skip to content

Commit

Permalink
Rollup merge of #131315 - shrirambalaji:issue-129599-fix-shrirambalaj…
Browse files Browse the repository at this point in the history
…i, r=onur-ozkan

bootstrap: add `std_features` config

Adding support for a std-features config under the rust section in config.toml during bootstrap. This allows rustc devs to build with specific feature flags for local development.
  • Loading branch information
matthiaskrgr authored Oct 6, 2024
2 parents 11c41ff + 8b96876 commit 3c4bc7d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 13 deletions.
10 changes: 10 additions & 0 deletions config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,16 @@
# Build compiler with the optimization enabled and -Zvalidate-mir, currently only for `std`
#validate-mir-opts = 3

# Configure `std` features used during bootstrap.
# Default features will be expanded in the following cases:
# - If `rust.llvm-libunwind` or `target.llvm-libunwind` is enabled:
# - "llvm-libunwind" will be added for in-tree LLVM builds.
# - "system-llvm-libunwind" will be added for system LLVM builds.
# - If `rust.backtrace` is enabled, "backtrace" will be added.
# - If `rust.profiler` or `target.profiler` is enabled, "profiler" will be added.
# - If building for a zkvm target, "compiler-builtins-mem" will be added.
#std-features = ["panic_unwind"]

# =============================================================================
# Options for specific targets
#
Expand Down
12 changes: 11 additions & 1 deletion src/bootstrap/src/core/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! how the build runs.

use std::cell::{Cell, RefCell};
use std::collections::{HashMap, HashSet};
use std::collections::{BTreeSet, HashMap, HashSet};
use std::fmt::{self, Display};
use std::io::IsTerminal;
use std::path::{Path, PathBuf, absolute};
Expand Down Expand Up @@ -287,6 +287,7 @@ pub struct Config {
pub rust_profile_generate: Option<String>,
pub rust_lto: RustcLto,
pub rust_validate_mir_opts: Option<u32>,
pub rust_std_features: BTreeSet<String>,
pub llvm_profile_use: Option<String>,
pub llvm_profile_generate: bool,
pub llvm_libunwind_default: Option<LlvmLibunwind>,
Expand Down Expand Up @@ -1152,6 +1153,7 @@ define_config! {
download_rustc: Option<StringOrBool> = "download-rustc",
lto: Option<String> = "lto",
validate_mir_opts: Option<u32> = "validate-mir-opts",
std_features: Option<BTreeSet<String>> = "std-features",
}
}

Expand Down Expand Up @@ -1645,6 +1647,7 @@ impl Config {
let mut optimize = None;
let mut omit_git_hash = None;
let mut lld_enabled = None;
let mut std_features = None;

let mut is_user_configured_rust_channel = false;

Expand Down Expand Up @@ -1703,6 +1706,7 @@ impl Config {
stack_protector,
strip,
lld_mode,
std_features: std_features_toml,
} = rust;

is_user_configured_rust_channel = channel.is_some();
Expand All @@ -1722,6 +1726,7 @@ impl Config {
debuginfo_level_tools = debuginfo_level_tools_toml;
debuginfo_level_tests = debuginfo_level_tests_toml;
lld_enabled = lld_enabled_toml;
std_features = std_features_toml;

optimize = optimize_toml;
omit_git_hash = omit_git_hash_toml;
Expand Down Expand Up @@ -2118,6 +2123,9 @@ impl Config {
);
}

let default_std_features = BTreeSet::from([String::from("panic-unwind")]);
config.rust_std_features = std_features.unwrap_or(default_std_features);

let default = debug == Some(true);
config.rust_debug_assertions = debug_assertions.unwrap_or(default);
config.rust_debug_assertions_std =
Expand Down Expand Up @@ -3016,6 +3024,7 @@ fn check_incompatible_options_for_ci_rustc(
description,
incremental,
default_linker,
std_features,

// Rest of the options can simply be ignored.
debug: _,
Expand Down Expand Up @@ -3077,6 +3086,7 @@ fn check_incompatible_options_for_ci_rustc(
err!(current_rust_config.default_linker, default_linker);
err!(current_rust_config.stack_protector, stack_protector);
err!(current_rust_config.lto, lto);
err!(current_rust_config.std_features, std_features);

warn!(current_rust_config.channel, channel);
warn!(current_rust_config.description, description);
Expand Down
22 changes: 22 additions & 0 deletions src/bootstrap/src/core/config/tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::BTreeSet;
use std::env;
use std::fs::{File, remove_file};
use std::io::Write;
Expand Down Expand Up @@ -326,3 +327,24 @@ fn verbose_tests_default_value() {
let config = Config::parse(Flags::parse(&["build".into(), "compiler".into(), "-v".into()]));
assert_eq!(config.verbose_tests, true);
}

#[test]
fn parse_rust_std_features() {
let config = parse("rust.std-features = [\"panic-unwind\", \"backtrace\"]");
let expected_features: BTreeSet<String> =
["panic-unwind", "backtrace"].into_iter().map(|s| s.to_string()).collect();
assert_eq!(config.rust_std_features, expected_features);
}

#[test]
fn parse_rust_std_features_empty() {
let config = parse("rust.std-features = []");
let expected_features: BTreeSet<String> = BTreeSet::new();
assert_eq!(config.rust_std_features, expected_features);
}

#[test]
#[should_panic]
fn parse_rust_std_features_invalid() {
parse("rust.std-features = \"backtrace\"");
}
27 changes: 15 additions & 12 deletions src/bootstrap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! also check out the `src/bootstrap/README.md` file for more information.

use std::cell::{Cell, RefCell};
use std::collections::{HashMap, HashSet};
use std::collections::{BTreeSet, HashMap, HashSet};
use std::fmt::Display;
use std::fs::{self, File};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -645,28 +645,31 @@ impl Build {
&self.config.rust_info
}

/// Gets the space-separated set of activated features for the standard
/// library.
/// Gets the space-separated set of activated features for the standard library.
/// This can be configured with the `std-features` key in config.toml.
fn std_features(&self, target: TargetSelection) -> String {
let mut features = " panic-unwind".to_string();
let mut features: BTreeSet<&str> =
self.config.rust_std_features.iter().map(|s| s.as_str()).collect();

match self.config.llvm_libunwind(target) {
LlvmLibunwind::InTree => features.push_str(" llvm-libunwind"),
LlvmLibunwind::System => features.push_str(" system-llvm-libunwind"),
LlvmLibunwind::No => {}
}
LlvmLibunwind::InTree => features.insert("llvm-libunwind"),
LlvmLibunwind::System => features.insert("system-llvm-libunwind"),
LlvmLibunwind::No => false,
};

if self.config.backtrace {
features.push_str(" backtrace");
features.insert("backtrace");
}
if self.config.profiler_enabled(target) {
features.push_str(" profiler");
features.insert("profiler");
}
// Generate memcpy, etc. FIXME: Remove this once compiler-builtins
// automatically detects this target.
if target.contains("zkvm") {
features.push_str(" compiler-builtins-mem");
features.insert("compiler-builtins-mem");
}
features

features.into_iter().collect::<Vec<_>>().join(" ")
}

/// Gets the space-separated set of activated features for the compiler.
Expand Down

0 comments on commit 3c4bc7d

Please sign in to comment.