Skip to content

Commit 9152ded

Browse files
authored
Merge pull request rust-lang#1225 from bjorn3/build_system_rework
Use -Zcodegen-backend instead of a custom rustc driver
2 parents 944a48d + 88d058f commit 9152ded

File tree

13 files changed

+118
-301
lines changed

13 files changed

+118
-301
lines changed

build_system/build_backend.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,6 @@ pub(crate) fn build_backend(
3434
_ => unreachable!(),
3535
}
3636

37-
// Set the rpath to make the cg_clif executable find librustc_codegen_cranelift without changing
38-
// LD_LIBRARY_PATH
39-
if cfg!(unix) {
40-
if cfg!(target_os = "macos") {
41-
rustflags += " -Csplit-debuginfo=unpacked \
42-
-Clink-arg=-Wl,-rpath,@loader_path/../lib \
43-
-Zosx-rpath-install-name";
44-
} else {
45-
rustflags += " -Clink-arg=-Wl,-rpath=$ORIGIN/../lib ";
46-
}
47-
}
48-
4937
cmd.env("RUSTFLAGS", rustflags);
5038

5139
eprintln!("[BUILD] rustc_codegen_cranelift");

build_system/build_sysroot.rs

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::env;
21
use std::fs;
32
use std::path::{Path, PathBuf};
43
use std::process::{self, Command};
@@ -22,35 +21,28 @@ pub(crate) fn build_sysroot(
2221
fs::create_dir_all(target_dir.join("lib")).unwrap();
2322

2423
// Copy the backend
25-
for file in ["cg_clif", "cg_clif_build_sysroot"] {
26-
try_hard_link(
27-
cg_clif_build_dir.join(get_file_name(file, "bin")),
28-
target_dir.join("bin").join(get_file_name(file, "bin")),
29-
);
30-
}
31-
3224
let cg_clif_dylib = get_file_name("rustc_codegen_cranelift", "dylib");
33-
try_hard_link(
34-
cg_clif_build_dir.join(&cg_clif_dylib),
35-
target_dir
36-
.join(if cfg!(windows) {
37-
// Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
38-
// binaries.
39-
"bin"
40-
} else {
41-
"lib"
42-
})
43-
.join(cg_clif_dylib),
44-
);
45-
46-
// Build and copy cargo wrapper
47-
let mut build_cargo_wrapper_cmd = Command::new("rustc");
48-
build_cargo_wrapper_cmd
49-
.arg("scripts/cargo-clif.rs")
50-
.arg("-o")
51-
.arg(target_dir.join("cargo-clif"))
52-
.arg("-g");
53-
spawn_and_wait(build_cargo_wrapper_cmd);
25+
let cg_clif_dylib_path = target_dir
26+
.join(if cfg!(windows) {
27+
// Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
28+
// binaries.
29+
"bin"
30+
} else {
31+
"lib"
32+
})
33+
.join(&cg_clif_dylib);
34+
try_hard_link(cg_clif_build_dir.join(cg_clif_dylib), &cg_clif_dylib_path);
35+
36+
// Build and copy rustc and cargo wrappers
37+
for wrapper in ["rustc-clif", "cargo-clif"] {
38+
let mut build_cargo_wrapper_cmd = Command::new("rustc");
39+
build_cargo_wrapper_cmd
40+
.arg(PathBuf::from("scripts").join(format!("{wrapper}.rs")))
41+
.arg("-o")
42+
.arg(target_dir.join(wrapper))
43+
.arg("-g");
44+
spawn_and_wait(build_cargo_wrapper_cmd);
45+
}
5446

5547
let default_sysroot = super::rustc_info::get_default_sysroot();
5648

@@ -117,7 +109,13 @@ pub(crate) fn build_sysroot(
117109
}
118110
}
119111
SysrootKind::Clif => {
120-
build_clif_sysroot_for_triple(channel, target_dir, host_triple, None);
112+
build_clif_sysroot_for_triple(
113+
channel,
114+
target_dir,
115+
host_triple,
116+
&cg_clif_dylib_path,
117+
None,
118+
);
121119

122120
if host_triple != target_triple {
123121
// When cross-compiling it is often necessary to manually pick the right linker
@@ -126,14 +124,21 @@ pub(crate) fn build_sysroot(
126124
} else {
127125
None
128126
};
129-
build_clif_sysroot_for_triple(channel, target_dir, target_triple, linker);
127+
build_clif_sysroot_for_triple(
128+
channel,
129+
target_dir,
130+
target_triple,
131+
&cg_clif_dylib_path,
132+
linker,
133+
);
130134
}
131135

132136
// Copy std for the host to the lib dir. This is necessary for the jit mode to find
133137
// libstd.
134138
for file in fs::read_dir(host_rustlib_lib).unwrap() {
135139
let file = file.unwrap().path();
136-
if file.file_name().unwrap().to_str().unwrap().contains("std-") {
140+
let filename = file.file_name().unwrap().to_str().unwrap();
141+
if filename.contains("std-") && !filename.contains(".rlib") {
137142
try_hard_link(&file, target_dir.join("lib").join(file.file_name().unwrap()));
138143
}
139144
}
@@ -145,6 +150,7 @@ fn build_clif_sysroot_for_triple(
145150
channel: &str,
146151
target_dir: &Path,
147152
triple: &str,
153+
cg_clif_dylib_path: &Path,
148154
linker: Option<&str>,
149155
) {
150156
match fs::read_to_string(Path::new("build_sysroot").join("rustc_version")) {
@@ -178,7 +184,8 @@ fn build_clif_sysroot_for_triple(
178184
// Build sysroot
179185
let mut build_cmd = Command::new("cargo");
180186
build_cmd.arg("build").arg("--target").arg(triple).current_dir("build_sysroot");
181-
let mut rustflags = "--clif -Zforce-unstable-if-unmarked".to_string();
187+
let mut rustflags = "-Zforce-unstable-if-unmarked -Cpanic=abort".to_string();
188+
rustflags.push_str(&format!(" -Zcodegen-backend={}", cg_clif_dylib_path.to_str().unwrap()));
182189
if channel == "release" {
183190
build_cmd.arg("--release");
184191
rustflags.push_str(" -Zmir-opt-level=3");
@@ -188,10 +195,6 @@ fn build_clif_sysroot_for_triple(
188195
write!(rustflags, " -Clinker={}", linker).unwrap();
189196
}
190197
build_cmd.env("RUSTFLAGS", rustflags);
191-
build_cmd.env(
192-
"RUSTC",
193-
env::current_dir().unwrap().join(target_dir).join("bin").join("cg_clif_build_sysroot"),
194-
);
195198
build_cmd.env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif");
196199
spawn_and_wait(build_cmd);
197200

build_system/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ pub fn main() {
8686
arg => arg_error!("Unexpected argument {}", arg),
8787
}
8888
}
89+
target_dir = std::env::current_dir().unwrap().join(target_dir);
8990

9091
let host_triple = if let Ok(host_triple) = std::env::var("HOST_TRIPLE") {
9192
host_triple

docs/usage.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ This will build your project with rustc_codegen_cranelift instead of the usual L
1919
> You should prefer using the Cargo method.
2020
2121
```bash
22-
$ $cg_clif_dir/build/bin/cg_clif my_crate.rs
22+
$ $cg_clif_dir/build/rustc-clif my_crate.rs
2323
```
2424

2525
## Jit mode
@@ -38,7 +38,7 @@ $ $cg_clif_dir/build/cargo-clif jit
3838
or
3939

4040
```bash
41-
$ $cg_clif_dir/build/bin/cg_clif -Zunstable-features -Cllvm-args=mode=jit -Cprefer-dynamic my_crate.rs
41+
$ $cg_clif_dir/build/rustc-clif -Zunstable-features -Cllvm-args=mode=jit -Cprefer-dynamic my_crate.rs
4242
```
4343

4444
There is also an experimental lazy jit mode. In this mode functions are only compiled once they are
@@ -54,7 +54,7 @@ These are a few functions that allow you to easily run rust code from the shell
5454

5555
```bash
5656
function jit_naked() {
57-
echo "$@" | $cg_clif_dir/build/bin/cg_clif - -Zunstable-features -Cllvm-args=mode=jit -Cprefer-dynamic
57+
echo "$@" | $cg_clif_dir/build/rustc-clif - -Zunstable-features -Cllvm-args=mode=jit -Cprefer-dynamic
5858
}
5959

6060
function jit() {

scripts/cargo-clif.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,11 @@ use std::path::PathBuf;
55
use std::process::Command;
66

77
fn main() {
8-
if env::var("RUSTC_WRAPPER").map_or(false, |wrapper| wrapper.contains("sccache")) {
9-
eprintln!(
10-
"\x1b[1;93m=== Warning: Unsetting RUSTC_WRAPPER to prevent interference with sccache ===\x1b[0m"
11-
);
12-
env::remove_var("RUSTC_WRAPPER");
13-
}
14-
158
let sysroot = PathBuf::from(env::current_exe().unwrap().parent().unwrap());
169

17-
env::set_var("RUSTC", sysroot.join("bin/cg_clif".to_string() + env::consts::EXE_SUFFIX));
18-
19-
let mut rustdoc_flags = env::var("RUSTDOCFLAGS").unwrap_or(String::new());
20-
rustdoc_flags.push_str(" -Cpanic=abort -Zpanic-abort-tests -Zcodegen-backend=");
21-
rustdoc_flags.push_str(
10+
let mut rustflags = String::new();
11+
rustflags.push_str(" -Cpanic=abort -Zpanic-abort-tests -Zcodegen-backend=");
12+
rustflags.push_str(
2213
sysroot
2314
.join(if cfg!(windows) { "bin" } else { "lib" })
2415
.join(
@@ -29,9 +20,10 @@ fn main() {
2920
.to_str()
3021
.unwrap(),
3122
);
32-
rustdoc_flags.push_str(" --sysroot ");
33-
rustdoc_flags.push_str(sysroot.to_str().unwrap());
34-
env::set_var("RUSTDOCFLAGS", rustdoc_flags);
23+
rustflags.push_str(" --sysroot ");
24+
rustflags.push_str(sysroot.to_str().unwrap());
25+
env::set_var("RUSTFLAGS", env::var("RUSTFLAGS").unwrap_or(String::new()) + &rustflags);
26+
env::set_var("RUSTDOCFLAGS", env::var("RUSTDOCFLAGS").unwrap_or(String::new()) + &rustflags);
3527

3628
// Ensure that the right toolchain is used
3729
env::set_var("RUSTUP_TOOLCHAIN", env!("RUSTUP_TOOLCHAIN"));

scripts/config.sh

Lines changed: 0 additions & 6 deletions
This file was deleted.

scripts/ext_config.sh

Lines changed: 0 additions & 32 deletions
This file was deleted.

scripts/filter_profile.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
#![forbid(unsafe_code)]/* This line is ignored by bash
33
# This block is ignored by rustc
44
pushd $(dirname "$0")/../
5-
source scripts/config.sh
6-
RUSTC="$(pwd)/build/bin/cg_clif"
5+
RUSTC="$(pwd)/build/rustc-clif"
76
popd
87
PROFILE=$1 OUTPUT=$2 exec $RUSTC -Zunstable-options -Cllvm-args=mode=jit -Cprefer-dynamic $0
98
#*/

scripts/rustc-clif.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use std::env;
2+
use std::ffi::OsString;
3+
#[cfg(unix)]
4+
use std::os::unix::process::CommandExt;
5+
use std::path::PathBuf;
6+
use std::process::Command;
7+
8+
fn main() {
9+
let sysroot = PathBuf::from(env::current_exe().unwrap().parent().unwrap());
10+
11+
let cg_clif_dylib_path = sysroot.join(if cfg!(windows) { "bin" } else { "lib" }).join(
12+
env::consts::DLL_PREFIX.to_string() + "rustc_codegen_cranelift" + env::consts::DLL_SUFFIX,
13+
);
14+
15+
let mut args = std::env::args_os().skip(1).collect::<Vec<_>>();
16+
args.push(OsString::from("-Cpanic=abort"));
17+
args.push(OsString::from("-Zpanic-abort-tests"));
18+
let mut codegen_backend_arg = OsString::from("-Zcodegen-backend=");
19+
codegen_backend_arg.push(cg_clif_dylib_path);
20+
args.push(codegen_backend_arg);
21+
if !args.contains(&OsString::from("--sysroot")) {
22+
args.push(OsString::from("--sysroot"));
23+
args.push(OsString::from(sysroot.to_str().unwrap()));
24+
}
25+
26+
// Ensure that the right toolchain is used
27+
env::set_var("RUSTUP_TOOLCHAIN", env!("RUSTUP_TOOLCHAIN"));
28+
29+
#[cfg(unix)]
30+
Command::new("rustc").args(args).exec();
31+
32+
#[cfg(not(unix))]
33+
std::process::exit(
34+
Command::new("rustc").args(args).spawn().unwrap().wait().unwrap().code().unwrap_or(1),
35+
);
36+
}

scripts/setup_rust_fork.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
set -e
33

44
./y.rs build --no-unstable-features
5-
source scripts/config.sh
65

76
echo "[SETUP] Rust fork"
87
git clone https://github.com/rust-lang/rust.git || true
@@ -52,7 +51,7 @@ changelog-seen = 2
5251
ninja = false
5352
5453
[build]
55-
rustc = "$(pwd)/../build/bin/cg_clif"
54+
rustc = "$(pwd)/../build/rustc-clif"
5655
cargo = "$(rustup which cargo)"
5756
full-bootstrap = true
5857
local-rebuild = true

0 commit comments

Comments
 (0)