Skip to content

Commit a9d5cb8

Browse files
committed
Add MultipleScript (error on stable, error about not implemented on nightly)
1 parent db8dbaf commit a9d5cb8

File tree

6 files changed

+58
-40
lines changed

6 files changed

+58
-40
lines changed

crates/cargo-util-schemas/manifest.schema.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,13 @@
549549
{
550550
"description": "Path of Build Script if there's just one script.",
551551
"type": "string"
552+
},
553+
{
554+
"description": "Vector of paths if multiple build script are to be used.",
555+
"type": "array",
556+
"items": {
557+
"type": "string"
558+
}
552559
}
553560
]
554561
},

crates/cargo-util-schemas/src/manifest/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ impl TomlPackage {
260260
TomlPackageBuild::Auto(false) => Ok(None),
261261
TomlPackageBuild::Auto(true) => Err(UnresolvedError),
262262
TomlPackageBuild::SingleScript(value) => Ok(Some(std::slice::from_ref(value))),
263+
TomlPackageBuild::MultipleScript(scripts) => Ok(Some(scripts)),
263264
}
264265
}
265266

@@ -1712,6 +1713,9 @@ pub enum TomlPackageBuild {
17121713

17131714
/// Path of Build Script if there's just one script.
17141715
SingleScript(String),
1716+
1717+
/// Vector of paths if multiple build script are to be used.
1718+
MultipleScript(Vec<String>),
17151719
}
17161720

17171721
impl<'de> Deserialize<'de> for TomlPackageBuild {
@@ -1722,6 +1726,7 @@ impl<'de> Deserialize<'de> for TomlPackageBuild {
17221726
UntaggedEnumVisitor::new()
17231727
.bool(|b| Ok(TomlPackageBuild::Auto(b)))
17241728
.string(|s| Ok(TomlPackageBuild::SingleScript(s.to_owned())))
1729+
.seq(|value| value.deserialize().map(TomlPackageBuild::MultipleScript))
17251730
.deserialize(deserializer)
17261731
}
17271732
}

src/cargo/util/toml/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ fn normalize_toml(
344344
is_embedded,
345345
gctx,
346346
&inherit,
347+
features,
347348
)?;
348349
let package_name = &normalized_package
349350
.normalized_name()
@@ -607,6 +608,7 @@ fn normalize_package_toml<'a>(
607608
is_embedded: bool,
608609
gctx: &GlobalContext,
609610
inherit: &dyn Fn() -> CargoResult<&'a InheritableFields>,
611+
features: &Features,
610612
) -> CargoResult<Box<manifest::TomlPackage>> {
611613
let package_root = manifest_file.parent().unwrap();
612614

@@ -672,7 +674,10 @@ fn normalize_package_toml<'a>(
672674
let build = if is_embedded {
673675
Some(TomlPackageBuild::Auto(false))
674676
} else {
675-
targets::normalize_build(original_package.build.as_ref(), package_root)
677+
if let Some(TomlPackageBuild::MultipleScript(_)) = original_package.build {
678+
features.require(Feature::multiple_build_scripts())?;
679+
}
680+
targets::normalize_build(original_package.build.as_ref(), package_root)?
676681
};
677682
let metabuild = original_package.metabuild.clone();
678683
let default_target = original_package.default_target.clone();

src/cargo/util/toml/targets.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ pub(super) fn to_targets(
105105
if metabuild.is_some() {
106106
anyhow::bail!("cannot specify both `metabuild` and `build`");
107107
}
108-
assert_eq!(custom_build.len(), 1);
108+
if custom_build.len() > 1 {
109+
anyhow::bail!("multiple build scripts feature is not implemented yet! ")
110+
}
109111
let custom_build = Path::new(&custom_build[0]);
110112
let name = format!(
111113
"build-script-{}",
@@ -1080,30 +1082,33 @@ Cargo doesn't know which to use because multiple target files found at `{}` and
10801082
pub fn normalize_build(
10811083
build: Option<&TomlPackageBuild>,
10821084
package_root: &Path,
1083-
) -> Option<TomlPackageBuild> {
1085+
) -> CargoResult<Option<TomlPackageBuild>> {
10841086
const BUILD_RS: &str = "build.rs";
10851087
match build {
10861088
None => {
10871089
// If there is a `build.rs` file next to the `Cargo.toml`, assume it is
10881090
// a build script.
10891091
let build_rs = package_root.join(BUILD_RS);
10901092
if build_rs.is_file() {
1091-
Some(TomlPackageBuild::SingleScript(BUILD_RS.to_owned()))
1093+
Ok(Some(TomlPackageBuild::SingleScript(BUILD_RS.to_owned())))
10921094
} else {
1093-
Some(TomlPackageBuild::Auto(false))
1095+
Ok(Some(TomlPackageBuild::Auto(false)))
10941096
}
10951097
}
10961098
// Explicitly no build script.
1097-
Some(TomlPackageBuild::Auto(false)) => build.cloned(),
1099+
Some(TomlPackageBuild::Auto(false)) => Ok(build.cloned()),
10981100
Some(TomlPackageBuild::SingleScript(build_file)) => {
10991101
let build_file = paths::normalize_path(Path::new(build_file));
11001102
let build = build_file.into_os_string().into_string().expect(
11011103
"`build_file` started as a String and `normalize_path` shouldn't have changed that",
11021104
);
1103-
Some(TomlPackageBuild::SingleScript(build))
1105+
Ok(Some(TomlPackageBuild::SingleScript(build)))
11041106
}
11051107
Some(TomlPackageBuild::Auto(true)) => {
1106-
Some(TomlPackageBuild::SingleScript(BUILD_RS.to_owned()))
1108+
Ok(Some(TomlPackageBuild::SingleScript(BUILD_RS.to_owned())))
1109+
}
1110+
Some(TomlPackageBuild::MultipleScript(_scripts)) => {
1111+
anyhow::bail!("multiple build scripts feature is not implemented yet!");
11071112
}
11081113
}
11091114
}

tests/testsuite/bad_config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2733,7 +2733,7 @@ fn bad_opt_level() {
27332733
p.cargo("check")
27342734
.with_status(101)
27352735
.with_stderr_data(str![[r#"
2736-
[ERROR] invalid type: integer `3`, expected a boolean or string
2736+
[ERROR] invalid type: integer `3`, expected a boolean, string or array
27372737
--> Cargo.toml:7:25
27382738
|
27392739
7 | build = 3

tests/testsuite/build_scripts_multiple.rs

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ fn build_without_feature_enabled_aborts_with_error() {
2626
.masquerade_as_nightly_cargo(&["multiple-build-scripts"])
2727
.with_status(101)
2828
.with_stderr_data(str![[r#"
29-
[ERROR] invalid type: sequence, expected a boolean or string
30-
--> Cargo.toml:6:25
31-
|
32-
6 | build = ["build1.rs", "build2.rs"]
33-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
34-
|
29+
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
30+
31+
Caused by:
32+
feature `multiple-build-scripts` is required
33+
34+
The package requires the Cargo feature called `multiple-build-scripts`, but that feature is not stabilized in this version of Cargo ([..]).
35+
Consider adding `cargo-features = ["multiple-build-scripts"]` to the top of Cargo.toml (above the [package] table) to tell Cargo you are opting in to use this unstable feature.
36+
See https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#multiple-build-scripts for more information about the status of this feature.
3537
3638
"#]])
3739
.run();
@@ -64,12 +66,10 @@ fn empty_multiple_build_script_project() {
6466
.masquerade_as_nightly_cargo(&["multiple-build-scripts"])
6567
.with_status(101)
6668
.with_stderr_data(str![[r#"
67-
[ERROR] invalid type: sequence, expected a boolean or string
68-
--> Cargo.toml:8:25
69-
|
70-
8 | build = ["build1.rs", "build2.rs"]
71-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
72-
|
69+
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
70+
71+
Caused by:
72+
multiple build scripts feature is not implemented yet!
7373
7474
"#]])
7575
.run();
@@ -82,12 +82,10 @@ fn multiple_build_scripts_metadata() {
8282
.masquerade_as_nightly_cargo(&["multiple-build-scripts"])
8383
.with_status(101)
8484
.with_stderr_data(str![[r#"
85-
[ERROR] invalid type: sequence, expected a boolean or string
86-
--> Cargo.toml:8:25
87-
|
88-
8 | build = ["build1.rs", "build2.rs"]
89-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
90-
|
85+
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
86+
87+
Caused by:
88+
multiple build scripts feature is not implemented yet!
9189
9290
"#]])
9391
.run();
@@ -123,12 +121,10 @@ fn verify_package_multiple_build_scripts() {
123121
.masquerade_as_nightly_cargo(&["multiple-build-scripts"])
124122
.with_status(101)
125123
.with_stderr_data(str![[r#"
126-
[ERROR] invalid type: sequence, expected a boolean or string
127-
--> Cargo.toml:13:25
128-
|
129-
13 | build = ["build1.rs", "build2.rs"]
130-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
131-
|
124+
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
125+
126+
Caused by:
127+
multiple build scripts feature is not implemented yet!
132128
133129
"#]])
134130
.run();
@@ -187,12 +183,6 @@ fn verify_vendor_multiple_build_scripts() {
187183
.with_status(101)
188184
.with_stderr_data(str![[r#"
189185
[UPDATING] git repository `[ROOTURL]/dep`
190-
[ERROR] invalid type: sequence, expected a boolean or string
191-
--> ../home/.cargo/git/checkouts/dep-[HASH]/[..]/Cargo.toml:13:25
192-
|
193-
13 | build = ["build1.rs", "build2.rs"]
194-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
195-
|
196186
[ERROR] failed to sync
197187
198188
Caused by:
@@ -207,6 +197,12 @@ Caused by:
207197
Caused by:
208198
Unable to update [ROOTURL]/dep
209199
200+
Caused by:
201+
failed to parse manifest at `[ROOT]/home/.cargo/git/checkouts/dep-[HASH]/[..]/Cargo.toml`
202+
203+
Caused by:
204+
multiple build scripts feature is not implemented yet!
205+
210206
"#]])
211207
.run();
212208
}
@@ -258,7 +254,7 @@ fn bar() {
258254
// Editing bar.txt will recompile
259255

260256
p.change_file("assets/bar.txt", "bar updated");
261-
p.cargo("build -v")
257+
p.cargo("build -v")
262258
.with_stderr_data(str![[r#"
263259
[DIRTY] foo v0.1.0 ([ROOT]/foo): the file `assets/bar.txt` has changed ([TIME_DIFF_AFTER_LAST_BUILD])
264260
[COMPILING] foo v0.1.0 ([ROOT]/foo)

0 commit comments

Comments
 (0)