Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle --release flag separately for tools #136048

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/bootstrap/src/core/build_steps/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,10 +790,11 @@ impl Step for Analysis {
return None;
}

let mode = Mode::Std;
let src = builder
.stage_out(compiler, Mode::Std)
.stage_out(compiler, mode)
.join(target)
.join(builder.cargo_dir())
.join(builder.cargo_dir(&mode))
.join("deps")
.join("save-analysis");

Expand Down
5 changes: 3 additions & 2 deletions src/bootstrap/src/core/build_steps/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,10 +742,11 @@ impl Step for Clippy {
let compiler = builder.compiler(stage, host);

builder.ensure(tool::Clippy { compiler, target: self.host });
let mode = Mode::ToolRustc;
let mut cargo = tool::prepare_tool_cargo(
builder,
compiler,
Mode::ToolRustc,
mode,
host,
Kind::Test,
"src/tools/clippy",
Expand All @@ -755,7 +756,7 @@ impl Step for Clippy {

cargo.env("RUSTC_TEST_SUITE", builder.rustc(compiler));
cargo.env("RUSTC_LIB_PATH", builder.rustc_libdir(compiler));
let host_libs = builder.stage_out(compiler, Mode::ToolRustc).join(builder.cargo_dir());
let host_libs = builder.stage_out(compiler, mode).join(builder.cargo_dir(&mode));
cargo.env("HOST_LIBS", host_libs);

cargo.add_rustc_lib_path(builder);
Expand Down
6 changes: 3 additions & 3 deletions src/bootstrap/src/core/builder/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,9 @@ impl Builder<'_> {
assert_eq!(target, compiler.host);
}

if self.config.rust_optimize.is_release() &&
// cargo bench/install do not accept `--release` and miri doesn't want it
!matches!(cmd_kind, Kind::Bench | Kind::Install | Kind::Miri | Kind::MiriSetup | Kind::MiriTest)
if self.is_release_build(&mode) &&
// cargo bench/install do not accept `--release` and miri doesn't want it
!matches!(cmd_kind, Kind::Bench | Kind::Install | Kind::Miri | Kind::MiriSetup | Kind::MiriTest)
{
cargo.arg("--release");
}
Expand Down
49 changes: 48 additions & 1 deletion src/bootstrap/src/core/builder/tests.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::thread;

use super::*;
use crate::Flags;
use crate::core::build_steps::doc::DocumentationFormat;
use crate::core::config::Config;
use crate::{Flags, Mode};

static TEST_TRIPLE_1: &str = "i686-unknown-haiku";
static TEST_TRIPLE_2: &str = "i686-unknown-hurd-gnu";
Expand Down Expand Up @@ -861,3 +861,50 @@ fn test_test_coverage() {
assert_eq!(modes, expected);
}
}

#[test]
fn test_release_directory() {
let config = Config::parse_inner(
Flags::parse(&["check".into(), "--config=/does/not/exist".into()]),
|&_| toml::from_str(""),
);
let build = Build::new(config.clone());
let builder = Builder::new(&build);

assert_eq!(builder.cargo_dir(&Mode::Rustc), "release");
assert_eq!(builder.cargo_dir(&Mode::ToolStd), "debug");

let config = Config::parse_inner(
Flags::parse(&["check".into(), "--config=/does/not/exist".into(), "--release".into()]),
|&_| {
toml::from_str(
r#"
[rust]
optimize = false
"#,
)
},
);
let build = Build::new(config.clone());
let builder = Builder::new(&build);

assert_eq!(builder.cargo_dir(&Mode::Std), "release");
assert_eq!(builder.cargo_dir(&Mode::ToolBootstrap), "release");

let config = Config::parse_inner(
Flags::parse(&["check".into(), "--config=/does/not/exist".into()]),
|&_| {
toml::from_str(
r#"
[rust]
optimize = false
"#,
)
},
);
let build = Build::new(config.clone());
let builder = Builder::new(&build);

assert_eq!(builder.cargo_dir(&Mode::Rustc), "debug");
assert_eq!(builder.cargo_dir(&Mode::Codegen), "debug");
}
11 changes: 10 additions & 1 deletion src/bootstrap/src/core/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ pub struct Config {
pub incremental: bool,
pub dry_run: DryRun,
pub dump_bootstrap_shims: bool,
pub enable_release_build: bool,
/// Arguments appearing after `--` to be forwarded to tools,
/// e.g. `--fix-broken` or test arguments.
pub free_args: Vec<String>,
Expand Down Expand Up @@ -1355,6 +1356,7 @@ impl Config {
config.llvm_profile_generate = flags.llvm_profile_generate;
config.enable_bolt_settings = flags.enable_bolt_settings;
config.bypass_bootstrap_lock = flags.bypass_bootstrap_lock;
config.enable_release_build = flags.release || (CiEnv::is_ci() && !cfg!(test));

// Infer the rest of the configuration.

Expand Down Expand Up @@ -2182,7 +2184,14 @@ impl Config {
config.llvm_enzyme = llvm_enzyme.unwrap_or(false);
config.llvm_offload = llvm_offload.unwrap_or(false);
config.llvm_plugins = llvm_plugins.unwrap_or(false);
config.rust_optimize = optimize.unwrap_or(RustOptimize::Bool(true));

config.rust_optimize = match optimize {
Some(inner) => match inner {
RustOptimize::Bool(false) => RustOptimize::Bool(config.enable_release_build),
t => t,
},
None => RustOptimize::Bool(true),
};

// We make `x86_64-unknown-linux-gnu` use the self-contained linker by default, so we will
// build our internal lld and use it as the default linker, by setting the `rust.lld` config
Expand Down
4 changes: 4 additions & 0 deletions src/bootstrap/src/core/config/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ pub struct Flags {
/// Unless you know exactly what you are doing, you probably don't need this.
pub bypass_bootstrap_lock: bool,

#[arg(global = true, long)]
// Build artifacts in release mode
pub release: bool,

/// generate PGO profile with rustc build
#[arg(global = true, value_hint = clap::ValueHint::FilePath, long, value_name = "PROFILE")]
pub rust_profile_generate: Option<String>,
Expand Down
33 changes: 33 additions & 0 deletions src/bootstrap/src/core/config/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,3 +454,36 @@ fn check_rustc_if_unchanged_paths() {
assert!(config.src.join(p).exists(), "{p} doesn't exist.");
}
}

#[test]
fn test_release_options() {
let config = Config::parse_inner(
Flags::parse(&["check".into(), "--config=/does/not/exist".into()]),
|&_| {
toml::from_str(
r#"
[rust]
optimize = false
"#,
)
},
);

assert!(!config.enable_release_build);
assert!(!config.rust_optimize.is_release());

let config = Config::parse_inner(
Flags::parse(&["check".into(), "--config=/does/not/exist".into(), "--release".into()]),
|&_| {
toml::from_str(
r#"
[rust]
optimize = false
"#,
)
},
);

assert!(config.enable_release_build);
assert!(config.rust_optimize.is_release());
}
15 changes: 12 additions & 3 deletions src/bootstrap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,10 +660,19 @@ impl Build {
features.join(" ")
}

fn is_release_build(&self, mode: &Mode) -> bool {
match mode {
Mode::Std | Mode::Rustc => self.config.rust_optimize.is_release(),
Mode::Codegen | Mode::ToolBootstrap | Mode::ToolStd | Mode::ToolRustc => {
self.config.enable_release_build
}
}
}

/// Component directory that Cargo will produce output into (e.g.
/// release/debug)
fn cargo_dir(&self) -> &'static str {
if self.config.rust_optimize.is_release() { "release" } else { "debug" }
fn cargo_dir(&self, mode: &Mode) -> &'static str {
if self.is_release_build(mode) { "release" } else { "debug" }
}

fn tools_dir(&self, compiler: Compiler) -> PathBuf {
Expand Down Expand Up @@ -691,7 +700,7 @@ impl Build {
/// running a particular compiler, whether or not we're building the
/// standard library, and targeting the specified architecture.
fn cargo_out(&self, compiler: Compiler, mode: Mode, target: TargetSelection) -> PathBuf {
self.stage_out(compiler, mode).join(target).join(self.cargo_dir())
self.stage_out(compiler, mode).join(target).join(self.cargo_dir(&mode))
}

/// Root output directory of LLVM for `target`
Expand Down
5 changes: 5 additions & 0 deletions src/bootstrap/src/utils/change_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,4 +340,9 @@ pub const CONFIG_CHANGE_HISTORY: &[ChangeInfo] = &[
severity: ChangeSeverity::Info,
summary: "Change the compiler profile to default to rust.debug-assertions = true",
},
ChangeInfo {
change_id: 136048,
severity: ChangeSeverity::Info,
summary: "Added new flag support `--release`.",
},
];
Loading
Loading