Skip to content

Commit e0bd9e2

Browse files
committed
bcx reorg
1 parent 98b6f81 commit e0bd9e2

File tree

16 files changed

+306
-280
lines changed

16 files changed

+306
-280
lines changed

src/bin/cargo/commands/build.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
6565
)?;
6666

6767
if let Some(out_dir) = args.value_of_path("out-dir", config) {
68-
compile_opts.export_dir = Some(out_dir);
68+
compile_opts.build_config.export_dir = Some(out_dir);
6969
} else if let Some(out_dir) = config.build_config()?.out_dir.as_ref() {
7070
let out_dir = out_dir.resolve_path(config);
71-
compile_opts.export_dir = Some(out_dir);
71+
compile_opts.build_config.export_dir = Some(out_dir);
7272
}
73-
if compile_opts.export_dir.is_some() {
73+
if compile_opts.build_config.export_dir.is_some() {
7474
config
7575
.cli_unstable()
7676
.fail_if_stable_opt("--out-dir", 6790)?;

src/cargo/core/compiler/build_config.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::util::ProcessBuilder;
44
use crate::util::{CargoResult, Config, RustfixDiagnosticServer};
55
use serde::ser;
66
use std::cell::RefCell;
7+
use std::path::PathBuf;
78

89
/// Configuration information for a rustc build.
910
#[derive(Debug)]
@@ -26,7 +27,15 @@ pub struct BuildConfig {
2627
pub unit_graph: bool,
2728
/// An optional override of the rustc process for primary units
2829
pub primary_unit_rustc: Option<ProcessBuilder>,
30+
/// A thread used by `cargo fix` to receive messages on a socket regarding
31+
/// the success/failure of applying fixes.
2932
pub rustfix_diagnostic_server: RefCell<Option<RustfixDiagnosticServer>>,
33+
/// The directory to copy final artifacts to. Note that even if `out_dir` is
34+
/// set, a copy of artifacts still could be found a `target/(debug\release)`
35+
/// as usual.
36+
// Note that, although the cmd-line flag name is `out-dir`, in code we use
37+
// `export_dir`, to avoid confusion with out dir at `target/debug/deps`.
38+
pub export_dir: Option<PathBuf>,
3039
}
3140

3241
impl BuildConfig {
@@ -70,6 +79,7 @@ impl BuildConfig {
7079
unit_graph: false,
7180
primary_unit_rustc: None,
7281
rustfix_diagnostic_server: RefCell::new(None),
82+
export_dir: None,
7383
})
7484
}
7585

src/cargo/core/compiler/build_context/mod.rs

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::core::compiler::unit::UnitInterner;
2-
use crate::core::compiler::{BuildConfig, BuildOutput, CompileKind, Unit};
1+
use crate::core::compiler::unit_graph::UnitGraph;
2+
use crate::core::compiler::{BuildConfig, CompileKind, Unit};
33
use crate::core::profiles::Profiles;
44
use crate::core::{InternedString, Workspace};
55
use crate::core::{PackageId, PackageSet};
@@ -8,7 +8,6 @@ use crate::util::errors::CargoResult;
88
use crate::util::Rustc;
99
use std::collections::HashMap;
1010
use std::path::PathBuf;
11-
use std::str;
1211

1312
mod target_info;
1413
pub use self::target_info::{FileFlavor, RustcTargetData, TargetInfo};
@@ -29,35 +28,38 @@ pub struct BuildContext<'a, 'cfg> {
2928
/// Extra compiler args for either `rustc` or `rustdoc`.
3029
pub extra_compiler_args: HashMap<Unit<'a>, Vec<String>>,
3130
/// Package downloader.
32-
pub packages: &'a PackageSet<'cfg>,
33-
34-
/// Source of interning new units as they're created.
35-
pub units: &'a UnitInterner,
36-
31+
///
32+
/// This holds ownership of the `Package` objects.
33+
pub packages: PackageSet<'cfg>,
3734
/// Information about rustc and the target platform.
3835
pub target_data: RustcTargetData,
36+
/// The root units of `unit_graph` (units requested on the command-line).
37+
pub roots: Vec<Unit<'a>>,
38+
/// The dependency graph of units to compile.
39+
pub unit_graph: UnitGraph<'a>,
3940
}
4041

4142
impl<'a, 'cfg> BuildContext<'a, 'cfg> {
4243
pub fn new(
4344
ws: &'a Workspace<'cfg>,
44-
packages: &'a PackageSet<'cfg>,
45-
config: &'cfg Config,
45+
packages: PackageSet<'cfg>,
4646
build_config: &'a BuildConfig,
4747
profiles: Profiles,
48-
units: &'a UnitInterner,
4948
extra_compiler_args: HashMap<Unit<'a>, Vec<String>>,
5049
target_data: RustcTargetData,
50+
roots: Vec<Unit<'a>>,
51+
unit_graph: UnitGraph<'a>,
5152
) -> CargoResult<BuildContext<'a, 'cfg>> {
5253
Ok(BuildContext {
5354
ws,
55+
config: ws.config(),
5456
packages,
55-
config,
5657
build_config,
5758
profiles,
5859
extra_compiler_args,
59-
units,
6060
target_data,
61+
roots,
62+
unit_graph,
6163
})
6264
}
6365

@@ -104,15 +106,4 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
104106
pub fn extra_args_for(&self, unit: &Unit<'a>) -> Option<&Vec<String>> {
105107
self.extra_compiler_args.get(unit)
106108
}
107-
108-
/// If a build script is overridden, this returns the `BuildOutput` to use.
109-
///
110-
/// `lib_name` is the `links` library name and `kind` is whether it is for
111-
/// Host or Target.
112-
pub fn script_override(&self, lib_name: &str, kind: CompileKind) -> Option<&BuildOutput> {
113-
self.target_data
114-
.target_config(kind)
115-
.links_overrides
116-
.get(lib_name)
117-
}
118109
}

src/cargo/core/compiler/build_context/target_info.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use crate::core::compiler::CompileKind;
2-
use crate::core::compiler::CompileTarget;
1+
use crate::core::compiler::{BuildOutput, CompileKind, CompileTarget};
32
use crate::core::{Dependency, TargetKind, Workspace};
43
use crate::util::config::{Config, StringList, TargetConfig};
54
use crate::util::{CargoResult, CargoResultExt, ProcessBuilder, Rustc};
@@ -583,4 +582,12 @@ impl RustcTargetData {
583582
CompileKind::Target(s) => &self.target_config[&s],
584583
}
585584
}
585+
586+
/// If a build script is overridden, this returns the `BuildOutput` to use.
587+
///
588+
/// `lib_name` is the `links` library name and `kind` is whether it is for
589+
/// Host or Target.
590+
pub fn script_override(&self, lib_name: &str, kind: CompileKind) -> Option<&BuildOutput> {
591+
self.target_config(kind).links_overrides.get(lib_name)
592+
}
586593
}

src/cargo/core/compiler/compilation.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,7 @@ pub struct Compilation<'cfg> {
8787
}
8888

8989
impl<'cfg> Compilation<'cfg> {
90-
pub fn new<'a>(
91-
bcx: &BuildContext<'a, 'cfg>,
92-
default_kind: CompileKind,
93-
) -> CargoResult<Compilation<'cfg>> {
90+
pub fn new<'a>(bcx: &BuildContext<'a, 'cfg>) -> CargoResult<Compilation<'cfg>> {
9491
let mut rustc = bcx.rustc().process();
9592
let mut primary_rustc_process = bcx.build_config.primary_unit_rustc.clone();
9693
let mut rustc_workspace_wrapper_process = bcx.rustc().workspace_process();
@@ -104,6 +101,7 @@ impl<'cfg> Compilation<'cfg> {
104101
}
105102
}
106103

104+
let default_kind = bcx.build_config.requested_kind;
107105
Ok(Compilation {
108106
// TODO: deprecated; remove.
109107
native_dirs: BTreeSet::new(),

src/cargo/core/compiler/context/compilation_files.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,12 @@ impl OutputFile {
119119

120120
impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
121121
pub(super) fn new(
122-
roots: &[Unit<'a>],
122+
cx: &Context<'a, 'cfg>,
123123
host: Layout,
124124
target: HashMap<CompileTarget, Layout>,
125-
export_dir: Option<PathBuf>,
126-
ws: &'a Workspace<'cfg>,
127-
cx: &Context<'a, 'cfg>,
128125
) -> CompilationFiles<'a, 'cfg> {
129126
let mut metas = HashMap::new();
130-
for unit in roots {
127+
for unit in &cx.bcx.roots {
131128
metadata_of(unit, cx, &mut metas);
132129
}
133130
let outputs = metas
@@ -136,11 +133,11 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
136133
.map(|unit| (unit, LazyCell::new()))
137134
.collect();
138135
CompilationFiles {
139-
ws,
136+
ws: &cx.bcx.ws,
140137
host,
141138
target,
142-
export_dir,
143-
roots: roots.to_vec(),
139+
export_dir: cx.bcx.build_config.export_dir.clone(),
140+
roots: cx.bcx.roots.clone(),
144141
metas,
145142
outputs,
146143
}

src/cargo/core/compiler/context/mod.rs

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ use jobserver::Client;
1010
use crate::core::compiler::{self, compilation, Unit};
1111
use crate::core::PackageId;
1212
use crate::util::errors::{CargoResult, CargoResultExt};
13-
use crate::util::{profile, Config};
13+
use crate::util::profile;
1414

1515
use super::build_plan::BuildPlan;
1616
use super::custom_build::{self, BuildDeps, BuildScriptOutputs, BuildScripts};
1717
use super::fingerprint::Fingerprint;
1818
use super::job_queue::JobQueue;
1919
use super::layout::Layout;
20-
use super::unit_graph::{UnitDep, UnitGraph};
20+
use super::unit_graph::UnitDep;
2121
use super::{BuildContext, Compilation, CompileKind, CompileMode, Executor, FileFlavor};
2222

2323
mod compilation_files;
@@ -53,8 +53,6 @@ pub struct Context<'a, 'cfg> {
5353
/// with `-p` flags. If no flags are specified, then it is the defaults
5454
/// based on the current directory and the default workspace members.
5555
primary_packages: HashSet<PackageId>,
56-
/// The dependency graph of units to compile.
57-
unit_dependencies: UnitGraph<'a>,
5856
/// An abstraction of the files and directories that will be generated by
5957
/// the compilation. This is `None` until after `unit_dependencies` has
6058
/// been computed.
@@ -78,12 +76,7 @@ pub struct Context<'a, 'cfg> {
7876
}
7977

8078
impl<'a, 'cfg> Context<'a, 'cfg> {
81-
pub fn new(
82-
config: &'cfg Config,
83-
bcx: &'a BuildContext<'a, 'cfg>,
84-
unit_dependencies: UnitGraph<'a>,
85-
default_kind: CompileKind,
86-
) -> CargoResult<Self> {
79+
pub fn new(bcx: &'a BuildContext<'a, 'cfg>) -> CargoResult<Self> {
8780
// Load up the jobserver that we'll use to manage our parallelism. This
8881
// is the same as the GNU make implementation of a jobserver, and
8982
// intentionally so! It's hoped that we can interact with GNU make and
@@ -92,7 +85,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
9285
// Note that if we don't have a jobserver in our environment then we
9386
// create our own, and we create it with `n` tokens, but immediately
9487
// acquire one, because one token is ourself, a running process.
95-
let jobserver = match config.jobserver_from_env() {
88+
let jobserver = match bcx.config.jobserver_from_env() {
9689
Some(c) => c.clone(),
9790
None => {
9891
let client = Client::new(bcx.build_config.jobs as usize)
@@ -106,7 +99,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
10699

107100
Ok(Self {
108101
bcx,
109-
compilation: Compilation::new(bcx, default_kind)?,
102+
compilation: Compilation::new(bcx)?,
110103
build_script_outputs: Arc::new(Mutex::new(BuildScriptOutputs::default())),
111104
fingerprints: HashMap::new(),
112105
mtime_cache: HashMap::new(),
@@ -115,7 +108,6 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
115108
build_explicit_deps: HashMap::new(),
116109
jobserver,
117110
primary_packages: HashSet::new(),
118-
unit_dependencies,
119111
files: None,
120112
rmeta_required: HashSet::new(),
121113
rustc_clients: HashMap::new(),
@@ -125,21 +117,16 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
125117

126118
/// Starts compilation, waits for it to finish, and returns information
127119
/// about the result of compilation.
128-
pub fn compile(
129-
mut self,
130-
units: &[Unit<'a>],
131-
export_dir: Option<PathBuf>,
132-
exec: &Arc<dyn Executor>,
133-
) -> CargoResult<Compilation<'cfg>> {
134-
let mut queue = JobQueue::new(self.bcx, units);
120+
pub fn compile(mut self, exec: &Arc<dyn Executor>) -> CargoResult<Compilation<'cfg>> {
121+
let mut queue = JobQueue::new(self.bcx);
135122
let mut plan = BuildPlan::new();
136123
let build_plan = self.bcx.build_config.build_plan;
137-
self.prepare_units(export_dir, units)?;
124+
self.prepare_units()?;
138125
self.prepare()?;
139-
custom_build::build_map(&mut self, units)?;
126+
custom_build::build_map(&mut self)?;
140127
self.check_collistions()?;
141128

142-
for unit in units.iter() {
129+
for unit in &self.bcx.roots {
143130
// Build up a list of pending jobs, each of which represent
144131
// compiling a particular package. No actual work is executed as
145132
// part of this, that's all done next as part of the `execute`
@@ -168,7 +155,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
168155
}
169156

170157
// Collect the result of the build into `self.compilation`.
171-
for unit in units.iter() {
158+
for unit in &self.bcx.roots {
172159
// Collect tests and executables.
173160
for output in self.outputs(unit)?.iter() {
174161
if output.flavor == FileFlavor::DebugInfo || output.flavor == FileFlavor::Auxiliary
@@ -192,7 +179,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
192179
// If the unit has a build script, add `OUT_DIR` to the
193180
// environment variables.
194181
if unit.target.is_lib() {
195-
for dep in &self.unit_dependencies[unit] {
182+
for dep in &self.bcx.unit_graph[unit] {
196183
if dep.unit.mode.is_run_custom_build() {
197184
let out_dir = self
198185
.files()
@@ -283,11 +270,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
283270
Ok(None)
284271
}
285272

286-
pub fn prepare_units(
287-
&mut self,
288-
export_dir: Option<PathBuf>,
289-
units: &[Unit<'a>],
290-
) -> CargoResult<()> {
273+
pub fn prepare_units(&mut self) -> CargoResult<()> {
291274
let dest = self.bcx.profiles.get_dir_name();
292275
let host_layout = Layout::new(self.bcx.ws, None, &dest)?;
293276
let mut targets = HashMap::new();
@@ -296,12 +279,11 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
296279
targets.insert(target, layout);
297280
}
298281
self.primary_packages
299-
.extend(units.iter().map(|u| u.pkg.package_id()));
282+
.extend(self.bcx.roots.iter().map(|u| u.pkg.package_id()));
300283

301284
self.record_units_requiring_metadata();
302285

303-
let files =
304-
CompilationFiles::new(units, host_layout, targets, export_dir, self.bcx.ws, self);
286+
let files = CompilationFiles::new(self, host_layout, targets);
305287
self.files = Some(files);
306288
Ok(())
307289
}
@@ -345,7 +327,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
345327

346328
/// Direct dependencies for the given unit.
347329
pub fn unit_deps(&self, unit: &Unit<'a>) -> &[UnitDep<'a>] {
348-
&self.unit_dependencies[unit]
330+
&self.bcx.unit_graph[unit]
349331
}
350332

351333
/// Returns the RunCustomBuild Unit associated with the given Unit.
@@ -355,7 +337,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
355337
if unit.mode.is_run_custom_build() {
356338
return Some(unit);
357339
}
358-
self.unit_dependencies[&unit]
340+
self.bcx.unit_graph[&unit]
359341
.iter()
360342
.find(|unit_dep| {
361343
unit_dep.unit.mode.is_run_custom_build()
@@ -391,7 +373,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
391373
// Keep sorted for consistency.
392374
let mut inputs = BTreeSet::new();
393375
// Note: dev-deps are skipped if they are not present in the unit graph.
394-
for unit in self.unit_dependencies.keys() {
376+
for unit in self.bcx.unit_graph.keys() {
395377
inputs.insert(unit.pkg.manifest_path().to_path_buf());
396378
}
397379
Ok(inputs.into_iter().collect())
@@ -458,7 +440,8 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
458440
};
459441

460442
let mut keys = self
461-
.unit_dependencies
443+
.bcx
444+
.unit_graph
462445
.keys()
463446
.filter(|unit| !unit.mode.is_run_custom_build())
464447
.collect::<Vec<_>>();
@@ -502,7 +485,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
502485
/// Units which depend only on the metadata of others requires the others to
503486
/// actually produce metadata, so we'll record that here.
504487
fn record_units_requiring_metadata(&mut self) {
505-
for (key, deps) in self.unit_dependencies.iter() {
488+
for (key, deps) in self.bcx.unit_graph.iter() {
506489
for dep in deps {
507490
if self.only_requires_rmeta(key, &dep.unit) {
508491
self.rmeta_required.insert(dep.unit);

0 commit comments

Comments
 (0)