Skip to content

Commit

Permalink
Merge pull request rust-lang#1279 from bjorn3/build_system_rework
Browse files Browse the repository at this point in the history
Some refactorings for the build system
  • Loading branch information
bjorn3 authored Sep 12, 2022
2 parents 87bbc2d + a65c881 commit 107f9d8
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 123 deletions.
28 changes: 10 additions & 18 deletions build_system/abi_checker.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use super::build_sysroot;
use super::config;
use super::utils::spawn_and_wait;
use build_system::SysrootKind;
use std::env;
use std::path::Path;
use std::process::Command;

use super::build_sysroot;
use super::config;
use super::utils::{cargo_command, spawn_and_wait};
use super::SysrootKind;

pub(crate) fn run(
channel: &str,
sysroot_kind: SysrootKind,
target_dir: &Path,
cg_clif_build_dir: &Path,
cg_clif_dylib: &Path,
host_triple: &str,
target_triple: &str,
) {
Expand All @@ -29,32 +29,24 @@ pub(crate) fn run(
channel,
sysroot_kind,
target_dir,
cg_clif_build_dir,
cg_clif_dylib,
host_triple,
target_triple,
);

eprintln!("Running abi-checker");
let mut abi_checker_path = env::current_dir().unwrap();
abi_checker_path.push("abi-checker");
env::set_current_dir(abi_checker_path.clone()).unwrap();

let build_dir = abi_checker_path.parent().unwrap().join("build");
let cg_clif_dylib_path = build_dir.join(if cfg!(windows) { "bin" } else { "lib" }).join(
env::consts::DLL_PREFIX.to_string() + "rustc_codegen_cranelift" + env::consts::DLL_SUFFIX,
);
env::set_current_dir(&abi_checker_path.clone()).unwrap();

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

let mut cmd = Command::new("cargo");
cmd.arg("run");
cmd.arg("--target");
cmd.arg(target_triple);
let mut cmd = cargo_command("cargo", "run", Some(target_triple), &abi_checker_path);
cmd.arg("--");
cmd.arg("--pairs");
cmd.args(pairs);
cmd.arg("--add-rustc-codegen-backend");
cmd.arg(format!("cgclif:{}", cg_clif_dylib_path.display()));
cmd.arg(format!("cgclif:{}", cg_clif_dylib.display()));

spawn_and_wait(cmd);
}
16 changes: 10 additions & 6 deletions build_system/build_backend.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use std::env;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::path::PathBuf;

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

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

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

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

Path::new("target").join(host_triple).join(channel)
source_dir
.join("target")
.join(host_triple)
.join(channel)
.join(get_file_name("rustc_codegen_cranelift", "dylib"))
}
12 changes: 5 additions & 7 deletions build_system/build_sysroot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ 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::{spawn_and_wait, try_hard_link};
use super::utils::{cargo_command, spawn_and_wait, try_hard_link};
use super::SysrootKind;

pub(crate) fn build_sysroot(
channel: &str,
sysroot_kind: SysrootKind,
target_dir: &Path,
cg_clif_build_dir: &Path,
cg_clif_dylib_src: &Path,
host_triple: &str,
target_triple: &str,
) {
Expand All @@ -23,7 +23,6 @@ pub(crate) fn build_sysroot(
fs::create_dir_all(target_dir.join("lib")).unwrap();

// Copy the backend
let cg_clif_dylib = get_file_name("rustc_codegen_cranelift", "dylib");
let cg_clif_dylib_path = target_dir
.join(if cfg!(windows) {
// Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
Expand All @@ -32,8 +31,8 @@ pub(crate) fn build_sysroot(
} else {
"lib"
})
.join(&cg_clif_dylib);
try_hard_link(cg_clif_build_dir.join(cg_clif_dylib), &cg_clif_dylib_path);
.join(get_file_name("rustc_codegen_cranelift", "dylib"));
try_hard_link(cg_clif_dylib_src, &cg_clif_dylib_path);

// Build and copy rustc and cargo wrappers
for wrapper in ["rustc-clif", "cargo-clif"] {
Expand Down Expand Up @@ -186,8 +185,7 @@ fn build_clif_sysroot_for_triple(
}

// Build sysroot
let mut build_cmd = Command::new("cargo");
build_cmd.arg("build").arg("--target").arg(triple).current_dir("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()));
if channel == "release" {
Expand Down
3 changes: 2 additions & 1 deletion build_system/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{fs, process};
use std::fs;
use std::process;

fn load_config_file() -> Vec<(String, Option<String>)> {
fs::read_to_string("config.txt")
Expand Down
8 changes: 4 additions & 4 deletions build_system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,15 @@ pub fn main() {
process::exit(1);
}

let cg_clif_build_dir =
let cg_clif_dylib =
build_backend::build_backend(channel, &host_triple, use_unstable_features);
match command {
Command::Test => {
tests::run_tests(
channel,
sysroot_kind,
&target_dir,
&cg_clif_build_dir,
&cg_clif_dylib,
&host_triple,
&target_triple,
);
Expand All @@ -147,7 +147,7 @@ pub fn main() {
channel,
sysroot_kind,
&target_dir,
&cg_clif_build_dir,
&cg_clif_dylib,
&host_triple,
&target_triple,
);
Expand All @@ -157,7 +157,7 @@ pub fn main() {
channel,
sysroot_kind,
&target_dir,
&cg_clif_build_dir,
&cg_clif_dylib,
&host_triple,
&target_triple,
);
Expand Down
35 changes: 21 additions & 14 deletions build_system/prepare.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use std::env;
use std::ffi::OsStr;
use std::ffi::OsString;
use std::fs;
use std::path::Path;
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::{copy_dir_recursively, spawn_and_wait};
use super::utils::{cargo_command, copy_dir_recursively, spawn_and_wait};

pub(crate) fn prepare() {
prepare_sysroot();
Expand Down Expand Up @@ -53,8 +52,7 @@ pub(crate) fn prepare() {
);

eprintln!("[LLVM BUILD] simple-raytracer");
let mut build_cmd = Command::new("cargo");
build_cmd.arg("build").env_remove("CARGO_TARGET_DIR").current_dir("simple-raytracer");
let build_cmd = cargo_command("cargo", "build", None, Path::new("simple-raytracer"));
spawn_and_wait(build_cmd);
fs::copy(
Path::new("simple-raytracer/target/debug").join(get_file_name("main", "bin")),
Expand Down Expand Up @@ -156,26 +154,35 @@ fn init_git_repo(repo_dir: &Path) {
spawn_and_wait(git_commit_cmd);
}

fn get_patches(crate_name: &str) -> Vec<OsString> {
let mut patches: Vec<_> = fs::read_dir("patches")
fn get_patches(source_dir: &Path, crate_name: &str) -> Vec<PathBuf> {
let mut patches: Vec<_> = fs::read_dir(source_dir.join("patches"))
.unwrap()
.map(|entry| entry.unwrap().path())
.filter(|path| path.extension() == Some(OsStr::new("patch")))
.map(|path| path.file_name().unwrap().to_owned())
.filter(|file_name| {
file_name.to_str().unwrap().split_once("-").unwrap().1.starts_with(crate_name)
.filter(|path| {
path.file_name()
.unwrap()
.to_str()
.unwrap()
.split_once("-")
.unwrap()
.1
.starts_with(crate_name)
})
.collect();
patches.sort();
patches
}

fn apply_patches(crate_name: &str, target_dir: &Path) {
for patch in get_patches(crate_name) {
eprintln!("[PATCH] {:?} <- {:?}", target_dir.file_name().unwrap(), patch);
let patch_arg = env::current_dir().unwrap().join("patches").join(patch);
for patch in get_patches(&std::env::current_dir().unwrap(), crate_name) {
eprintln!(
"[PATCH] {:?} <- {:?}",
target_dir.file_name().unwrap(),
patch.file_name().unwrap()
);
let mut apply_patch_cmd = Command::new("git");
apply_patch_cmd.arg("am").arg(patch_arg).arg("-q").current_dir(target_dir);
apply_patch_cmd.arg("am").arg(patch).arg("-q").current_dir(target_dir);
spawn_and_wait(apply_patch_cmd);
}
}
Loading

0 comments on commit 107f9d8

Please sign in to comment.