Skip to content

Commit 20a829a

Browse files
authored
Merge pull request #1339 from bjorn3/better_compiler_management
Many build system improvements
2 parents cf22470 + bdcbf47 commit 20a829a

16 files changed

+367
-478
lines changed

.github/workflows/main.yml

+6-11
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,6 @@ jobs:
6262
steps:
6363
- uses: actions/checkout@v3
6464

65-
- name: Cache cargo registry and index
66-
uses: actions/cache@v3
67-
with:
68-
path: |
69-
~/.cargo/registry
70-
~/.cargo/git
71-
key: ${{ runner.os }}-${{ matrix.env.TARGET_TRIPLE }}-cargo-registry-and-index-${{ hashFiles('**/Cargo.lock') }}
72-
7365
- name: Cache cargo target dir
7466
uses: actions/cache@v3
7567
with:
@@ -99,9 +91,12 @@ jobs:
9991
sudo apt-get update
10092
sudo apt-get install -y gcc-s390x-linux-gnu qemu-user
10193
102-
- name: Windows setup
103-
if: matrix.os == 'windows-latest'
104-
run: git config --global core.autocrlf false
94+
- name: Use sparse cargo registry
95+
run: |
96+
cat >> ~/.cargo/config.toml <<EOF
97+
[unstable]
98+
sparse-registry = true
99+
EOF
105100
106101
- name: Prepare dependencies
107102
run: ./y.rs prepare

.github/workflows/nightly-cranelift.yml

+10-4
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ jobs:
1313
steps:
1414
- uses: actions/checkout@v3
1515

16-
- name: Prepare dependencies
16+
- name: Use sparse cargo registry
1717
run: |
18-
git config --global user.email "[email protected]"
19-
git config --global user.name "User"
20-
./y.rs prepare
18+
cat >> ~/.cargo/config.toml <<EOF
19+
[unstable]
20+
sparse-registry = true
21+
EOF
22+
23+
- name: Prepare dependencies
24+
run: ./y.rs prepare
2125

2226
- name: Patch Cranelift
2327
run: |
@@ -32,6 +36,8 @@ jobs:
3236
3337
cat Cargo.toml
3438
39+
cargo fetch
40+
3541
- name: Build without unstable features
3642
# This is the config rust-lang/rust uses for builds
3743
run: ./y.rs build --no-unstable-features

.github/workflows/rustc.yml

+16-24
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,21 @@ jobs:
1010
steps:
1111
- uses: actions/checkout@v3
1212

13-
- name: Cache cargo registry and index
14-
uses: actions/cache@v3
15-
with:
16-
path: |
17-
~/.cargo/registry
18-
~/.cargo/git
19-
key: ${{ runner.os }}-cargo-registry-and-index-${{ hashFiles('**/Cargo.lock') }}
20-
2113
- name: Cache cargo target dir
2214
uses: actions/cache@v3
2315
with:
2416
path: build/cg_clif
2517
key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('rust-toolchain', '**/Cargo.lock') }}
2618

27-
- name: Prepare dependencies
19+
- name: Use sparse cargo registry
2820
run: |
29-
git config --global user.email "[email protected]"
30-
git config --global user.name "User"
31-
./y.rs prepare
21+
cat >> ~/.cargo/config.toml <<EOF
22+
[unstable]
23+
sparse-registry = true
24+
EOF
25+
26+
- name: Prepare dependencies
27+
run: ./y.rs prepare
3228

3329
- name: Test
3430
run: ./scripts/test_bootstrap.sh
@@ -38,25 +34,21 @@ jobs:
3834
steps:
3935
- uses: actions/checkout@v3
4036

41-
- name: Cache cargo registry and index
42-
uses: actions/cache@v3
43-
with:
44-
path: |
45-
~/.cargo/registry
46-
~/.cargo/git
47-
key: ${{ runner.os }}-cargo-registry-and-index-${{ hashFiles('**/Cargo.lock') }}
48-
4937
- name: Cache cargo target dir
5038
uses: actions/cache@v3
5139
with:
5240
path: build/cg_clif
5341
key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('rust-toolchain', '**/Cargo.lock') }}
5442

55-
- name: Prepare dependencies
43+
- name: Use sparse cargo registry
5644
run: |
57-
git config --global user.email "[email protected]"
58-
git config --global user.name "User"
59-
./y.rs prepare
45+
cat >> ~/.cargo/config.toml <<EOF
46+
[unstable]
47+
sparse-registry = true
48+
EOF
49+
50+
- name: Prepare dependencies
51+
run: ./y.rs prepare
6052

6153
- name: Test
6254
run: ./scripts/test_rustc_tests.sh

.vscode/settings.json

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{
2+
"editor.formatOnSave": true,
3+
24
// source for rustc_* is not included in the rust-src component; disable the errors about this
35
"rust-analyzer.diagnostics.disabled": ["unresolved-extern-crate", "unresolved-macro-call"],
46
"rust-analyzer.imports.granularity.enforce": true,

Readme.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ If not please open an issue.
88
## Building and testing
99

1010
```bash
11-
$ git clone https://github.com/bjorn3/rustc_codegen_cranelift.git
11+
$ git clone https://github.com/bjorn3/rustc_codegen_cranelift
1212
$ cd rustc_codegen_cranelift
13-
$ ./y.rs prepare # download and patch sysroot src and install hyperfine for benchmarking
13+
$ ./y.rs prepare
1414
$ ./y.rs build
1515
```
1616

@@ -20,13 +20,12 @@ To run the test suite replace the last command with:
2020
$ ./test.sh
2121
```
2222

23-
This will implicitly build cg_clif too. Both `y.rs build` and `test.sh` accept a `--debug` argument to
24-
build in debug mode.
23+
For more docs on how to build and test see [build_system/usage.txt](build_system/usage.txt) or the help message of `./y.rs`.
2524

26-
Alternatively you can download a pre built version from [GHA]. It is listed in the artifacts section
25+
Alternatively you can download a pre built version from [Github Actions]. It is listed in the artifacts section
2726
of workflow runs. Unfortunately due to GHA restrictions you need to be logged in to access it.
2827

29-
[GHA]: https://github.com/bjorn3/rustc_codegen_cranelift/actions?query=branch%3Amaster+event%3Apush+is%3Asuccess
28+
[Github Actions]: https://github.com/bjorn3/rustc_codegen_cranelift/actions?query=branch%3Amaster+event%3Apush+is%3Asuccess
3029

3130
## Usage
3231

build_system/abi_cafe.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,36 @@ use super::SysrootKind;
1010
pub(crate) static ABI_CAFE_REPO: GitRepo =
1111
GitRepo::github("Gankra", "abi-cafe", "4c6dc8c9c687e2b3a760ff2176ce236872b37212", "abi-cafe");
1212

13-
static ABI_CAFE: CargoProject = CargoProject::new(&ABI_CAFE_REPO.source_dir(), "abi_cafe");
13+
pub(crate) static ABI_CAFE: CargoProject =
14+
CargoProject::new(&ABI_CAFE_REPO.source_dir(), "abi_cafe");
1415

1516
pub(crate) fn run(
1617
channel: &str,
1718
sysroot_kind: SysrootKind,
1819
dirs: &Dirs,
1920
cg_clif_dylib: &Path,
20-
host_triple: &str,
21-
target_triple: &str,
21+
host_compiler: &Compiler,
2222
) {
2323
if !config::get_bool("testsuite.abi-cafe") {
2424
eprintln!("[SKIP] abi-cafe");
2525
return;
2626
}
2727

28-
if host_triple != target_triple {
29-
eprintln!("[SKIP] abi-cafe (cross-compilation not supported)");
30-
return;
31-
}
32-
3328
eprintln!("Building sysroot for abi-cafe");
3429
build_sysroot::build_sysroot(
3530
dirs,
3631
channel,
3732
sysroot_kind,
3833
cg_clif_dylib,
39-
host_triple,
40-
target_triple,
34+
host_compiler,
35+
&host_compiler.triple,
4136
);
4237

4338
eprintln!("Running abi-cafe");
4439

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

47-
let mut cmd = ABI_CAFE.run(&Compiler::host(), dirs);
42+
let mut cmd = ABI_CAFE.run(host_compiler, dirs);
4843
cmd.arg("--");
4944
cmd.arg("--pairs");
5045
cmd.args(pairs);

build_system/bench.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,19 @@ pub(crate) static SIMPLE_RAYTRACER_LLVM: CargoProject =
2121
pub(crate) static SIMPLE_RAYTRACER: CargoProject =
2222
CargoProject::new(&SIMPLE_RAYTRACER_REPO.source_dir(), "simple_raytracer");
2323

24-
pub(crate) fn benchmark(dirs: &Dirs) {
25-
benchmark_simple_raytracer(dirs);
24+
pub(crate) fn benchmark(dirs: &Dirs, host_compiler: &Compiler) {
25+
benchmark_simple_raytracer(dirs, host_compiler);
2626
}
2727

28-
fn benchmark_simple_raytracer(dirs: &Dirs) {
28+
fn benchmark_simple_raytracer(dirs: &Dirs, host_compiler: &Compiler) {
2929
if std::process::Command::new("hyperfine").output().is_err() {
3030
eprintln!("Hyperfine not installed");
3131
eprintln!("Hint: Try `cargo install hyperfine` to install hyperfine");
3232
std::process::exit(1);
3333
}
3434

3535
eprintln!("[LLVM BUILD] simple-raytracer");
36-
let host_compiler = Compiler::host();
37-
let build_cmd = SIMPLE_RAYTRACER_LLVM.build(&host_compiler, dirs);
36+
let build_cmd = SIMPLE_RAYTRACER_LLVM.build(host_compiler, dirs);
3837
spawn_and_wait(build_cmd);
3938
fs::copy(
4039
SIMPLE_RAYTRACER_LLVM

build_system/build_backend.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ use super::path::{Dirs, RelPath};
55
use super::rustc_info::get_file_name;
66
use super::utils::{is_ci, CargoProject, Compiler};
77

8-
static CG_CLIF: CargoProject = CargoProject::new(&RelPath::SOURCE, "cg_clif");
8+
pub(crate) static CG_CLIF: CargoProject = CargoProject::new(&RelPath::SOURCE, "cg_clif");
99

1010
pub(crate) fn build_backend(
1111
dirs: &Dirs,
1212
channel: &str,
13-
host_triple: &str,
13+
host_compiler: &Compiler,
1414
use_unstable_features: bool,
1515
) -> PathBuf {
16-
let mut cmd = CG_CLIF.build(&Compiler::host(), dirs);
16+
let mut cmd = CG_CLIF.build(&host_compiler, dirs);
1717

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

@@ -48,7 +48,7 @@ pub(crate) fn build_backend(
4848

4949
CG_CLIF
5050
.target_dir(dirs)
51-
.join(host_triple)
51+
.join(&host_compiler.triple)
5252
.join(channel)
5353
.join(get_file_name("rustc_codegen_cranelift", "dylib"))
5454
}

build_system/build_sysroot.rs

+25-27
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub(crate) fn build_sysroot(
1717
channel: &str,
1818
sysroot_kind: SysrootKind,
1919
cg_clif_dylib_src: &Path,
20-
host_triple: &str,
20+
host_compiler: &Compiler,
2121
target_triple: &str,
2222
) {
2323
eprintln!("[BUILD] sysroot {:?}", sysroot_kind);
@@ -53,7 +53,7 @@ pub(crate) fn build_sysroot(
5353

5454
let default_sysroot = super::rustc_info::get_default_sysroot();
5555

56-
let host_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(host_triple).join("lib");
56+
let host_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(&host_compiler.triple).join("lib");
5757
let target_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(target_triple).join("lib");
5858
fs::create_dir_all(&host_rustlib_lib).unwrap();
5959
fs::create_dir_all(&target_rustlib_lib).unwrap();
@@ -83,7 +83,7 @@ pub(crate) fn build_sysroot(
8383
SysrootKind::None => {} // Nothing to do
8484
SysrootKind::Llvm => {
8585
for file in fs::read_dir(
86-
default_sysroot.join("lib").join("rustlib").join(host_triple).join("lib"),
86+
default_sysroot.join("lib").join("rustlib").join(&host_compiler.triple).join("lib"),
8787
)
8888
.unwrap()
8989
{
@@ -103,7 +103,7 @@ pub(crate) fn build_sysroot(
103103
try_hard_link(&file, host_rustlib_lib.join(file.file_name().unwrap()));
104104
}
105105

106-
if target_triple != host_triple {
106+
if target_triple != host_compiler.triple {
107107
for file in fs::read_dir(
108108
default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"),
109109
)
@@ -115,21 +115,24 @@ pub(crate) fn build_sysroot(
115115
}
116116
}
117117
SysrootKind::Clif => {
118-
build_clif_sysroot_for_triple(dirs, channel, host_triple, &cg_clif_dylib_path, None);
119-
120-
if host_triple != target_triple {
121-
// When cross-compiling it is often necessary to manually pick the right linker
122-
let linker = match target_triple {
123-
"aarch64-unknown-linux-gnu" => Some("aarch64-linux-gnu-gcc"),
124-
"s390x-unknown-linux-gnu" => Some("s390x-linux-gnu-gcc"),
125-
_ => None,
126-
};
118+
build_clif_sysroot_for_triple(
119+
dirs,
120+
channel,
121+
host_compiler.clone(),
122+
&cg_clif_dylib_path,
123+
);
124+
125+
if host_compiler.triple != target_triple {
127126
build_clif_sysroot_for_triple(
128127
dirs,
129128
channel,
130-
target_triple,
129+
{
130+
let mut target_compiler = host_compiler.clone();
131+
target_compiler.triple = target_triple.to_owned();
132+
target_compiler.set_cross_linker_and_runner();
133+
target_compiler
134+
},
131135
&cg_clif_dylib_path,
132-
linker,
133136
);
134137
}
135138

@@ -150,14 +153,14 @@ pub(crate) static ORIG_BUILD_SYSROOT: RelPath = RelPath::SOURCE.join("build_sysr
150153
pub(crate) static BUILD_SYSROOT: RelPath = RelPath::DOWNLOAD.join("sysroot");
151154
pub(crate) static SYSROOT_RUSTC_VERSION: RelPath = BUILD_SYSROOT.join("rustc_version");
152155
pub(crate) static SYSROOT_SRC: RelPath = BUILD_SYSROOT.join("sysroot_src");
153-
static STANDARD_LIBRARY: CargoProject = CargoProject::new(&BUILD_SYSROOT, "build_sysroot");
156+
pub(crate) static STANDARD_LIBRARY: CargoProject =
157+
CargoProject::new(&BUILD_SYSROOT, "build_sysroot");
154158

155159
fn build_clif_sysroot_for_triple(
156160
dirs: &Dirs,
157161
channel: &str,
158-
triple: &str,
162+
mut compiler: Compiler,
159163
cg_clif_dylib_path: &Path,
160-
linker: Option<&str>,
161164
) {
162165
match fs::read_to_string(SYSROOT_RUSTC_VERSION.to_path(dirs)) {
163166
Err(e) => {
@@ -177,7 +180,7 @@ fn build_clif_sysroot_for_triple(
177180
}
178181
}
179182

180-
let build_dir = STANDARD_LIBRARY.target_dir(dirs).join(triple).join(channel);
183+
let build_dir = STANDARD_LIBRARY.target_dir(dirs).join(&compiler.triple).join(channel);
181184

182185
if !super::config::get_bool("keep_sysroot") {
183186
// Cleanup the deps dir, but keep build scripts and the incremental cache for faster
@@ -188,18 +191,13 @@ fn build_clif_sysroot_for_triple(
188191
}
189192

190193
// Build sysroot
191-
let mut rustflags = "-Zforce-unstable-if-unmarked -Cpanic=abort".to_string();
194+
let mut rustflags = " -Zforce-unstable-if-unmarked -Cpanic=abort".to_string();
192195
rustflags.push_str(&format!(" -Zcodegen-backend={}", cg_clif_dylib_path.to_str().unwrap()));
193196
rustflags.push_str(&format!(" --sysroot={}", DIST_DIR.to_path(dirs).to_str().unwrap()));
194197
if channel == "release" {
195198
rustflags.push_str(" -Zmir-opt-level=3");
196199
}
197-
if let Some(linker) = linker {
198-
use std::fmt::Write;
199-
write!(rustflags, " -Clinker={}", linker).unwrap();
200-
}
201-
let mut compiler = Compiler::with_triple(triple.to_owned());
202-
compiler.rustflags = rustflags;
200+
compiler.rustflags += &rustflags;
203201
let mut build_cmd = STANDARD_LIBRARY.build(&compiler, dirs);
204202
if channel == "release" {
205203
build_cmd.arg("--release");
@@ -219,7 +217,7 @@ fn build_clif_sysroot_for_triple(
219217
};
220218
try_hard_link(
221219
entry.path(),
222-
RUSTLIB_DIR.to_path(dirs).join(triple).join("lib").join(entry.file_name()),
220+
RUSTLIB_DIR.to_path(dirs).join(&compiler.triple).join("lib").join(entry.file_name()),
223221
);
224222
}
225223
}

0 commit comments

Comments
 (0)