@@ -5,29 +5,41 @@ use crate::utils::{
55} ;
66
77use std:: fs;
8- use std:: path:: Path ;
8+ use std:: path:: { Path , PathBuf } ;
99
1010fn prepare_libcore (
1111 sysroot_path : & Path ,
1212 libgccjit12_patches : bool ,
1313 cross_compile : bool ,
14+ sysroot_source : Option < String > ,
1415) -> Result < ( ) , String > {
15- let rustc_path = match get_rustc_path ( ) {
16- Some ( path) => path,
17- None => return Err ( "`rustc` path not found" . to_string ( ) ) ,
18- } ;
16+ let rustlib_dir: PathBuf ;
1917
20- let parent = match rustc_path. parent ( ) {
21- Some ( path) => path,
22- None => return Err ( format ! ( "No parent for `{}`" , rustc_path. display( ) ) ) ,
23- } ;
18+ if let Some ( path) = sysroot_source {
19+ rustlib_dir = Path :: new ( & path)
20+ . canonicalize ( )
21+ . map_err ( |error| format ! ( "Failed to canonicalize path: {:?}" , error) ) ?;
22+ if !rustlib_dir. is_dir ( ) {
23+ return Err ( format ! ( "Custom sysroot path {:?} not found" , rustlib_dir) ) ;
24+ }
25+ } else {
26+ let rustc_path = match get_rustc_path ( ) {
27+ Some ( path) => path,
28+ None => return Err ( "`rustc` path not found" . to_string ( ) ) ,
29+ } ;
30+
31+ let parent = match rustc_path. parent ( ) {
32+ Some ( path) => path,
33+ None => return Err ( format ! ( "No parent for `{}`" , rustc_path. display( ) ) ) ,
34+ } ;
2435
25- let rustlib_dir = parent
26- . join ( "../lib/rustlib/src/rust" )
27- . canonicalize ( )
28- . map_err ( |error| format ! ( "Failed to canonicalize path: {:?}" , error) ) ?;
29- if !rustlib_dir. is_dir ( ) {
30- return Err ( "Please install `rust-src` component" . to_string ( ) ) ;
36+ rustlib_dir = parent
37+ . join ( "../lib/rustlib/src/rust" )
38+ . canonicalize ( )
39+ . map_err ( |error| format ! ( "Failed to canonicalize path: {:?}" , error) ) ?;
40+ if !rustlib_dir. is_dir ( ) {
41+ return Err ( "Please install `rust-src` component" . to_string ( ) ) ;
42+ }
3143 }
3244
3345 let sysroot_dir = sysroot_path. join ( "sysroot_src" ) ;
@@ -151,27 +163,39 @@ struct PrepareArg {
151163 cross_compile : bool ,
152164 only_libcore : bool ,
153165 libgccjit12_patches : bool ,
166+ sysroot_source : Option < String > ,
154167}
155168
156169impl PrepareArg {
157170 fn new ( ) -> Result < Option < Self > , String > {
158171 let mut only_libcore = false ;
159172 let mut cross_compile = false ;
160173 let mut libgccjit12_patches = false ;
174+ let mut sysroot_source = None ;
161175
162- for arg in std:: env:: args ( ) . skip ( 2 ) {
176+ let mut args = std:: env:: args ( ) . skip ( 2 ) ;
177+ while let Some ( arg) = args. next ( ) {
163178 match arg. as_str ( ) {
164179 "--only-libcore" => only_libcore = true ,
165180 "--cross" => cross_compile = true ,
166181 "--libgccjit12-patches" => libgccjit12_patches = true ,
182+ "--sysroot-source" => {
183+ if let Some ( path) = args. next ( ) {
184+ sysroot_source = Some ( path) ;
185+ } else {
186+ return Err (
187+ "Expected a value after `--sysroot-source`, found nothing" . to_string ( )
188+ ) ;
189+ }
190+ }
167191 "--help" => {
168192 Self :: usage ( ) ;
169193 return Ok ( None ) ;
170194 }
171195 a => return Err ( format ! ( "Unknown argument `{a}`" ) ) ,
172196 }
173197 }
174- Ok ( Some ( Self { cross_compile, only_libcore, libgccjit12_patches } ) )
198+ Ok ( Some ( Self { cross_compile, only_libcore, libgccjit12_patches, sysroot_source } ) )
175199 }
176200
177201 fn usage ( ) {
@@ -182,6 +206,7 @@ impl PrepareArg {
182206 --only-libcore : Only setup libcore and don't clone other repositories
183207 --cross : Apply the patches needed to do cross-compilation
184208 --libgccjit12-patches : Apply patches needed for libgccjit12
209+ --sysroot-source : Specify custom path for sysroot source
185210 --help : Show this help"#
186211 )
187212 }
@@ -193,7 +218,12 @@ pub fn run() -> Result<(), String> {
193218 None => return Ok ( ( ) ) ,
194219 } ;
195220 let sysroot_path = get_sysroot_dir ( ) ;
196- prepare_libcore ( & sysroot_path, args. libgccjit12_patches , args. cross_compile ) ?;
221+ prepare_libcore (
222+ & sysroot_path,
223+ args. libgccjit12_patches ,
224+ args. cross_compile ,
225+ args. sysroot_source ,
226+ ) ?;
197227
198228 if !args. only_libcore {
199229 cargo_install ( "hyperfine" ) ?;
0 commit comments