Skip to content

Commit 0bb3715

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 72ad7a0 commit 0bb3715

File tree

3 files changed

+47
-24
lines changed

3 files changed

+47
-24
lines changed

Diff for: README.md

+13-18
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
```

Diff for: rust-toolchain

+3
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"]

Diff for: src/main.rs

+31-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use rustc_hir::{
88
use rustc_middle::hir::map::Map;
99
use rustc_middle::ty::{TyCtxt, TyKind};
1010
use std::collections::HashMap;
11-
use std::env;
1211
use std::fs;
1312
use std::process::Command;
1413

@@ -93,7 +92,37 @@ impl<'a> rustc_driver::Callbacks for Callbacks<'a> {
9392
}
9493
}
9594

95+
// absolutely awful hack to fix the sysroot when running out-of-tree
96+
// Taken from #78926, which took it from src/bin/miri.rs, which in turn took it from clippy ... rustc is a mess.
97+
// FIXME(jyn514): implement https://github.com/rust-lang/rust/pull/78926#issuecomment-726653035 instead
98+
// FIXME(jyn514): Why is this a compile-time constant? Won't that break when this is distributed?
99+
// maybe use `rustc --print sysroot` instead?
100+
101+
/// Returns the "default sysroot" that will be used if no `--sysroot` flag is set.
102+
/// Should be a compile-time constant.
103+
fn compile_time_sysroot() -> String {
104+
/* NOTE: this doesn't matter for example-analyzer, which is always built out-of-tree,
105+
* but might matter if it's ever incorporated into rustdoc.
106+
if option_env!("RUSTC_STAGE").is_some() {
107+
// This is being built as part of rustc, and gets shipped with rustup.
108+
// We can rely on the sysroot computation in librustc_session.
109+
return None;
110+
}
111+
*/
112+
// For builds outside rustc, we need to ensure that we got a sysroot
113+
// that gets used as a default. The sysroot computation in librustc_session would
114+
// end up somewhere in the build dir (see `get_or_default_sysroot`).
115+
// Taken from PR <https://github.com/Manishearth/rust-clippy/pull/911>.
116+
let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
117+
let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
118+
match (home, toolchain) {
119+
(Some(home), Some(toolchain)) => format!("{}/toolchains/{}", home, toolchain),
120+
_ => option_env!("RUST_SYSROOT").expect("to build example-analyzer without rustup, set RUST_SYSROOT to `rustc --print sysroot`").into(),
121+
}
122+
}
123+
96124
fn main() {
125+
let sysroot = compile_time_sysroot();
97126
// Run Cargo to get the `rustc` commands used to check each example.
98127
// This gives us all the necessary flags (eg --extern) to get the example to compile.
99128
let cargo_output = {
@@ -129,6 +158,7 @@ fn main() {
129158
let mut args: Vec<_> = command
130159
.split(" ")
131160
.filter(|s| *s != "--error-format=json" && *s != "--json=diagnostic-rendered-ansi")
161+
.chain(vec!["--sysroot", &sysroot])
132162
.collect();
133163

134164
// FIXME(willcrichton): when compiling warp, for some reason the --cfg flags
@@ -145,11 +175,6 @@ fn main() {
145175
args.remove(*i);
146176
}
147177

148-
// Add sysroot to compiler args
149-
let toolchain_path = env::var("HOME").unwrap()
150-
+ "/.rustup/toolchains/nightly-2020-12-09-x86_64-apple-darwin";
151-
args.extend(vec!["--sysroot", &toolchain_path]);
152-
153178
let args: Vec<_> = args.into_iter().map(|arg| arg.to_string()).collect();
154179

155180
// Try to find file name from the arg list so we can save it in the call locations

0 commit comments

Comments
 (0)