Skip to content

Commit

Permalink
Use the crate name instead of the package name
Browse files Browse the repository at this point in the history
This doesn't work because it uses the name of the build script, which is
always build_script_build. I'm not sure what to change it to - the name
of the library crate could be different than the name of the package,
and there could be multiple different crates being compiled in the same
package.
  • Loading branch information
jyn514 committed Apr 20, 2021
1 parent 24d4b91 commit 156254c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
34 changes: 17 additions & 17 deletions src/cargo/core/compiler/custom_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::core::compiler::context::Metadata;
use crate::core::compiler::job_queue::JobState;
use crate::core::{profiles::ProfileRoot, PackageId};
use crate::util::errors::{CargoResult, CargoResultExt};
use crate::util::interning::InternedString;
use crate::util::machine_message::{self, Message};
use crate::util::{self, internal, paths, profile};
use cargo_platform::Cfg;
Expand Down Expand Up @@ -268,7 +267,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
}
})
.collect::<Vec<_>>();
let pkg_name = unit.pkg.name();
let crate_name = unit.target.crate_name();
let pkg_descr = unit.pkg.to_string();
let build_script_outputs = Arc::clone(&cx.build_script_outputs);
let id = unit.pkg.package_id();
Expand All @@ -278,7 +277,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
let host_target_root = cx.files().host_dest().to_path_buf();
let all = (
id,
pkg_name,
crate_name.clone(),
pkg_descr.clone(),
Arc::clone(&build_script_outputs),
output_file.clone(),
Expand Down Expand Up @@ -398,7 +397,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
paths::write(&root_output_file, util::path2bytes(&script_out_dir)?)?;
let parsed_output = BuildOutput::parse(
&output.stdout,
pkg_name,
crate_name,
&pkg_descr,
&script_out_dir,
&script_out_dir,
Expand All @@ -420,12 +419,12 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
// itself to run when we actually end up just discarding what we calculated
// above.
let fresh = Work::new(move |state| {
let (id, pkg_name, pkg_descr, build_script_outputs, output_file, script_out_dir) = all;
let (id, crate_name, pkg_descr, build_script_outputs, output_file, script_out_dir) = all;
let output = match prev_output {
Some(output) => output,
None => BuildOutput::parse_file(
&output_file,
pkg_name,
crate_name,
&pkg_descr,
&prev_script_out_dir,
&script_out_dir,
Expand Down Expand Up @@ -477,7 +476,7 @@ fn insert_warnings_in_build_outputs(
impl BuildOutput {
pub fn parse_file(
path: &Path,
pkg_name: InternedString,
crate_name: String,
pkg_descr: &str,
script_out_dir_when_generated: &Path,
script_out_dir: &Path,
Expand All @@ -487,7 +486,7 @@ impl BuildOutput {
let contents = paths::read_bytes(path)?;
BuildOutput::parse(
&contents,
pkg_name,
crate_name,
pkg_descr,
script_out_dir_when_generated,
script_out_dir,
Expand All @@ -497,10 +496,12 @@ impl BuildOutput {
}

// Parses the output of a script.
// The `pkg_name` is used for error messages.
// The `pkg_descr` is used for error messages.
// The `crate_name` is used for determining if RUSTC_BOOTSTRAP should be allowed.
pub fn parse(
input: &[u8],
pkg_name: InternedString,
// Takes String instead of InternedString so passing `unit.pkg.name()` will give a compile error.
crate_name: String,
pkg_descr: &str,
script_out_dir_when_generated: &Path,
script_out_dir: &Path,
Expand Down Expand Up @@ -589,13 +590,12 @@ impl BuildOutput {
// behavior, so still only give a warning.
// NOTE: cargo only allows nightly features on RUSTC_BOOTSTRAP=1, but we
// want setting any value of RUSTC_BOOTSTRAP to downgrade this to a warning
// (so that `RUSTC_BOOTSTRAP=pkg_name` will work)
// (so that `RUSTC_BOOTSTRAP=crate_name` will work)
let rustc_bootstrap_allows = |name: &str| {
std::env::var("RUSTC_BOOTSTRAP").map_or(false, |var| {
var.split(',').any(|s| s == name)
})
std::env::var("RUSTC_BOOTSTRAP")
.map_or(false, |var| var.split(',').any(|s| s == name))
};
if nightly_features_allowed || rustc_bootstrap_allows(&*pkg_name) {
if nightly_features_allowed || rustc_bootstrap_allows(&*crate_name) {
warnings.push(format!("Cannot set `RUSTC_BOOTSTRAP={}` from {}.\n\
note: Crates cannot set `RUSTC_BOOTSTRAP` themselves, as doing so would subvert the stability guarantees of Rust for your project.",
val, whence
Expand All @@ -608,7 +608,7 @@ impl BuildOutput {
help: If you're sure you want to do this in your project, set the environment variable `RUSTC_BOOTSTRAP={}` before running cargo instead.",
val,
whence,
pkg_name,
crate_name,
);
}
} else {
Expand Down Expand Up @@ -865,7 +865,7 @@ fn prev_build_output(cx: &mut Context<'_, '_>, unit: &Unit) -> (Option<BuildOutp
(
BuildOutput::parse_file(
&output_file,
unit.pkg.name(),
unit.target.crate_name(),
&unit.pkg.to_string(),
&prev_script_out_dir,
&script_out_dir,
Expand Down
12 changes: 9 additions & 3 deletions tests/testsuite/build_script_env.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Tests for build.rs rerun-if-env-changed and rustc-env
use cargo_test_support::basic_manifest;
use cargo_test_support::project;
use cargo_test_support::sleep_ms;

Expand Down Expand Up @@ -110,6 +111,7 @@ fn rerun_if_env_or_file_changes() {
#[cargo_test]
fn rustc_bootstrap() {
let p = project()
.file("Cargo.toml", &basic_manifest("has-dashes", "0.0.1"))
.file("src/main.rs", "fn main() {}")
.file(
"build.rs",
Expand All @@ -122,7 +124,9 @@ fn rustc_bootstrap() {
.build();
p.cargo("build")
.with_stderr_contains("error: Cannot set `RUSTC_BOOTSTRAP=1` [..]")
.with_stderr_contains("help: [..] set the environment variable `RUSTC_BOOTSTRAP=foo` [..]")
.with_stderr_contains(
"help: [..] set the environment variable `RUSTC_BOOTSTRAP=has_dashes` [..]",
)
.with_status(101)
.run();
p.cargo("build")
Expand All @@ -131,14 +135,16 @@ fn rustc_bootstrap() {
.run();
// RUSTC_BOOTSTRAP set to the name of the crate
p.cargo("build")
.env("RUSTC_BOOTSTRAP", "foo")
.env("RUSTC_BOOTSTRAP", "has_dashes")
.with_stderr_contains("warning: Cannot set `RUSTC_BOOTSTRAP=1` [..]")
.run();
// RUSTC_BOOTSTRAP set to some random value
p.cargo("build")
.env("RUSTC_BOOTSTRAP", "bar")
.with_stderr_contains("error: Cannot set `RUSTC_BOOTSTRAP=1` [..]")
.with_stderr_contains("help: [..] set the environment variable `RUSTC_BOOTSTRAP=foo` [..]")
.with_stderr_contains(
"help: [..] set the environment variable `RUSTC_BOOTSTRAP=has_dashes` [..]",
)
.with_status(101)
.run();
}

0 comments on commit 156254c

Please sign in to comment.