Skip to content

Commit a6070ec

Browse files
hzellercopybara-github
authored andcommitted
Update different type used for Sysroot in crubit
Used to be PathBuf in rustc, now new Sysroot struct. PiperOrigin-RevId: 778440356 Change-Id: I1be2c5e00a43c35454716c1a16948d3dca3a9df0
1 parent c65afa7 commit a6070ec

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

cargo/cc_bindings_from_rs/run_compiler/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ path = "../../../cc_bindings_from_rs/run_compiler.rs"
1818
[dependencies]
1919
arc_anyhow = { path = "../../../cargo/common/arc_anyhow"}
2020
either.workspace = true
21+
rustversion.workspace = true

cc_bindings_from_rs/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ rust_library(
134134
srcs = [
135135
"run_compiler.rs",
136136
],
137+
proc_macro_deps = [
138+
"@crate_index//:rustversion",
139+
],
137140
# LINT.IfChange
138141
rustc_flags = ["-Zallow-features=rustc_private,cfg_accessible"],
139142
# LINT.ThenChange(//docs/overview/unstable_features.md)

cc_bindings_from_rs/run_compiler.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use rustc_interface::interface::Compiler;
2121
use rustc_middle::ty::TyCtxt; // See also <internal link>/ty.html#import-conventions
2222
use rustc_session::config::ErrorOutputType;
2323
use rustc_session::EarlyDiagCtxt;
24+
use std::path::PathBuf;
2425

2526
/// Wrapper around `rustc_driver::RunCompiler::run` that exposes a
2627
/// simplified API:
@@ -202,6 +203,16 @@ mod tests {
202203
Ok(())
203204
}
204205

206+
#[rustversion::before(2025-06-25)]
207+
fn sysroot_path() -> PathBuf {
208+
get_sysroot_for_testing()
209+
}
210+
211+
#[rustversion::since(2025-06-25)]
212+
fn sysroot_path() -> PathBuf {
213+
get_sysroot_for_testing().path().to_path_buf()
214+
}
215+
205216
/// `test_run_compiler_no_output_file` tests that we stop the compilation
206217
/// midway (i.e. that we return `Stop` from `after_analysis`).
207218
#[test]
@@ -216,7 +227,7 @@ mod tests {
216227
// Default parameters.
217228
"run_compiler_unittest_executable".to_string(),
218229
"--crate-type=lib".to_string(),
219-
format!("--sysroot={}", get_sysroot_for_testing().display()),
230+
format!("--sysroot={}", sysroot_path().display()),
220231
rs_path.display().to_string(),
221232
// Test-specific parameter: asking for after-analysis output
222233
"-o".to_string(),

cc_bindings_from_rs/run_compiler_test_support.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,17 @@ use itertools::Itertools;
1919

2020
use rustc_middle::ty::TyCtxt;
2121
use rustc_session::config::{CrateType, Input, Options, OutputType, OutputTypes};
22+
23+
#[rustversion::since(2025-06-25)]
24+
use rustc_session::config::Sysroot;
25+
2226
use rustc_span::def_id::LocalDefId;
2327
use rustc_target::spec::TargetTuple;
2428

25-
use std::path::{Path, PathBuf};
29+
use std::path::Path;
30+
31+
#[rustversion::before(2025-06-25)]
32+
use std::path::PathBuf;
2633

2734
#[cfg(oss)]
2835
const TOOLCHAIN_ROOT: &str = "rust_linux_x86_64__x86_64-unknown-linux-gnu__nightly_tools/rust_toolchain/lib/rustlib/x86_64-unknown-linux-gnu";
@@ -35,6 +42,8 @@ const TOOLCHAIN_ROOT: &str = env!("G3_SYSROOT_PATH");
3542
/// The sysroot is used internally by `run_compiler_for_testing`, but it may
3643
/// also be passed as `--sysroot=...` in `rustc_args` argument of
3744
/// `run_compiler`
45+
46+
#[rustversion::before(2025-06-25)]
3847
pub fn get_sysroot_for_testing() -> PathBuf {
3948
let runfiles = runfiles::Runfiles::create().unwrap();
4049
let loc = runfiles.rlocation(Path::new(TOOLCHAIN_ROOT)).expect("Failed to locate runfile");
@@ -43,6 +52,15 @@ pub fn get_sysroot_for_testing() -> PathBuf {
4352
loc
4453
}
4554

55+
#[rustversion::since(2025-06-25)]
56+
pub fn get_sysroot_for_testing() -> Sysroot {
57+
let runfiles = runfiles::Runfiles::create().unwrap();
58+
let loc = runfiles.rlocation(Path::new(TOOLCHAIN_ROOT)).expect("Failed to locate runfile");
59+
assert!(loc.exists(), "Sysroot directory '{}' doesn't exist", loc.display());
60+
assert!(loc.is_dir(), "Provided sysroot '{}' is not a directory", loc.display());
61+
Sysroot::new(Some(loc))
62+
}
63+
4664
/// If a rustc --target arg is necessary, sets it up and returns its value.
4765
///
4866
/// We use a target json in some configurations. Its filename needs to match the

0 commit comments

Comments
 (0)