Skip to content

Commit b68356b

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

File tree

6 files changed

+60
-40
lines changed

6 files changed

+60
-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

@@ -1710,6 +1711,9 @@ pub enum TomlPackageBuild {
17101711

17111712
/// Path of Build Script if there's just one script.
17121713
SingleScript(String),
1714+
1715+
/// Vector of paths if multiple build script are to be used.
1716+
MultipleScript(Vec<String>),
17131717
}
17141718

17151719
impl<'de> Deserialize<'de> for TomlPackageBuild {
@@ -1720,6 +1724,7 @@ impl<'de> Deserialize<'de> for TomlPackageBuild {
17201724
UntaggedEnumVisitor::new()
17211725
.bool(|b| Ok(TomlPackageBuild::Auto(b)))
17221726
.string(|s| Ok(TomlPackageBuild::SingleScript(s.to_owned())))
1727+
.seq(|value| value.deserialize().map(TomlPackageBuild::MultipleScript))
17231728
.deserialize(deserializer)
17241729
}
17251730
}

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: 29 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();
@@ -98,14 +96,13 @@ fn verify_package_multiple_build_scripts() {
9896
let p = basic_empty_project();
9997

10098
p.cargo("package")
99+
.masquerade_as_nightly_cargo(&["multiple-build-scripts"])
101100
.with_status(101)
102101
.with_stderr_data(str![[r#"
103-
[ERROR] invalid type: sequence, expected a boolean or string
104-
--> Cargo.toml:8:25
105-
|
106-
8 | build = ["build1.rs", "build2.rs"]
107-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
108-
|
102+
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
103+
104+
Caused by:
105+
multiple build scripts feature is not implemented yet!
109106
110107
"#]])
111108
.run();
@@ -152,15 +149,10 @@ fn verify_vendor_multiple_build_scripts() {
152149
.build();
153150

154151
p.cargo("vendor --respect-source-config")
152+
.masquerade_as_nightly_cargo(&["multiple-build-scripts"])
155153
.with_status(101)
156154
.with_stderr_data(str![[r#"
157155
[UPDATING] git repository `[ROOTURL]/dep`
158-
[ERROR] invalid type: sequence, expected a boolean or string
159-
--> ../home/.cargo/git/checkouts/dep-[HASH]/[..]/Cargo.toml:8:25
160-
|
161-
8 | build = ["build1.rs", "build2.rs"]
162-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
163-
|
164156
[ERROR] failed to sync
165157
166158
Caused by:
@@ -175,6 +167,12 @@ Caused by:
175167
Caused by:
176168
Unable to update [ROOTURL]/dep
177169
170+
Caused by:
171+
failed to parse manifest at `[ROOT]/home/.cargo/git/checkouts/dep-[HASH]/[..]/Cargo.toml`
172+
173+
Caused by:
174+
multiple build scripts feature is not implemented yet!
175+
178176
"#]])
179177
.run();
180178
}
@@ -226,7 +224,7 @@ fn bar() {
226224
// Editing bar.txt will recompile
227225

228226
p.change_file("assets/bar.txt", "bar updated");
229-
p.cargo("build -v")
227+
p.cargo("build -v")
230228
.with_stderr_data(str![[r#"
231229
[DIRTY] foo v0.1.0 ([ROOT]/foo): the file `assets/bar.txt` has changed ([TIME_DIFF_AFTER_LAST_BUILD])
232230
[COMPILING] foo v0.1.0 ([ROOT]/foo)

0 commit comments

Comments
 (0)