@@ -14,6 +14,7 @@ use std::ffi::{OsStr, OsString};
1414use std:: fs;
1515use std:: path:: PathBuf ;
1616use std:: process:: Command ;
17+ use walkdir:: WalkDir ;
1718
1819use crate :: builder:: Kind ;
1920use crate :: core:: config:: Target ;
@@ -177,6 +178,32 @@ than building it.
177178 continue ;
178179 }
179180
181+ // Check if there exists a built-in target in the list of supported targets.
182+ let mut has_target = false ;
183+ let target_str = target. to_string ( ) ;
184+
185+ let supported_target_list =
186+ output ( & mut Command :: new ( & build. config . initial_rustc ) . args ( [ "--print" , "target-list" ] ) ) ;
187+
188+ has_target |= supported_target_list. contains ( & target_str) ;
189+
190+ // If not, check for a valid file location that may have been specified
191+ // by the user for the custom target.
192+ if let Some ( custom_target_path) = env:: var_os ( "RUST_TARGET_PATH" ) {
193+ let target_os_str = OsStr :: new ( & target_str) ;
194+ let walker = WalkDir :: new ( custom_target_path) . into_iter ( ) ;
195+ for entry in walker. filter_map ( |e| e. ok ( ) ) {
196+ has_target |= entry. file_name ( ) == target_os_str;
197+ }
198+ }
199+
200+ if !has_target && ![ "A" , "B" , "C" ] . contains ( & target_str. as_str ( ) ) {
201+ panic ! (
202+ "No such target exists in the target list,
203+ specify a correct location of the JSON specification file for custom targets!"
204+ ) ;
205+ }
206+
180207 if !build. config . dry_run ( ) {
181208 cmd_finder. must_have ( build. cc ( * target) ) ;
182209 if let Some ( ar) = build. ar ( * target) {
0 commit comments