@@ -10,28 +10,27 @@ use std::fmt;
1010use std:: path:: PathBuf ;
1111use std:: process:: Command ;
1212
13- struct Wasm32Check {
13+ struct Wasm32Check < ' target > {
14+ target : & ' target str ,
1415 rustc_path : PathBuf ,
1516 sysroot : PathBuf ,
1617 found : bool ,
1718 is_rustup : bool ,
1819}
1920
20- impl fmt:: Display for Wasm32Check {
21+ impl fmt:: Display for Wasm32Check < ' _ > {
2122 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
22- let target = "wasm32-unknown-unknown" ;
23-
2423 if !self . found {
2524 let rustup_string = if self . is_rustup {
2625 "It looks like Rustup is being used." . to_owned ( )
2726 } else {
28- format ! ( "It looks like Rustup is not being used. For non-Rustup setups, the {} target needs to be installed manually. See https://drager.github.io/wasm-pack/book/prerequisites/non-rustup-setups.html on how to do this." , target)
27+ format ! ( "It looks like Rustup is not being used. For non-Rustup setups, the {} target needs to be installed manually. See https://drager.github.io/wasm-pack/book/prerequisites/non-rustup-setups.html on how to do this." , self . target)
2928 } ;
3029
3130 writeln ! (
3231 f,
3332 "{} target not found in sysroot: {:?}" ,
34- target, self . sysroot
33+ self . target, self . sysroot
3534 )
3635 . and_then ( |_| {
3736 writeln ! (
@@ -51,14 +50,14 @@ impl fmt::Display for Wasm32Check {
5150 }
5251}
5352
54- /// Ensure that `rustup` has the `wasm32-unknown-unknown` target installed for
53+ /// Ensure that `rustup` has the requested target installed for
5554/// current toolchain
56- pub fn check_for_wasm32_target ( ) -> Result < ( ) > {
55+ pub fn check_for_wasm_target ( target : & str ) -> Result < ( ) > {
5756 let msg = format ! ( "{}Checking for the Wasm target..." , emoji:: TARGET ) ;
5857 PBAR . info ( & msg) ;
5958
6059 // Check if wasm32 target is present, otherwise bail.
61- match check_wasm32_target ( ) {
60+ match check_target ( target ) {
6261 Ok ( ref wasm32_check) if wasm32_check. found => Ok ( ( ) ) ,
6362 Ok ( wasm32_check) => bail ! ( "{}" , wasm32_check) ,
6463 Err ( err) => Err ( err) ,
@@ -81,43 +80,32 @@ fn get_rustc_sysroot() -> Result<PathBuf> {
8180 }
8281}
8382
84- /// Get wasm32-unknown-unknown target libdir
85- fn get_rustc_wasm32_unknown_unknown_target_libdir ( ) -> Result < PathBuf > {
83+ /// Get target libdir
84+ fn get_rustc_target_libdir ( target : & str ) -> Result < PathBuf > {
8685 let command = Command :: new ( "rustc" )
87- . args ( & [
88- "--target" ,
89- "wasm32-unknown-unknown" ,
90- "--print" ,
91- "target-libdir" ,
92- ] )
86+ . args ( & [ "--target" , target, "--print" , "target-libdir" ] )
9387 . output ( ) ?;
9488
9589 if command. status . success ( ) {
9690 Ok ( String :: from_utf8 ( command. stdout ) ?. trim ( ) . into ( ) )
9791 } else {
9892 Err ( anyhow ! (
99- "Getting rustc's wasm32-unknown-unknown target wasn't successful. Got {}" ,
93+ "Getting rustc's {target} target wasn't successful. Got {}" ,
10094 command. status
10195 ) )
10296 }
10397}
10498
105- fn does_wasm32_target_libdir_exist ( ) -> bool {
106- let result = get_rustc_wasm32_unknown_unknown_target_libdir ( ) ;
99+ fn does_target_libdir_exist ( target : & str ) -> bool {
100+ let result = get_rustc_target_libdir ( target ) ;
107101
108102 match result {
109- Ok ( wasm32_target_libdir_path) => {
110- if wasm32_target_libdir_path. exists ( ) {
111- info ! (
112- "Found wasm32-unknown-unknown in {:?}" ,
113- wasm32_target_libdir_path
114- ) ;
103+ Ok ( target_libdir_path) => {
104+ if target_libdir_path. exists ( ) {
105+ info ! ( "Found {target} in {:?}" , target_libdir_path) ;
115106 true
116107 } else {
117- info ! (
118- "Failed to find wasm32-unknown-unknown in {:?}" ,
119- wasm32_target_libdir_path
120- ) ;
108+ info ! ( "Failed to find {target} in {:?}" , target_libdir_path) ;
121109 false
122110 }
123111 }
@@ -128,12 +116,13 @@ fn does_wasm32_target_libdir_exist() -> bool {
128116 }
129117}
130118
131- fn check_wasm32_target ( ) -> Result < Wasm32Check > {
119+ fn check_target ( target : & ' _ str ) -> Result < Wasm32Check < ' _ > > {
132120 let sysroot = get_rustc_sysroot ( ) ?;
133121 let rustc_path = which:: which ( "rustc" ) ?;
134122
135- if does_wasm32_target_libdir_exist ( ) {
123+ if does_target_libdir_exist ( target ) {
136124 Ok ( Wasm32Check {
125+ target,
137126 rustc_path,
138127 sysroot,
139128 found : true ,
@@ -142,16 +131,18 @@ fn check_wasm32_target() -> Result<Wasm32Check> {
142131 // If it doesn't exist, then we need to check if we're using rustup.
143132 } else {
144133 // If sysroot contains "rustup", then we can assume we're using rustup
145- // and use rustup to add the wasm32-unknown-unknown target.
134+ // and use rustup to add the requested target.
146135 if sysroot. to_string_lossy ( ) . contains ( "rustup" ) {
147- rustup_add_wasm_target ( ) . map ( |( ) | Wasm32Check {
136+ rustup_add_wasm_target ( target) . map ( |( ) | Wasm32Check {
137+ target,
148138 rustc_path,
149139 sysroot,
150140 found : true ,
151141 is_rustup : true ,
152142 } )
153143 } else {
154144 Ok ( Wasm32Check {
145+ target,
155146 rustc_path,
156147 sysroot,
157148 found : false ,
@@ -161,11 +152,11 @@ fn check_wasm32_target() -> Result<Wasm32Check> {
161152 }
162153}
163154
164- /// Add wasm32-unknown-unknown using `rustup`.
165- fn rustup_add_wasm_target ( ) -> Result < ( ) > {
155+ /// Add target using `rustup`.
156+ fn rustup_add_wasm_target ( target : & str ) -> Result < ( ) > {
166157 let mut cmd = Command :: new ( "rustup" ) ;
167- cmd. arg ( "target" ) . arg ( "add" ) . arg ( "wasm32-unknown-unknown" ) ;
168- child:: run ( cmd, "rustup" ) . context ( "Adding the wasm32-unknown-unknown target with rustup" ) ?;
158+ cmd. arg ( "target" ) . arg ( "add" ) . arg ( target ) ;
159+ child:: run ( cmd, "rustup" ) . with_context ( || format ! ( "Adding the {target} target with rustup" ) ) ?;
169160
170161 Ok ( ( ) )
171162}
0 commit comments