@@ -12,6 +12,7 @@ use crate::util::errors::CargoResult;
12
12
use crate :: util:: profile;
13
13
use anyhow:: { bail, Context as _} ;
14
14
use filetime:: FileTime ;
15
+ use itertools:: Itertools ;
15
16
use jobserver:: Client ;
16
17
17
18
use super :: build_plan:: BuildPlan ;
@@ -185,6 +186,32 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
185
186
plan. output_plan ( self . bcx . config ) ;
186
187
}
187
188
189
+ // Add `OUT_DIR` to env vars if unit has a build script.
190
+ let units_with_build_script = & self
191
+ . bcx
192
+ . roots
193
+ . iter ( )
194
+ . filter ( |unit| self . build_scripts . contains_key ( unit) )
195
+ . dedup_by ( |x, y| x. pkg . package_id ( ) == y. pkg . package_id ( ) )
196
+ . collect :: < Vec < _ > > ( ) ;
197
+ for unit in units_with_build_script {
198
+ for dep in & self . bcx . unit_graph [ unit] {
199
+ if dep. unit . mode . is_run_custom_build ( ) {
200
+ let out_dir = self
201
+ . files ( )
202
+ . build_script_out_dir ( & dep. unit )
203
+ . display ( )
204
+ . to_string ( ) ;
205
+ let script_meta = self . get_run_build_script_metadata ( & dep. unit ) ;
206
+ self . compilation
207
+ . extra_env
208
+ . entry ( script_meta)
209
+ . or_insert_with ( Vec :: new)
210
+ . push ( ( "OUT_DIR" . to_string ( ) , out_dir) ) ;
211
+ }
212
+ }
213
+ }
214
+
188
215
// Collect the result of the build into `self.compilation`.
189
216
for unit in & self . bcx . roots {
190
217
// Collect tests and executables.
@@ -213,26 +240,6 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
213
240
}
214
241
}
215
242
216
- // If the unit has a build script, add `OUT_DIR` to the
217
- // environment variables.
218
- if unit. target . is_lib ( ) {
219
- for dep in & self . bcx . unit_graph [ unit] {
220
- if dep. unit . mode . is_run_custom_build ( ) {
221
- let out_dir = self
222
- . files ( )
223
- . build_script_out_dir ( & dep. unit )
224
- . display ( )
225
- . to_string ( ) ;
226
- let script_meta = self . get_run_build_script_metadata ( & dep. unit ) ;
227
- self . compilation
228
- . extra_env
229
- . entry ( script_meta)
230
- . or_insert_with ( Vec :: new)
231
- . push ( ( "OUT_DIR" . to_string ( ) , out_dir) ) ;
232
- }
233
- }
234
- }
235
-
236
243
// Collect information for `rustdoc --test`.
237
244
if unit. mode . is_doc_test ( ) {
238
245
let mut unstable_opts = false ;
0 commit comments