Skip to content

Commit

Permalink
Add msrv check for new syntax
Browse files Browse the repository at this point in the history
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
  • Loading branch information
Rustin170506 committed Dec 23, 2023
1 parent a76564b commit 5906455
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/cargo/core/compiler/custom_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ use crate::core::{profiles::ProfileRoot, PackageId, Target};
use crate::util::errors::CargoResult;
use crate::util::machine_message::{self, Message};
use crate::util::{internal, profile};
use crate::util_schemas::manifest::RustVersion;
use anyhow::{bail, Context as _};
use cargo_platform::Cfg;
use cargo_util::paths;
use std::collections::hash_map::{Entry, HashMap};
use std::collections::{BTreeSet, HashSet};
use std::path::{Path, PathBuf};
use std::str;
use std::str::{self, FromStr};
use std::sync::{Arc, Mutex};

/// Deprecated: A build script instruction that tells Cargo to display a warning after the
Expand Down Expand Up @@ -413,8 +414,10 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
let nightly_features_allowed = cx.bcx.config.nightly_features_allowed;
let extra_check_cfg = cx.bcx.config.cli_unstable().check_cfg;
let targets: Vec<Target> = unit.pkg.targets().to_vec();
let msrv = unit.pkg.rust_version().cloned();
// Need a separate copy for the fresh closure.
let targets_fresh = targets.clone();
let msrv_fresh = msrv.clone();

let env_profile_name = unit.profile.name.to_uppercase();
let built_with_debuginfo = cx
Expand Down Expand Up @@ -560,6 +563,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
extra_check_cfg,
nightly_features_allowed,
&targets,
&msrv,
)?;

if json_messages {
Expand Down Expand Up @@ -588,6 +592,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
extra_check_cfg,
nightly_features_allowed,
&targets_fresh,
&msrv_fresh,
)?,
};

Expand Down Expand Up @@ -644,6 +649,7 @@ impl BuildOutput {
extra_check_cfg: bool,
nightly_features_allowed: bool,
targets: &[Target],
msrv: &Option<RustVersion>,
) -> CargoResult<BuildOutput> {
let contents = paths::read_bytes(path)?;
BuildOutput::parse(
Expand All @@ -655,6 +661,7 @@ impl BuildOutput {
extra_check_cfg,
nightly_features_allowed,
targets,
msrv,
)
}

Expand All @@ -675,6 +682,7 @@ impl BuildOutput {
extra_check_cfg: bool,
nightly_features_allowed: bool,
targets: &[Target],
msrv: &Option<RustVersion>,
) -> CargoResult<BuildOutput> {
let mut library_paths = Vec::new();
let mut library_links = Vec::new();
Expand Down Expand Up @@ -715,6 +723,25 @@ impl BuildOutput {
const DOCS_LINK_SUGGESTION: &str = "See https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script \
for more information about build script outputs.";

fn check_minimum_supported_rust_version_for_new_syntax(
pkg_descr: &str,
msrv: &Option<RustVersion>,
) -> CargoResult<()> {
let new_syntax_added_in = &RustVersion::from_str("1.77.0")?;

if let Some(msrv) = msrv {
if msrv < new_syntax_added_in {
bail!(
"the `cargo::` syntax for build script output instructions was added in \
Rust 1.77.0, but the minimum supported Rust version of `{pkg_descr}` is {msrv}.\n\
{DOCS_LINK_SUGGESTION}"
);
}
}

Ok(())
}

fn parse_directive<'a>(
whence: &str,
line: &str,
Expand Down Expand Up @@ -768,6 +795,7 @@ impl BuildOutput {
};
let mut old_syntax = false;
let (key, value) = if let Some(data) = line.strip_prefix("cargo::") {
check_minimum_supported_rust_version_for_new_syntax(pkg_descr, msrv)?;
// For instance, `cargo::rustc-flags=foo` or `cargo::metadata=foo=bar`.
parse_directive(whence.as_str(), line, data, old_syntax)?
} else if let Some(data) = line.strip_prefix("cargo:") {
Expand Down Expand Up @@ -1220,6 +1248,7 @@ fn prev_build_output(cx: &mut Context<'_, '_>, unit: &Unit) -> (Option<BuildOutp
cx.bcx.config.cli_unstable().check_cfg,
cx.bcx.config.nightly_features_allowed,
unit.pkg.targets(),
&unit.pkg.rust_version().cloned(),
)
.ok(),
prev_script_out_dir,
Expand Down

0 comments on commit 5906455

Please sign in to comment.