Pass around Compiler instead of target triples

This commit is contained in:
bjorn3 2023-01-13 12:16:11 +00:00
parent cf22470de7
commit 70a1cb9e62
7 changed files with 64 additions and 84 deletions

View File

@ -17,34 +17,28 @@ pub(crate) fn run(
sysroot_kind: SysrootKind, sysroot_kind: SysrootKind,
dirs: &Dirs, dirs: &Dirs,
cg_clif_dylib: &Path, cg_clif_dylib: &Path,
host_triple: &str, host_compiler: &Compiler,
target_triple: &str,
) { ) {
if !config::get_bool("testsuite.abi-cafe") { if !config::get_bool("testsuite.abi-cafe") {
eprintln!("[SKIP] abi-cafe"); eprintln!("[SKIP] abi-cafe");
return; return;
} }
if host_triple != target_triple {
eprintln!("[SKIP] abi-cafe (cross-compilation not supported)");
return;
}
eprintln!("Building sysroot for abi-cafe"); eprintln!("Building sysroot for abi-cafe");
build_sysroot::build_sysroot( build_sysroot::build_sysroot(
dirs, dirs,
channel, channel,
sysroot_kind, sysroot_kind,
cg_clif_dylib, cg_clif_dylib,
host_triple, host_compiler,
target_triple, &host_compiler.triple,
); );
eprintln!("Running abi-cafe"); eprintln!("Running abi-cafe");
let pairs = ["rustc_calls_cgclif", "cgclif_calls_rustc", "cgclif_calls_cc", "cc_calls_cgclif"]; let pairs = ["rustc_calls_cgclif", "cgclif_calls_rustc", "cgclif_calls_cc", "cc_calls_cgclif"];
let mut cmd = ABI_CAFE.run(&Compiler::host(), dirs); let mut cmd = ABI_CAFE.run(host_compiler, dirs);
cmd.arg("--"); cmd.arg("--");
cmd.arg("--pairs"); cmd.arg("--pairs");
cmd.args(pairs); cmd.args(pairs);

View File

@ -21,11 +21,11 @@
pub(crate) static SIMPLE_RAYTRACER: CargoProject = pub(crate) static SIMPLE_RAYTRACER: CargoProject =
CargoProject::new(&SIMPLE_RAYTRACER_REPO.source_dir(), "simple_raytracer"); CargoProject::new(&SIMPLE_RAYTRACER_REPO.source_dir(), "simple_raytracer");
pub(crate) fn benchmark(dirs: &Dirs) { pub(crate) fn benchmark(dirs: &Dirs, host_compiler: &Compiler) {
benchmark_simple_raytracer(dirs); benchmark_simple_raytracer(dirs, host_compiler);
} }
fn benchmark_simple_raytracer(dirs: &Dirs) { fn benchmark_simple_raytracer(dirs: &Dirs, host_compiler: &Compiler) {
if std::process::Command::new("hyperfine").output().is_err() { if std::process::Command::new("hyperfine").output().is_err() {
eprintln!("Hyperfine not installed"); eprintln!("Hyperfine not installed");
eprintln!("Hint: Try `cargo install hyperfine` to install hyperfine"); eprintln!("Hint: Try `cargo install hyperfine` to install hyperfine");
@ -33,8 +33,7 @@ fn benchmark_simple_raytracer(dirs: &Dirs) {
} }
eprintln!("[LLVM BUILD] simple-raytracer"); eprintln!("[LLVM BUILD] simple-raytracer");
let host_compiler = Compiler::host(); let build_cmd = SIMPLE_RAYTRACER_LLVM.build(host_compiler, dirs);
let build_cmd = SIMPLE_RAYTRACER_LLVM.build(&host_compiler, dirs);
spawn_and_wait(build_cmd); spawn_and_wait(build_cmd);
fs::copy( fs::copy(
SIMPLE_RAYTRACER_LLVM SIMPLE_RAYTRACER_LLVM

View File

@ -10,10 +10,10 @@
pub(crate) fn build_backend( pub(crate) fn build_backend(
dirs: &Dirs, dirs: &Dirs,
channel: &str, channel: &str,
host_triple: &str, host_compiler: &Compiler,
use_unstable_features: bool, use_unstable_features: bool,
) -> PathBuf { ) -> PathBuf {
let mut cmd = CG_CLIF.build(&Compiler::host(), dirs); let mut cmd = CG_CLIF.build(&host_compiler, dirs);
cmd.env("CARGO_BUILD_INCREMENTAL", "true"); // Force incr comp even in release mode cmd.env("CARGO_BUILD_INCREMENTAL", "true"); // Force incr comp even in release mode
@ -48,7 +48,7 @@ pub(crate) fn build_backend(
CG_CLIF CG_CLIF
.target_dir(dirs) .target_dir(dirs)
.join(host_triple) .join(&host_compiler.triple)
.join(channel) .join(channel)
.join(get_file_name("rustc_codegen_cranelift", "dylib")) .join(get_file_name("rustc_codegen_cranelift", "dylib"))
} }

View File

@ -17,7 +17,7 @@ pub(crate) fn build_sysroot(
channel: &str, channel: &str,
sysroot_kind: SysrootKind, sysroot_kind: SysrootKind,
cg_clif_dylib_src: &Path, cg_clif_dylib_src: &Path,
host_triple: &str, host_compiler: &Compiler,
target_triple: &str, target_triple: &str,
) { ) {
eprintln!("[BUILD] sysroot {:?}", sysroot_kind); eprintln!("[BUILD] sysroot {:?}", sysroot_kind);
@ -53,7 +53,7 @@ pub(crate) fn build_sysroot(
let default_sysroot = super::rustc_info::get_default_sysroot(); let default_sysroot = super::rustc_info::get_default_sysroot();
let host_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(host_triple).join("lib"); let host_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(&host_compiler.triple).join("lib");
let target_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(target_triple).join("lib"); let target_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(target_triple).join("lib");
fs::create_dir_all(&host_rustlib_lib).unwrap(); fs::create_dir_all(&host_rustlib_lib).unwrap();
fs::create_dir_all(&target_rustlib_lib).unwrap(); fs::create_dir_all(&target_rustlib_lib).unwrap();
@ -83,7 +83,7 @@ pub(crate) fn build_sysroot(
SysrootKind::None => {} // Nothing to do SysrootKind::None => {} // Nothing to do
SysrootKind::Llvm => { SysrootKind::Llvm => {
for file in fs::read_dir( for file in fs::read_dir(
default_sysroot.join("lib").join("rustlib").join(host_triple).join("lib"), default_sysroot.join("lib").join("rustlib").join(&host_compiler.triple).join("lib"),
) )
.unwrap() .unwrap()
{ {
@ -103,7 +103,7 @@ pub(crate) fn build_sysroot(
try_hard_link(&file, host_rustlib_lib.join(file.file_name().unwrap())); try_hard_link(&file, host_rustlib_lib.join(file.file_name().unwrap()));
} }
if target_triple != host_triple { if target_triple != host_compiler.triple {
for file in fs::read_dir( for file in fs::read_dir(
default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"), default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"),
) )
@ -115,9 +115,15 @@ pub(crate) fn build_sysroot(
} }
} }
SysrootKind::Clif => { SysrootKind::Clif => {
build_clif_sysroot_for_triple(dirs, channel, host_triple, &cg_clif_dylib_path, None); build_clif_sysroot_for_triple(
dirs,
channel,
host_compiler.clone(),
&cg_clif_dylib_path,
None,
);
if host_triple != target_triple { if host_compiler.triple != target_triple {
// When cross-compiling it is often necessary to manually pick the right linker // When cross-compiling it is often necessary to manually pick the right linker
let linker = match target_triple { let linker = match target_triple {
"aarch64-unknown-linux-gnu" => Some("aarch64-linux-gnu-gcc"), "aarch64-unknown-linux-gnu" => Some("aarch64-linux-gnu-gcc"),
@ -127,7 +133,11 @@ pub(crate) fn build_sysroot(
build_clif_sysroot_for_triple( build_clif_sysroot_for_triple(
dirs, dirs,
channel, channel,
target_triple, {
let mut target_compiler = host_compiler.clone();
target_compiler.triple = target_triple.to_owned();
target_compiler
},
&cg_clif_dylib_path, &cg_clif_dylib_path,
linker, linker,
); );
@ -155,7 +165,7 @@ pub(crate) fn build_sysroot(
fn build_clif_sysroot_for_triple( fn build_clif_sysroot_for_triple(
dirs: &Dirs, dirs: &Dirs,
channel: &str, channel: &str,
triple: &str, mut compiler: Compiler,
cg_clif_dylib_path: &Path, cg_clif_dylib_path: &Path,
linker: Option<&str>, linker: Option<&str>,
) { ) {
@ -177,7 +187,7 @@ fn build_clif_sysroot_for_triple(
} }
} }
let build_dir = STANDARD_LIBRARY.target_dir(dirs).join(triple).join(channel); let build_dir = STANDARD_LIBRARY.target_dir(dirs).join(&compiler.triple).join(channel);
if !super::config::get_bool("keep_sysroot") { if !super::config::get_bool("keep_sysroot") {
// Cleanup the deps dir, but keep build scripts and the incremental cache for faster // Cleanup the deps dir, but keep build scripts and the incremental cache for faster
@ -188,7 +198,7 @@ fn build_clif_sysroot_for_triple(
} }
// Build sysroot // Build sysroot
let mut rustflags = "-Zforce-unstable-if-unmarked -Cpanic=abort".to_string(); let mut rustflags = " -Zforce-unstable-if-unmarked -Cpanic=abort".to_string();
rustflags.push_str(&format!(" -Zcodegen-backend={}", cg_clif_dylib_path.to_str().unwrap())); rustflags.push_str(&format!(" -Zcodegen-backend={}", cg_clif_dylib_path.to_str().unwrap()));
rustflags.push_str(&format!(" --sysroot={}", DIST_DIR.to_path(dirs).to_str().unwrap())); rustflags.push_str(&format!(" --sysroot={}", DIST_DIR.to_path(dirs).to_str().unwrap()));
if channel == "release" { if channel == "release" {
@ -198,8 +208,7 @@ fn build_clif_sysroot_for_triple(
use std::fmt::Write; use std::fmt::Write;
write!(rustflags, " -Clinker={}", linker).unwrap(); write!(rustflags, " -Clinker={}", linker).unwrap();
} }
let mut compiler = Compiler::with_triple(triple.to_owned()); compiler.rustflags += &rustflags;
compiler.rustflags = rustflags;
let mut build_cmd = STANDARD_LIBRARY.build(&compiler, dirs); let mut build_cmd = STANDARD_LIBRARY.build(&compiler, dirs);
if channel == "release" { if channel == "release" {
build_cmd.arg("--release"); build_cmd.arg("--release");
@ -219,7 +228,7 @@ fn build_clif_sysroot_for_triple(
}; };
try_hard_link( try_hard_link(
entry.path(), entry.path(),
RUSTLIB_DIR.to_path(dirs).join(triple).join("lib").join(entry.file_name()), RUSTLIB_DIR.to_path(dirs).join(&compiler.triple).join("lib").join(entry.file_name()),
); );
} }
} }

View File

@ -2,7 +2,7 @@
use std::path::PathBuf; use std::path::PathBuf;
use std::process; use std::process;
use self::utils::is_ci; use self::utils::{is_ci, Compiler};
mod abi_cafe; mod abi_cafe;
mod bench; mod bench;
@ -121,24 +121,16 @@ pub fn main() {
} }
} }
let host_triple = if let Ok(host_triple) = std::env::var("HOST_TRIPLE") { let host_compiler = Compiler::llvm_with_triple(
host_triple std::env::var("HOST_TRIPLE")
} else if let Some(host_triple) = config::get_value("host") { .ok()
host_triple .or_else(|| config::get_value("host"))
} else { .unwrap_or_else(|| rustc_info::get_host_triple()),
rustc_info::get_host_triple() );
}; let target_triple = std::env::var("TARGET_TRIPLE")
let target_triple = if let Ok(target_triple) = std::env::var("TARGET_TRIPLE") { .ok()
if target_triple != "" { .or_else(|| config::get_value("target"))
target_triple .unwrap_or_else(|| host_compiler.triple.clone());
} else {
host_triple.clone() // Empty target triple can happen on GHA
}
} else if let Some(target_triple) = config::get_value("target") {
target_triple
} else {
host_triple.clone()
};
// FIXME allow changing the location of these dirs using cli arguments // FIXME allow changing the location of these dirs using cli arguments
let current_dir = std::env::current_dir().unwrap(); let current_dir = std::env::current_dir().unwrap();
@ -167,7 +159,7 @@ pub fn main() {
} }
let cg_clif_dylib = let cg_clif_dylib =
build_backend::build_backend(&dirs, channel, &host_triple, use_unstable_features); build_backend::build_backend(&dirs, channel, &host_compiler, use_unstable_features);
match command { match command {
Command::Prepare => { Command::Prepare => {
// Handled above // Handled above
@ -178,18 +170,16 @@ pub fn main() {
channel, channel,
sysroot_kind, sysroot_kind,
&cg_clif_dylib, &cg_clif_dylib,
&host_triple, &host_compiler,
&target_triple, &target_triple,
); );
abi_cafe::run( if host_compiler.triple == target_triple {
channel, abi_cafe::run(channel, sysroot_kind, &dirs, &cg_clif_dylib, &host_compiler);
sysroot_kind, } else {
&dirs, eprintln!("[SKIP] abi-cafe (cross-compilation not supported)");
&cg_clif_dylib, return;
&host_triple, }
&target_triple,
);
} }
Command::Build => { Command::Build => {
build_sysroot::build_sysroot( build_sysroot::build_sysroot(
@ -197,7 +187,7 @@ pub fn main() {
channel, channel,
sysroot_kind, sysroot_kind,
&cg_clif_dylib, &cg_clif_dylib,
&host_triple, &host_compiler,
&target_triple, &target_triple,
); );
} }
@ -207,10 +197,10 @@ pub fn main() {
channel, channel,
sysroot_kind, sysroot_kind,
&cg_clif_dylib, &cg_clif_dylib,
&host_triple, &host_compiler,
&target_triple, &target_triple,
); );
bench::benchmark(&dirs); bench::benchmark(&dirs, &host_compiler);
} }
} }
} }

View File

@ -385,10 +385,11 @@ pub(crate) fn run_tests(
channel: &str, channel: &str,
sysroot_kind: SysrootKind, sysroot_kind: SysrootKind,
cg_clif_dylib: &Path, cg_clif_dylib: &Path,
host_triple: &str, host_compiler: &Compiler,
target_triple: &str, target_triple: &str,
) { ) {
let runner = TestRunner::new(dirs.clone(), host_triple.to_string(), target_triple.to_string()); let runner =
TestRunner::new(dirs.clone(), host_compiler.triple.clone(), target_triple.to_string());
if config::get_bool("testsuite.no_sysroot") { if config::get_bool("testsuite.no_sysroot") {
build_sysroot::build_sysroot( build_sysroot::build_sysroot(
@ -396,7 +397,7 @@ pub(crate) fn run_tests(
channel, channel,
SysrootKind::None, SysrootKind::None,
cg_clif_dylib, cg_clif_dylib,
&host_triple, host_compiler,
&target_triple, &target_triple,
); );
@ -415,7 +416,7 @@ pub(crate) fn run_tests(
channel, channel,
sysroot_kind, sysroot_kind,
cg_clif_dylib, cg_clif_dylib,
&host_triple, host_compiler,
&target_triple, &target_triple,
); );
} }
@ -445,7 +446,7 @@ impl TestRunner {
pub fn new(dirs: Dirs, host_triple: String, target_triple: String) -> Self { pub fn new(dirs: Dirs, host_triple: String, target_triple: String) -> Self {
let is_native = host_triple == target_triple; let is_native = host_triple == target_triple;
let jit_supported = let jit_supported =
target_triple.contains("x86_64") && is_native && !host_triple.contains("windows"); is_native && host_triple.contains("x86_64") && !host_triple.contains("windows");
let mut rustflags = env::var("RUSTFLAGS").ok().unwrap_or("".to_string()); let mut rustflags = env::var("RUSTFLAGS").ok().unwrap_or("".to_string());
let mut runner = vec![]; let mut runner = vec![];

View File

@ -5,10 +5,9 @@
use std::process::{self, Command, Stdio}; use std::process::{self, Command, Stdio};
use super::path::{Dirs, RelPath}; use super::path::{Dirs, RelPath};
use super::rustc_info::{ use super::rustc_info::{get_cargo_path, get_rustc_path, get_rustdoc_path, get_wrapper_file_name};
get_cargo_path, get_host_triple, get_rustc_path, get_rustdoc_path, get_wrapper_file_name,
};
#[derive(Clone, Debug)]
pub(crate) struct Compiler { pub(crate) struct Compiler {
pub(crate) cargo: PathBuf, pub(crate) cargo: PathBuf,
pub(crate) rustc: PathBuf, pub(crate) rustc: PathBuf,
@ -20,19 +19,7 @@ pub(crate) struct Compiler {
} }
impl Compiler { impl Compiler {
pub(crate) fn host() -> Compiler { pub(crate) fn llvm_with_triple(triple: String) -> Compiler {
Compiler {
cargo: get_cargo_path(),
rustc: get_rustc_path(),
rustdoc: get_rustdoc_path(),
rustflags: String::new(),
rustdocflags: String::new(),
triple: get_host_triple(),
runner: vec![],
}
}
pub(crate) fn with_triple(triple: String) -> Compiler {
Compiler { Compiler {
cargo: get_cargo_path(), cargo: get_cargo_path(),
rustc: get_rustc_path(), rustc: get_rustc_path(),