Skip to content

Commit d213c8c

Browse files
authored
Removing compiler build and using rustup (#33)
Changing to use `rustup` for the Rust compiler now that we are not on our own fork. - [x] Need `build.rs` so that correct rust version is used with components installed - [x] Need to update README.md - [x] Building with `Makefile` needs to be removed - [x] Handling tests with `Makefile` needs to change to use `cargo` - [x] CI needs to be updated succesfully - [x] Need to handle golden tests difference - [x] Need to make sure this will still work with `mir-semantics` ([see this PR](runtimeverification/mir-semantics#439)) Moved to future work: - [ ] Change tests to use `cargo test` ? Could remove `Makefile` entirely then - [ ] Need to make sure this will still work with extracting stable-mir-json for cargo projects / `std lib`
1 parent 1cc3e92 commit d213c8c

35 files changed

+6912
-3905
lines changed

.github/workflows/test.yml

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,12 @@ jobs:
2828
- name: "Set up nightly Rust" # https://github.com/rust-lang/rustup/issues/3409
2929
uses: dtolnay/rust-toolchain@master
3030
with:
31-
toolchain: nightly-2024-08-28
31+
toolchain: nightly-2024-11-29 # Hardcoded version, same as is in the build.rs
3232

33-
- name: 'Set up tree for rust dependency'
34-
run: make setup
35-
36-
- name: 'Cache smir_pretty and rustc'
37-
uses: Swatinem/rust-cache@v2
38-
with:
39-
workspaces: |
40-
.
41-
deps/rust/src
42-
cache-directories: |
43-
target
44-
deps/rust/src/build
45-
deps/rust/src/target
46-
47-
- name: 'Build smir_pretty and its rustc dependency'
48-
run: | # rustc bootstrap checks this and refuses stage 1 in "CI"
49-
export GITHUB_ACTIONS="in denial" && \
50-
echo "GITHUB_ACTIONS = ${GITHUB_ACTIONS}" && \
51-
make build_all
52-
53-
- name: 'Run smir integration tests'
33+
- name: 'Build smir_pretty'
5434
run: |
55-
make integration-test
35+
cargo build -vv
5636
57-
- name: 'Clean up toolchain'
58-
if: always()
37+
- name: 'Run smir integration tests'
5938
run: |
60-
make rustup-clear-toolchain
39+
make integration-test

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,13 @@
22
name = "smir_pretty"
33
version = "0.1.0"
44
edition = "2021"
5-
# rust-version = "1.78.0" # I think we get latest available rust by unsetting this and setting rust-toolchain.toml.toolchain.channel = nightly
65

76
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
87

98
[dependencies]
109
dot-writer = "0.1.4"
1110
tracing = "0.1"
12-
# serde = { version = "=1.0.202", features = ["derive"] }
13-
# serde_cbor = "0.11"
14-
# serde_json = "1.0"
15-
# tar = "0.4"
1611

1712
[package.metadata.rust-analyzer]
1813
# This package uses rustc crates.
19-
rustc_private=true
14+
rustc_private=true

Makefile

Lines changed: 5 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,30 @@
1-
TARGET ?= debug
2-
STAGE ?= 1
3-
ifneq (0, $(shell test "${STAGE}" -gt 0 2>/dev/null; echo "$$?"))
4-
$(error STAGE must be set to a number greater than 0)
5-
endif
6-
ifneq (${TARGET}, $(filter ${TARGET},debug release))
7-
$(error TARGET must be set to one of debug/release)
8-
endif
9-
RUST_DIR=${CURDIR}/deps/rust
10-
STAGE_FILE=${RUST_DIR}/stage
11-
RUST_SRC=${RUST_DIR}/src
12-
RUST_ARCH=$(shell "${PWD}"/rustc_arch.sh)
13-
RUST_BUILD_DIR=${RUST_SRC}/build/${RUST_ARCH}
14-
RUST_INSTALL_DIR=${RUST_BUILD_DIR}/stage${STAGE}
15-
RUST_LIB_DIR=${RUST_INSTALL_DIR}/lib
16-
RUST_DEP_DIR=${RUST_BUILD_DIR}/stage1-rustc/${RUST_ARCH}/release/deps
17-
TARGET_DEP_DIR=${CURDIR}/target/${TARGET}/deps
18-
TEMP_DIR=${RUST_DIR}/temp
19-
#############################################
20-
# depend on the rust compiler
21-
RUST_REPO=https://github.com/rust-lang/rust
22-
# tip of the `beta` branch on 2025-01-14
23-
RUST_BRANCH=beta
24-
RUST_COMMIT=fe9b975
25-
#############################################
26-
TOOLCHAIN_NAME=smir_pretty
271
RELEASE_FLAG=
28-
ifeq (${TARGET}, release)
29-
RELEASE_FLAG=--release
30-
endif
2+
TOOLCHAIN_NAME=''
313

324
default: build
335

34-
build_all: rust_build rust_set_toolchain build
35-
36-
setup: rust_clone
37-
38-
update: ${RUST_SRC}
39-
cd "${RUST_SRC}"; git fetch origin; git checkout ${RUST_COMMIT}
40-
416
build:
427
cargo build ${RELEASE_FLAG}
438

44-
clean:
45-
cd "${RUST_SRC}" && ./x.py clean
46-
-rm -r "${TEMP_DIR}"
47-
-rm -r "${RUST_DIR}"/tests
48-
-rm -r ./target
49-
50-
distclean:
51-
cd "${RUST_SRC}" && git clean -dffx
52-
-rm -r "${TEMP_DIR}"
53-
-rm -r "${RUST_DIR}"/tests
54-
-rm -r ./target
55-
56-
# this clean removes old backup files which accumulate and lead to slow build times
57-
prebuild_clean: ${RUST_SRC}
58-
-find -name '*.old' -delete
59-
-rm -r "${TEMP_DIR}"
60-
61-
# NOTE: a deeper clone depth is needed for the build process
62-
rust_clone:
63-
git clone --depth 70 --single-branch --branch "${RUST_BRANCH}" "${RUST_REPO}" "${RUST_SRC}" && \
64-
cd "${RUST_SRC}" && \
65-
git checkout ${RUST_COMMIT}
66-
67-
68-
# rust_build for linking against custom rustc is involved
69-
#
70-
# 1. core rust compiler must be built via ./x.py build/install (we also build the test harness here)
71-
# 2. rustc-dev component must be installed (./x.py build/install does _not_ handle, must be done manually)
72-
# 3. HACK(only for ./x.py install) we copy required libraries to the libdir
73-
# 4. finally, use rustup to create custom toolchain
74-
75-
rust_build: ${RUST_SRC} prebuild_clean
76-
cd "${RUST_SRC}"; ./x.py build src/tools/compiletest
77-
cd "${RUST_SRC}"; ./x.py build --stage ${STAGE} --set rust.debug-logging=true compiler/rustc library/std
78-
cd "${RUST_SRC}"; ./x.py dist --set rust.debug-logging=true rustc-dev
79-
mkdir -p "${TEMP_DIR}"
80-
cd "${RUST_SRC}"; tar xf ./build/dist/rustc-dev*tar.gz -C "${TEMP_DIR}"
81-
"${TEMP_DIR}"/rustc-dev*/install.sh --prefix="${RUST_INSTALL_DIR}" --sysconfdir="${RUST_INSTALL_DIR}" > "${RUST_DIR}"/rustc-dev-install.log 2>&1
82-
83-
rust_lib_copy:
84-
cd "${RUST_LIB_DIR}"; cp libLLVM* rustlib/*/lib/
85-
86-
rust_set_toolchain: ${RUST_LIB_DIR}
87-
rustup toolchain link "${TOOLCHAIN_NAME}" "${RUST_INSTALL_DIR}"
88-
rustup override set "${TOOLCHAIN_NAME}"
89-
echo ${STAGE} > ${STAGE_FILE}
9+
clean: rustup-clear-toolchain
10+
cargo clean
9011

9112
.PHONY: rustup-clear-toolchain
9213
rustup-clear-toolchain:
9314
rustup override unset
9415
rustup override unset --nonexistent
9516
rustup toolchain uninstall "${TOOLCHAIN_NAME}"
9617

97-
generate_ui_tests:
98-
mkdir -p "${RUST_DIR}"/tests
99-
cd "${RUST_SRC}"; ./get_runpass.sh tests/ui > "${RUST_DIR}"/tests_ui_sources
100-
-cd "${RUST_SRC}"; ./ui_compiletest.sh "${RUST_SRC}" "${RUST_DIR}"/tests/ui/upstream "${RUST_DIR}"/tests_ui_sources --pass check --force-rerun 2>&1 > "${RUST_DIR}"/tests_ui_upstream.log
101-
-cd "${RUST_SRC}"; RUST_BIN="${PWD}"/run.sh ./ui_compiletest.sh "${RUST_SRC}" "${RUST_DIR}"/tests/ui/smir "${RUST_DIR}"/tests_ui_sources --pass check --force-rerun 2>&1 > "${RUST_DIR}"/tests_ui_smir.log
102-
10318
TESTDIR=$(CURDIR)/tests/integration/programs
10419

10520
.PHONY: integration-test
10621
integration-test: TESTS ?= $(shell find $(TESTDIR) -type f -name "*.rs")
107-
integration-test: SMIR ?= $(CURDIR)/run.sh -Z no-codegen
22+
integration-test: SMIR ?= cargo run -- "-Zno-codegen"
10823
# override this to tweak how expectations are formatted
10924
integration-test: NORMALIZE ?= jq -S -e -f $(TESTDIR)/../normalise-filter.jq
11025
# override this to re-make golden files
11126
integration-test: DIFF ?= | diff -
112-
integration-test: build
27+
integration-test:
11328
errors=""; \
11429
report() { echo "$$1: $$2"; errors="$$errors\n$$1: $$2"; }; \
11530
for rust in ${TESTS}; do \

README.md

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,16 @@
11
# Rust Stable MIR Pretty Printing
22

3-
This package provides:
4-
5-
1. a library crate that provides:
6-
- a `rustc` compiler wrapper which can access stable MIR APIs
7-
- a pretty-printer for a large fragment of stable MIR
8-
2. a `rustc` wrapper binary that uses (1)-(2) to pretty-print Rust source files as stable MIR using the `.smir.json` extension.
9-
10-
It is designed so that anyone can use this library crate as a jumping off point for their own tools which might use stable MIR APIs.
3+
This package provides a program that will emit a JSON serialisation of the Stable MIR of a Rust program
114

125
## Building
136

14-
For first-time builds, run:
15-
16-
```shell
17-
make setup build_all
18-
```
19-
20-
If the underlying `rustc` branch is updated and this crate needs to be rebuilt on top of it, run:
21-
227
```shell
23-
make update build_all
8+
cargo build
249
```
2510

26-
If the source code changes locally for this crate only and it needs to be rebuilt, run:
11+
NOTE: requries [rustup](https://www.rust-lang.org/tools/install)
2712

28-
```shell
29-
make build
30-
```
13+
The `build.rs` script will ensure that the correct version of rust and the required components are installed and defaulted. What `rustup` commands are run can be seen by adding verbosity flag `-vv` to `cargo`.
3114

3215
## Usage
3316

@@ -36,7 +19,7 @@ The options that this tool accepts are identical to `rustc`.
3619
To generate stable MIR output without building a binary, you can invoke the tool as follows:
3720

3821
```shell
39-
./run.sh -Z no-codegen <crate_root>
22+
cargo run -- <rustc_flags> <path_from_crate_root>
4023
```
4124

4225
There is experimental support for rendering the Stable-MIR items and their basic blocks as a
@@ -53,17 +36,6 @@ There are a few environment variables that can be set to control the tools outpu
5336
2. `LINK_INST` - use a richer key-structure for the link-time `functions` map which uses keys that are pairs of a function type (`Ty`) _and_ an function instance kind (`InstanceKind`)
5437
3. `DEBUG` - serialize additional data in the JSON file and dump logs to stdout
5538

56-
### Invocation Details
57-
58-
We use an uncommon build process where we link against a patched rustc installed in this repo.
59-
However, since `cargo build` does not set `rpath` for dynamic linking, we must manually point the program loader/dynamic linker at the required runtime libraries.
60-
Note that the `cargo run` command appears to prepard the rustlib directories automatically to the dynamic link search path.
61-
If you wish to run the tool manually, you will need to tell the program loader/dynamic linker where to find the missing libraries by:
62-
63-
1. setting `LD_LIBRARY_PATH`
64-
2. setting the `rpath` attribute on the binary ELF file
65-
3. manually invoking the loader (usually `/usr/lib/ld-linux.so.2`) with its specific options
66-
6739
## Tests
6840

6941
### Running the Tests

build.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use std::process::Command;
2+
3+
fn main() {
4+
let status = Command::new("rustup")
5+
.args(&["install", "nightly-2024-11-29"])
6+
.status()
7+
.expect("build.rs failed to install nightly-2024-11-29");
8+
9+
println!("Installed nightly-2024-11-29: {}", status);
10+
11+
let status = Command::new("rustup")
12+
.args(&["default", "nightly-2024-11-29"])
13+
.status()
14+
.expect("build.rs failed to default nightly-2024-11-29");
15+
16+
println!("Defaulted nightly-2024-11-29: {}", status);
17+
18+
let status = Command::new("rustup")
19+
.args(&["component", "add", "rustc-dev"])
20+
.status()
21+
.expect("build.rs failed to install rustc-dev");
22+
23+
println!("Added component rustc-dev: {}", status);
24+
25+
let status = Command::new("rustup")
26+
.args(&["component", "add", "llvm-tools"])
27+
.status()
28+
.expect("build.rs failed to install llvm-tools");
29+
30+
println!("Added component llvm-tools: {}", status);
31+
}

deps/rust/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

run.sh

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

rust-toolchain.toml

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

rustc_arch.sh

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

0 commit comments

Comments
 (0)