@@ -18,7 +18,7 @@ use crate::{cfg_flag::CfgFlag, CargoConfig, CargoWorkspace, Package};
1818
1919#[ derive( Debug , Default , Clone , PartialEq , Eq ) ]
2020pub struct WorkspaceBuildScripts {
21- pub ( crate ) outputs : ArenaMap < Package , BuildScriptOutput > ,
21+ outputs : ArenaMap < Package , Option < BuildScriptOutput > > ,
2222 error : Option < String > ,
2323}
2424
@@ -72,6 +72,7 @@ impl WorkspaceBuildScripts {
7272
7373 cmd
7474 }
75+
7576 pub ( crate ) fn run (
7677 config : & CargoConfig ,
7778 workspace : & CargoWorkspace ,
@@ -91,13 +92,13 @@ impl WorkspaceBuildScripts {
9192 cmd. current_dir ( workspace. workspace_root ( ) ) ;
9293
9394 let mut res = WorkspaceBuildScripts :: default ( ) ;
95+ let outputs = & mut res. outputs ;
9496 // NB: Cargo.toml could have been modified between `cargo metadata` and
9597 // `cargo check`. We shouldn't assume that package ids we see here are
9698 // exactly those from `config`.
9799 let mut by_id: FxHashMap < String , Package > = FxHashMap :: default ( ) ;
98-
99100 for package in workspace. packages ( ) {
100- res . outputs . insert ( package, BuildScriptOutput :: default ( ) ) ;
101+ outputs. insert ( package, None ) ;
101102 by_id. insert ( workspace[ package] . id . clone ( ) , package) ;
102103 }
103104
@@ -141,7 +142,8 @@ impl WorkspaceBuildScripts {
141142 }
142143 acc
143144 } ;
144- let package_build_data = & mut res. outputs [ package] ;
145+ let package_build_data =
146+ outputs[ package] . get_or_insert_with ( Default :: default) ;
145147 // cargo_metadata crate returns default (empty) path for
146148 // older cargos, which is not absolute, so work around that.
147149 if !message. out_dir . as_str ( ) . is_empty ( ) {
@@ -167,7 +169,9 @@ impl WorkspaceBuildScripts {
167169 message. filenames . iter ( ) . find ( |name| is_dylib ( name) )
168170 {
169171 let filename = AbsPathBuf :: assert ( PathBuf :: from ( & filename) ) ;
170- res. outputs [ package] . proc_macro_dylib_path = Some ( filename) ;
172+ outputs[ package]
173+ . get_or_insert_with ( Default :: default)
174+ . proc_macro_dylib_path = Some ( filename) ;
171175 }
172176 }
173177 }
@@ -189,17 +193,18 @@ impl WorkspaceBuildScripts {
189193 ) ?;
190194
191195 for package in workspace. packages ( ) {
192- let package_build_data = & mut res. outputs [ package] ;
193- tracing:: info!(
194- "{} BuildScriptOutput: {:?}" ,
195- workspace[ package] . manifest. parent( ) . display( ) ,
196- package_build_data,
197- ) ;
198- // inject_cargo_env(package, package_build_data);
199- if let Some ( out_dir) = & package_build_data. out_dir {
200- // NOTE: cargo and rustc seem to hide non-UTF-8 strings from env! and option_env!()
201- if let Some ( out_dir) = out_dir. as_os_str ( ) . to_str ( ) . map ( |s| s. to_owned ( ) ) {
202- package_build_data. envs . push ( ( "OUT_DIR" . to_string ( ) , out_dir) ) ;
196+ if let Some ( package_build_data) = & mut outputs[ package] {
197+ tracing:: info!(
198+ "{} BuildScriptOutput: {:?}" ,
199+ workspace[ package] . manifest. parent( ) . display( ) ,
200+ package_build_data,
201+ ) ;
202+ // inject_cargo_env(package, package_build_data);
203+ if let Some ( out_dir) = & package_build_data. out_dir {
204+ // NOTE: cargo and rustc seem to hide non-UTF-8 strings from env! and option_env!()
205+ if let Some ( out_dir) = out_dir. as_os_str ( ) . to_str ( ) . map ( |s| s. to_owned ( ) ) {
206+ package_build_data. envs . push ( ( "OUT_DIR" . to_string ( ) , out_dir) ) ;
207+ }
203208 }
204209 }
205210 }
@@ -218,6 +223,10 @@ impl WorkspaceBuildScripts {
218223 pub fn error ( & self ) -> Option < & str > {
219224 self . error . as_deref ( )
220225 }
226+
227+ pub ( crate ) fn get_output ( & self , idx : Package ) -> Option < & BuildScriptOutput > {
228+ self . outputs . get ( idx) ?. as_ref ( )
229+ }
221230}
222231
223232// FIXME: File a better way to know if it is a dylib.
0 commit comments