@@ -8,7 +8,6 @@ use rustc_hir::{
8
8
use rustc_middle:: hir:: map:: Map ;
9
9
use rustc_middle:: ty:: { TyCtxt , TyKind } ;
10
10
use std:: collections:: HashMap ;
11
- use std:: env;
12
11
use std:: fs;
13
12
use std:: process:: Command ;
14
13
@@ -93,7 +92,37 @@ impl<'a> rustc_driver::Callbacks for Callbacks<'a> {
93
92
}
94
93
}
95
94
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
+
96
124
fn main ( ) {
125
+ let sysroot = compile_time_sysroot ( ) ;
97
126
// Run Cargo to get the `rustc` commands used to check each example.
98
127
// This gives us all the necessary flags (eg --extern) to get the example to compile.
99
128
let cargo_output = {
@@ -129,6 +158,7 @@ fn main() {
129
158
let mut args: Vec < _ > = command
130
159
. split ( " " )
131
160
. filter ( |s| * s != "--error-format=json" && * s != "--json=diagnostic-rendered-ansi" )
161
+ . chain ( vec ! [ "--sysroot" , & sysroot] )
132
162
. collect ( ) ;
133
163
134
164
// FIXME(willcrichton): when compiling warp, for some reason the --cfg flags
@@ -145,11 +175,6 @@ fn main() {
145
175
args. remove ( * i) ;
146
176
}
147
177
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
-
153
178
let args: Vec < _ > = args. into_iter ( ) . map ( |arg| arg. to_string ( ) ) . collect ( ) ;
154
179
155
180
// Try to find file name from the arg list so we can save it in the call locations
0 commit comments