100
100
//!
101
101
//! - rust nightly + `wasm32-unknown-unknown` toolchain
102
102
//!
103
- //! or
104
- //!
105
- //! - rust stable and version at least 1.68.0 + `wasm32-unknown-unknown` toolchain
106
- //!
107
- //! If a specific rust is installed with `rustup`, it is important that the wasm target is
108
- //! installed as well. For example if installing the rust from 20.02.2020 using `rustup
103
+ //! If a specific rust nightly is installed with `rustup`, it is important that the wasm target is
104
+ //! installed as well. For example if installing the rust nightly from 20.02.2020 using `rustup
109
105
//! install nightly-2020-02-20`, the wasm target needs to be installed as well `rustup target add
110
106
//! wasm32-unknown-unknown --toolchain nightly-2020-02-20`.
111
107
@@ -115,11 +111,9 @@ use std::{
115
111
path:: { Path , PathBuf } ,
116
112
process:: Command ,
117
113
} ;
118
- use version:: Version ;
119
114
120
115
mod builder;
121
116
mod prerequisites;
122
- mod version;
123
117
mod wasm_project;
124
118
125
119
pub use builder:: { WasmBuilder , WasmBuilderSelectProject } ;
@@ -178,59 +172,53 @@ fn copy_file_if_changed(src: PathBuf, dst: PathBuf) {
178
172
}
179
173
}
180
174
181
- /// Get a cargo command that should be used to invoke the compilation.
182
- fn get_cargo_command ( ) -> CargoCommand {
175
+ /// Get a cargo command that compiles with nightly
176
+ fn get_nightly_cargo ( ) -> CargoCommand {
183
177
let env_cargo =
184
178
CargoCommand :: new ( & env:: var ( "CARGO" ) . expect ( "`CARGO` env variable is always set by cargo" ) ) ;
185
179
let default_cargo = CargoCommand :: new ( "cargo" ) ;
180
+ let rustup_run_nightly = CargoCommand :: new_with_args ( "rustup" , & [ "run" , "nightly" , "cargo" ] ) ;
186
181
let wasm_toolchain = env:: var ( WASM_BUILD_TOOLCHAIN ) . ok ( ) ;
187
182
188
183
// First check if the user requested a specific toolchain
189
- if let Some ( cmd) =
190
- wasm_toolchain. map ( |t| CargoCommand :: new_with_args ( "rustup" , & [ "run" , & t, "cargo" ] ) )
191
- {
184
+ if let Some ( cmd) = wasm_toolchain. and_then ( |t| get_rustup_nightly ( Some ( t) ) ) {
192
185
cmd
193
- } else if env_cargo. supports_substrate_wasm_env ( ) {
186
+ } else if env_cargo. is_nightly ( ) {
194
187
env_cargo
195
- } else if default_cargo. supports_substrate_wasm_env ( ) {
188
+ } else if default_cargo. is_nightly ( ) {
196
189
default_cargo
190
+ } else if rustup_run_nightly. is_nightly ( ) {
191
+ rustup_run_nightly
197
192
} else {
198
- // If no command before provided us with a cargo that supports our Substrate wasm env, we
199
- // try to search one with rustup. If that fails as well, we return the default cargo and let
200
- // the prequisities check fail.
201
- get_rustup_command ( ) . unwrap_or ( default_cargo)
193
+ // If no command before provided us with a nightly compiler, we try to search one
194
+ // with rustup. If that fails as well, we return the default cargo and let the prequisities
195
+ // check fail.
196
+ get_rustup_nightly ( None ) . unwrap_or ( default_cargo)
202
197
}
203
198
}
204
199
205
- /// Get the newest rustup command that supports our Substrate wasm env.
206
- ///
207
- /// Stable versions are always favored over nightly versions even if the nightly versions are
208
- /// newer.
209
- fn get_rustup_command ( ) -> Option < CargoCommand > {
200
+ /// Get a nightly from rustup. If `selected` is `Some(_)`, a `CargoCommand` using the given
201
+ /// nightly is returned.
202
+ fn get_rustup_nightly ( selected : Option < String > ) -> Option < CargoCommand > {
210
203
let host = format ! ( "-{}" , env:: var( "HOST" ) . expect( "`HOST` is always set by cargo" ) ) ;
211
204
212
- let output = Command :: new ( "rustup" ) . args ( & [ "toolchain" , "list" ] ) . output ( ) . ok ( ) ?. stdout ;
213
- let lines = output. as_slice ( ) . lines ( ) ;
214
-
215
- let mut versions = Vec :: new ( ) ;
216
- for line in lines. filter_map ( |l| l. ok ( ) ) {
217
- let rustup_version = line. trim_end_matches ( & host) ;
218
-
219
- let cmd = CargoCommand :: new_with_args ( "rustup" , & [ "run" , & rustup_version, "cargo" ] ) ;
205
+ let version = match selected {
206
+ Some ( selected) => selected,
207
+ None => {
208
+ let output = Command :: new ( "rustup" ) . args ( & [ "toolchain" , "list" ] ) . output ( ) . ok ( ) ?. stdout ;
209
+ let lines = output. as_slice ( ) . lines ( ) ;
220
210
221
- if !cmd. supports_substrate_wasm_env ( ) {
222
- continue
223
- }
224
-
225
- let Some ( cargo_version) = cmd. version ( ) else { continue ; } ;
211
+ let mut latest_nightly = None ;
212
+ for line in lines. filter_map ( |l| l. ok ( ) ) {
213
+ if line. starts_with ( "nightly-" ) && line. ends_with ( & host) {
214
+ // Rustup prints them sorted
215
+ latest_nightly = Some ( line. clone ( ) ) ;
216
+ }
217
+ }
226
218
227
- versions. push ( ( cargo_version, rustup_version. to_string ( ) ) ) ;
228
- }
229
-
230
- // Sort by the parsed version to get the latest version (greatest version) at the end of the
231
- // vec.
232
- versions. sort_by_key ( |v| v. 0 ) ;
233
- let version = & versions. last ( ) ?. 1 ;
219
+ latest_nightly?. trim_end_matches ( & host) . into ( )
220
+ } ,
221
+ } ;
234
222
235
223
Some ( CargoCommand :: new_with_args ( "rustup" , & [ "run" , & version, "cargo" ] ) )
236
224
}
@@ -240,23 +228,17 @@ fn get_rustup_command() -> Option<CargoCommand> {
240
228
struct CargoCommand {
241
229
program : String ,
242
230
args : Vec < String > ,
243
- version : Option < Version > ,
244
231
}
245
232
246
233
impl CargoCommand {
247
234
fn new ( program : & str ) -> Self {
248
- let version = Self :: extract_version ( program, & [ ] ) ;
249
-
250
- CargoCommand { program : program. into ( ) , args : Vec :: new ( ) , version }
235
+ CargoCommand { program : program. into ( ) , args : Vec :: new ( ) }
251
236
}
252
237
253
238
fn new_with_args ( program : & str , args : & [ & str ] ) -> Self {
254
- let version = Self :: extract_version ( program, args) ;
255
-
256
239
CargoCommand {
257
240
program : program. into ( ) ,
258
241
args : args. iter ( ) . map ( ToString :: to_string) . collect ( ) ,
259
- version,
260
242
}
261
243
}
262
244
@@ -266,40 +248,20 @@ impl CargoCommand {
266
248
cmd
267
249
}
268
250
269
- fn extract_version ( program : & str , args : & [ & str ] ) -> Option < Version > {
270
- let version = Command :: new ( program)
271
- . args ( args)
272
- . arg ( "--version" )
273
- . output ( )
274
- . ok ( )
275
- . and_then ( |o| String :: from_utf8 ( o. stdout ) . ok ( ) ) ?;
276
-
277
- Version :: extract ( & version)
278
- }
279
-
280
- /// Returns the version of this cargo command or `None` if it failed to extract the version.
281
- fn version ( & self ) -> Option < Version > {
282
- self . version
283
- }
284
-
285
- /// Check if the supplied cargo command supports our Substrate wasm environment.
286
- ///
287
- /// This means that either the cargo version is at minimum 1.68.0 or this is a nightly cargo.
288
- ///
289
- /// Assumes that cargo version matches the rustc version.
290
- fn supports_substrate_wasm_env ( & self ) -> bool {
251
+ /// Check if the supplied cargo command is a nightly version
252
+ fn is_nightly ( & self ) -> bool {
291
253
// `RUSTC_BOOTSTRAP` tells a stable compiler to behave like a nightly. So, when this env
292
254
// variable is set, we can assume that whatever rust compiler we have, it is a nightly
293
255
// compiler. For "more" information, see:
294
256
// https://github.com/rust-lang/rust/blob/fa0f7d0080d8e7e9eb20aa9cbf8013f96c81287f/src/libsyntax/feature_gate/check.rs#L891
295
- if env:: var ( "RUSTC_BOOTSTRAP" ) . is_ok ( ) {
296
- return true
297
- }
298
-
299
- let Some ( version ) = self . version ( ) else { return false } ;
300
-
301
- // Check if major and minor are greater or equal than 1.68 or this is a nightly.
302
- version . major > 1 || ( version . major == 1 && version . minor >= 68 ) || version . is_nightly
257
+ env:: var ( "RUSTC_BOOTSTRAP" ) . is_ok ( ) ||
258
+ self . command ( )
259
+ . arg ( "--version" )
260
+ . output ( )
261
+ . map_err ( |_| ( ) )
262
+ . and_then ( |o| String :: from_utf8 ( o . stdout ) . map_err ( |_| ( ) ) )
263
+ . unwrap_or_default ( )
264
+ . contains ( "-nightly" )
303
265
}
304
266
}
305
267
0 commit comments