Skip to content

Commit

Permalink
Merge pull request rust-lang#1298 from bjorn3/build_system_rework3
Browse files Browse the repository at this point in the history
Introduce CargoProject type and use it where possible
  • Loading branch information
bjorn3 authored Nov 27, 2022
2 parents f99140e + cdae9ba commit a00c7a0
Show file tree
Hide file tree
Showing 7 changed files with 456 additions and 299 deletions.
15 changes: 9 additions & 6 deletions build_system/abi_cafe.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use std::env;
use std::path::Path;

use super::build_sysroot;
use super::config;
use super::prepare;
use super::utils::{cargo_command, spawn_and_wait};
use super::prepare::GitRepo;
use super::utils::{spawn_and_wait, CargoProject, Compiler};
use super::SysrootKind;

pub(crate) static ABI_CAFE_REPO: GitRepo =
GitRepo::github("Gankra", "abi-cafe", "4c6dc8c9c687e2b3a760ff2176ce236872b37212", "abi-cafe");

static ABI_CAFE: CargoProject = CargoProject::git(&ABI_CAFE_REPO, ".");

pub(crate) fn run(
channel: &str,
sysroot_kind: SysrootKind,
Expand Down Expand Up @@ -36,17 +40,16 @@ pub(crate) fn run(
);

eprintln!("Running abi-cafe");
let abi_cafe_path = prepare::ABI_CAFE.source_dir();
env::set_current_dir(abi_cafe_path.clone()).unwrap();

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

let mut cmd = cargo_command("cargo", "run", Some(target_triple), &abi_cafe_path);
let mut cmd = ABI_CAFE.run(&Compiler::host());
cmd.arg("--");
cmd.arg("--pairs");
cmd.args(pairs);
cmd.arg("--add-rustc-codegen-backend");
cmd.arg(format!("cgclif:{}", cg_clif_dylib.display()));
cmd.current_dir(ABI_CAFE.source_dir());

spawn_and_wait(cmd);
}
10 changes: 6 additions & 4 deletions build_system/build_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ use std::env;
use std::path::PathBuf;

use super::rustc_info::get_file_name;
use super::utils::{cargo_command, is_ci};
use super::utils::{is_ci, CargoProject, Compiler};

static CG_CLIF: CargoProject = CargoProject::local(".");

pub(crate) fn build_backend(
channel: &str,
host_triple: &str,
use_unstable_features: bool,
) -> PathBuf {
let source_dir = std::env::current_dir().unwrap();
let mut cmd = cargo_command("cargo", "build", Some(host_triple), &source_dir);
let mut cmd = CG_CLIF.build(&Compiler::host());

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

Expand Down Expand Up @@ -41,7 +42,8 @@ pub(crate) fn build_backend(
eprintln!("[BUILD] rustc_codegen_cranelift");
super::utils::spawn_and_wait(cmd);

source_dir
CG_CLIF
.source_dir()
.join("target")
.join(host_triple)
.join(channel)
Expand Down
13 changes: 9 additions & 4 deletions build_system/build_sysroot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::{Path, PathBuf};
use std::process::{self, Command};

use super::rustc_info::{get_file_name, get_rustc_version, get_wrapper_file_name};
use super::utils::{cargo_command, spawn_and_wait, try_hard_link};
use super::utils::{spawn_and_wait, try_hard_link, CargoProject, Compiler};
use super::SysrootKind;

pub(crate) fn build_sysroot(
Expand Down Expand Up @@ -149,6 +149,8 @@ pub(crate) fn build_sysroot(
}
}

static STANDARD_LIBRARY: CargoProject = CargoProject::local("build_sysroot");

fn build_clif_sysroot_for_triple(
channel: &str,
target_dir: &Path,
Expand Down Expand Up @@ -185,19 +187,22 @@ fn build_clif_sysroot_for_triple(
}

// Build sysroot
let mut build_cmd = cargo_command("cargo", "build", Some(triple), Path::new("build_sysroot"));
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!(" --sysroot={}", target_dir.to_str().unwrap()));
if channel == "release" {
build_cmd.arg("--release");
rustflags.push_str(" -Zmir-opt-level=3");
}
if let Some(linker) = linker {
use std::fmt::Write;
write!(rustflags, " -Clinker={}", linker).unwrap();
}
build_cmd.env("RUSTFLAGS", rustflags);
let mut compiler = Compiler::with_triple(triple.to_owned());
compiler.rustflags = rustflags;
let mut build_cmd = STANDARD_LIBRARY.build(&compiler);
if channel == "release" {
build_cmd.arg("--release");
}
build_cmd.env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif");
spawn_and_wait(build_cmd);

Expand Down
50 changes: 15 additions & 35 deletions build_system/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,7 @@ use std::path::{Path, PathBuf};
use std::process::Command;

use super::rustc_info::{get_file_name, get_rustc_path, get_rustc_version};
use super::utils::{cargo_command, copy_dir_recursively, spawn_and_wait};

pub(crate) const ABI_CAFE: GitRepo =
GitRepo::github("Gankra", "abi-cafe", "4c6dc8c9c687e2b3a760ff2176ce236872b37212", "abi-cafe");

pub(crate) const RAND: GitRepo =
GitRepo::github("rust-random", "rand", "0f933f9c7176e53b2a3c7952ded484e1783f0bf1", "rand");

pub(crate) const REGEX: GitRepo =
GitRepo::github("rust-lang", "regex", "341f207c1071f7290e3f228c710817c280c8dca1", "regex");

pub(crate) const PORTABLE_SIMD: GitRepo = GitRepo::github(
"rust-lang",
"portable-simd",
"d5cd4a8112d958bd3a252327e0d069a6363249bd",
"portable-simd",
);

pub(crate) const SIMPLE_RAYTRACER: GitRepo = GitRepo::github(
"ebobby",
"simple-raytracer",
"804a7a21b9e673a482797aa289a18ed480e4d813",
"<none>",
);
use super::utils::{copy_dir_recursively, spawn_and_wait, Compiler};

pub(crate) fn prepare() {
if Path::new("download").exists() {
Expand All @@ -42,22 +19,25 @@ pub(crate) fn prepare() {
eprintln!("[INSTALL] hyperfine");
Command::new("cargo").arg("install").arg("hyperfine").spawn().unwrap().wait().unwrap();

ABI_CAFE.fetch();
RAND.fetch();
REGEX.fetch();
PORTABLE_SIMD.fetch();
SIMPLE_RAYTRACER.fetch();
super::abi_cafe::ABI_CAFE_REPO.fetch();
super::tests::RAND_REPO.fetch();
super::tests::REGEX_REPO.fetch();
super::tests::PORTABLE_SIMD_REPO.fetch();
super::tests::SIMPLE_RAYTRACER_REPO.fetch();

eprintln!("[LLVM BUILD] simple-raytracer");
let build_cmd = cargo_command("cargo", "build", None, &SIMPLE_RAYTRACER.source_dir());
let host_compiler = Compiler::host();
let build_cmd = super::tests::SIMPLE_RAYTRACER.build(&host_compiler);
spawn_and_wait(build_cmd);
fs::copy(
SIMPLE_RAYTRACER
.source_dir()
.join("target")
super::tests::SIMPLE_RAYTRACER
.target_dir()
.join(&host_compiler.triple)
.join("debug")
.join(get_file_name("main", "bin")),
SIMPLE_RAYTRACER.source_dir().join(get_file_name("raytracer_cg_llvm", "bin")),
super::tests::SIMPLE_RAYTRACER_REPO
.source_dir()
.join(get_file_name("raytracer_cg_llvm", "bin")),
)
.unwrap();
}
Expand Down Expand Up @@ -100,7 +80,7 @@ enum GitRepoUrl {
}

impl GitRepo {
const fn github(
pub(crate) const fn github(
user: &'static str,
repo: &'static str,
rev: &'static str,
Expand Down
20 changes: 20 additions & 0 deletions build_system/rustc_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ pub(crate) fn get_host_triple() -> String {
.to_owned()
}

pub(crate) fn get_cargo_path() -> PathBuf {
let cargo_path = Command::new("rustup")
.stderr(Stdio::inherit())
.args(&["which", "cargo"])
.output()
.unwrap()
.stdout;
Path::new(String::from_utf8(cargo_path).unwrap().trim()).to_owned()
}

pub(crate) fn get_rustc_path() -> PathBuf {
let rustc_path = Command::new("rustup")
.stderr(Stdio::inherit())
Expand All @@ -33,6 +43,16 @@ pub(crate) fn get_rustc_path() -> PathBuf {
Path::new(String::from_utf8(rustc_path).unwrap().trim()).to_owned()
}

pub(crate) fn get_rustdoc_path() -> PathBuf {
let rustc_path = Command::new("rustup")
.stderr(Stdio::inherit())
.args(&["which", "rustdoc"])
.output()
.unwrap()
.stdout;
Path::new(String::from_utf8(rustc_path).unwrap().trim()).to_owned()
}

pub(crate) fn get_default_sysroot() -> PathBuf {
let default_sysroot = Command::new("rustc")
.stderr(Stdio::inherit())
Expand Down
Loading

0 comments on commit a00c7a0

Please sign in to comment.