Skip to content

Commit 226152b

Browse files
committed
Make the build process easier
Notice I didn't say simpler. This does a quite a few things: - Pins a version of nightly using `rust-toolchain`. This avoids confusion when rustc changes its API in a later version. When rebasing over rust-lang/master, rust-toolchain should also be updated. - Replaces CUSTOM_RUSTDOC with `rustup toolchain link`, which allows determining the sysroot without hardcoding `.rustup/toolchain` in the instructions (*not* the binary). - Replaces 'copy paste what cargo did with additional arguments' with `cargo rustdoc` - Determines the sysroot in `src/main.rs` with rustup instead of hard-coding it. This is an absolute hack, see the comments for details, but it's used by clippy and miri so it works in practice. - Minor changes to the README to distinguish the current directory
1 parent 982ee2d commit 226152b

File tree

3 files changed

+50
-20
lines changed

3 files changed

+50
-20
lines changed

README.md

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,39 @@
55
```bash
66
git clone -b linked-examples https://github.com/willcrichton/rust
77
cd rust
8-
./x.py --stage 1 build
9-
export CUSTOM_RUSTDOC=$(pwd)/build/x86_64-apple-darwin/stage1/bin/rustdoc
8+
./x.py build
9+
# replace `apple-darwin` with your current target as appropriate
10+
rustup toolchain link custom-rustdoc build/x86_64-apple-darwin/stage1
1011
cd ..
1112
git clone https://github.com/willcrichton/example-analyzer
12-
cd example_analyzer
13-
rustup toolchain install nightly --profile default --component rustc-dev
14-
rustup override set nightly
13+
cd example-analyzer
1514
cargo build
1615
```
1716

1817
## Example
1918

20-
2119
```bash
20+
# NOTE: the directory you run this from is important since the project uses
21+
# `rust-toolchain`
22+
export LD_LIBRARY_PATH=$(rustc --print sysroot)/lib
2223
cd doctest
23-
export DYLD_LIBRARY_PATH=$HOME/.rustup/toolchains/nightly-x86_64-apple-darwin/lib:$DYLD_LIBRARY_PATH
2424
../target/debug/example-analyzer
25-
cargo clean && cargo doc -vv
26-
# copy the command within Running `...` and:
27-
# 1. replace rustdoc with $CUSTOM_RUSTDOC
28-
# 2. add the flag "--call-locations .call_locations.json" to the end
29-
# 3. run the command
30-
open target/doc/doctest/index.html
25+
cargo +custom-rustdoc rustdoc --open -- --call-locations .call_locations.json
3126
```
3227

3328
## Development
3429

35-
If you change the Rust repo (ie rustdoc) then run:
30+
If you change the Rust repo (i.e. rustdoc) then run:
3631

3732
```
38-
./x.py --stage 1 build
39-
# also re-run the $CUSTOM_RUSTDOC command
33+
(cd ../../rust && ./x.py build)
34+
# also re-run `cargo rustdoc`
4035
```
4136

4237
If you change example-analyzer then run:
4338

4439
```
45-
cargo build
40+
(cd .. && cargo build)
4641
../target/debug/example-analyzer
47-
# also the $CUSTOM_RUSTDOC command
42+
# also re-run `cargo rustdoc`
4843
```

rust-toolchain

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[toolchain]
2+
channel = "nightly-2020-12-09"
3+
components = ["rustc-dev", "llvm-tools-preview"]

src/main.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,38 @@ use std::process::Command;
7777
use regex::Regex;
7878
use std::fs;
7979

80+
// absolutely awful hack to fix the sysroot when running out-of-tree
81+
// Taken from #78926, which took it from src/bin/miri.rs, which in turn took it from clippy ... rustc is a mess.
82+
// FIXME(jyn514): implement https://github.com/rust-lang/rust/pull/78926#issuecomment-726653035 instead
83+
// FIXME(jyn514): Why is this a compile-time constant? Won't that break when this is distributed?
84+
// maybe use `rustc --print sysroot` instead?
85+
86+
/// Returns the "default sysroot" that will be used if no `--sysroot` flag is set.
87+
/// Should be a compile-time constant.
88+
fn compile_time_sysroot() -> String {
89+
/* NOTE: this doesn't matter for example-analyzer, which is always built out-of-tree,
90+
* but might matter if it's ever incorporated into rustdoc.
91+
if option_env!("RUSTC_STAGE").is_some() {
92+
// This is being built as part of rustc, and gets shipped with rustup.
93+
// We can rely on the sysroot computation in librustc_session.
94+
return None;
95+
}
96+
*/
97+
// For builds outside rustc, we need to ensure that we got a sysroot
98+
// that gets used as a default. The sysroot computation in librustc_session would
99+
// end up somewhere in the build dir (see `get_or_default_sysroot`).
100+
// Taken from PR <https://github.com/Manishearth/rust-clippy/pull/911>.
101+
let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
102+
let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
103+
match (home, toolchain) {
104+
(Some(home), Some(toolchain)) => format!("{}/toolchains/{}", home, toolchain),
105+
_ => option_env!("RUST_SYSROOT").expect("to build example-analyzer without rustup, set RUST_SYSROOT to `rustc --print sysroot`").into(),
106+
}
107+
}
108+
80109
fn main() {
110+
let sysroot = compile_time_sysroot();
111+
81112
Command::new("cargo")
82113
.args(&["clean", "--target-dir", "target/debug/examples"])
83114
.output().unwrap();
@@ -97,7 +128,8 @@ fn main() {
97128
.for_each(|command| {
98129
let args: Vec<_> = command
99130
.split(" ")
100-
.chain(vec!["--sysroot", "/Users/will/.rustup/toolchains/nightly-x86_64-apple-darwin"].into_iter())
131+
// We need to overwrite the default that librustc_session would compute.
132+
.chain(vec!["--sysroot", &sysroot])
101133
.map(|s| s.to_string())
102134
.collect();
103135
let file_name = args[4].to_string();
@@ -109,4 +141,4 @@ fn main() {
109141

110142
let json = rustc_serialize::json::encode(&calls).unwrap();
111143
fs::write(".call_locations.json", json).unwrap();
112-
}
144+
}

0 commit comments

Comments
 (0)