@@ -97,61 +97,31 @@ impl MinimalConfig {
9797 config. src = src;
9898 }
9999
100- if cfg ! ( test) {
101- // Use the build directory of the original x.py invocation, so that we can set `initial_rustc` properly.
102- config. out = Path :: new (
103- & env:: var_os ( "CARGO_TARGET_DIR" ) . expect ( "cargo test directly is not supported" ) ,
104- )
105- . parent ( )
106- . unwrap ( )
107- . to_path_buf ( ) ;
108- }
100+ set_config_output_dir ( & mut config. out ) ;
101+
102+ let toml: TomlConfig =
103+ set_and_return_toml_config ( config. src . clone ( ) , config_flag, & mut config. config ) ;
109104
110- let toml = if let Some ( toml_path) = Self :: config_path ( config. src . clone ( ) , config_flag) {
111- config. config = Some ( toml_path. clone ( ) ) ;
112- get_toml ( & toml_path)
113- } else {
114- config. config = None ;
115- TomlConfig :: default ( )
116- } ;
117105 if let Some ( build) = toml. build . unwrap_or_default ( ) . build {
118106 config. build = TargetSelection :: from_user ( & build) ;
119107 }
120108
121- // NOTE: Bootstrap spawns various commands with different working directories.
122- // To avoid writing to random places on the file system, `config.out` needs to be an absolute path.
123- if !config. out . is_absolute ( ) {
124- // `canonicalize` requires the path to already exist. Use our vendored copy of `absolute` instead.
125- config. out = crate :: util:: absolute ( & config. out ) ;
126- }
127-
128109 if config. dry_run ( ) {
129110 let dir = config. out . join ( "tmp-dry-run" ) ;
130111 t ! ( fs:: create_dir_all( & dir) ) ;
131112 config. out = dir;
132113 }
114+ // NOTE: Bootstrap spawns various commands with different working directories.
115+ // To avoid writing to random places on the file system, `config.out` needs to be an absolute path.
116+ else if !config. out . is_absolute ( ) {
117+ // `canonicalize` requires the path to already exist. Use our vendored copy of `absolute` instead.
118+ config. out = crate :: util:: absolute ( & config. out ) ;
119+ }
133120
134- let stage0_json = t ! ( std:: fs:: read( & config. src. join( "src" ) . join( "stage0.json" ) ) ) ;
135- config. stage0_metadata = t ! ( serde_json:: from_slice:: <Stage0Metadata >( & stage0_json) ) ;
121+ config. stage0_metadata = deserialize_stage0_metadata ( & config. src ) ;
136122
137123 config
138124 }
139-
140- /// Read from `--config`, then `RUST_BOOTSTRAP_CONFIG`, then `./config.toml`, then `config.toml` in the root directory.
141- ///
142- /// Give a hard error if `--config` or `RUST_BOOTSTRAP_CONFIG` are set to a missing path,
143- /// but not if `config.toml` hasn't been created.
144- fn config_path ( src : PathBuf , config_flag : Option < PathBuf > ) -> Option < PathBuf > {
145- let toml_path =
146- config_flag. or_else ( || env:: var_os ( "RUST_BOOTSTRAP_CONFIG" ) . map ( PathBuf :: from) ) ;
147- let using_default_path = toml_path. is_none ( ) ;
148- let mut toml_path = toml_path. unwrap_or_else ( || PathBuf :: from ( "config.toml" ) ) ;
149- if using_default_path && !toml_path. exists ( ) {
150- toml_path = src. join ( toml_path) ;
151- }
152-
153- if !using_default_path || toml_path. exists ( ) { Some ( toml_path) } else { None }
154- }
155125}
156126
157127impl MinimalConfig {
@@ -235,10 +205,12 @@ impl MinimalConfig {
235205}
236206
237207#[ cfg( test) ]
208+ /// Shared helper function to be used in `MinimalConfig::parse` and `bootstrap::config::Config::parse`
238209pub ( crate ) fn get_toml < T : Deserialize < ' static > + Default > ( _file : & Path ) -> T {
239210 T :: default ( )
240211}
241212#[ cfg( not( test) ) ]
213+ /// Shared helper function to be used in `MinimalConfig::parse` and `bootstrap::config::Config::parse`
242214pub ( crate ) fn get_toml < T : Deserialize < ' static > + Default > ( file : & Path ) -> T {
243215 let contents =
244216 t ! ( fs:: read_to_string( file) , format!( "config file {} not found" , file. display( ) ) ) ;
@@ -253,6 +225,59 @@ pub(crate) fn get_toml<T: Deserialize<'static> + Default>(file: &Path) -> T {
253225 }
254226}
255227
228+ /// Shared helper function to be used in `MinimalConfig::parse` and `bootstrap::config::Config::parse`
229+ ///
230+ /// Use the build directory of the original x.py invocation, so that we can set `initial_rustc` properly.
231+ #[ allow( unused_variables) ]
232+ pub ( crate ) fn set_config_output_dir ( output_path : & mut PathBuf ) {
233+ #[ cfg( test) ]
234+ {
235+ * output_path = Path :: new (
236+ & env:: var_os ( "CARGO_TARGET_DIR" ) . expect ( "cargo test directly is not supported" ) ,
237+ )
238+ . parent ( )
239+ . unwrap ( )
240+ . to_path_buf ( ) ;
241+ }
242+ }
243+
244+ /// Shared helper function to be used in `MinimalConfig::parse` and `bootstrap::config::Config::parse`
245+ pub ( crate ) fn set_and_return_toml_config < T : Deserialize < ' static > + Default > (
246+ src : PathBuf ,
247+ config_flag : Option < PathBuf > ,
248+ cfg_path : & mut Option < PathBuf > ,
249+ ) -> T {
250+ /// Read from `--config`, then `RUST_BOOTSTRAP_CONFIG`, then `./config.toml`, then `config.toml` in the root directory.
251+ ///
252+ /// Give a hard error if `--config` or `RUST_BOOTSTRAP_CONFIG` are set to a missing path,
253+ /// but not if `config.toml` hasn't been created.
254+ fn config_path ( src : & PathBuf , config_flag : Option < PathBuf > ) -> Option < PathBuf > {
255+ let toml_path =
256+ config_flag. or_else ( || env:: var_os ( "RUST_BOOTSTRAP_CONFIG" ) . map ( PathBuf :: from) ) ;
257+ let using_default_path = toml_path. is_none ( ) ;
258+ let mut toml_path = toml_path. unwrap_or_else ( || PathBuf :: from ( "config.toml" ) ) ;
259+ if using_default_path && !toml_path. exists ( ) {
260+ toml_path = src. join ( toml_path) ;
261+ }
262+
263+ if !using_default_path || toml_path. exists ( ) { Some ( toml_path) } else { None }
264+ }
265+
266+ if let Some ( toml_path) = config_path ( & src, config_flag) {
267+ * cfg_path = Some ( toml_path. clone ( ) ) ;
268+ get_toml ( & toml_path)
269+ } else {
270+ * cfg_path = None ;
271+ T :: default ( )
272+ }
273+ }
274+
275+ /// Shared helper function to be used in `MinimalConfig::parse` and `bootstrap::config::Config::parse`
276+ pub ( crate ) fn deserialize_stage0_metadata ( stage0_metadata_path : & PathBuf ) -> Stage0Metadata {
277+ let stage0_json = t ! ( std:: fs:: read( stage0_metadata_path. join( "src" ) . join( "stage0.json" ) ) ) ;
278+ t ! ( serde_json:: from_slice:: <Stage0Metadata >( & stage0_json) )
279+ }
280+
256281fn src ( ) -> Option < PathBuf > {
257282 // Infer the source directory. This is non-trivial because we want to support a downloaded bootstrap binary,
258283 // running on a completely machine from where it was compiled.
0 commit comments