Skip to content

Commit 80e9188

Browse files
committed
Rewrite cargo.sh in rust
1 parent 9fd8fa2 commit 80e9188

File tree

10 files changed

+121
-63
lines changed

10 files changed

+121
-63
lines changed

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Assuming `$cg_clif_dir` is the directory you cloned this repo into and you follo
3737
In the directory with your project (where you can do the usual `cargo build`), run:
3838

3939
```bash
40-
$ $cg_clif_dir/build/cargo.sh build
40+
$ $cg_clif_dir/build/cargo build
4141
```
4242

4343
This will build your project with rustc_codegen_cranelift instead of the usual LLVM backend.

build_system/build_sysroot.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,14 @@ pub(crate) fn build_sysroot(
4343
);
4444
}
4545

46-
// Copy supporting files
47-
try_hard_link("rust-toolchain", target_dir.join("rust-toolchain"));
48-
try_hard_link("scripts/config.sh", target_dir.join("config.sh"));
49-
try_hard_link("scripts/cargo.sh", target_dir.join("cargo.sh"));
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.rs")
50+
.arg("-o")
51+
.arg(target_dir.join("cargo"))
52+
.arg("-g");
53+
spawn_and_wait(build_cargo_wrapper_cmd);
5054

5155
let default_sysroot = crate::rustc_info::get_default_sysroot();
5256

docs/usage.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Assuming `$cg_clif_dir` is the directory you cloned this repo into and you follo
99
In the directory with your project (where you can do the usual `cargo build`), run:
1010

1111
```bash
12-
$ $cg_clif_dir/build/cargo.sh build
12+
$ $cg_clif_dir/build/cargo build
1313
```
1414

1515
This will build your project with rustc_codegen_cranelift instead of the usual LLVM backend.
@@ -30,7 +30,7 @@ In jit mode cg_clif will immediately execute your code without creating an execu
3030
> The jit mode will probably need cargo integration to make this possible.
3131
3232
```bash
33-
$ $cg_clif_dir/build/cargo.sh jit
33+
$ $cg_clif_dir/build/cargo jit
3434
```
3535

3636
or
@@ -44,7 +44,7 @@ first called. It currently does not work with multi-threaded programs. When a no
4444
function is called from another thread than the main thread, you will get an ICE.
4545

4646
```bash
47-
$ $cg_clif_dir/build/cargo.sh lazy-jit
47+
$ $cg_clif_dir/build/cargo lazy-jit
4848
```
4949

5050
## Shell

scripts/cargo.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
use std::env;
2+
use std::os::unix::process::CommandExt;
3+
use std::path::PathBuf;
4+
use std::process::{Command, Stdio};
5+
6+
fn main() {
7+
if env::var("RUSTC_WRAPPER").map_or(false, |wrapper| wrapper.contains("sccache")) {
8+
eprintln!(
9+
"\x1b[1;93m=== Warning: Unsetting RUSTC_WRAPPER to prevent interference with sccache ===\x1b[0m"
10+
);
11+
env::remove_var("RUSTC_WRAPPER");
12+
}
13+
14+
let sysroot = PathBuf::from(env::current_exe().unwrap().parent().unwrap());
15+
16+
env::set_var("RUSTC", sysroot.join("bin/cg_clif".to_string() + env::consts::EXE_SUFFIX));
17+
18+
let mut rustdoc_flags = env::var("RUSTDOCFLAGS").unwrap_or(String::new());
19+
rustdoc_flags.push_str(" -Cpanic=abort -Zpanic-abort-tests -Zcodegen-backend=");
20+
rustdoc_flags.push_str(
21+
sysroot
22+
.join(if cfg!(windows) { "bin" } else { "lib" })
23+
.join(
24+
env::consts::DLL_PREFIX.to_string()
25+
+ "rustc_codegen_cranelift"
26+
+ env::consts::DLL_SUFFIX,
27+
)
28+
.to_str()
29+
.unwrap(),
30+
);
31+
rustdoc_flags.push_str(" --sysroot ");
32+
rustdoc_flags.push_str(sysroot.to_str().unwrap());
33+
env::set_var("RUSTDOCFLAGS", rustdoc_flags);
34+
35+
let default_sysroot = Command::new("rustc")
36+
.stderr(Stdio::inherit())
37+
.args(&["--print", "sysroot"])
38+
.output()
39+
.unwrap()
40+
.stdout;
41+
let default_sysroot = std::str::from_utf8(&default_sysroot).unwrap().trim();
42+
43+
let extra_ld_lib_path =
44+
default_sysroot.to_string() + ":" + sysroot.join("lib").to_str().unwrap();
45+
if cfg!(target_os = "macos") {
46+
env::set_var(
47+
"DYLD_LIBRARY_PATH",
48+
env::var("DYLD_LIBRARY_PATH").unwrap_or(String::new()) + ":" + &extra_ld_lib_path,
49+
);
50+
} else if cfg!(unix) {
51+
env::set_var(
52+
"LD_LIBRARY_PATH",
53+
env::var("LD_LIBRARY_PATH").unwrap_or(String::new()) + ":" + &extra_ld_lib_path,
54+
);
55+
}
56+
57+
// Ensure that the right toolchain is used
58+
env::set_var("RUSTUP_TOOLCHAIN", env!("RUSTUP_TOOLCHAIN"));
59+
60+
let args: Vec<_> = match env::args().nth(1).as_deref() {
61+
Some("jit") => {
62+
env::set_var(
63+
"RUSTFLAGS",
64+
env::var("RUSTFLAGS").unwrap_or(String::new()) + " -Cprefer-dynamic",
65+
);
66+
std::array::IntoIter::new(["rustc".to_string()])
67+
.chain(env::args().skip(2))
68+
.chain(["--".to_string(), "-Cllvm-args=mode=jit".to_string()])
69+
.collect()
70+
}
71+
Some("lazy-jit") => {
72+
env::set_var(
73+
"RUSTFLAGS",
74+
env::var("RUSTFLAGS").unwrap_or(String::new()) + " -Cprefer-dynamic",
75+
);
76+
std::array::IntoIter::new(["rustc".to_string()])
77+
.chain(env::args().skip(2))
78+
.chain(["--".to_string(), "-Cllvm-args=mode=jit-lazy".to_string()])
79+
.collect()
80+
}
81+
_ => env::args().skip(1).collect(),
82+
};
83+
84+
Command::new("cargo").args(args).exec();
85+
}

scripts/cargo.sh

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

scripts/config.sh

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,6 @@
22

33
set -e
44

5-
dylib=$(echo "" | rustc --print file-names --crate-type dylib --crate-name rustc_codegen_cranelift -)
6-
7-
if echo "$RUSTC_WRAPPER" | grep sccache; then
8-
echo
9-
echo -e "\x1b[1;93m=== Warning: Unset RUSTC_WRAPPER to prevent interference with sccache ===\x1b[0m"
10-
echo
11-
export RUSTC_WRAPPER=
12-
fi
13-
14-
dir=$(cd "$(dirname "${BASH_SOURCE[0]}")"; pwd)
15-
16-
export RUSTC=$dir"/bin/cg_clif"
17-
18-
export RUSTDOCFLAGS=$linker' -Cpanic=abort -Zpanic-abort-tests '\
19-
'-Zcodegen-backend='$dir'/lib/'$dylib' --sysroot '$dir
20-
5+
dir=$(cd "$(dirname "${BASH_SOURCE[0]}")/../build"; pwd)
216
export LD_LIBRARY_PATH="$(rustc --print sysroot)/lib:"$dir"/lib"
227
export DYLD_LIBRARY_PATH=$LD_LIBRARY_PATH

scripts/ext_config.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Note to people running shellcheck: this file should only be sourced, not executed directly.
22

3-
# Various env vars that should only be set for the build system but not for cargo.sh
3+
# Various env vars that should only be set for the build system
44

55
set -e
66

scripts/filter_profile.rs

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

1011
//! This program filters away uninteresting samples and trims uninteresting frames for stackcollapse

scripts/setup_rust_fork.sh

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

44
./y.rs build
5-
source build/config.sh
5+
source scripts/config.sh
66

77
echo "[SETUP] Rust fork"
88
git clone https://github.com/rust-lang/rust.git || true

scripts/tests.sh

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
set -e
44

5-
source build/config.sh
5+
source scripts/config.sh
66
source scripts/ext_config.sh
7-
MY_RUSTC="$RUSTC $RUSTFLAGS -L crate=target/out --out-dir target/out -Cdebuginfo=2"
7+
export RUSTC=false # ensure that cg_llvm isn't accidentally used
8+
MY_RUSTC="$(pwd)/build/bin/cg_clif $RUSTFLAGS -L crate=target/out --out-dir target/out -Cdebuginfo=2"
89

910
function no_sysroot_tests() {
1011
echo "[BUILD] mini_core"
@@ -75,64 +76,64 @@ function base_sysroot_tests() {
7576

7677
function extended_sysroot_tests() {
7778
pushd rand
78-
cargo clean
79+
../build/cargo clean
7980
if [[ "$HOST_TRIPLE" = "$TARGET_TRIPLE" ]]; then
8081
echo "[TEST] rust-random/rand"
81-
../build/cargo.sh test --workspace
82+
../build/cargo test --workspace
8283
else
8384
echo "[AOT] rust-random/rand"
84-
../build/cargo.sh build --workspace --target $TARGET_TRIPLE --tests
85+
../build/cargo build --workspace --target $TARGET_TRIPLE --tests
8586
fi
8687
popd
8788

8889
pushd simple-raytracer
8990
if [[ "$HOST_TRIPLE" = "$TARGET_TRIPLE" ]]; then
9091
echo "[BENCH COMPILE] ebobby/simple-raytracer"
91-
hyperfine --runs "${RUN_RUNS:-10}" --warmup 1 --prepare "cargo clean" \
92+
hyperfine --runs "${RUN_RUNS:-10}" --warmup 1 --prepare "../build/cargo clean" \
9293
"RUSTC=rustc RUSTFLAGS='' cargo build" \
93-
"../build/cargo.sh build"
94+
"../build/cargo build"
9495

9596
echo "[BENCH RUN] ebobby/simple-raytracer"
9697
cp ./target/debug/main ./raytracer_cg_clif
9798
hyperfine --runs "${RUN_RUNS:-10}" ./raytracer_cg_llvm ./raytracer_cg_clif
9899
else
99-
cargo clean
100+
../build/cargo clean
100101
echo "[BENCH COMPILE] ebobby/simple-raytracer (skipped)"
101102
echo "[COMPILE] ebobby/simple-raytracer"
102-
../build/cargo.sh build --target $TARGET_TRIPLE
103+
../build/cargo build --target $TARGET_TRIPLE
103104
echo "[BENCH RUN] ebobby/simple-raytracer (skipped)"
104105
fi
105106
popd
106107

107108
pushd build_sysroot/sysroot_src/library/core/tests
108109
echo "[TEST] libcore"
109-
cargo clean
110+
../../../../../build/cargo clean
110111
if [[ "$HOST_TRIPLE" = "$TARGET_TRIPLE" ]]; then
111-
../../../../../build/cargo.sh test
112+
../../../../../build/cargo test
112113
else
113-
../../../../../build/cargo.sh build --target $TARGET_TRIPLE --tests
114+
../../../../../build/cargo build --target $TARGET_TRIPLE --tests
114115
fi
115116
popd
116117

117118
pushd regex
118119
echo "[TEST] rust-lang/regex example shootout-regex-dna"
119-
cargo clean
120+
../build/cargo clean
120121
export RUSTFLAGS="$RUSTFLAGS --cap-lints warn" # newer aho_corasick versions throw a deprecation warning
121122
# Make sure `[codegen mono items] start` doesn't poison the diff
122-
../build/cargo.sh build --example shootout-regex-dna --target $TARGET_TRIPLE
123+
../build/cargo build --example shootout-regex-dna --target $TARGET_TRIPLE
123124
if [[ "$HOST_TRIPLE" = "$TARGET_TRIPLE" ]]; then
124125
cat examples/regexdna-input.txt \
125-
| ../build/cargo.sh run --example shootout-regex-dna --target $TARGET_TRIPLE \
126+
| ../build/cargo run --example shootout-regex-dna --target $TARGET_TRIPLE \
126127
| grep -v "Spawned thread" > res.txt
127128
diff -u res.txt examples/regexdna-output.txt
128129
fi
129130

130131
if [[ "$HOST_TRIPLE" = "$TARGET_TRIPLE" ]]; then
131132
echo "[TEST] rust-lang/regex tests"
132-
../build/cargo.sh test --tests -- --exclude-should-panic --test-threads 1 -Zunstable-options -q
133+
../build/cargo test --tests -- --exclude-should-panic --test-threads 1 -Zunstable-options -q
133134
else
134135
echo "[AOT] rust-lang/regex tests"
135-
../build/cargo.sh build --tests --target $TARGET_TRIPLE
136+
../build/cargo build --tests --target $TARGET_TRIPLE
136137
fi
137138
popd
138139
}

0 commit comments

Comments
 (0)