From 0bb371536794b0bebd9b3f5ee211298aad98e513 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Thu, 10 Dec 2020 17:23:55 -0500 Subject: [PATCH 1/3] 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 --- README.md | 31 +++++++++++++------------------ rust-toolchain | 3 +++ src/main.rs | 37 +++++++++++++++++++++++++++++++------ 3 files changed, 47 insertions(+), 24 deletions(-) create mode 100644 rust-toolchain diff --git a/README.md b/README.md index 4164111349..cba99526ef 100644 --- a/README.md +++ b/README.md @@ -5,44 +5,39 @@ ```bash git clone -b linked-examples https://github.com/willcrichton/rust cd rust -./x.py --stage 1 build -export CUSTOM_RUSTDOC=$(pwd)/build/x86_64-apple-darwin/stage1/bin/rustdoc +./x.py build +# replace `apple-darwin` with your current target as appropriate +rustup toolchain link custom-rustdoc build/x86_64-apple-darwin/stage1 cd .. git clone https://github.com/willcrichton/example-analyzer -cd example_analyzer -rustup toolchain install nightly --profile default --component rustc-dev -rustup override set nightly +cd example-analyzer cargo build ``` ## Example - ```bash +# NOTE: the directory you run this from is important since the project uses +# `rust-toolchain` +export LD_LIBRARY_PATH=$(rustc --print sysroot)/lib cd doctest -export DYLD_LIBRARY_PATH=$HOME/.rustup/toolchains/nightly-x86_64-apple-darwin/lib:$DYLD_LIBRARY_PATH ../target/debug/example-analyzer -cargo clean && cargo doc -vv -# copy the command within Running `...` and: -# 1. replace rustdoc with $CUSTOM_RUSTDOC -# 2. add the flag "--call-locations .call_locations.json" to the end -# 3. run the command -open target/doc/doctest/index.html +cargo +custom-rustdoc rustdoc --open -- --call-locations .call_locations.json ``` ## Development -If you change the Rust repo (ie rustdoc) then run: +If you change the Rust repo (i.e. rustdoc) then run: ``` -./x.py --stage 1 build -# also re-run the $CUSTOM_RUSTDOC command +(cd ../../rust && ./x.py build) +# also re-run `cargo rustdoc` ``` If you change example-analyzer then run: ``` -cargo build +(cd .. && cargo build) ../target/debug/example-analyzer -# also the $CUSTOM_RUSTDOC command +# also re-run `cargo rustdoc` ``` diff --git a/rust-toolchain b/rust-toolchain new file mode 100644 index 0000000000..6dd6e19c3f --- /dev/null +++ b/rust-toolchain @@ -0,0 +1,3 @@ +[toolchain] +channel = "nightly-2020-12-09" +components = ["rustc-dev", "llvm-tools-preview"] diff --git a/src/main.rs b/src/main.rs index 224183bfb0..214bcc7205 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,6 @@ use rustc_hir::{ use rustc_middle::hir::map::Map; use rustc_middle::ty::{TyCtxt, TyKind}; use std::collections::HashMap; -use std::env; use std::fs; use std::process::Command; @@ -93,7 +92,37 @@ impl<'a> rustc_driver::Callbacks for Callbacks<'a> { } } +// absolutely awful hack to fix the sysroot when running out-of-tree +// Taken from #78926, which took it from src/bin/miri.rs, which in turn took it from clippy ... rustc is a mess. +// FIXME(jyn514): implement https://github.com/rust-lang/rust/pull/78926#issuecomment-726653035 instead +// FIXME(jyn514): Why is this a compile-time constant? Won't that break when this is distributed? +// maybe use `rustc --print sysroot` instead? + +/// Returns the "default sysroot" that will be used if no `--sysroot` flag is set. +/// Should be a compile-time constant. +fn compile_time_sysroot() -> String { + /* NOTE: this doesn't matter for example-analyzer, which is always built out-of-tree, + * but might matter if it's ever incorporated into rustdoc. + if option_env!("RUSTC_STAGE").is_some() { + // This is being built as part of rustc, and gets shipped with rustup. + // We can rely on the sysroot computation in librustc_session. + return None; + } + */ + // For builds outside rustc, we need to ensure that we got a sysroot + // that gets used as a default. The sysroot computation in librustc_session would + // end up somewhere in the build dir (see `get_or_default_sysroot`). + // Taken from PR . + let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME")); + let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN")); + match (home, toolchain) { + (Some(home), Some(toolchain)) => format!("{}/toolchains/{}", home, toolchain), + _ => option_env!("RUST_SYSROOT").expect("to build example-analyzer without rustup, set RUST_SYSROOT to `rustc --print sysroot`").into(), + } +} + fn main() { + let sysroot = compile_time_sysroot(); // Run Cargo to get the `rustc` commands used to check each example. // This gives us all the necessary flags (eg --extern) to get the example to compile. let cargo_output = { @@ -129,6 +158,7 @@ fn main() { let mut args: Vec<_> = command .split(" ") .filter(|s| *s != "--error-format=json" && *s != "--json=diagnostic-rendered-ansi") + .chain(vec!["--sysroot", &sysroot]) .collect(); // FIXME(willcrichton): when compiling warp, for some reason the --cfg flags @@ -145,11 +175,6 @@ fn main() { args.remove(*i); } - // Add sysroot to compiler args - let toolchain_path = env::var("HOME").unwrap() - + "/.rustup/toolchains/nightly-2020-12-09-x86_64-apple-darwin"; - args.extend(vec!["--sysroot", &toolchain_path]); - let args: Vec<_> = args.into_iter().map(|arg| arg.to_string()).collect(); // Try to find file name from the arg list so we can save it in the call locations From fc7fbf1d52ca1f5a0a853d135ca62d0f87f321a0 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Fri, 11 Dec 2020 11:37:03 -0500 Subject: [PATCH 2/3] Mac uses DYLD_LIBRARY_PATH --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index cba99526ef..f6b1441a95 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ cargo build ```bash # NOTE: the directory you run this from is important since the project uses # `rust-toolchain` +# On MacOS, use `DYLD_LIBRARY_PATH` instead. export LD_LIBRARY_PATH=$(rustc --print sysroot)/lib cd doctest ../target/debug/example-analyzer From c02af322ea07fb63c5820678f03e5c223fa95d78 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Fri, 11 Dec 2020 12:01:33 -0500 Subject: [PATCH 3/3] Update rust-toolchain This avoids an incremental compilation bug fixed on nightly: https://github.com/rust-lang/rust/issues/79661 --- rust-toolchain | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust-toolchain b/rust-toolchain index 6dd6e19c3f..3a473ccaf0 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1,3 +1,3 @@ [toolchain] -channel = "nightly-2020-12-09" +channel = "nightly-2020-12-11" components = ["rustc-dev", "llvm-tools-preview"]