Skip to content

Commit 520a61f

Browse files
authored
Merge pull request rust-lang#1104 from bjorn3/build_system_refactor
Build system refactor
2 parents 9410b58 + 8315730 commit 520a61f

File tree

14 files changed

+160
-95
lines changed

14 files changed

+160
-95
lines changed

.github/workflows/main.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,13 @@ jobs:
5151
export COMPILE_RUNS=2
5252
export RUN_RUNS=2
5353
54-
./test.sh --release
54+
./test.sh
55+
56+
- name: Package prebuilt cg_clif
57+
run: tar cvfJ cg_clif.tar.xz build
58+
59+
- name: Upload prebuilt cg_clif
60+
uses: actions/upload-artifact@v2
61+
with:
62+
name: cg_clif-${{ runner.os }}
63+
path: cg_clif.tar.xz

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ perf.data
66
perf.data.old
77
*.events
88
*.string*
9-
/build_sysroot/sysroot
9+
/build
1010
/build_sysroot/sysroot_src
1111
/rust
1212
/rand

Readme.md

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,56 @@
22

33
> ⚠⚠⚠ Certain kinds of FFI don't work yet. ⚠⚠⚠
44
5-
The goal of this project is to create an alternative codegen backend for the rust compiler based on [Cranelift](https://github.com/bytecodealliance/wasmtime/blob/master/cranelift). This has the potential to improve compilation times in debug mode. If your project doesn't use any of the things listed under "Not yet supported", it should work fine. If not please open an issue.
5+
The goal of this project is to create an alternative codegen backend for the rust compiler based on [Cranelift](https://github.com/bytecodealliance/wasmtime/blob/master/cranelift).
6+
This has the potential to improve compilation times in debug mode.
7+
If your project doesn't use any of the things listed under "Not yet supported", it should work fine.
8+
If not please open an issue.
69

710
## Building and testing
811

912
```bash
1013
$ git clone https://github.com/bjorn3/rustc_codegen_cranelift.git
1114
$ cd rustc_codegen_cranelift
1215
$ ./prepare.sh # download and patch sysroot src and install hyperfine for benchmarking
13-
$ ./test.sh --release
16+
$ ./build.sh
1417
```
1518

16-
If you want to only build but not test you should replace the last command with:
19+
To run the test suite replace the last command with:
1720

1821
```bash
19-
$ cargo build --release
20-
$ ./build_sysroot/build_sysroot.sh
22+
$ ./test.sh
2123
```
2224

25+
This will implicitly build cg_clif too. Both `build.sh` and `test.sh` accept a `--debug` argument to
26+
build in debug mode.
27+
28+
Alternatively you can download a pre built version from [GHA]. It is listed in the artifacts section
29+
of workflow runs. Unfortunately due to GHA restrictions you need to be logged in to access it.
30+
31+
[GHA]: https://github.com/bjorn3/rustc_codegen_cranelift/actions?query=branch%3Amaster+event%3Apush+is%3Asuccess
32+
2333
## Usage
2434

2535
rustc_codegen_cranelift can be used as a near-drop-in replacement for `cargo build` or `cargo run` for existing projects.
2636

27-
Assuming `$cg_clif_dir` is the directory you cloned this repo into and you followed the instructions (`prepare.sh` and `test.sh`).
37+
Assuming `$cg_clif_dir` is the directory you cloned this repo into and you followed the instructions (`prepare.sh` and `build.sh` or `test.sh`).
2838

2939
### Cargo
3040

3141
In the directory with your project (where you can do the usual `cargo build`), run:
3242

3343
```bash
34-
$ $cg_clif_dir/cargo.sh run
44+
$ $cg_clif_dir/build/cargo.sh run
3545
```
3646

3747
This should build and run your project with rustc_codegen_cranelift instead of the usual LLVM backend.
3848

39-
If you compiled cg_clif in debug mode (aka you didn't pass `--release` to `./test.sh`) you should set `CHANNEL="debug"`.
40-
4149
### Rustc
4250

4351
> You should prefer using the Cargo method.
4452
4553
```bash
46-
$ $cg_clif_dir/target/release/cg_clif my_crate.rs
54+
$ $cg_clif_dir/build/cg_clif my_crate.rs
4755
```
4856

4957
### Jit mode
@@ -54,13 +62,13 @@ In jit mode cg_clif will immediately execute your code without creating an execu
5462
> The jit mode will probably need cargo integration to make this possible.
5563
5664
```bash
57-
$ $cg_clif_dir/cargo.sh jit
65+
$ $cg_clif_dir/build/cargo.sh jit
5866
```
5967

6068
or
6169

6270
```bash
63-
$ $cg_clif_dir/target/release/cg_clif --jit my_crate.rs
71+
$ $cg_clif_dir/build/cg_clif --jit my_crate.rs
6472
```
6573

6674
### Shell
@@ -69,7 +77,7 @@ These are a few functions that allow you to easily run rust code from the shell
6977

7078
```bash
7179
function jit_naked() {
72-
echo "$@" | $cg_clif_dir/target/release/cg_clif - --jit
80+
echo "$@" | $cg_clif_dir/build/cg_clif - --jit
7381
}
7482

7583
function jit() {

build.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Settings
5+
export CHANNEL="release"
6+
build_sysroot=1
7+
target_dir='build'
8+
while [[ $# != 0 ]]; do
9+
case $1 in
10+
"--debug")
11+
export CHANNEL="debug"
12+
;;
13+
"--without-sysroot")
14+
build_sysroot=0
15+
;;
16+
"--target-dir")
17+
target_dir=$2
18+
shift
19+
;;
20+
*)
21+
echo "Unknown flag '$1'"
22+
echo "Usage: ./build.sh [--debug] [--without-sysroot] [--target-dir DIR]"
23+
;;
24+
esac
25+
shift
26+
done
27+
28+
# Build cg_clif
29+
export RUSTFLAGS="-Zrun_dsymutil=no"
30+
if [[ "$CHANNEL" == "release" ]]; then
31+
cargo build --release
32+
else
33+
cargo build
34+
fi
35+
36+
rm -rf $target_dir
37+
mkdir $target_dir
38+
cp -a target/$CHANNEL/cg_clif{,_build_sysroot} target/$CHANNEL/*rustc_codegen_cranelift* $target_dir/
39+
cp -a rust-toolchain scripts/config.sh scripts/cargo.sh $target_dir
40+
41+
if [[ "$build_sysroot" == "1" ]]; then
42+
echo "[BUILD] sysroot"
43+
export CG_CLIF_INCR_CACHE_DISABLED=1
44+
dir=$(pwd)
45+
cd $target_dir
46+
time $dir/build_sysroot/build_sysroot.sh
47+
fi

build_sysroot/build_sysroot.sh

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,24 @@
33
# Requires the CHANNEL env var to be set to `debug` or `release.`
44

55
set -e
6-
cd $(dirname "$0")
76

8-
if [ -z $CHANNEL ]; then
9-
export CHANNEL='release'
10-
fi
7+
source ./config.sh
118

12-
pushd ../ >/dev/null
13-
source ./scripts/config.sh
14-
popd >/dev/null
9+
dir=$(pwd)
1510

16-
# We expect the target dir in the default location. Guard against the user changing it.
17-
export CARGO_TARGET_DIR=target
11+
# Use rustc with cg_clif as hotpluggable backend instead of the custom cg_clif driver so that
12+
# build scripts are still compiled using cg_llvm.
13+
export RUSTC=$dir"/cg_clif_build_sysroot"
14+
export RUSTFLAGS=$RUSTFLAGS" --clif"
15+
16+
cd $(dirname "$0")
1817

1918
# Cleanup for previous run
2019
# v Clean target dir except for build scripts and incremental cache
21-
rm -r target/*/{debug,release}/{build,deps,examples,libsysroot*,native} 2>/dev/null || true
22-
rm -r sysroot/ 2>/dev/null || true
20+
#rm -r target/*/{debug,release}/{build,deps,examples,libsysroot*,native} 2>/dev/null || true
2321

24-
# Use rustc with cg_clif as hotpluggable backend instead of the custom cg_clif driver so that
25-
# build scripts are still compiled using cg_llvm.
26-
export RUSTC=$(pwd)/../"target/"$CHANNEL"/cg_clif_build_sysroot"
27-
export RUSTFLAGS=$RUSTFLAGS" --clif"
22+
# We expect the target dir in the default location. Guard against the user changing it.
23+
export CARGO_TARGET_DIR=target
2824

2925
# Build libs
3026
export RUSTFLAGS="$RUSTFLAGS -Zforce-unstable-if-unmarked -Cpanic=abort"
@@ -39,5 +35,5 @@ else
3935
fi
4036

4137
# Copy files to sysroot
42-
mkdir -p sysroot/lib/rustlib/$TARGET_TRIPLE/lib/
43-
cp -r target/$TARGET_TRIPLE/$sysroot_channel/deps/* sysroot/lib/rustlib/$TARGET_TRIPLE/lib/
38+
mkdir -p $dir/sysroot/lib/rustlib/$TARGET_TRIPLE/lib/
39+
cp -a target/$TARGET_TRIPLE/$sysroot_channel/deps/* $dir/sysroot/lib/rustlib/$TARGET_TRIPLE/lib/

build_sysroot/prepare_sysroot_src.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fi
1212

1313
rm -rf $DST_DIR
1414
mkdir -p $DST_DIR/library
15-
cp -r $SRC_DIR/library $DST_DIR/
15+
cp -a $SRC_DIR/library $DST_DIR/
1616

1717
pushd $DST_DIR
1818
echo "[GIT] init"

clean_all.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash --verbose
22
set -e
33

4-
rm -rf target/ build_sysroot/{sysroot/,sysroot_src/,target/} perf.data{,.old}
4+
rm -rf target/ build/ build_sysroot/{sysroot_src/,target/} perf.data{,.old}
55
rm -rf rand/ regex/ simple-raytracer/

cargo.sh renamed to scripts/cargo.sh

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
#!/bin/bash
22

3-
if [ -z $CHANNEL ]; then
4-
export CHANNEL='release'
5-
fi
6-
7-
pushd $(dirname "$0") >/dev/null
8-
source scripts/config.sh
3+
dir=$(dirname "$0")
4+
source $dir/config.sh
95

106
# read nightly compiler from rust-toolchain file
11-
TOOLCHAIN=$(cat rust-toolchain)
12-
13-
popd >/dev/null
7+
TOOLCHAIN=$(cat $dir/rust-toolchain)
148

159
cmd=$1
16-
shift
10+
shift || true
1711

1812
if [[ "$cmd" = "jit" ]]; then
1913
cargo +${TOOLCHAIN} rustc $@ -- --jit

scripts/config.sh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,19 @@ echo
3939
export RUSTC_WRAPPER=
4040
fi
4141

42-
export RUSTC=$(pwd)/"target/"$CHANNEL"/cg_clif"
42+
dir=$(cd $(dirname "$BASH_SOURCE"); pwd)
43+
44+
export RUSTC=$dir"/cg_clif"
4345
export RUSTFLAGS=$linker
4446
export RUSTDOCFLAGS=$linker' -Ztrim-diagnostic-paths=no -Cpanic=abort -Zpanic-abort-tests '\
45-
'-Zcodegen-backend='$(pwd)'/target/'$CHANNEL'/librustc_codegen_cranelift.'$dylib_ext' --sysroot '$(pwd)'/build_sysroot/sysroot'
47+
'-Zcodegen-backend='$dir'/librustc_codegen_cranelift.'$dylib_ext' --sysroot '$dir'/sysroot'
4648

4749
# FIXME remove once the atomic shim is gone
4850
if [[ `uname` == 'Darwin' ]]; then
4951
export RUSTFLAGS="$RUSTFLAGS -Clink-arg=-undefined -Clink-arg=dynamic_lookup"
5052
fi
5153

52-
export LD_LIBRARY_PATH="$(pwd)/target/out:$(pwd)/build_sysroot/sysroot/lib/rustlib/"$TARGET_TRIPLE"/lib:\
53-
$(pwd)/target/"$CHANNEL":$(rustc --print sysroot)/lib"
54+
export LD_LIBRARY_PATH="$dir:$(rustc --print sysroot)/lib:$dir/target/out:$dir/sysroot/lib/rustlib/"$TARGET_TRIPLE"/lib"
5455
export DYLD_LIBRARY_PATH=$LD_LIBRARY_PATH
5556

5657
export CG_CLIF_DISPLAY_CG_TIME=1

scripts/filter_profile.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#!/bin/bash
22
#![forbid(unsafe_code)]/* This line is ignored by bash
33
# This block is ignored by rustc
4-
CHANNEL="release"
54
pushd $(dirname "$0")/../
6-
source scripts/config.sh
5+
source build/config.sh
76
popd
87
PROFILE=$1 OUTPUT=$2 exec $RUSTC $RUSTFLAGS --jit $0
98
#*/

scripts/rustup.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ case $1 in
2626
git add rust-toolchain build_sysroot/Cargo.lock
2727
git commit -m "Rustup to $(rustc -V)"
2828
;;
29+
"push")
30+
cg_clif=$(pwd)
31+
pushd ../rust
32+
branch=update_cg_clif-$(date +%Y-%m-%d)
33+
git checkout -b $branch
34+
git subtree pull --prefix=compiler/rustc_codegen_cranelift/ https://github.com/bjorn3/rustc_codegen_cranelift.git master
35+
git push -u my $branch
36+
popd
37+
;;
2938
*)
3039
echo "Unknown command '$1'"
3140
echo "Usage: ./rustup.sh prepare|commit"

0 commit comments

Comments
 (0)