@@ -18,7 +18,7 @@ use stdx::JodChild;
18
18
use crate :: { cfg_flag:: CfgFlag , CargoConfig } ;
19
19
20
20
#[ derive( Debug , Clone , Default , PartialEq , Eq ) ]
21
- pub ( crate ) struct BuildData {
21
+ pub ( crate ) struct PackageBuildData {
22
22
/// List of config flags defined by this package's build script
23
23
pub ( crate ) cfgs : Vec < CfgFlag > ,
24
24
/// List of cargo-related environment variables with their value
@@ -32,6 +32,16 @@ pub(crate) struct BuildData {
32
32
pub ( crate ) proc_macro_dylib_path : Option < AbsPathBuf > ,
33
33
}
34
34
35
+ #[ derive( Debug , Default , PartialEq , Eq , Clone ) ]
36
+ pub ( crate ) struct WorkspaceBuildData {
37
+ per_package : FxHashMap < String , PackageBuildData > ,
38
+ }
39
+
40
+ #[ derive( Debug , Default , PartialEq , Eq , Clone ) ]
41
+ pub struct BuildDataResult {
42
+ per_workspace : FxHashMap < AbsPathBuf , WorkspaceBuildData > ,
43
+ }
44
+
35
45
#[ derive( Clone , Debug ) ]
36
46
pub ( crate ) struct BuildDataConfig {
37
47
cargo_toml : AbsPathBuf ,
@@ -52,13 +62,6 @@ pub struct BuildDataCollector {
52
62
configs : FxHashMap < AbsPathBuf , BuildDataConfig > ,
53
63
}
54
64
55
- #[ derive( Debug , Default , PartialEq , Eq , Clone ) ]
56
- pub struct BuildDataResult {
57
- data : FxHashMap < AbsPathBuf , BuildDataMap > ,
58
- }
59
-
60
- pub ( crate ) type BuildDataMap = FxHashMap < String , BuildData > ;
61
-
62
65
impl BuildDataCollector {
63
66
pub ( crate ) fn add_config ( & mut self , workspace_root : & AbsPath , config : BuildDataConfig ) {
64
67
self . configs . insert ( workspace_root. to_path_buf ( ) , config) ;
@@ -67,7 +70,7 @@ impl BuildDataCollector {
67
70
pub fn collect ( & mut self , progress : & dyn Fn ( String ) ) -> Result < BuildDataResult > {
68
71
let mut res = BuildDataResult :: default ( ) ;
69
72
for ( path, config) in self . configs . iter ( ) {
70
- res. data . insert (
73
+ res. per_workspace . insert (
71
74
path. clone ( ) ,
72
75
collect_from_workspace (
73
76
& config. cargo_toml ,
@@ -81,9 +84,15 @@ impl BuildDataCollector {
81
84
}
82
85
}
83
86
87
+ impl WorkspaceBuildData {
88
+ pub ( crate ) fn get ( & self , package_id : & str ) -> Option < & PackageBuildData > {
89
+ self . per_package . get ( package_id)
90
+ }
91
+ }
92
+
84
93
impl BuildDataResult {
85
- pub ( crate ) fn get ( & self , root : & AbsPath ) -> Option < & BuildDataMap > {
86
- self . data . get ( & root . to_path_buf ( ) )
94
+ pub ( crate ) fn get ( & self , workspace_root : & AbsPath ) -> Option < & WorkspaceBuildData > {
95
+ self . per_workspace . get ( workspace_root )
87
96
}
88
97
}
89
98
@@ -102,7 +111,7 @@ fn collect_from_workspace(
102
111
cargo_features : & CargoConfig ,
103
112
packages : & Vec < cargo_metadata:: Package > ,
104
113
progress : & dyn Fn ( String ) ,
105
- ) -> Result < BuildDataMap > {
114
+ ) -> Result < WorkspaceBuildData > {
106
115
let mut cmd = Command :: new ( toolchain:: cargo ( ) ) ;
107
116
cmd. args ( & [ "check" , "--workspace" , "--message-format=json" , "--manifest-path" ] )
108
117
. arg ( cargo_toml. as_ref ( ) ) ;
@@ -136,7 +145,7 @@ fn collect_from_workspace(
136
145
let child_stdout = child. stdout . take ( ) . unwrap ( ) ;
137
146
let stdout = BufReader :: new ( child_stdout) ;
138
147
139
- let mut res = BuildDataMap :: default ( ) ;
148
+ let mut res = WorkspaceBuildData :: default ( ) ;
140
149
for message in cargo_metadata:: Message :: parse_stream ( stdout) . flatten ( ) {
141
150
match message {
142
151
Message :: BuildScriptExecuted ( BuildScript {
@@ -154,16 +163,17 @@ fn collect_from_workspace(
154
163
}
155
164
acc
156
165
} ;
157
- let res = res. entry ( package_id. repr . clone ( ) ) . or_default ( ) ;
166
+ let package_build_data =
167
+ res. per_package . entry ( package_id. repr . clone ( ) ) . or_default ( ) ;
158
168
// cargo_metadata crate returns default (empty) path for
159
169
// older cargos, which is not absolute, so work around that.
160
170
if !out_dir. as_str ( ) . is_empty ( ) {
161
171
let out_dir = AbsPathBuf :: assert ( PathBuf :: from ( out_dir. into_os_string ( ) ) ) ;
162
- res . out_dir = Some ( out_dir) ;
163
- res . cfgs = cfgs;
172
+ package_build_data . out_dir = Some ( out_dir) ;
173
+ package_build_data . cfgs = cfgs;
164
174
}
165
175
166
- res . envs = env;
176
+ package_build_data . envs = env;
167
177
}
168
178
Message :: CompilerArtifact ( message) => {
169
179
progress ( format ! ( "metadata {}" , message. target. name) ) ;
@@ -173,8 +183,9 @@ fn collect_from_workspace(
173
183
// Skip rmeta file
174
184
if let Some ( filename) = message. filenames . iter ( ) . find ( |name| is_dylib ( name) ) {
175
185
let filename = AbsPathBuf :: assert ( PathBuf :: from ( & filename) ) ;
176
- let res = res. entry ( package_id. repr . clone ( ) ) . or_default ( ) ;
177
- res. proc_macro_dylib_path = Some ( filename) ;
186
+ let package_build_data =
187
+ res. per_package . entry ( package_id. repr . clone ( ) ) . or_default ( ) ;
188
+ package_build_data. proc_macro_dylib_path = Some ( filename) ;
178
189
}
179
190
}
180
191
}
@@ -188,12 +199,12 @@ fn collect_from_workspace(
188
199
}
189
200
190
201
for package in packages {
191
- let build_data = res. entry ( package. id . repr . clone ( ) ) . or_default ( ) ;
192
- inject_cargo_env ( package, build_data ) ;
193
- if let Some ( out_dir) = & build_data . out_dir {
202
+ let package_build_data = res. per_package . entry ( package. id . repr . clone ( ) ) . or_default ( ) ;
203
+ inject_cargo_env ( package, package_build_data ) ;
204
+ if let Some ( out_dir) = & package_build_data . out_dir {
194
205
// NOTE: cargo and rustc seem to hide non-UTF-8 strings from env! and option_env!()
195
206
if let Some ( out_dir) = out_dir. to_str ( ) . map ( |s| s. to_owned ( ) ) {
196
- build_data . envs . push ( ( "OUT_DIR" . to_string ( ) , out_dir) ) ;
207
+ package_build_data . envs . push ( ( "OUT_DIR" . to_string ( ) , out_dir) ) ;
197
208
}
198
209
}
199
210
}
@@ -212,7 +223,7 @@ fn is_dylib(path: &Utf8Path) -> bool {
212
223
/// Recreates the compile-time environment variables that Cargo sets.
213
224
///
214
225
/// Should be synced with <https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates>
215
- fn inject_cargo_env ( package : & cargo_metadata:: Package , build_data : & mut BuildData ) {
226
+ fn inject_cargo_env ( package : & cargo_metadata:: Package , build_data : & mut PackageBuildData ) {
216
227
let env = & mut build_data. envs ;
217
228
218
229
// FIXME: Missing variables:
0 commit comments