1- use std:: collections:: { BTreeSet , HashMap , HashSet } ;
1+ use std:: collections:: { BTreeSet , HashMap } ;
22use std:: env;
33use std:: ffi:: { OsStr , OsString } ;
44use std:: path:: PathBuf ;
@@ -7,9 +7,8 @@ use cargo_platform::CfgExpr;
77use semver:: Version ;
88
99use 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 } ;
1312use crate :: util:: { self , config, join_paths, process, CargoResult , Config , ProcessBuilder } ;
1413
1514/// Structure with enough information to run `rustdoc --test`.
@@ -22,20 +21,35 @@ pub struct Doctest {
2221 pub unstable_opts : bool ,
2322 /// The -Clinker value to use.
2423 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 > ,
2541}
2642
2743/// A structure returning the result of a compilation.
2844pub struct Compilation < ' cfg > {
2945 /// 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 > ,
3347
3448 /// An array of all binaries created.
35- pub binaries : Vec < ( Unit , PathBuf ) > ,
49+ pub binaries : Vec < UnitOutput > ,
3650
3751 /// An array of all cdylibs created.
38- pub cdylibs : Vec < ( Unit , PathBuf ) > ,
52+ pub cdylibs : Vec < UnitOutput > ,
3953
4054 /// All directories for the output of native build commands.
4155 ///
@@ -60,17 +74,14 @@ pub struct Compilation<'cfg> {
6074
6175 /// Extra environment variables that were passed to compilations and should
6276 /// 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 ) > > ,
6481
6582 /// Libraries to test with rustdoc.
6683 pub to_doc_test : Vec < Doctest > ,
6784
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-
7485 /// The target host triple.
7586 pub host : String ,
7687
@@ -127,8 +138,6 @@ impl<'cfg> Compilation<'cfg> {
127138 cdylibs : Vec :: new ( ) ,
128139 extra_env : HashMap :: new ( ) ,
129140 to_doc_test : Vec :: new ( ) ,
130- cfgs : HashMap :: new ( ) ,
131- rustdocflags : HashMap :: new ( ) ,
132141 config : bcx. config ,
133142 host : bcx. host_triple ( ) . to_string ( ) ,
134143 rustc_process : rustc,
@@ -144,7 +153,13 @@ impl<'cfg> Compilation<'cfg> {
144153 } )
145154 }
146155
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.
148163 pub fn rustc_process (
149164 & self ,
150165 unit : & Unit ,
@@ -160,14 +175,18 @@ impl<'cfg> Compilation<'cfg> {
160175 } ;
161176
162177 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 )
164179 }
165180
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 > {
168187 let rustdoc = process ( & * self . config . rustdoc ( ) ?) ;
169188 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 ) ?;
171190 if unit. target . edition ( ) != Edition :: Edition2015 {
172191 p. arg ( format ! ( "--edition={}" , unit. target. edition( ) ) ) ;
173192 }
@@ -179,25 +198,37 @@ impl<'cfg> Compilation<'cfg> {
179198 Ok ( p)
180199 }
181200
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!
183207 pub fn host_process < T : AsRef < OsStr > > (
184208 & self ,
185209 cmd : T ,
186210 pkg : & Package ,
187211 ) -> CargoResult < ProcessBuilder > {
188- self . fill_env ( process ( cmd) , pkg, CompileKind :: Host , false )
212+ self . fill_env ( process ( cmd) , pkg, None , CompileKind :: Host , false )
189213 }
190214
191215 pub fn target_runner ( & self , kind : CompileKind ) -> Option < & ( PathBuf , Vec < String > ) > {
192216 self . target_runners . get ( & kind) . and_then ( |x| x. as_ref ( ) )
193217 }
194218
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.
196226 pub fn target_process < T : AsRef < OsStr > > (
197227 & self ,
198228 cmd : T ,
199229 kind : CompileKind ,
200230 pkg : & Package ,
231+ script_meta : Option < Metadata > ,
201232 ) -> CargoResult < ProcessBuilder > {
202233 let builder = if let Some ( ( runner, args) ) = self . target_runner ( kind) {
203234 let mut builder = process ( runner) ;
@@ -207,7 +238,7 @@ impl<'cfg> Compilation<'cfg> {
207238 } else {
208239 process ( cmd)
209240 } ;
210- self . fill_env ( builder, pkg, kind, false )
241+ self . fill_env ( builder, pkg, script_meta , kind, false )
211242 }
212243
213244 /// Prepares a new process with an appropriate environment to run against
@@ -219,6 +250,7 @@ impl<'cfg> Compilation<'cfg> {
219250 & self ,
220251 mut cmd : ProcessBuilder ,
221252 pkg : & Package ,
253+ script_meta : Option < Metadata > ,
222254 kind : CompileKind ,
223255 is_rustc_tool : bool ,
224256 ) -> CargoResult < ProcessBuilder > {
@@ -258,9 +290,11 @@ impl<'cfg> Compilation<'cfg> {
258290 let search_path = join_paths ( & search_path, util:: dylib_path_envvar ( ) ) ?;
259291
260292 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+ }
264298 }
265299 }
266300
0 commit comments