Skip to content

Commit

Permalink
feat: new arg --cargo-config for passing --config to cargo
Browse files Browse the repository at this point in the history
This is another step toward making opt-dist work in sandboxed environments.

`--cargo-config` is added to the following subcommands:

* `binary_stats compile`
* `bench_runtime_local`
* `bench_local`
* `profile_local`

The current rustc-perf uses `tempfile::Tempdir` as the working
directory when collecting profiles from some of these packages.
This "tmp" working directory usage make it impossible for Cargo to pick
up the correct vendor sources setting in `.cargo/config.toml` bundled
in the rustc-src tarball.

We can leverage [`cargo --config`][1] to specify additional Cargo's
configuration. Training data then could build with correct vendored
settings via the `--cargo-config` flag.

[1]: https://doc.rust-lang.org/nightly/cargo/commands/cargo.html#option-cargo---config
  • Loading branch information
weihanglo committed May 29, 2024
1 parent 892c681 commit 8fe150e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
3 changes: 3 additions & 0 deletions collector/src/artifact_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ pub fn compile_and_get_stats(

let mut cmd = Command::new(&toolchain.components.cargo);
cmd.arg("build").arg("--target-dir").arg(tempdir.path());
for config in &toolchain.components.cargo_config {
cmd.arg("--config").arg(config);
}
match profile {
CargoProfile::Debug => {}
CargoProfile::Release => {
Expand Down
14 changes: 9 additions & 5 deletions collector/src/bin/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ struct LocalOptions {
#[arg(long)]
cargo: Option<PathBuf>,

/// Arguments passed to `cargo --config <value>`.
#[arg(long)]
cargo_config: Option<Vec<String>>,

/// Exclude all benchmarks matching a prefix in this comma-separated list
#[arg(long, value_delimiter = ',')]
exclude: Vec<String>,
Expand Down Expand Up @@ -845,7 +849,7 @@ fn main_result() -> anyhow::Result<i32> {
*ToolchainConfig::default()
.rustdoc(opts.rustdoc.as_deref())
.clippy(opts.clippy.as_deref())
.cargo(local.cargo.as_deref())
.cargo(local.cargo.as_deref(), local.cargo_config.as_deref())
.id(local.id.as_deref()),
"",
target_triple,
Expand Down Expand Up @@ -1070,7 +1074,7 @@ fn main_result() -> anyhow::Result<i32> {
*ToolchainConfig::default()
.rustdoc(opts.rustdoc.as_deref())
.clippy(opts.clippy.as_deref())
.cargo(local.cargo.as_deref())
.cargo(local.cargo.as_deref(), local.cargo_config.as_deref())
.id(local.id.as_deref()),
suffix,
target_triple.clone(),
Expand Down Expand Up @@ -1228,7 +1232,7 @@ fn binary_stats_compile(
&[codegen_backend],
&local.rustc,
*ToolchainConfig::default()
.cargo(local.cargo.as_deref())
.cargo(local.cargo.as_deref(), local.cargo_config.as_deref())
.id(local.id.as_deref()),
"",
target_triple.to_string(),
Expand All @@ -1240,7 +1244,7 @@ fn binary_stats_compile(
&[codegen_backend2],
&rustc,
*ToolchainConfig::default()
.cargo(local.cargo.as_deref())
.cargo(local.cargo.as_deref(), local.cargo_config.as_deref())
.id(local.id.as_deref()),
"",
target_triple.to_string(),
Expand Down Expand Up @@ -1484,7 +1488,7 @@ fn get_local_toolchain_for_runtime_benchmarks(
&[CodegenBackend::Llvm],
&local.rustc,
*ToolchainConfig::default()
.cargo(local.cargo.as_deref())
.cargo(local.cargo.as_deref(), local.cargo_config.as_deref())
.id(local.id.as_deref()),
"",
target_triple.to_string(),
Expand Down
4 changes: 4 additions & 0 deletions collector/src/compile/execute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ impl<'a> CargoProcess<'a> {
if let Some(c) = &self.toolchain.components.clippy {
cmd.env("CLIPPY", &*FAKE_CLIPPY).env("CLIPPY_REAL", c);
}

for config in &self.toolchain.components.cargo_config {
cmd.arg("--config").arg(config);
}
cmd
}

Expand Down
4 changes: 4 additions & 0 deletions collector/src/runtime/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,10 @@ fn start_cargo_build(
#[cfg(feature = "precise-cachegrind")]
command.arg("--features").arg("benchlib/precise-cachegrind");

for config in &toolchain.components.cargo_config {
command.arg("--config").arg(config);
}

CargoArtifactIter::from_cargo_cmd(command)
.map_err(|error| anyhow::anyhow!("Failed to start cargo: {:?}", error))
}
Expand Down
17 changes: 12 additions & 5 deletions collector/src/toolchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ pub struct ToolchainComponents {
pub rustdoc: Option<PathBuf>,
pub clippy: Option<PathBuf>,
pub cargo: PathBuf,
pub cargo_config: Vec<String>,
pub lib_rustc: Option<PathBuf>,
pub lib_std: Option<PathBuf>,
pub lib_test: Option<PathBuf>,
Expand Down Expand Up @@ -329,6 +330,8 @@ pub struct ToolchainConfig<'a> {
rustdoc: Option<&'a Path>,
clippy: Option<&'a Path>,
cargo: Option<&'a Path>,
/// For `cargo --config <value>`.
cargo_config: Option<&'a [String]>,
id: Option<&'a str>,
}

Expand All @@ -343,8 +346,9 @@ impl<'a> ToolchainConfig<'a> {
self
}

pub fn cargo(&mut self, cargo: Option<&'a Path>) -> &mut Self {
pub fn cargo(&mut self, cargo: Option<&'a Path>, config: Option<&'a [String]>) -> &mut Self {
self.cargo = cargo;
self.cargo_config = config;
self
}

Expand Down Expand Up @@ -520,13 +524,16 @@ pub fn get_local_toolchain(
debug!("found cargo: {:?}", &cargo);
cargo
};

let lib_dir = get_lib_dir_from_rustc(&rustc).context("Cannot find libdir for rustc")?;

let mut components =
ToolchainComponents::from_binaries_and_libdir(rustc, rustdoc, clippy, cargo, &lib_dir)?;
components.cargo_config = toolchain_config
.cargo_config
.map(|c| c.into())
.unwrap_or_default();
Ok(Toolchain {
components: ToolchainComponents::from_binaries_and_libdir(
rustc, rustdoc, clippy, cargo, &lib_dir,
)?,
components,
id,
triple: target_triple,
})
Expand Down

0 comments on commit 8fe150e

Please sign in to comment.