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