@@ -102,21 +102,26 @@ fn init_late_loggers() {
102
102
103
103
/// Returns the "default sysroot" that Miri will use if no `--sysroot` flag is set.
104
104
/// Should be a compile-time constant.
105
- fn compile_time_sysroot ( ) -> String {
105
+ fn compile_time_sysroot ( ) -> Option < String > {
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.
109
+ return None ;
110
+ }
111
+ // For builds outside rustc, we need to ensure that we got a sysroot
112
+ // that gets used as a default. The sysroot computation in librustc would
113
+ // end up somewhere in the build dir.
106
114
// Taken from PR <https://github.com/Manishearth/rust-clippy/pull/911>.
107
115
let home = option_env ! ( "RUSTUP_HOME" ) . or ( option_env ! ( "MULTIRUST_HOME" ) ) ;
108
116
let toolchain = option_env ! ( "RUSTUP_TOOLCHAIN" ) . or ( option_env ! ( "MULTIRUST_TOOLCHAIN" ) ) ;
109
- match ( home, toolchain) {
117
+ Some ( match ( home, toolchain) {
110
118
( Some ( home) , Some ( toolchain) ) => format ! ( "{}/toolchains/{}" , home, toolchain) ,
111
119
_ => {
112
120
option_env ! ( "RUST_SYSROOT" )
113
- . expect (
114
- "could not find sysroot. Either set `MIRI_SYSROOT` at run-time, or at \
115
- build-time specify `RUST_SYSROOT` env var or use rustup or multirust",
116
- )
121
+ . expect ( "To build Miri without rustup, set the `RUST_SYSROOT` env var at build time" )
117
122
. to_owned ( )
118
123
}
119
- }
124
+ } )
120
125
}
121
126
122
127
fn main ( ) {
@@ -165,14 +170,17 @@ fn main() {
165
170
}
166
171
}
167
172
168
- // Determine sysroot.
169
- let sysroot_flag = "--sysroot" . to_string ( ) ;
170
- if !rustc_args. contains ( & sysroot_flag) {
171
- // We need to *always* set a --sysroot, as the "default" rustc uses is
172
- // somewhere in the directory miri was built in.
173
- // If no --sysroot is given, fall back to env vars that are read at *compile-time*.
174
- rustc_args. push ( sysroot_flag) ;
175
- rustc_args. push ( compile_time_sysroot ( ) ) ;
173
+ // Determine sysroot if needed. Make sure we always call `compile_time_sysroot`
174
+ // as that also does some sanity-checks of the environment we were built in.
175
+ // FIXME: Ideally we'd turn a bad build env into a compile-time error, but
176
+ // CTFE does not seem powerful enough for that yet.
177
+ if let Some ( sysroot) = compile_time_sysroot ( ) {
178
+ let sysroot_flag = "--sysroot" . to_string ( ) ;
179
+ if !rustc_args. contains ( & sysroot_flag) {
180
+ // We need to overwrite the default that librustc would compute.
181
+ rustc_args. push ( sysroot_flag) ;
182
+ rustc_args. push ( sysroot) ;
183
+ }
176
184
}
177
185
178
186
// Finally, add the default flags all the way in the beginning, but after the binary name.
0 commit comments