@@ -9,54 +9,40 @@ use crate::core::resolver::HasDevUnits;
99use crate :: core:: { PackageId , PackageSet , Resolve , Workspace } ;
1010use crate :: ops:: { self , Packages } ;
1111use crate :: util:: errors:: CargoResult ;
12- use crate :: GlobalContext ;
12+
1313use std:: collections:: { HashMap , HashSet } ;
1414use std:: path:: PathBuf ;
1515
1616use super :: BuildConfig ;
1717
18- /// Parse the `-Zbuild-std` flag.
19- pub fn parse_unstable_flag ( value : Option < & str > ) -> Vec < String > {
18+ fn std_crates < ' a > ( crates : & ' a [ String ] , units : Option < & [ Unit ] > ) -> HashSet < & ' a str > {
19+ let mut crates = HashSet :: from_iter ( crates . iter ( ) . map ( |s| s . as_str ( ) ) ) ;
2020 // This is a temporary hack until there is a more principled way to
2121 // declare dependencies in Cargo.toml.
22- let value = value. unwrap_or ( "std" ) ;
23- let mut crates: HashSet < & str > = value. split ( ',' ) . collect ( ) ;
22+ if crates. is_empty ( ) {
23+ crates. insert ( "std" ) ;
24+ }
2425 if crates. contains ( "std" ) {
2526 crates. insert ( "core" ) ;
2627 crates. insert ( "alloc" ) ;
2728 crates. insert ( "proc_macro" ) ;
2829 crates. insert ( "panic_unwind" ) ;
2930 crates. insert ( "compiler_builtins" ) ;
30- } else if crates. contains ( "core" ) {
31- crates. insert ( "compiler_builtins" ) ;
32- }
33- crates. into_iter ( ) . map ( |s| s. to_string ( ) ) . collect ( )
34- }
35-
36- pub ( crate ) fn std_crates ( gctx : & GlobalContext , units : Option < & [ Unit ] > ) -> Option < Vec < String > > {
37- let crates = gctx. cli_unstable ( ) . build_std . as_ref ( ) ?. clone ( ) ;
38-
39- // Only build libtest if it looks like it is needed.
40- let mut crates = crates. clone ( ) ;
41- // If we know what units we're building, we can filter for libtest depending on the jobs.
42- if let Some ( units) = units {
43- if units
44- . iter ( )
45- . any ( |unit| unit. mode . is_rustc_test ( ) && unit. target . harness ( ) )
46- {
47- // Only build libtest when libstd is built (libtest depends on libstd)
48- if crates. iter ( ) . any ( |c| c == "std" ) && !crates. iter ( ) . any ( |c| c == "test" ) {
49- crates. push ( "test" . to_string ( ) ) ;
31+ // Only build libtest if it looks like it is needed (libtest depends on libstd)
32+ // If we know what units we're building, we can filter for libtest depending on the jobs.
33+ if let Some ( units) = units {
34+ if units
35+ . iter ( )
36+ . any ( |unit| unit. mode . is_rustc_test ( ) && unit. target . harness ( ) )
37+ {
38+ crates. insert ( "test" ) ;
5039 }
5140 }
52- } else {
53- // We don't know what jobs are going to be run, so download libtest just in case.
54- if !crates. iter ( ) . any ( |c| c == "test" ) {
55- crates. push ( "test" . to_string ( ) )
56- }
41+ } else if crates. contains ( "core" ) {
42+ crates. insert ( "compiler_builtins" ) ;
5743 }
5844
59- Some ( crates)
45+ crates
6046}
6147
6248/// Resolve the standard library dependencies.
@@ -66,14 +52,16 @@ pub fn resolve_std<'gctx>(
6652 build_config : & BuildConfig ,
6753 crates : & [ String ] ,
6854) -> CargoResult < ( PackageSet < ' gctx > , Resolve , ResolvedFeatures ) > {
55+ let crates = std_crates ( crates, None ) ;
56+
6957 if build_config. build_plan {
7058 ws. gctx ( )
7159 . shell ( )
7260 . warn ( "-Zbuild-std does not currently fully support --build-plan" ) ?;
7361 }
7462
7563 // check that targets support building std
76- if crates. contains ( & "std" . to_string ( ) ) {
64+ if crates. contains ( "std" ) {
7765 let unsupported_targets = target_data. get_unsupported_std_targets ( ) ;
7866 if !unsupported_targets. is_empty ( ) {
7967 anyhow:: bail!(
@@ -95,7 +83,7 @@ pub fn resolve_std<'gctx>(
9583 std_ws. set_require_optional_deps ( false ) ;
9684 // `sysroot` is not in the default set because it is optional, but it needs
9785 // to be part of the resolve in case we do need it or `libtest`.
98- let mut spec_pkgs = Vec :: from ( crates) ;
86+ let mut spec_pkgs: Vec < String > = crates. iter ( ) . map ( |s| s . to_string ( ) ) . collect ( ) ;
9987 spec_pkgs. push ( "sysroot" . to_string ( ) ) ;
10088 let spec = Packages :: Packages ( spec_pkgs) ;
10189 let specs = spec. to_package_id_specs ( & std_ws) ?;
@@ -128,11 +116,13 @@ pub fn resolve_std<'gctx>(
128116 ) )
129117}
130118
131- /// Generate a list of root `Unit`s for the standard library.
119+ /// Generates a map of root units for the standard library for each kind requested .
132120///
133- /// The given slice of crate names is the root set.
121+ /// * `crates` is the arg value from `-Zbuild-std`.
122+ /// * `units` is the root units of the build.
134123pub fn generate_std_roots (
135124 crates : & [ String ] ,
125+ units : & [ Unit ] ,
136126 std_resolve : & Resolve ,
137127 std_features : & ResolvedFeatures ,
138128 kinds : & [ CompileKind ] ,
@@ -141,8 +131,7 @@ pub fn generate_std_roots(
141131 profiles : & Profiles ,
142132 target_data : & RustcTargetData < ' _ > ,
143133) -> CargoResult < HashMap < CompileKind , Vec < Unit > > > {
144- // Generate the root Units for the standard library.
145- let std_ids = crates
134+ let std_ids = std_crates ( crates, Some ( units) )
146135 . iter ( )
147136 . map ( |crate_name| std_resolve. query ( crate_name) )
148137 . collect :: < CargoResult < Vec < PackageId > > > ( ) ?;
0 commit comments