Skip to content

Commit 107f9d8

Browse files
authored
Merge pull request rust-lang#1279 from bjorn3/build_system_rework
Some refactorings for the build system
2 parents 87bbc2d + a65c881 commit 107f9d8

File tree

8 files changed

+160
-123
lines changed

8 files changed

+160
-123
lines changed

build_system/abi_checker.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
use super::build_sysroot;
2-
use super::config;
3-
use super::utils::spawn_and_wait;
4-
use build_system::SysrootKind;
51
use std::env;
62
use std::path::Path;
7-
use std::process::Command;
3+
4+
use super::build_sysroot;
5+
use super::config;
6+
use super::utils::{cargo_command, spawn_and_wait};
7+
use super::SysrootKind;
88

99
pub(crate) fn run(
1010
channel: &str,
1111
sysroot_kind: SysrootKind,
1212
target_dir: &Path,
13-
cg_clif_build_dir: &Path,
13+
cg_clif_dylib: &Path,
1414
host_triple: &str,
1515
target_triple: &str,
1616
) {
@@ -29,32 +29,24 @@ pub(crate) fn run(
2929
channel,
3030
sysroot_kind,
3131
target_dir,
32-
cg_clif_build_dir,
32+
cg_clif_dylib,
3333
host_triple,
3434
target_triple,
3535
);
3636

3737
eprintln!("Running abi-checker");
3838
let mut abi_checker_path = env::current_dir().unwrap();
3939
abi_checker_path.push("abi-checker");
40-
env::set_current_dir(abi_checker_path.clone()).unwrap();
41-
42-
let build_dir = abi_checker_path.parent().unwrap().join("build");
43-
let cg_clif_dylib_path = build_dir.join(if cfg!(windows) { "bin" } else { "lib" }).join(
44-
env::consts::DLL_PREFIX.to_string() + "rustc_codegen_cranelift" + env::consts::DLL_SUFFIX,
45-
);
40+
env::set_current_dir(&abi_checker_path.clone()).unwrap();
4641

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

49-
let mut cmd = Command::new("cargo");
50-
cmd.arg("run");
51-
cmd.arg("--target");
52-
cmd.arg(target_triple);
44+
let mut cmd = cargo_command("cargo", "run", Some(target_triple), &abi_checker_path);
5345
cmd.arg("--");
5446
cmd.arg("--pairs");
5547
cmd.args(pairs);
5648
cmd.arg("--add-rustc-codegen-backend");
57-
cmd.arg(format!("cgclif:{}", cg_clif_dylib_path.display()));
49+
cmd.arg(format!("cgclif:{}", cg_clif_dylib.display()));
5850

5951
spawn_and_wait(cmd);
6052
}

build_system/build_backend.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
use std::env;
2-
use std::path::{Path, PathBuf};
3-
use std::process::Command;
2+
use std::path::PathBuf;
43

5-
use super::utils::is_ci;
4+
use super::rustc_info::get_file_name;
5+
use super::utils::{cargo_command, is_ci};
66

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

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

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

44-
Path::new("target").join(host_triple).join(channel)
44+
source_dir
45+
.join("target")
46+
.join(host_triple)
47+
.join(channel)
48+
.join(get_file_name("rustc_codegen_cranelift", "dylib"))
4549
}

build_system/build_sysroot.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ use std::path::{Path, PathBuf};
33
use std::process::{self, Command};
44

55
use super::rustc_info::{get_file_name, get_rustc_version, get_wrapper_file_name};
6-
use super::utils::{spawn_and_wait, try_hard_link};
6+
use super::utils::{cargo_command, spawn_and_wait, try_hard_link};
77
use super::SysrootKind;
88

99
pub(crate) fn build_sysroot(
1010
channel: &str,
1111
sysroot_kind: SysrootKind,
1212
target_dir: &Path,
13-
cg_clif_build_dir: &Path,
13+
cg_clif_dylib_src: &Path,
1414
host_triple: &str,
1515
target_triple: &str,
1616
) {
@@ -23,7 +23,6 @@ pub(crate) fn build_sysroot(
2323
fs::create_dir_all(target_dir.join("lib")).unwrap();
2424

2525
// Copy the backend
26-
let cg_clif_dylib = get_file_name("rustc_codegen_cranelift", "dylib");
2726
let cg_clif_dylib_path = target_dir
2827
.join(if cfg!(windows) {
2928
// Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
@@ -32,8 +31,8 @@ pub(crate) fn build_sysroot(
3231
} else {
3332
"lib"
3433
})
35-
.join(&cg_clif_dylib);
36-
try_hard_link(cg_clif_build_dir.join(cg_clif_dylib), &cg_clif_dylib_path);
34+
.join(get_file_name("rustc_codegen_cranelift", "dylib"));
35+
try_hard_link(cg_clif_dylib_src, &cg_clif_dylib_path);
3736

3837
// Build and copy rustc and cargo wrappers
3938
for wrapper in ["rustc-clif", "cargo-clif"] {
@@ -186,8 +185,7 @@ fn build_clif_sysroot_for_triple(
186185
}
187186

188187
// Build sysroot
189-
let mut build_cmd = Command::new("cargo");
190-
build_cmd.arg("build").arg("--target").arg(triple).current_dir("build_sysroot");
188+
let mut build_cmd = cargo_command("cargo", "build", Some(triple), Path::new("build_sysroot"));
191189
let mut rustflags = "-Zforce-unstable-if-unmarked -Cpanic=abort".to_string();
192190
rustflags.push_str(&format!(" -Zcodegen-backend={}", cg_clif_dylib_path.to_str().unwrap()));
193191
if channel == "release" {

build_system/config.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::{fs, process};
1+
use std::fs;
2+
use std::process;
23

34
fn load_config_file() -> Vec<(String, Option<String>)> {
45
fs::read_to_string("config.txt")

build_system/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,15 @@ pub fn main() {
130130
process::exit(1);
131131
}
132132

133-
let cg_clif_build_dir =
133+
let cg_clif_dylib =
134134
build_backend::build_backend(channel, &host_triple, use_unstable_features);
135135
match command {
136136
Command::Test => {
137137
tests::run_tests(
138138
channel,
139139
sysroot_kind,
140140
&target_dir,
141-
&cg_clif_build_dir,
141+
&cg_clif_dylib,
142142
&host_triple,
143143
&target_triple,
144144
);
@@ -147,7 +147,7 @@ pub fn main() {
147147
channel,
148148
sysroot_kind,
149149
&target_dir,
150-
&cg_clif_build_dir,
150+
&cg_clif_dylib,
151151
&host_triple,
152152
&target_triple,
153153
);
@@ -157,7 +157,7 @@ pub fn main() {
157157
channel,
158158
sysroot_kind,
159159
&target_dir,
160-
&cg_clif_build_dir,
160+
&cg_clif_dylib,
161161
&host_triple,
162162
&target_triple,
163163
);

build_system/prepare.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use std::env;
22
use std::ffi::OsStr;
3-
use std::ffi::OsString;
43
use std::fs;
5-
use std::path::Path;
4+
use std::path::{Path, PathBuf};
65
use std::process::Command;
76

87
use super::rustc_info::{get_file_name, get_rustc_path, get_rustc_version};
9-
use super::utils::{copy_dir_recursively, spawn_and_wait};
8+
use super::utils::{cargo_command, copy_dir_recursively, spawn_and_wait};
109

1110
pub(crate) fn prepare() {
1211
prepare_sysroot();
@@ -53,8 +52,7 @@ pub(crate) fn prepare() {
5352
);
5453

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

159-
fn get_patches(crate_name: &str) -> Vec<OsString> {
160-
let mut patches: Vec<_> = fs::read_dir("patches")
157+
fn get_patches(source_dir: &Path, crate_name: &str) -> Vec<PathBuf> {
158+
let mut patches: Vec<_> = fs::read_dir(source_dir.join("patches"))
161159
.unwrap()
162160
.map(|entry| entry.unwrap().path())
163161
.filter(|path| path.extension() == Some(OsStr::new("patch")))
164-
.map(|path| path.file_name().unwrap().to_owned())
165-
.filter(|file_name| {
166-
file_name.to_str().unwrap().split_once("-").unwrap().1.starts_with(crate_name)
162+
.filter(|path| {
163+
path.file_name()
164+
.unwrap()
165+
.to_str()
166+
.unwrap()
167+
.split_once("-")
168+
.unwrap()
169+
.1
170+
.starts_with(crate_name)
167171
})
168172
.collect();
169173
patches.sort();
170174
patches
171175
}
172176

173177
fn apply_patches(crate_name: &str, target_dir: &Path) {
174-
for patch in get_patches(crate_name) {
175-
eprintln!("[PATCH] {:?} <- {:?}", target_dir.file_name().unwrap(), patch);
176-
let patch_arg = env::current_dir().unwrap().join("patches").join(patch);
178+
for patch in get_patches(&std::env::current_dir().unwrap(), crate_name) {
179+
eprintln!(
180+
"[PATCH] {:?} <- {:?}",
181+
target_dir.file_name().unwrap(),
182+
patch.file_name().unwrap()
183+
);
177184
let mut apply_patch_cmd = Command::new("git");
178-
apply_patch_cmd.arg("am").arg(patch_arg).arg("-q").current_dir(target_dir);
185+
apply_patch_cmd.arg("am").arg(patch).arg("-q").current_dir(target_dir);
179186
spawn_and_wait(apply_patch_cmd);
180187
}
181188
}

0 commit comments

Comments
 (0)