Skip to content

Commit 41bd02b

Browse files
committed
core::compiler: remove Executor trait
Signed-off-by: Nick Cameron <[email protected]>
1 parent 1382b44 commit 41bd02b

File tree

6 files changed

+47
-128
lines changed

6 files changed

+47
-128
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ use super::job_queue::JobQueue;
1818
use super::layout::Layout;
1919
use super::lto::Lto;
2020
use super::unit_graph::UnitDep;
21-
use super::{
22-
BuildContext, Compilation, CompileKind, CompileMode, Executor, FileFlavor, RustDocFingerprint,
23-
};
21+
use super::{BuildContext, Compilation, CompileKind, CompileMode, FileFlavor, RustDocFingerprint};
2422

2523
mod compilation_files;
2624
use self::compilation_files::CompilationFiles;
@@ -126,7 +124,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
126124

127125
/// Starts compilation, waits for it to finish, and returns information
128126
/// about the result of compilation.
129-
pub fn compile(mut self, exec: &Arc<dyn Executor>) -> CargoResult<Compilation<'cfg>> {
127+
pub fn compile(mut self) -> CargoResult<Compilation<'cfg>> {
130128
let mut queue = JobQueue::new(self.bcx);
131129
let mut plan = BuildPlan::new();
132130
let build_plan = self.bcx.build_config.build_plan;
@@ -156,7 +154,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
156154
// function which will run everything in order with proper
157155
// parallelism.
158156
let force_rebuild = self.bcx.build_config.force_rebuild;
159-
super::compile(&mut self, &mut queue, &mut plan, unit, exec, force_rebuild)?;
157+
super::compile(&mut self, &mut queue, &mut plan, unit, force_rebuild)?;
160158
}
161159

162160
// Now that we've got the full job queue and we've done all our

src/cargo/core/compiler/mod.rs

Lines changed: 36 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -90,60 +90,11 @@ impl LinkType {
9090
}
9191
}
9292

93-
/// A glorified callback for executing calls to rustc. Rather than calling rustc
94-
/// directly, we'll use an `Executor`, giving clients an opportunity to intercept
95-
/// the build calls.
96-
pub trait Executor: Send + Sync + 'static {
97-
/// Called after a rustc process invocation is prepared up-front for a given
98-
/// unit of work (may still be modified for runtime-known dependencies, when
99-
/// the work is actually executed).
100-
fn init(&self, _cx: &Context<'_, '_>, _unit: &Unit) {}
101-
102-
/// In case of an `Err`, Cargo will not continue with the build process for
103-
/// this package.
104-
fn exec(
105-
&self,
106-
cmd: &ProcessBuilder,
107-
id: PackageId,
108-
target: &Target,
109-
mode: CompileMode,
110-
on_stdout_line: &mut dyn FnMut(&str) -> CargoResult<()>,
111-
on_stderr_line: &mut dyn FnMut(&str) -> CargoResult<()>,
112-
) -> CargoResult<()>;
113-
114-
/// Queried when queuing each unit of work. If it returns true, then the
115-
/// unit will always be rebuilt, independent of whether it needs to be.
116-
fn force_rebuild(&self, _unit: &Unit) -> bool {
117-
false
118-
}
119-
}
120-
121-
/// A `DefaultExecutor` calls rustc without doing anything else. It is Cargo's
122-
/// default behaviour.
123-
#[derive(Copy, Clone)]
124-
pub struct DefaultExecutor;
125-
126-
impl Executor for DefaultExecutor {
127-
fn exec(
128-
&self,
129-
cmd: &ProcessBuilder,
130-
_id: PackageId,
131-
_target: &Target,
132-
_mode: CompileMode,
133-
on_stdout_line: &mut dyn FnMut(&str) -> CargoResult<()>,
134-
on_stderr_line: &mut dyn FnMut(&str) -> CargoResult<()>,
135-
) -> CargoResult<()> {
136-
cmd.exec_with_streaming(on_stdout_line, on_stderr_line, false)
137-
.map(drop)
138-
}
139-
}
140-
14193
fn compile<'cfg>(
14294
cx: &mut Context<'_, 'cfg>,
14395
jobs: &mut JobQueue<'cfg>,
14496
plan: &mut BuildPlan,
14597
unit: &Unit,
146-
exec: &Arc<dyn Executor>,
14798
force_rebuild: bool,
14899
) -> CargoResult<()> {
149100
let bcx = cx.bcx;
@@ -163,15 +114,14 @@ fn compile<'cfg>(
163114
// We run these targets later, so this is just a no-op for now.
164115
Job::new_fresh()
165116
} else if build_plan {
166-
Job::new_dirty(rustc(cx, unit, &exec.clone())?)
117+
Job::new_dirty(rustc(cx, unit)?)
167118
} else {
168-
let force = exec.force_rebuild(unit) || force_rebuild;
169-
let mut job = fingerprint::prepare_target(cx, unit, force)?;
119+
let mut job = fingerprint::prepare_target(cx, unit, force_rebuild)?;
170120
job.before(if job.freshness() == Freshness::Dirty {
171121
let work = if unit.mode.is_doc() || unit.mode.is_doc_scrape() {
172122
rustdoc(cx, unit)?
173123
} else {
174-
rustc(cx, unit, exec)?
124+
rustc(cx, unit)?
175125
};
176126
work.then(link_targets(cx, unit, false)?)
177127
} else {
@@ -198,7 +148,7 @@ fn compile<'cfg>(
198148
// Be sure to compile all dependencies of this target as well.
199149
let deps = Vec::from(cx.unit_deps(unit)); // Create vec due to mutable borrow.
200150
for dep in deps {
201-
compile(cx, jobs, plan, &dep.unit, exec, false)?;
151+
compile(cx, jobs, plan, &dep.unit, false)?;
202152
}
203153
if build_plan {
204154
plan.add(cx, unit)?;
@@ -207,7 +157,7 @@ fn compile<'cfg>(
207157
Ok(())
208158
}
209159

210-
fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> CargoResult<Work> {
160+
fn rustc(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Work> {
211161
let mut rustc = prepare_rustc(cx, &unit.target.rustc_crate_types(), unit)?;
212162
let build_plan = cx.bcx.build_config.build_plan;
213163

@@ -248,10 +198,6 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> Car
248198
let mut output_options = OutputOptions::new(cx, unit);
249199
let package_id = unit.pkg.package_id();
250200
let target = Target::clone(&unit.target);
251-
let mode = unit.mode;
252-
253-
exec.init(cx, unit);
254-
let exec = exec.clone();
255201

256202
let root_output = cx.files().host_dest().to_path_buf();
257203
let target_dir = cx.bcx.ws.target_dir().into_path_unlocked();
@@ -339,38 +285,37 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> Car
339285
if build_plan {
340286
state.build_plan(buildkey, rustc.clone(), outputs.clone());
341287
} else {
342-
exec.exec(
343-
&rustc,
344-
package_id,
345-
&target,
346-
mode,
347-
&mut |line| on_stdout_line(state, line, package_id, &target),
348-
&mut |line| {
349-
on_stderr_line(
350-
state,
351-
line,
352-
package_id,
353-
&manifest_path,
354-
&target,
355-
&mut output_options,
356-
)
357-
},
358-
)
359-
.map_err(verbose_if_simple_exit_code)
360-
.with_context(|| {
361-
// adapted from rustc_errors/src/lib.rs
362-
let warnings = match output_options.warnings_seen {
363-
0 => String::new(),
364-
1 => "; 1 warning emitted".to_string(),
365-
count => format!("; {} warnings emitted", count),
366-
};
367-
let errors = match output_options.errors_seen {
368-
0 => String::new(),
369-
1 => " due to previous error".to_string(),
370-
count => format!(" due to {} previous errors", count),
371-
};
372-
format!("could not compile `{}`{}{}", name, errors, warnings)
373-
})?;
288+
rustc
289+
.exec_with_streaming(
290+
&mut |line| on_stdout_line(state, line, package_id, &target),
291+
&mut |line| {
292+
on_stderr_line(
293+
state,
294+
line,
295+
package_id,
296+
&manifest_path,
297+
&target,
298+
&mut output_options,
299+
)
300+
},
301+
false,
302+
)
303+
.map(drop)
304+
.map_err(verbose_if_simple_exit_code)
305+
.with_context(|| {
306+
// adapted from rustc_errors/src/lib.rs
307+
let warnings = match output_options.warnings_seen {
308+
0 => String::new(),
309+
1 => "; 1 warning emitted".to_string(),
310+
count => format!("; {} warnings emitted", count),
311+
};
312+
let errors = match output_options.errors_seen {
313+
0 => String::new(),
314+
1 => " due to previous error".to_string(),
315+
count => format!(" due to {} previous errors", count),
316+
};
317+
format!("could not compile `{}`{}{}", name, errors, warnings)
318+
})?;
374319
// Exec should never return with success *and* generate an error.
375320
debug_assert_eq!(output_options.errors_seen, 0);
376321
}

src/cargo/ops/cargo_compile/mod.rs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,14 @@
3232
use std::collections::{HashMap, HashSet};
3333
use std::fmt::Write;
3434
use std::hash::{Hash, Hasher};
35-
use std::sync::Arc;
3635

3736
use crate::core::compiler::rustdoc::RustdocScrapeExamples;
3837
use crate::core::compiler::unit_dependencies::{build_unit_dependencies, IsArtifact};
3938
use crate::core::compiler::unit_graph::{self, UnitDep, UnitGraph};
39+
use crate::core::compiler::UnitInterner;
4040
use crate::core::compiler::{standard_lib, CrateType, TargetInfo};
4141
use crate::core::compiler::{BuildConfig, BuildContext, Compilation, Context};
4242
use crate::core::compiler::{CompileKind, CompileMode, CompileTarget, RustcTargetData, Unit};
43-
use crate::core::compiler::{DefaultExecutor, Executor, UnitInterner};
4443
use crate::core::profiles::{Profiles, UnitFor};
4544
use crate::core::resolver::features::{self, CliFeatures, FeaturesFor};
4645
use crate::core::resolver::{HasDevUnits, Resolve};
@@ -115,32 +114,14 @@ impl CompileOptions {
115114
}
116115
}
117116

118-
/// Compiles!
119-
///
120-
/// This uses the [`DefaultExecutor`]. To use a custom [`Executor`], see [`compile_with_exec`].
121117
pub fn compile<'a>(ws: &Workspace<'a>, options: &CompileOptions) -> CargoResult<Compilation<'a>> {
122-
let exec: Arc<dyn Executor> = Arc::new(DefaultExecutor);
123-
compile_with_exec(ws, options, &exec)
124-
}
125-
126-
/// Like [`compile`] but allows specifying a custom [`Executor`]
127-
/// that will be able to intercept build calls and add custom logic.
128-
///
129-
/// [`compile`] uses [`DefaultExecutor`] which just passes calls through.
130-
pub fn compile_with_exec<'a>(
131-
ws: &Workspace<'a>,
132-
options: &CompileOptions,
133-
exec: &Arc<dyn Executor>,
134-
) -> CargoResult<Compilation<'a>> {
135118
ws.emit_warnings()?;
136-
compile_ws(ws, options, exec)
119+
compile_ws(ws, options)
137120
}
138121

139-
/// Like [`compile_with_exec`] but without warnings from manifest parsing.
140122
pub fn compile_ws<'a>(
141123
ws: &Workspace<'a>,
142124
options: &CompileOptions,
143-
exec: &Arc<dyn Executor>,
144125
) -> CargoResult<Compilation<'a>> {
145126
let interner = UnitInterner::new();
146127
let bcx = create_bcx(ws, options, &interner)?;
@@ -150,7 +131,7 @@ pub fn compile_ws<'a>(
150131
}
151132
let _p = profile::start("compiling");
152133
let cx = Context::new(&bcx)?;
153-
cx.compile(exec)
134+
cx.compile()
154135
}
155136

156137
/// Executes `rustc --print <VALUE>`.

src/cargo/ops/cargo_install.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
22
use std::path::{Path, PathBuf};
3-
use std::sync::Arc;
43
use std::{env, fs};
54

6-
use crate::core::compiler::{CompileKind, DefaultExecutor, Executor, Freshness, UnitOutput};
5+
use crate::core::compiler::{CompileKind, Freshness, UnitOutput};
76
use crate::core::{Dependency, Edition, Package, PackageId, Source, SourceId, Workspace};
87
use crate::ops::CompileFilter;
98
use crate::ops::{common_for_install_and_uninstall::*, FilterRule};
@@ -296,8 +295,7 @@ impl<'cfg, 'a> InstallablePackage<'cfg, 'a> {
296295

297296
self.check_yanked_install()?;
298297

299-
let exec: Arc<dyn Executor> = Arc::new(DefaultExecutor);
300-
let compile = ops::compile_ws(&self.ws, self.opts, &exec).with_context(|| {
298+
let compile = ops::compile_ws(&self.ws, self.opts).with_context(|| {
301299
if let Some(td) = td_opt.take() {
302300
// preserve the temporary directory, so the user can inspect it
303301
td.into_path();

src/cargo/ops/cargo_package.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ use std::io::prelude::*;
44
use std::io::SeekFrom;
55
use std::path::{Path, PathBuf};
66
use std::rc::Rc;
7-
use std::sync::Arc;
87
use std::task::Poll;
98

10-
use crate::core::compiler::{BuildConfig, CompileMode, DefaultExecutor, Executor};
9+
use crate::core::compiler::{BuildConfig, CompileMode};
1110
use crate::core::resolver::CliFeatures;
1211
use crate::core::{Feature, Shell, Verbosity, Workspace};
1312
use crate::core::{Package, PackageId, PackageSet, Resolve, SourceId};
@@ -835,8 +834,7 @@ fn run_verify(
835834
None
836835
};
837836

838-
let exec: Arc<dyn Executor> = Arc::new(DefaultExecutor);
839-
ops::compile_with_exec(
837+
ops::compile(
840838
&ws,
841839
&ops::CompileOptions {
842840
build_config: BuildConfig::new(
@@ -857,7 +855,6 @@ fn run_verify(
857855
rustdoc_document_private_items: false,
858856
honor_rust_version: true,
859857
},
860-
&exec,
861858
)?;
862859

863860
// Check that `build.rs` didn't modify any files in the `src` directory.

src/cargo/ops/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::sources::CRATES_IO_DOMAIN;
22

33
pub use self::cargo_clean::{clean, CleanOptions};
44
pub use self::cargo_compile::{
5-
compile, compile_with_exec, compile_ws, create_bcx, print, resolve_all_features, CompileOptions,
5+
compile, compile_ws, create_bcx, print, resolve_all_features, CompileOptions,
66
};
77
pub use self::cargo_compile::{CompileFilter, FilterRule, LibRule, Packages};
88
pub use self::cargo_doc::{doc, DocOptions};

0 commit comments

Comments
 (0)