Skip to content

Commit 0e42a8a

Browse files
committed
Add msrv check for new syntax
Signed-off-by: hi-rustin <[email protected]>
1 parent a76564b commit 0e42a8a

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/cargo/core/compiler/custom_build.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ use crate::util::{internal, profile};
4242
use anyhow::{bail, Context as _};
4343
use cargo_platform::Cfg;
4444
use cargo_util::paths;
45+
use cargo_util_schemas::manifest::RustVersion;
4546
use std::collections::hash_map::{Entry, HashMap};
4647
use std::collections::{BTreeSet, HashSet};
4748
use std::path::{Path, PathBuf};
48-
use std::str;
49+
use std::str::{self, FromStr};
4950
use std::sync::{Arc, Mutex};
5051

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

419422
let env_profile_name = unit.profile.name.to_uppercase();
420423
let built_with_debuginfo = cx
@@ -560,6 +563,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
560563
extra_check_cfg,
561564
nightly_features_allowed,
562565
&targets,
566+
&msrv,
563567
)?;
564568

565569
if json_messages {
@@ -588,6 +592,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
588592
extra_check_cfg,
589593
nightly_features_allowed,
590594
&targets_fresh,
595+
&msrv_fresh,
591596
)?,
592597
};
593598

@@ -644,6 +649,7 @@ impl BuildOutput {
644649
extra_check_cfg: bool,
645650
nightly_features_allowed: bool,
646651
targets: &[Target],
652+
msrv: &Option<RustVersion>,
647653
) -> CargoResult<BuildOutput> {
648654
let contents = paths::read_bytes(path)?;
649655
BuildOutput::parse(
@@ -655,6 +661,7 @@ impl BuildOutput {
655661
extra_check_cfg,
656662
nightly_features_allowed,
657663
targets,
664+
msrv,
658665
)
659666
}
660667

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

726+
fn check_minimum_supported_rust_version_for_new_syntax(
727+
pkg_descr: &str,
728+
msrv: &Option<RustVersion>,
729+
) -> CargoResult<()> {
730+
let new_syntax_added_in = &RustVersion::from_str("1.77.0")?;
731+
732+
if let Some(msrv) = msrv {
733+
if msrv < new_syntax_added_in {
734+
bail!(
735+
"the `cargo::` syntax for build script output instructions was added in \
736+
Rust 1.77.0, but the minimum supported Rust version of `{pkg_descr}` is {msrv}.\n\
737+
{DOCS_LINK_SUGGESTION}"
738+
);
739+
}
740+
}
741+
742+
Ok(())
743+
}
744+
718745
fn parse_directive<'a>(
719746
whence: &str,
720747
line: &str,
@@ -768,6 +795,7 @@ impl BuildOutput {
768795
};
769796
let mut old_syntax = false;
770797
let (key, value) = if let Some(data) = line.strip_prefix("cargo::") {
798+
check_minimum_supported_rust_version_for_new_syntax(pkg_descr, msrv)?;
771799
// For instance, `cargo::rustc-flags=foo` or `cargo::metadata=foo=bar`.
772800
parse_directive(whence.as_str(), line, data, old_syntax)?
773801
} else if let Some(data) = line.strip_prefix("cargo:") {
@@ -1220,6 +1248,7 @@ fn prev_build_output(cx: &mut Context<'_, '_>, unit: &Unit) -> (Option<BuildOutp
12201248
cx.bcx.config.cli_unstable().check_cfg,
12211249
cx.bcx.config.nightly_features_allowed,
12221250
unit.pkg.targets(),
1251+
&unit.pkg.rust_version().cloned(),
12231252
)
12241253
.ok(),
12251254
prev_script_out_dir,

0 commit comments

Comments
 (0)