Skip to content

Commit e32b58f

Browse files
committed
refactor(compile): Move implicit artfacts into explicit artifacts
This now runs in build scripts and doctests *but* the unit check should prevent any side effects from happening.
1 parent 8c38ea1 commit e32b58f

File tree

4 files changed

+34
-30
lines changed

4 files changed

+34
-30
lines changed

src/cargo/core/compiler/artifact.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,40 @@ use std::ffi::OsString;
1212
/// if artifacts are present.
1313
pub fn get_env(
1414
build_runner: &BuildRunner<'_, '_>,
15+
unit: &Unit,
1516
dependencies: &[UnitDep],
1617
) -> CargoResult<HashMap<String, OsString>> {
1718
let mut env = HashMap::new();
19+
20+
// Add `CARGO_BIN_EXE_` environment variables for building tests.
21+
//
22+
// These aren't built for `cargo check`, so can't use `dependencies`
23+
if unit.target.is_test() || unit.target.is_bench() {
24+
for bin_target in unit
25+
.pkg
26+
.manifest()
27+
.targets()
28+
.iter()
29+
.filter(|target| target.is_bin())
30+
{
31+
let name = bin_target
32+
.binary_filename()
33+
.unwrap_or_else(|| bin_target.name().to_string());
34+
35+
// For `cargo check` builds we do not uplift the CARGO_BIN_EXE_ artifacts to the
36+
// artifact-dir. We do not want to provide a path to a non-existent binary but we still
37+
// need to provide *something* so `env!("CARGO_BIN_EXE_...")` macros will compile.
38+
let exe_path = build_runner
39+
.files()
40+
.bin_link_for_target(bin_target, unit.kind, build_runner.bcx)?
41+
.map(|path| path.as_os_str().to_os_string())
42+
.unwrap_or_else(|| OsString::from(format!("placeholder:{name}")));
43+
44+
let key = format!("CARGO_BIN_EXE_{name}");
45+
env.insert(key, exe_path);
46+
}
47+
}
48+
1849
for unit_dep in dependencies.iter().filter(|d| d.unit.artifact.is_true()) {
1950
for artifact_path in build_runner
2051
.outputs(&unit_dep.unit)?

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
279279
unstable_opts,
280280
linker: self.compilation.target_linker(unit.kind).clone(),
281281
script_metas,
282-
env: artifact::get_env(&self, self.unit_deps(unit))?,
282+
env: artifact::get_env(&self, unit, self.unit_deps(unit))?,
283283
});
284284
}
285285

src/cargo/core/compiler/custom_build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ fn build_work(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResul
377377
.inherit_jobserver(&build_runner.jobserver);
378378

379379
// Find all artifact dependencies and make their file and containing directory discoverable using environment variables.
380-
for (var, value) in artifact::get_env(build_runner, dependencies)? {
380+
for (var, value) in artifact::get_env(build_runner, unit, dependencies)? {
381381
cmd.env(&var, value);
382382
}
383383

src/cargo/core/compiler/mod.rs

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,34 +1741,7 @@ fn build_deps_args(
17411741
cmd.arg(arg);
17421742
}
17431743

1744-
// Add `CARGO_BIN_EXE_` environment variables for building tests.
1745-
if unit.target.is_test() || unit.target.is_bench() {
1746-
for bin_target in unit
1747-
.pkg
1748-
.manifest()
1749-
.targets()
1750-
.iter()
1751-
.filter(|target| target.is_bin())
1752-
{
1753-
let name = bin_target
1754-
.binary_filename()
1755-
.unwrap_or_else(|| bin_target.name().to_string());
1756-
1757-
// For `cargo check` builds we do not uplift the CARGO_BIN_EXE_ artifacts to the
1758-
// artifact-dir. We do not want to provide a path to a non-existent binary but we still
1759-
// need to provide *something* so `env!("CARGO_BIN_EXE_...")` macros will compile.
1760-
let exe_path = build_runner
1761-
.files()
1762-
.bin_link_for_target(bin_target, unit.kind, build_runner.bcx)?
1763-
.map(|path| path.as_os_str().to_os_string())
1764-
.unwrap_or_else(|| OsString::from(format!("placeholder:{name}")));
1765-
1766-
let key = format!("CARGO_BIN_EXE_{name}");
1767-
cmd.env(&key, exe_path);
1768-
}
1769-
}
1770-
1771-
for (var, env) in artifact::get_env(build_runner, deps)? {
1744+
for (var, env) in artifact::get_env(build_runner, unit, deps)? {
17721745
cmd.env(&var, env);
17731746
}
17741747

0 commit comments

Comments
 (0)