Skip to content

Commit 236fe87

Browse files
committed
Use the library name to decide whether the override should be allowed from the top-level.
If there's no library, give a hard error unless features are unconditionally allowed with RUSTC_BOOTSTRAP=1.
1 parent 6d6f466 commit 236fe87

File tree

2 files changed

+64
-25
lines changed

2 files changed

+64
-25
lines changed

src/cargo/core/compiler/custom_build.rs

+32-14
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,12 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
269269
}
270270
})
271271
.collect::<Vec<_>>();
272-
let crate_name = unit.target.crate_name();
272+
let library_name = unit
273+
.pkg
274+
.targets()
275+
.iter()
276+
.find(|t| t.is_lib())
277+
.map(|t| t.crate_name());
273278
let pkg_descr = unit.pkg.to_string();
274279
let build_script_outputs = Arc::clone(&cx.build_script_outputs);
275280
let id = unit.pkg.package_id();
@@ -279,7 +284,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
279284
let host_target_root = cx.files().host_dest().to_path_buf();
280285
let all = (
281286
id,
282-
crate_name.clone(),
287+
library_name.clone(),
283288
pkg_descr.clone(),
284289
Arc::clone(&build_script_outputs),
285290
output_file.clone(),
@@ -399,7 +404,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
399404
paths::write(&root_output_file, paths::path2bytes(&script_out_dir)?)?;
400405
let parsed_output = BuildOutput::parse(
401406
&output.stdout,
402-
crate_name,
407+
library_name,
403408
&pkg_descr,
404409
&script_out_dir,
405410
&script_out_dir,
@@ -421,12 +426,12 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
421426
// itself to run when we actually end up just discarding what we calculated
422427
// above.
423428
let fresh = Work::new(move |state| {
424-
let (id, crate_name, pkg_descr, build_script_outputs, output_file, script_out_dir) = all;
429+
let (id, library_name, pkg_descr, build_script_outputs, output_file, script_out_dir) = all;
425430
let output = match prev_output {
426431
Some(output) => output,
427432
None => BuildOutput::parse_file(
428433
&output_file,
429-
crate_name,
434+
library_name,
430435
&pkg_descr,
431436
&prev_script_out_dir,
432437
&script_out_dir,
@@ -478,7 +483,7 @@ fn insert_warnings_in_build_outputs(
478483
impl BuildOutput {
479484
pub fn parse_file(
480485
path: &Path,
481-
crate_name: String,
486+
library_name: Option<String>,
482487
pkg_descr: &str,
483488
script_out_dir_when_generated: &Path,
484489
script_out_dir: &Path,
@@ -488,7 +493,7 @@ impl BuildOutput {
488493
let contents = paths::read_bytes(path)?;
489494
BuildOutput::parse(
490495
&contents,
491-
crate_name,
496+
library_name,
492497
pkg_descr,
493498
script_out_dir_when_generated,
494499
script_out_dir,
@@ -499,11 +504,11 @@ impl BuildOutput {
499504

500505
// Parses the output of a script.
501506
// The `pkg_descr` is used for error messages.
502-
// The `crate_name` is used for determining if RUSTC_BOOTSTRAP should be allowed.
507+
// The `library_name` is used for determining if RUSTC_BOOTSTRAP should be allowed.
503508
pub fn parse(
504509
input: &[u8],
505510
// Takes String instead of InternedString so passing `unit.pkg.name()` will give a compile error.
506-
crate_name: String,
511+
library_name: Option<String>,
507512
pkg_descr: &str,
508513
script_out_dir_when_generated: &Path,
509514
script_out_dir: &Path,
@@ -592,12 +597,21 @@ impl BuildOutput {
592597
// behavior, so still only give a warning.
593598
// NOTE: cargo only allows nightly features on RUSTC_BOOTSTRAP=1, but we
594599
// want setting any value of RUSTC_BOOTSTRAP to downgrade this to a warning
595-
// (so that `RUSTC_BOOTSTRAP=crate_name` will work)
596-
let rustc_bootstrap_allows = |name: &str| {
600+
// (so that `RUSTC_BOOTSTRAP=library_name` will work)
601+
let rustc_bootstrap_allows = |name: Option<&str>| {
602+
let name = match name {
603+
// as of 2021, no binaries on crates.io use RUSTC_BOOTSTRAP, so
604+
// fine-grained opt-outs aren't needed. end-users can always use
605+
// RUSTC_BOOTSTRAP=1 from the top-level if it's really a problem.
606+
None => return false,
607+
Some(n) => n,
608+
};
597609
std::env::var("RUSTC_BOOTSTRAP")
598610
.map_or(false, |var| var.split(',').any(|s| s == name))
599611
};
600-
if nightly_features_allowed || rustc_bootstrap_allows(&*crate_name) {
612+
if nightly_features_allowed
613+
|| rustc_bootstrap_allows(library_name.as_deref())
614+
{
601615
warnings.push(format!("Cannot set `RUSTC_BOOTSTRAP={}` from {}.\n\
602616
note: Crates cannot set `RUSTC_BOOTSTRAP` themselves, as doing so would subvert the stability guarantees of Rust for your project.",
603617
val, whence
@@ -610,7 +624,7 @@ impl BuildOutput {
610624
help: If you're sure you want to do this in your project, set the environment variable `RUSTC_BOOTSTRAP={}` before running cargo instead.",
611625
val,
612626
whence,
613-
crate_name,
627+
library_name.as_deref().unwrap_or("1"),
614628
);
615629
}
616630
} else {
@@ -867,7 +881,11 @@ fn prev_build_output(cx: &mut Context<'_, '_>, unit: &Unit) -> (Option<BuildOutp
867881
(
868882
BuildOutput::parse_file(
869883
&output_file,
870-
unit.target.crate_name(),
884+
unit.pkg
885+
.targets()
886+
.iter()
887+
.find(|t| t.is_lib())
888+
.map(|t| t.crate_name()),
871889
&unit.pkg.to_string(),
872890
&prev_script_out_dir,
873891
&script_out_dir,

tests/testsuite/build_script_env.rs

+32-11
Original file line numberDiff line numberDiff line change
@@ -110,35 +110,35 @@ fn rerun_if_env_or_file_changes() {
110110

111111
#[cargo_test]
112112
fn rustc_bootstrap() {
113+
let build_rs = r#"
114+
fn main() {
115+
println!("cargo:rustc-env=RUSTC_BOOTSTRAP=1");
116+
}
117+
"#;
113118
let p = project()
114119
.file("Cargo.toml", &basic_manifest("has-dashes", "0.0.1"))
115-
.file("src/main.rs", "fn main() {}")
116-
.file(
117-
"build.rs",
118-
r#"
119-
fn main() {
120-
println!("cargo:rustc-env=RUSTC_BOOTSTRAP=1");
121-
}
122-
"#,
123-
)
120+
.file("src/lib.rs", "#![feature(rustc_attrs)]")
121+
.file("build.rs", build_rs)
124122
.build();
123+
// RUSTC_BOOTSTRAP unset on stable should error
125124
p.cargo("build")
126125
.with_stderr_contains("error: Cannot set `RUSTC_BOOTSTRAP=1` [..]")
127126
.with_stderr_contains(
128127
"help: [..] set the environment variable `RUSTC_BOOTSTRAP=has_dashes` [..]",
129128
)
130129
.with_status(101)
131130
.run();
131+
// RUSTC_BOOTSTRAP unset on nightly should warn
132132
p.cargo("build")
133133
.masquerade_as_nightly_cargo()
134134
.with_stderr_contains("warning: Cannot set `RUSTC_BOOTSTRAP=1` [..]")
135135
.run();
136-
// RUSTC_BOOTSTRAP set to the name of the crate
136+
// RUSTC_BOOTSTRAP set to the name of the library should warn
137137
p.cargo("build")
138138
.env("RUSTC_BOOTSTRAP", "has_dashes")
139139
.with_stderr_contains("warning: Cannot set `RUSTC_BOOTSTRAP=1` [..]")
140140
.run();
141-
// RUSTC_BOOTSTRAP set to some random value
141+
// RUSTC_BOOTSTRAP set to some random value should error
142142
p.cargo("build")
143143
.env("RUSTC_BOOTSTRAP", "bar")
144144
.with_stderr_contains("error: Cannot set `RUSTC_BOOTSTRAP=1` [..]")
@@ -147,4 +147,25 @@ fn rustc_bootstrap() {
147147
)
148148
.with_status(101)
149149
.run();
150+
151+
// Tests for binaries instead of libraries
152+
let p = project()
153+
.file("Cargo.toml", &basic_manifest("foo", "0.0.1"))
154+
.file("src/main.rs", "#![feature(rustc_attrs)] fn main()")
155+
.file("build.rs", build_rs)
156+
.build();
157+
// RUSTC_BOOTSTRAP unconditionally set when there's no library should warn
158+
p.cargo("build")
159+
.masquerade_as_nightly_cargo()
160+
.with_stderr_contains("warning: Cannot set `RUSTC_BOOTSTRAP=1` [..]")
161+
.run();
162+
// RUSTC_BOOTSTRAP conditionally set when there's no library should error (regardless of the value)
163+
p.cargo("build")
164+
.env("RUSTC_BOOTSTRAP", "foo")
165+
.with_stderr_contains("error: Cannot set `RUSTC_BOOTSTRAP=1` [..]")
166+
.with_stderr_does_not_contain(
167+
"help: [..] set the environment variable `RUSTC_BOOTSTRAP=1` [..]",
168+
)
169+
.with_status(101)
170+
.run();
150171
}

0 commit comments

Comments
 (0)