1
- use std:: collections:: { BTreeSet , HashMap , HashSet } ;
1
+ use std:: collections:: { BTreeSet , HashMap } ;
2
2
use std:: env;
3
3
use std:: ffi:: { OsStr , OsString } ;
4
4
use std:: path:: PathBuf ;
@@ -7,9 +7,8 @@ use cargo_platform::CfgExpr;
7
7
use semver:: Version ;
8
8
9
9
use super :: BuildContext ;
10
- use crate :: core:: compiler:: CompileKind ;
11
- use crate :: core:: compiler:: Unit ;
12
- use crate :: core:: { Edition , Package , PackageId } ;
10
+ use crate :: core:: compiler:: { CompileKind , Metadata , Unit } ;
11
+ use crate :: core:: { Edition , Package } ;
13
12
use crate :: util:: { self , config, join_paths, process, CargoResult , Config , ProcessBuilder } ;
14
13
15
14
/// Structure with enough information to run `rustdoc --test`.
@@ -22,20 +21,35 @@ pub struct Doctest {
22
21
pub unstable_opts : bool ,
23
22
/// The -Clinker value to use.
24
23
pub linker : Option < PathBuf > ,
24
+ /// The script metadata, if this unit's package has a build script.
25
+ ///
26
+ /// This is used for indexing [`Compilation::extra_env`].
27
+ pub script_meta : Option < Metadata > ,
28
+ }
29
+
30
+ /// Information about the output of a unit.
31
+ #[ derive( Ord , PartialOrd , Eq , PartialEq ) ]
32
+ pub struct UnitOutput {
33
+ /// The unit that generated this output.
34
+ pub unit : Unit ,
35
+ /// Path to the unit's primary output (an executable or cdylib).
36
+ pub path : PathBuf ,
37
+ /// The script metadata, if this unit's package has a build script.
38
+ ///
39
+ /// This is used for indexing [`Compilation::extra_env`].
40
+ pub script_meta : Option < Metadata > ,
25
41
}
26
42
27
43
/// A structure returning the result of a compilation.
28
44
pub struct Compilation < ' cfg > {
29
45
/// An array of all tests created during this compilation.
30
- /// `(unit, path_to_test_exe)` where `unit` contains information such as the
31
- /// package, compile target, etc.
32
- pub tests : Vec < ( Unit , PathBuf ) > ,
46
+ pub tests : Vec < UnitOutput > ,
33
47
34
48
/// An array of all binaries created.
35
- pub binaries : Vec < ( Unit , PathBuf ) > ,
49
+ pub binaries : Vec < UnitOutput > ,
36
50
37
51
/// An array of all cdylibs created.
38
- pub cdylibs : Vec < ( Unit , PathBuf ) > ,
52
+ pub cdylibs : Vec < UnitOutput > ,
39
53
40
54
/// All directories for the output of native build commands.
41
55
///
@@ -60,17 +74,14 @@ pub struct Compilation<'cfg> {
60
74
61
75
/// Extra environment variables that were passed to compilations and should
62
76
/// be passed to future invocations of programs.
63
- pub extra_env : HashMap < PackageId , Vec < ( String , String ) > > ,
77
+ ///
78
+ /// The key is the build script metadata for uniquely identifying the
79
+ /// `RunCustomBuild` unit that generated these env vars.
80
+ pub extra_env : HashMap < Metadata , Vec < ( String , String ) > > ,
64
81
65
82
/// Libraries to test with rustdoc.
66
83
pub to_doc_test : Vec < Doctest > ,
67
84
68
- /// Features per package enabled during this compilation.
69
- pub cfgs : HashMap < PackageId , HashSet < String > > ,
70
-
71
- /// Flags to pass to rustdoc when invoked from cargo test, per package.
72
- pub rustdocflags : HashMap < PackageId , Vec < String > > ,
73
-
74
85
/// The target host triple.
75
86
pub host : String ,
76
87
@@ -127,8 +138,6 @@ impl<'cfg> Compilation<'cfg> {
127
138
cdylibs : Vec :: new ( ) ,
128
139
extra_env : HashMap :: new ( ) ,
129
140
to_doc_test : Vec :: new ( ) ,
130
- cfgs : HashMap :: new ( ) ,
131
- rustdocflags : HashMap :: new ( ) ,
132
141
config : bcx. config ,
133
142
host : bcx. host_triple ( ) . to_string ( ) ,
134
143
rustc_process : rustc,
@@ -144,7 +153,13 @@ impl<'cfg> Compilation<'cfg> {
144
153
} )
145
154
}
146
155
147
- /// See `process`.
156
+ /// Returns a [`ProcessBuilder`] for running `rustc`.
157
+ ///
158
+ /// `is_primary` is true if this is a "primary package", which means it
159
+ /// was selected by the user on the command-line (such as with a `-p`
160
+ /// flag), see [`crate::core::compiler::Context::primary_packages`].
161
+ ///
162
+ /// `is_workspace` is true if this is a workspace member.
148
163
pub fn rustc_process (
149
164
& self ,
150
165
unit : & Unit ,
@@ -160,14 +175,18 @@ impl<'cfg> Compilation<'cfg> {
160
175
} ;
161
176
162
177
let cmd = fill_rustc_tool_env ( rustc, unit) ;
163
- self . fill_env ( cmd, & unit. pkg , unit. kind , true )
178
+ self . fill_env ( cmd, & unit. pkg , None , unit. kind , true )
164
179
}
165
180
166
- /// See `process`.
167
- pub fn rustdoc_process ( & self , unit : & Unit ) -> CargoResult < ProcessBuilder > {
181
+ /// Returns a [`ProcessBuilder`] for running `rustdoc`.
182
+ pub fn rustdoc_process (
183
+ & self ,
184
+ unit : & Unit ,
185
+ script_meta : Option < Metadata > ,
186
+ ) -> CargoResult < ProcessBuilder > {
168
187
let rustdoc = process ( & * self . config . rustdoc ( ) ?) ;
169
188
let cmd = fill_rustc_tool_env ( rustdoc, unit) ;
170
- let mut p = self . fill_env ( cmd, & unit. pkg , unit. kind , true ) ?;
189
+ let mut p = self . fill_env ( cmd, & unit. pkg , script_meta , unit. kind , true ) ?;
171
190
if unit. target . edition ( ) != Edition :: Edition2015 {
172
191
p. arg ( format ! ( "--edition={}" , unit. target. edition( ) ) ) ;
173
192
}
@@ -179,25 +198,37 @@ impl<'cfg> Compilation<'cfg> {
179
198
Ok ( p)
180
199
}
181
200
182
- /// See `process`.
201
+ /// Returns a [`ProcessBuilder`] appropriate for running a process for the
202
+ /// host platform.
203
+ ///
204
+ /// This is currently only used for running build scripts. If you use this
205
+ /// for anything else, please be extra careful on how environment
206
+ /// variables are set!
183
207
pub fn host_process < T : AsRef < OsStr > > (
184
208
& self ,
185
209
cmd : T ,
186
210
pkg : & Package ,
187
211
) -> CargoResult < ProcessBuilder > {
188
- self . fill_env ( process ( cmd) , pkg, CompileKind :: Host , false )
212
+ self . fill_env ( process ( cmd) , pkg, None , CompileKind :: Host , false )
189
213
}
190
214
191
215
pub fn target_runner ( & self , kind : CompileKind ) -> Option < & ( PathBuf , Vec < String > ) > {
192
216
self . target_runners . get ( & kind) . and_then ( |x| x. as_ref ( ) )
193
217
}
194
218
195
- /// See `process`.
219
+ /// Returns a [`ProcessBuilder`] appropriate for running a process for the
220
+ /// target platform. This is typically used for `cargo run` and `cargo
221
+ /// test`.
222
+ ///
223
+ /// `script_meta` is the metadata for the `RunCustomBuild` unit that this
224
+ /// unit used for its build script. Use `None` if the package did not have
225
+ /// a build script.
196
226
pub fn target_process < T : AsRef < OsStr > > (
197
227
& self ,
198
228
cmd : T ,
199
229
kind : CompileKind ,
200
230
pkg : & Package ,
231
+ script_meta : Option < Metadata > ,
201
232
) -> CargoResult < ProcessBuilder > {
202
233
let builder = if let Some ( ( runner, args) ) = self . target_runner ( kind) {
203
234
let mut builder = process ( runner) ;
@@ -207,7 +238,7 @@ impl<'cfg> Compilation<'cfg> {
207
238
} else {
208
239
process ( cmd)
209
240
} ;
210
- self . fill_env ( builder, pkg, kind, false )
241
+ self . fill_env ( builder, pkg, script_meta , kind, false )
211
242
}
212
243
213
244
/// Prepares a new process with an appropriate environment to run against
@@ -219,6 +250,7 @@ impl<'cfg> Compilation<'cfg> {
219
250
& self ,
220
251
mut cmd : ProcessBuilder ,
221
252
pkg : & Package ,
253
+ script_meta : Option < Metadata > ,
222
254
kind : CompileKind ,
223
255
is_rustc_tool : bool ,
224
256
) -> CargoResult < ProcessBuilder > {
@@ -258,9 +290,11 @@ impl<'cfg> Compilation<'cfg> {
258
290
let search_path = join_paths ( & search_path, util:: dylib_path_envvar ( ) ) ?;
259
291
260
292
cmd. env ( util:: dylib_path_envvar ( ) , & search_path) ;
261
- if let Some ( env) = self . extra_env . get ( & pkg. package_id ( ) ) {
262
- for & ( ref k, ref v) in env {
263
- cmd. env ( k, v) ;
293
+ if let Some ( meta) = script_meta {
294
+ if let Some ( env) = self . extra_env . get ( & meta) {
295
+ for ( k, v) in env {
296
+ cmd. env ( k, v) ;
297
+ }
264
298
}
265
299
}
266
300
0 commit comments