Allow testing a cranelift backend built into rustc itself

This avoids building cranelift twice in rust's CI and is a lot easier
than trying to make building of codegen backends work from within a
cargo invocation done by rust's build system.
This commit is contained in:
bjorn3 2023-02-15 19:32:58 +00:00
parent c87dfd9c9d
commit 5b3bc29008
8 changed files with 101 additions and 48 deletions

View File

@ -1,10 +1,8 @@
use std::path::Path;
use super::build_sysroot; use super::build_sysroot;
use super::path::Dirs; use super::path::Dirs;
use super::prepare::GitRepo; use super::prepare::GitRepo;
use super::utils::{spawn_and_wait, CargoProject, Compiler}; use super::utils::{spawn_and_wait, CargoProject, Compiler};
use super::SysrootKind; use super::{CodegenBackend, SysrootKind};
static ABI_CAFE_REPO: GitRepo = static ABI_CAFE_REPO: GitRepo =
GitRepo::github("Gankra", "abi-cafe", "4c6dc8c9c687e2b3a760ff2176ce236872b37212", "abi-cafe"); GitRepo::github("Gankra", "abi-cafe", "4c6dc8c9c687e2b3a760ff2176ce236872b37212", "abi-cafe");
@ -15,7 +13,7 @@ pub(crate) fn run(
channel: &str, channel: &str,
sysroot_kind: SysrootKind, sysroot_kind: SysrootKind,
dirs: &Dirs, dirs: &Dirs,
cg_clif_dylib: &Path, cg_clif_dylib: &CodegenBackend,
rustup_toolchain_name: Option<&str>, rustup_toolchain_name: Option<&str>,
bootstrap_host_compiler: &Compiler, bootstrap_host_compiler: &Compiler,
) { ) {
@ -41,7 +39,14 @@ pub(crate) fn run(
cmd.arg("--pairs"); cmd.arg("--pairs");
cmd.args(pairs); cmd.args(pairs);
cmd.arg("--add-rustc-codegen-backend"); cmd.arg("--add-rustc-codegen-backend");
cmd.arg(format!("cgclif:{}", cg_clif_dylib.display())); match cg_clif_dylib {
CodegenBackend::Local(path) => {
cmd.arg(format!("cgclif:{}", path.display()));
}
CodegenBackend::Builtin(name) => {
cmd.arg(format!("cgclif:{name}"));
}
}
cmd.current_dir(ABI_CAFE.source_dir(dirs)); cmd.current_dir(ABI_CAFE.source_dir(dirs));
spawn_and_wait(cmd); spawn_and_wait(cmd);

View File

@ -5,7 +5,7 @@
use super::path::{Dirs, RelPath}; use super::path::{Dirs, RelPath};
use super::rustc_info::{get_file_name, get_rustc_version}; use super::rustc_info::{get_file_name, get_rustc_version};
use super::utils::{remove_dir_if_exists, spawn_and_wait, try_hard_link, CargoProject, Compiler}; use super::utils::{remove_dir_if_exists, spawn_and_wait, try_hard_link, CargoProject, Compiler};
use super::SysrootKind; use super::{CodegenBackend, SysrootKind};
static DIST_DIR: RelPath = RelPath::DIST; static DIST_DIR: RelPath = RelPath::DIST;
static BIN_DIR: RelPath = RelPath::DIST.join("bin"); static BIN_DIR: RelPath = RelPath::DIST.join("bin");
@ -15,7 +15,7 @@ pub(crate) fn build_sysroot(
dirs: &Dirs, dirs: &Dirs,
channel: &str, channel: &str,
sysroot_kind: SysrootKind, sysroot_kind: SysrootKind,
cg_clif_dylib_src: &Path, cg_clif_dylib_src: &CodegenBackend,
bootstrap_host_compiler: &Compiler, bootstrap_host_compiler: &Compiler,
rustup_toolchain_name: Option<&str>, rustup_toolchain_name: Option<&str>,
target_triple: String, target_triple: String,
@ -28,17 +28,23 @@ pub(crate) fn build_sysroot(
let is_native = bootstrap_host_compiler.triple == target_triple; let is_native = bootstrap_host_compiler.triple == target_triple;
// Copy the backend let cg_clif_dylib_path = match cg_clif_dylib_src {
let cg_clif_dylib_path = if cfg!(windows) { CodegenBackend::Local(src_path) => {
// Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the // Copy the backend
// binaries. let cg_clif_dylib_path = if cfg!(windows) {
BIN_DIR // Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
} else { // binaries.
LIB_DIR BIN_DIR
} } else {
.to_path(dirs) LIB_DIR
.join(cg_clif_dylib_src.file_name().unwrap()); }
try_hard_link(cg_clif_dylib_src, &cg_clif_dylib_path); .to_path(dirs)
.join(src_path.file_name().unwrap());
try_hard_link(src_path, &cg_clif_dylib_path);
CodegenBackend::Local(cg_clif_dylib_path)
}
CodegenBackend::Builtin(name) => CodegenBackend::Builtin(name.clone()),
};
// Build and copy rustc and cargo wrappers // Build and copy rustc and cargo wrappers
let wrapper_base_name = get_file_name(&bootstrap_host_compiler.rustc, "____", "bin"); let wrapper_base_name = get_file_name(&bootstrap_host_compiler.rustc, "____", "bin");
@ -65,6 +71,9 @@ pub(crate) fn build_sysroot(
.env("RUSTC", &bootstrap_host_compiler.rustc) .env("RUSTC", &bootstrap_host_compiler.rustc)
.env("RUSTDOC", &bootstrap_host_compiler.rustdoc); .env("RUSTDOC", &bootstrap_host_compiler.rustdoc);
} }
if let CodegenBackend::Builtin(name) = cg_clif_dylib_src {
build_cargo_wrapper_cmd.env("BUILTIN_BACKEND", name);
}
spawn_and_wait(build_cargo_wrapper_cmd); spawn_and_wait(build_cargo_wrapper_cmd);
try_hard_link(wrapper_path, BIN_DIR.to_path(dirs).join(wrapper_name)); try_hard_link(wrapper_path, BIN_DIR.to_path(dirs).join(wrapper_name));
} }
@ -159,7 +168,7 @@ fn build_sysroot_for_triple(
dirs: &Dirs, dirs: &Dirs,
channel: &str, channel: &str,
compiler: Compiler, compiler: Compiler,
cg_clif_dylib_path: &Path, cg_clif_dylib_path: &CodegenBackend,
sysroot_kind: SysrootKind, sysroot_kind: SysrootKind,
) -> SysrootTarget { ) -> SysrootTarget {
match sysroot_kind { match sysroot_kind {
@ -167,7 +176,7 @@ fn build_sysroot_for_triple(
.unwrap_or(SysrootTarget { triple: compiler.triple, libs: vec![] }), .unwrap_or(SysrootTarget { triple: compiler.triple, libs: vec![] }),
SysrootKind::Llvm => build_llvm_sysroot_for_triple(compiler), SysrootKind::Llvm => build_llvm_sysroot_for_triple(compiler),
SysrootKind::Clif => { SysrootKind::Clif => {
build_clif_sysroot_for_triple(dirs, channel, compiler, &cg_clif_dylib_path) build_clif_sysroot_for_triple(dirs, channel, compiler, cg_clif_dylib_path)
} }
} }
} }
@ -211,7 +220,7 @@ fn build_clif_sysroot_for_triple(
dirs: &Dirs, dirs: &Dirs,
channel: &str, channel: &str,
mut compiler: Compiler, mut compiler: Compiler,
cg_clif_dylib_path: &Path, cg_clif_dylib_path: &CodegenBackend,
) -> SysrootTarget { ) -> SysrootTarget {
match fs::read_to_string(SYSROOT_RUSTC_VERSION.to_path(dirs)) { match fs::read_to_string(SYSROOT_RUSTC_VERSION.to_path(dirs)) {
Err(e) => { Err(e) => {
@ -249,7 +258,14 @@ 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())); match cg_clif_dylib_path {
CodegenBackend::Local(path) => {
rustflags.push_str(&format!(" -Zcodegen-backend={}", path.to_str().unwrap()));
}
CodegenBackend::Builtin(name) => {
rustflags.push_str(&format!(" -Zcodegen-backend={name}"));
}
};
// Necessary for MinGW to find rsbegin.o and rsend.o // Necessary for MinGW to find rsbegin.o and rsend.o
rustflags rustflags
.push_str(&format!(" --sysroot {}", RTSTARTUP_SYSROOT.to_path(dirs).to_str().unwrap())); .push_str(&format!(" --sysroot {}", RTSTARTUP_SYSROOT.to_path(dirs).to_str().unwrap()));

View File

@ -43,6 +43,12 @@ pub(crate) enum SysrootKind {
Llvm, Llvm,
} }
#[derive(Clone, Debug)]
pub(crate) enum CodegenBackend {
Local(PathBuf),
Builtin(String),
}
pub(crate) fn main() { pub(crate) fn main() {
if env::var("RUST_BACKTRACE").is_err() { if env::var("RUST_BACKTRACE").is_err() {
env::set_var("RUST_BACKTRACE", "1"); env::set_var("RUST_BACKTRACE", "1");
@ -79,6 +85,7 @@ pub(crate) fn main() {
let mut sysroot_kind = SysrootKind::Clif; let mut sysroot_kind = SysrootKind::Clif;
let mut use_unstable_features = true; let mut use_unstable_features = true;
let mut frozen = false; let mut frozen = false;
let mut use_backend = None;
while let Some(arg) = args.next().as_deref() { while let Some(arg) = args.next().as_deref() {
match arg { match arg {
"--out-dir" => { "--out-dir" => {
@ -98,6 +105,12 @@ pub(crate) fn main() {
} }
"--no-unstable-features" => use_unstable_features = false, "--no-unstable-features" => use_unstable_features = false,
"--frozen" => frozen = true, "--frozen" => frozen = true,
"--use-backend" => {
use_backend = Some(match args.next() {
Some(name) => name,
None => arg_error!("--use-backend requires argument"),
});
}
flag if flag.starts_with("-") => arg_error!("Unknown flag {}", flag), flag if flag.starts_with("-") => arg_error!("Unknown flag {}", flag),
arg => arg_error!("Unexpected argument {}", arg), arg => arg_error!("Unexpected argument {}", arg),
} }
@ -164,12 +177,16 @@ pub(crate) fn main() {
env::set_var("RUSTC", "rustc_should_be_set_explicitly"); env::set_var("RUSTC", "rustc_should_be_set_explicitly");
env::set_var("RUSTDOC", "rustdoc_should_be_set_explicitly"); env::set_var("RUSTDOC", "rustdoc_should_be_set_explicitly");
let cg_clif_dylib = build_backend::build_backend( let cg_clif_dylib = if let Some(name) = use_backend {
&dirs, CodegenBackend::Builtin(name)
channel, } else {
&bootstrap_host_compiler, CodegenBackend::Local(build_backend::build_backend(
use_unstable_features, &dirs,
); channel,
&bootstrap_host_compiler,
use_unstable_features,
))
};
match command { match command {
Command::Prepare => { Command::Prepare => {
// Handled above // Handled above

View File

@ -3,11 +3,10 @@
use super::path::{Dirs, RelPath}; use super::path::{Dirs, RelPath};
use super::prepare::GitRepo; use super::prepare::GitRepo;
use super::utils::{spawn_and_wait, spawn_and_wait_with_input, CargoProject, Compiler}; use super::utils::{spawn_and_wait, spawn_and_wait_with_input, CargoProject, Compiler};
use super::SysrootKind; use super::{CodegenBackend, SysrootKind};
use std::env; use std::env;
use std::ffi::OsStr; use std::ffi::OsStr;
use std::fs; use std::fs;
use std::path::Path;
use std::process::Command; use std::process::Command;
static BUILD_EXAMPLE_OUT_DIR: RelPath = RelPath::BUILD.join("example"); static BUILD_EXAMPLE_OUT_DIR: RelPath = RelPath::BUILD.join("example");
@ -215,7 +214,7 @@ pub(crate) fn run_tests(
dirs: &Dirs, dirs: &Dirs,
channel: &str, channel: &str,
sysroot_kind: SysrootKind, sysroot_kind: SysrootKind,
cg_clif_dylib: &Path, cg_clif_dylib: &CodegenBackend,
bootstrap_host_compiler: &Compiler, bootstrap_host_compiler: &Compiler,
rustup_toolchain_name: Option<&str>, rustup_toolchain_name: Option<&str>,
target_triple: String, target_triple: String,

View File

@ -29,6 +29,10 @@ OPTIONS:
--frozen --frozen
Require Cargo.lock and cache are up to date Require Cargo.lock and cache are up to date
--use-backend NAME
Use the existing Cranelift (or other) backend of the rustc with which we built.
Warning: This is meant for use in rust's CI only!
REQUIREMENTS: REQUIREMENTS:
* Rustup: By default rustup is used to install the right nightly version. If you don't want to * Rustup: By default rustup is used to install the right nightly version. If you don't want to
use rustup, you can manually install the nightly version indicated by rust-toolchain.toml and use rustup, you can manually install the nightly version indicated by rust-toolchain.toml and

View File

@ -12,17 +12,21 @@ fn main() {
let mut rustflags = String::new(); let mut rustflags = String::new();
rustflags.push_str(" -Cpanic=abort -Zpanic-abort-tests -Zcodegen-backend="); rustflags.push_str(" -Cpanic=abort -Zpanic-abort-tests -Zcodegen-backend=");
rustflags.push_str( if let Some(name) = option_env!("BUILTIN_BACKEND") {
sysroot rustflags.push_str(name);
.join(if cfg!(windows) { "bin" } else { "lib" }) } else {
.join( rustflags.push_str(
env::consts::DLL_PREFIX.to_string() sysroot
+ "rustc_codegen_cranelift" .join(if cfg!(windows) { "bin" } else { "lib" })
+ env::consts::DLL_SUFFIX, .join(
) env::consts::DLL_PREFIX.to_string()
.to_str() + "rustc_codegen_cranelift"
.unwrap(), + env::consts::DLL_SUFFIX,
); )
.to_str()
.unwrap(),
);
}
rustflags.push_str(" --sysroot "); rustflags.push_str(" --sysroot ");
rustflags.push_str(sysroot.to_str().unwrap()); rustflags.push_str(sysroot.to_str().unwrap());
env::set_var("RUSTFLAGS", env::var("RUSTFLAGS").unwrap_or(String::new()) + &rustflags); env::set_var("RUSTFLAGS", env::var("RUSTFLAGS").unwrap_or(String::new()) + &rustflags);

View File

@ -19,9 +19,13 @@ fn main() {
let mut args = vec![]; let mut args = vec![];
args.push(OsString::from("-Cpanic=abort")); args.push(OsString::from("-Cpanic=abort"));
args.push(OsString::from("-Zpanic-abort-tests")); args.push(OsString::from("-Zpanic-abort-tests"));
let mut codegen_backend_arg = OsString::from("-Zcodegen-backend="); if let Some(name) = option_env!("BUILTIN_BACKEND") {
codegen_backend_arg.push(cg_clif_dylib_path); args.push(OsString::from(format!("-Zcodegen-backend={name}")))
args.push(codegen_backend_arg); } else {
let mut codegen_backend_arg = OsString::from("-Zcodegen-backend=");
codegen_backend_arg.push(cg_clif_dylib_path);
args.push(codegen_backend_arg);
}
if !passed_args.iter().any(|arg| { if !passed_args.iter().any(|arg| {
arg == "--sysroot" || arg.to_str().map(|s| s.starts_with("--sysroot=")) == Some(true) arg == "--sysroot" || arg.to_str().map(|s| s.starts_with("--sysroot=")) == Some(true)
}) { }) {

View File

@ -19,9 +19,13 @@ fn main() {
let mut args = vec![]; let mut args = vec![];
args.push(OsString::from("-Cpanic=abort")); args.push(OsString::from("-Cpanic=abort"));
args.push(OsString::from("-Zpanic-abort-tests")); args.push(OsString::from("-Zpanic-abort-tests"));
let mut codegen_backend_arg = OsString::from("-Zcodegen-backend="); if let Some(name) = option_env!("BUILTIN_BACKEND") {
codegen_backend_arg.push(cg_clif_dylib_path); args.push(OsString::from(format!("-Zcodegen-backend={name}")))
args.push(codegen_backend_arg); } else {
let mut codegen_backend_arg = OsString::from("-Zcodegen-backend=");
codegen_backend_arg.push(cg_clif_dylib_path);
args.push(codegen_backend_arg);
}
if !passed_args.iter().any(|arg| { if !passed_args.iter().any(|arg| {
arg == "--sysroot" || arg.to_str().map(|s| s.starts_with("--sysroot=")) == Some(true) arg == "--sysroot" || arg.to_str().map(|s| s.starts_with("--sysroot=")) == Some(true)
}) { }) {