Skip to content

Commit

Permalink
Apply LTO config to rustdoc
Browse files Browse the repository at this point in the history
Before, the LTO configuration from `config.toml` was not applied to `rustdoc`.
  • Loading branch information
Kobzol committed Jan 22, 2025
1 parent b605c65 commit 1fe351b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
19 changes: 16 additions & 3 deletions src/bootstrap/src/core/build_steps/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ use std::{env, fs};
use crate::core::build_steps::toolstate::ToolState;
use crate::core::build_steps::{compile, llvm};
use crate::core::builder;
use crate::core::builder::{Builder, Cargo as CargoCommand, RunConfig, ShouldRun, Step};
use crate::core::config::{DebuginfoLevel, TargetSelection};
use crate::core::builder::{
Builder, Cargo as CargoCommand, RunConfig, ShouldRun, Step, cargo_profile_var,
};
use crate::core::config::{DebuginfoLevel, RustcLto, TargetSelection};
use crate::utils::channel::GitInfo;
use crate::utils::exec::{BootstrapCommand, command};
use crate::utils::helpers::{add_dylib_path, exe, t};
Expand Down Expand Up @@ -645,7 +647,7 @@ impl Step for Rustdoc {
}

// NOTE: Never modify the rustflags here, it breaks the build cache for other tools!
let cargo = prepare_tool_cargo(
let mut cargo = prepare_tool_cargo(
builder,
build_compiler,
Mode::ToolRustc,
Expand All @@ -656,6 +658,17 @@ impl Step for Rustdoc {
features.as_slice(),
);

// rustdoc is performance sensitive, so apply LTO to it.
let lto = match builder.config.rust_lto {
RustcLto::Off => Some("off"),
RustcLto::Thin => Some("thin"),
RustcLto::Fat => Some("fat"),
RustcLto::ThinLocal => None,
};
if let Some(lto) = lto {
cargo.env(cargo_profile_var("LTO", &builder.config), lto);
}

let _guard = builder.msg_tool(
Kind::Build,
Mode::ToolRustc,
Expand Down
12 changes: 7 additions & 5 deletions src/bootstrap/src/core/builder/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::core::config::flags::Color;
use crate::utils::build_stamp;
use crate::utils::helpers::{self, LldThreads, check_cfg_arg, linker_args, linker_flags};
use crate::{
BootstrapCommand, CLang, Compiler, DocTests, DryRun, EXTRA_CHECK_CFGS, GitRepo, Mode,
BootstrapCommand, CLang, Compiler, Config, DocTests, DryRun, EXTRA_CHECK_CFGS, GitRepo, Mode,
TargetSelection, command, prepare_behaviour_dump_dir, t,
};

Expand Down Expand Up @@ -473,10 +473,7 @@ impl Builder<'_> {
build_stamp::clear_if_dirty(self, &my_out, &rustdoc);
}

let profile_var = |name: &str| {
let profile = if self.config.rust_optimize.is_release() { "RELEASE" } else { "DEV" };
format!("CARGO_PROFILE_{}_{}", profile, name)
};
let profile_var = |name: &str| cargo_profile_var(name, &self.config);

// See comment in rustc_llvm/build.rs for why this is necessary, largely llvm-config
// needs to not accidentally link to libLLVM in stage0/lib.
Expand Down Expand Up @@ -1221,3 +1218,8 @@ impl Builder<'_> {
}
}
}

pub fn cargo_profile_var(name: &str, config: &Config) -> String {
let profile = if config.rust_optimize.is_release() { "RELEASE" } else { "DEV" };
format!("CARGO_PROFILE_{}_{}", profile, name)
}
2 changes: 1 addition & 1 deletion src/bootstrap/src/core/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{env, fs};

use clap::ValueEnum;

pub use self::cargo::Cargo;
pub use self::cargo::{Cargo, cargo_profile_var};
pub use crate::Compiler;
use crate::core::build_steps::{
check, clean, clippy, compile, dist, doc, gcc, install, llvm, run, setup, test, tool, vendor,
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: 135832,
severity: ChangeSeverity::Info,
summary: "Rustdoc now respects the value of rust.lto.",
},
];

0 comments on commit 1fe351b

Please sign in to comment.