Skip to content

Commit

Permalink
Merge pull request rust-lang#1340 from bjorn3/non_rustup_build
Browse files Browse the repository at this point in the history
Push up a lot of rustc and cargo references
  • Loading branch information
bjorn3 authored Jan 14, 2023
2 parents 395eaa1 + 629eab7 commit b31b74e
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 100 deletions.
10 changes: 5 additions & 5 deletions build_system/abi_cafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ pub(crate) fn run(
sysroot_kind: SysrootKind,
dirs: &Dirs,
cg_clif_dylib: &Path,
host_compiler: &Compiler,
bootstrap_host_compiler: &Compiler,
) {
if !config::get_bool("testsuite.abi-cafe") {
eprintln!("[SKIP] abi-cafe");
eprintln!("[RUN] abi-cafe (skipped)");
return;
}

Expand All @@ -31,15 +31,15 @@ pub(crate) fn run(
channel,
sysroot_kind,
cg_clif_dylib,
host_compiler,
&host_compiler.triple,
bootstrap_host_compiler,
bootstrap_host_compiler.triple.clone(),
);

eprintln!("Running abi-cafe");

let pairs = ["rustc_calls_cgclif", "cgclif_calls_rustc", "cgclif_calls_cc", "cc_calls_cgclif"];

let mut cmd = ABI_CAFE.run(host_compiler, dirs);
let mut cmd = ABI_CAFE.run(bootstrap_host_compiler, dirs);
cmd.arg("--");
cmd.arg("--pairs");
cmd.args(pairs);
Expand Down
10 changes: 5 additions & 5 deletions build_system/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,24 @@ pub(crate) static SIMPLE_RAYTRACER_LLVM: CargoProject =
pub(crate) static SIMPLE_RAYTRACER: CargoProject =
CargoProject::new(&SIMPLE_RAYTRACER_REPO.source_dir(), "simple_raytracer");

pub(crate) fn benchmark(dirs: &Dirs, host_compiler: &Compiler) {
benchmark_simple_raytracer(dirs, host_compiler);
pub(crate) fn benchmark(dirs: &Dirs, bootstrap_host_compiler: &Compiler) {
benchmark_simple_raytracer(dirs, bootstrap_host_compiler);
}

fn benchmark_simple_raytracer(dirs: &Dirs, host_compiler: &Compiler) {
fn benchmark_simple_raytracer(dirs: &Dirs, bootstrap_host_compiler: &Compiler) {
if std::process::Command::new("hyperfine").output().is_err() {
eprintln!("Hyperfine not installed");
eprintln!("Hint: Try `cargo install hyperfine` to install hyperfine");
std::process::exit(1);
}

eprintln!("[LLVM BUILD] simple-raytracer");
let build_cmd = SIMPLE_RAYTRACER_LLVM.build(host_compiler, dirs);
let build_cmd = SIMPLE_RAYTRACER_LLVM.build(bootstrap_host_compiler, dirs);
spawn_and_wait(build_cmd);
fs::copy(
SIMPLE_RAYTRACER_LLVM
.target_dir(dirs)
.join(&host_compiler.triple)
.join(&bootstrap_host_compiler.triple)
.join("debug")
.join(get_file_name("main", "bin")),
RelPath::BUILD.to_path(dirs).join(get_file_name("raytracer_cg_llvm", "bin")),
Expand Down
6 changes: 3 additions & 3 deletions build_system/build_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ pub(crate) static CG_CLIF: CargoProject = CargoProject::new(&RelPath::SOURCE, "c
pub(crate) fn build_backend(
dirs: &Dirs,
channel: &str,
host_compiler: &Compiler,
bootstrap_host_compiler: &Compiler,
use_unstable_features: bool,
) -> PathBuf {
let mut cmd = CG_CLIF.build(&host_compiler, dirs);
let mut cmd = CG_CLIF.build(&bootstrap_host_compiler, dirs);

cmd.env("CARGO_BUILD_INCREMENTAL", "true"); // Force incr comp even in release mode

Expand Down Expand Up @@ -48,7 +48,7 @@ pub(crate) fn build_backend(

CG_CLIF
.target_dir(dirs)
.join(&host_compiler.triple)
.join(&bootstrap_host_compiler.triple)
.join(channel)
.join(get_file_name("rustc_codegen_cranelift", "dylib"))
}
56 changes: 36 additions & 20 deletions build_system/build_sysroot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use std::path::Path;
use std::process::{self, Command};

use super::path::{Dirs, RelPath};
use super::rustc_info::{get_file_name, get_rustc_version, get_wrapper_file_name};
use super::rustc_info::{
get_file_name, get_rustc_version, get_toolchain_name, get_wrapper_file_name,
};
use super::utils::{spawn_and_wait, try_hard_link, CargoProject, Compiler};
use super::SysrootKind;

Expand All @@ -17,15 +19,17 @@ pub(crate) fn build_sysroot(
channel: &str,
sysroot_kind: SysrootKind,
cg_clif_dylib_src: &Path,
host_compiler: &Compiler,
target_triple: &str,
) {
bootstrap_host_compiler: &Compiler,
target_triple: String,
) -> Compiler {
eprintln!("[BUILD] sysroot {:?}", sysroot_kind);

DIST_DIR.ensure_fresh(dirs);
BIN_DIR.ensure_exists(dirs);
LIB_DIR.ensure_exists(dirs);

let is_native = bootstrap_host_compiler.triple == target_triple;

// Copy the backend
let cg_clif_dylib_path = if cfg!(windows) {
// Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
Expand All @@ -42,32 +46,34 @@ pub(crate) fn build_sysroot(
for wrapper in ["rustc-clif", "rustdoc-clif", "cargo-clif"] {
let wrapper_name = get_wrapper_file_name(wrapper, "bin");

let mut build_cargo_wrapper_cmd = Command::new("rustc");
let mut build_cargo_wrapper_cmd = Command::new(&bootstrap_host_compiler.rustc);
build_cargo_wrapper_cmd
.env("TOOLCHAIN_NAME", get_toolchain_name())
.arg(RelPath::SCRIPTS.to_path(dirs).join(&format!("{wrapper}.rs")))
.arg("-o")
.arg(DIST_DIR.to_path(dirs).join(wrapper_name))
.arg("-g");
spawn_and_wait(build_cargo_wrapper_cmd);
}

let default_sysroot = super::rustc_info::get_default_sysroot();
let default_sysroot = super::rustc_info::get_default_sysroot(&bootstrap_host_compiler.rustc);

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 host_rustlib_lib =
RUSTLIB_DIR.to_path(dirs).join(&bootstrap_host_compiler.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(&target_rustlib_lib).unwrap();

if target_triple == "x86_64-pc-windows-gnu" {
if !default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib").exists() {
if !default_sysroot.join("lib").join("rustlib").join(&target_triple).join("lib").exists() {
eprintln!(
"The x86_64-pc-windows-gnu target needs to be installed first before it is possible \
to compile a sysroot for it.",
);
process::exit(1);
}
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"),
)
.unwrap()
{
Expand All @@ -83,7 +89,11 @@ pub(crate) fn build_sysroot(
SysrootKind::None => {} // Nothing to do
SysrootKind::Llvm => {
for file in fs::read_dir(
default_sysroot.join("lib").join("rustlib").join(&host_compiler.triple).join("lib"),
default_sysroot
.join("lib")
.join("rustlib")
.join(&bootstrap_host_compiler.triple)
.join("lib"),
)
.unwrap()
{
Expand All @@ -103,9 +113,9 @@ pub(crate) fn build_sysroot(
try_hard_link(&file, host_rustlib_lib.join(file.file_name().unwrap()));
}

if target_triple != host_compiler.triple {
if !is_native {
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"),
)
.unwrap()
{
Expand All @@ -118,19 +128,19 @@ pub(crate) fn build_sysroot(
build_clif_sysroot_for_triple(
dirs,
channel,
host_compiler.clone(),
bootstrap_host_compiler.clone(),
&cg_clif_dylib_path,
);

if host_compiler.triple != target_triple {
if !is_native {
build_clif_sysroot_for_triple(
dirs,
channel,
{
let mut target_compiler = host_compiler.clone();
target_compiler.triple = target_triple.to_owned();
target_compiler.set_cross_linker_and_runner();
target_compiler
let mut bootstrap_target_compiler = bootstrap_host_compiler.clone();
bootstrap_target_compiler.triple = target_triple.clone();
bootstrap_target_compiler.set_cross_linker_and_runner();
bootstrap_target_compiler
},
&cg_clif_dylib_path,
);
Expand All @@ -147,6 +157,12 @@ pub(crate) fn build_sysroot(
}
}
}

let mut target_compiler = Compiler::clif_with_triple(&dirs, target_triple);
if !is_native {
target_compiler.set_cross_linker_and_runner();
}
target_compiler
}

pub(crate) static ORIG_BUILD_SYSROOT: RelPath = RelPath::SOURCE.join("build_sysroot");
Expand All @@ -169,7 +185,7 @@ fn build_clif_sysroot_for_triple(
process::exit(1);
}
Ok(source_version) => {
let rustc_version = get_rustc_version();
let rustc_version = get_rustc_version(&compiler.rustc);
if source_version != rustc_version {
eprintln!("The patched sysroot source is outdated");
eprintln!("Source version: {}", source_version.trim());
Expand Down
41 changes: 27 additions & 14 deletions build_system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub fn main() {
}
}

let host_compiler = Compiler::llvm_with_triple(
let bootstrap_host_compiler = Compiler::bootstrap_with_triple(
std::env::var("HOST_TRIPLE")
.ok()
.or_else(|| config::get_value("host"))
Expand All @@ -106,7 +106,7 @@ pub fn main() {
let target_triple = std::env::var("TARGET_TRIPLE")
.ok()
.or_else(|| config::get_value("target"))
.unwrap_or_else(|| host_compiler.triple.clone());
.unwrap_or_else(|| bootstrap_host_compiler.triple.clone());

// FIXME allow changing the location of these dirs using cli arguments
let current_dir = std::env::current_dir().unwrap();
Expand Down Expand Up @@ -134,8 +134,15 @@ pub fn main() {
process::exit(0);
}

let cg_clif_dylib =
build_backend::build_backend(&dirs, channel, &host_compiler, use_unstable_features);
env::set_var("RUSTC", "rustc_should_be_set_explicitly");
env::set_var("RUSTDOC", "rustdoc_should_be_set_explicitly");

let cg_clif_dylib = build_backend::build_backend(
&dirs,
channel,
&bootstrap_host_compiler,
use_unstable_features,
);
match command {
Command::Prepare => {
// Handled above
Expand All @@ -146,14 +153,20 @@ pub fn main() {
channel,
sysroot_kind,
&cg_clif_dylib,
&host_compiler,
&target_triple,
&bootstrap_host_compiler,
target_triple.clone(),
);

if host_compiler.triple == target_triple {
abi_cafe::run(channel, sysroot_kind, &dirs, &cg_clif_dylib, &host_compiler);
if bootstrap_host_compiler.triple == target_triple {
abi_cafe::run(
channel,
sysroot_kind,
&dirs,
&cg_clif_dylib,
&bootstrap_host_compiler,
);
} else {
eprintln!("[SKIP] abi-cafe (cross-compilation not supported)");
eprintln!("[RUN] abi-cafe (skipped, cross-compilation not supported)");
return;
}
}
Expand All @@ -163,8 +176,8 @@ pub fn main() {
channel,
sysroot_kind,
&cg_clif_dylib,
&host_compiler,
&target_triple,
&bootstrap_host_compiler,
target_triple,
);
}
Command::Bench => {
Expand All @@ -173,10 +186,10 @@ pub fn main() {
channel,
sysroot_kind,
&cg_clif_dylib,
&host_compiler,
&target_triple,
&bootstrap_host_compiler,
target_triple,
);
bench::benchmark(&dirs, &host_compiler);
bench::benchmark(&dirs, &bootstrap_host_compiler);
}
}
}
4 changes: 2 additions & 2 deletions build_system/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub(crate) fn prepare(dirs: &Dirs) {
}

fn prepare_sysroot(dirs: &Dirs) {
let sysroot_src_orig = get_default_sysroot().join("lib/rustlib/src/rust");
let sysroot_src_orig = get_default_sysroot(Path::new("rustc")).join("lib/rustlib/src/rust");
assert!(sysroot_src_orig.exists());

eprintln!("[COPY] sysroot src");
Expand All @@ -50,7 +50,7 @@ fn prepare_sysroot(dirs: &Dirs) {
&SYSROOT_SRC.to_path(dirs).join("library"),
);

let rustc_version = get_rustc_version();
let rustc_version = get_rustc_version(Path::new("rustc"));
fs::write(SYSROOT_RUSTC_VERSION.to_path(dirs), &rustc_version).unwrap();

eprintln!("[GIT] init");
Expand Down
18 changes: 14 additions & 4 deletions build_system/rustc_info.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

pub(crate) fn get_rustc_version() -> String {
pub(crate) fn get_rustc_version(rustc: &Path) -> String {
let version_info =
Command::new("rustc").stderr(Stdio::inherit()).args(&["-V"]).output().unwrap().stdout;
Command::new(rustc).stderr(Stdio::inherit()).args(&["-V"]).output().unwrap().stdout;
String::from_utf8(version_info).unwrap()
}

Expand All @@ -23,6 +23,16 @@ pub(crate) fn get_host_triple() -> String {
.to_owned()
}

pub(crate) fn get_toolchain_name() -> String {
let active_toolchain = Command::new("rustup")
.stderr(Stdio::inherit())
.args(&["show", "active-toolchain"])
.output()
.unwrap()
.stdout;
String::from_utf8(active_toolchain).unwrap().trim().split_once(' ').unwrap().0.to_owned()
}

pub(crate) fn get_cargo_path() -> PathBuf {
let cargo_path = Command::new("rustup")
.stderr(Stdio::inherit())
Expand Down Expand Up @@ -53,8 +63,8 @@ pub(crate) fn get_rustdoc_path() -> PathBuf {
Path::new(String::from_utf8(rustc_path).unwrap().trim()).to_owned()
}

pub(crate) fn get_default_sysroot() -> PathBuf {
let default_sysroot = Command::new("rustc")
pub(crate) fn get_default_sysroot(rustc: &Path) -> PathBuf {
let default_sysroot = Command::new(rustc)
.stderr(Stdio::inherit())
.args(&["--print", "sysroot"])
.output()
Expand Down
Loading

0 comments on commit b31b74e

Please sign in to comment.