Skip to content

Commit 0febd42

Browse files
committed
Attach all build scripts to the target
1 parent 79a5e07 commit 0febd42

File tree

2 files changed

+258
-55
lines changed

2 files changed

+258
-55
lines changed

src/cargo/util/toml/targets.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -105,22 +105,21 @@ pub(super) fn to_targets(
105105
if metabuild.is_some() {
106106
anyhow::bail!("cannot specify both `metabuild` and `build`");
107107
}
108-
if custom_build.len() > 1 {
109-
anyhow::bail!("multiple build scripts feature is not implemented yet! ")
108+
for script in custom_build {
109+
let script_path = Path::new(script);
110+
let name = format!(
111+
"build-script-{}",
112+
script_path
113+
.file_stem()
114+
.and_then(|s| s.to_str())
115+
.unwrap_or("")
116+
);
117+
targets.push(Target::custom_build_target(
118+
&name,
119+
package_root.join(script_path),
120+
edition,
121+
));
110122
}
111-
let custom_build = Path::new(&custom_build[0]);
112-
let name = format!(
113-
"build-script-{}",
114-
custom_build
115-
.file_stem()
116-
.and_then(|s| s.to_str())
117-
.unwrap_or("")
118-
);
119-
targets.push(Target::custom_build_target(
120-
&name,
121-
package_root.join(custom_build),
122-
edition,
123-
));
124123
}
125124
if let Some(metabuild) = metabuild {
126125
// Verify names match available build deps.
@@ -1107,9 +1106,7 @@ pub fn normalize_build(
11071106
Some(TomlPackageBuild::Auto(true)) => {
11081107
Ok(Some(TomlPackageBuild::SingleScript(BUILD_RS.to_owned())))
11091108
}
1110-
Some(TomlPackageBuild::MultipleScript(_scripts)) => {
1111-
anyhow::bail!("multiple build scripts feature is not implemented yet!");
1112-
}
1109+
Some(TomlPackageBuild::MultipleScript(_scripts)) => Ok(build.cloned()),
11131110
}
11141111
}
11151112

tests/testsuite/build_scripts_multiple.rs

Lines changed: 243 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
//! Tests for multiple build scripts feature.
22
3+
use cargo_test_support::compare::assert_e2e;
34
use cargo_test_support::git;
45
use cargo_test_support::prelude::*;
6+
use cargo_test_support::publish::validate_crate_contents;
57
use cargo_test_support::str;
68
use cargo_test_support::{project, Project};
9+
use std::fs::File;
710

811
#[cargo_test]
912
fn build_without_feature_enabled_aborts_with_error() {
@@ -64,12 +67,10 @@ fn empty_multiple_build_script_project() {
6467
let p = basic_empty_project();
6568
p.cargo("check")
6669
.masquerade_as_nightly_cargo(&["multiple-build-scripts"])
67-
.with_status(101)
70+
.with_status(0)
6871
.with_stderr_data(str![[r#"
69-
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
70-
71-
Caused by:
72-
multiple build scripts feature is not implemented yet!
72+
[COMPILING] foo v0.1.0 ([ROOT]/foo)
73+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
7374
7475
"#]])
7576
.run();
@@ -80,14 +81,107 @@ fn multiple_build_scripts_metadata() {
8081
let p = basic_empty_project();
8182
p.cargo("metadata --format-version=1")
8283
.masquerade_as_nightly_cargo(&["multiple-build-scripts"])
83-
.with_status(101)
84-
.with_stderr_data(str![[r#"
85-
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
86-
87-
Caused by:
88-
multiple build scripts feature is not implemented yet!
89-
90-
"#]])
84+
.with_status(0)
85+
.with_stderr_data("")
86+
.with_stdout_data(
87+
str![[r#"
88+
{
89+
"metadata": null,
90+
"packages": [
91+
{
92+
"authors": [],
93+
"categories": [],
94+
"default_run": null,
95+
"dependencies": [],
96+
"description": null,
97+
"documentation": null,
98+
"edition": "2024",
99+
"features": {},
100+
"homepage": null,
101+
"id": "path+[ROOTURL]/foo#0.1.0",
102+
"keywords": [],
103+
"license": null,
104+
"license_file": null,
105+
"links": null,
106+
"manifest_path": "[ROOT]/foo/Cargo.toml",
107+
"metadata": null,
108+
"name": "foo",
109+
"publish": null,
110+
"readme": null,
111+
"repository": null,
112+
"rust_version": null,
113+
"source": null,
114+
"targets": [
115+
{
116+
"crate_types": [
117+
"bin"
118+
],
119+
"doc": true,
120+
"doctest": false,
121+
"edition": "2024",
122+
"kind": [
123+
"bin"
124+
],
125+
"name": "foo",
126+
"src_path": "[ROOT]/foo/src/main.rs",
127+
"test": true
128+
},
129+
{
130+
"crate_types": [
131+
"bin"
132+
],
133+
"doc": false,
134+
"doctest": false,
135+
"edition": "2024",
136+
"kind": [
137+
"custom-build"
138+
],
139+
"name": "build-script-build1",
140+
"src_path": "[ROOT]/foo/build1.rs",
141+
"test": false
142+
},
143+
{
144+
"crate_types": [
145+
"bin"
146+
],
147+
"doc": false,
148+
"doctest": false,
149+
"edition": "2024",
150+
"kind": [
151+
"custom-build"
152+
],
153+
"name": "build-script-build2",
154+
"src_path": "[ROOT]/foo/build2.rs",
155+
"test": false
156+
}
157+
],
158+
"version": "0.1.0"
159+
}
160+
],
161+
"resolve": {
162+
"nodes": [
163+
{
164+
"dependencies": [],
165+
"deps": [],
166+
"features": [],
167+
"id": "path+[ROOTURL]/foo#0.1.0"
168+
}
169+
],
170+
"root": "path+[ROOTURL]/foo#0.1.0"
171+
},
172+
"target_directory": "[ROOT]/foo/target",
173+
"version": 1,
174+
"workspace_default_members": [
175+
"path+[ROOTURL]/foo#0.1.0"
176+
],
177+
"workspace_members": [
178+
"path+[ROOTURL]/foo#0.1.0"
179+
],
180+
"workspace_root": "[ROOT]/foo"
181+
}
182+
"#]]
183+
.is_json(),
184+
)
91185
.run();
92186
}
93187

@@ -119,15 +213,91 @@ fn verify_package_multiple_build_scripts() {
119213

120214
p.cargo("package")
121215
.masquerade_as_nightly_cargo(&["multiple-build-scripts"])
122-
.with_status(101)
216+
.with_status(0)
123217
.with_stderr_data(str![[r#"
124-
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
125-
126-
Caused by:
127-
multiple build scripts feature is not implemented yet!
218+
[PACKAGING] foo v0.1.0 ([ROOT]/foo)
219+
[PACKAGED] 5 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
220+
[VERIFYING] foo v0.1.0 ([ROOT]/foo)
221+
[COMPILING] foo v0.1.0 ([ROOT]/foo/target/package/foo-0.1.0)
222+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
128223
129224
"#]])
130225
.run();
226+
227+
let f = File::open(&p.root().join("target/package/foo-0.1.0.crate")).unwrap();
228+
validate_crate_contents(
229+
f,
230+
"foo-0.1.0.crate",
231+
&[
232+
"Cargo.toml",
233+
"Cargo.toml.orig",
234+
"src/main.rs",
235+
"build1.rs",
236+
"Cargo.lock",
237+
],
238+
[(
239+
"Cargo.toml",
240+
str![[r##"
241+
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
242+
#
243+
# When uploading crates to the registry Cargo will automatically
244+
# "normalize" Cargo.toml files for maximal compatibility
245+
# with all versions of Cargo and also rewrite `path` dependencies
246+
# to registry (e.g., crates.io) dependencies.
247+
#
248+
# If you are reading this file be aware that the original Cargo.toml
249+
# will likely look very different (and much more reasonable).
250+
# See Cargo.toml.orig for the original contents.
251+
252+
cargo-features = ["multiple-build-scripts"]
253+
254+
[package]
255+
edition = "2024"
256+
name = "foo"
257+
version = "0.1.0"
258+
authors = []
259+
build = [
260+
"build1.rs",
261+
"build2.rs",
262+
]
263+
include = [
264+
"src/main.rs",
265+
"build1.rs",
266+
]
267+
autolib = false
268+
autobins = false
269+
autoexamples = false
270+
autotests = false
271+
autobenches = false
272+
description = "foo"
273+
documentation = "docs.rs/foo"
274+
readme = false
275+
license = "MIT"
276+
277+
[[bin]]
278+
name = "foo"
279+
path = "src/main.rs"
280+
281+
"##]],
282+
)],
283+
);
284+
}
285+
286+
fn add_git_vendor_config(p: &Project, git_project: &Project) {
287+
p.change_file(
288+
".cargo/config.toml",
289+
&format!(
290+
r#"
291+
[source."git+{url}"]
292+
git = "{url}"
293+
replace-with = 'vendor'
294+
295+
[source.vendor]
296+
directory = 'vendor'
297+
"#,
298+
url = git_project.url()
299+
),
300+
);
131301
}
132302

133303
#[cargo_test]
@@ -180,31 +350,67 @@ fn verify_vendor_multiple_build_scripts() {
180350

181351
p.cargo("vendor --respect-source-config")
182352
.masquerade_as_nightly_cargo(&["multiple-build-scripts"])
183-
.with_status(101)
353+
.with_status(0)
184354
.with_stderr_data(str![[r#"
185355
[UPDATING] git repository `[ROOTURL]/dep`
186-
[ERROR] failed to sync
356+
[LOCKING] 1 package to latest [..] compatible version
357+
Vendoring dep v0.1.0 ([ROOTURL]/dep#[..]) ([ROOT]/home/.cargo/git/checkouts/dep-[HASH]/[..]) to vendor/dep
358+
To use vendored sources, add this to your .cargo/config.toml for this project:
187359
188-
Caused by:
189-
failed to load lockfile for [ROOT]/foo
190-
191-
Caused by:
192-
failed to get `dep` as a dependency of package `foo v0.1.0 ([ROOT]/foo)`
193-
194-
Caused by:
195-
failed to load source for dependency `dep`
196-
197-
Caused by:
198-
Unable to update [ROOTURL]/dep
199-
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!
205360
206361
"#]])
207362
.run();
363+
add_git_vendor_config(&p, &git_project);
364+
365+
assert_e2e().eq(
366+
p.read_file("vendor/dep/Cargo.toml"),
367+
str![[r##"
368+
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
369+
#
370+
# When uploading crates to the registry Cargo will automatically
371+
# "normalize" Cargo.toml files for maximal compatibility
372+
# with all versions of Cargo and also rewrite `path` dependencies
373+
# to registry (e.g., crates.io) dependencies.
374+
#
375+
# If you are reading this file be aware that the original Cargo.toml
376+
# will likely look very different (and much more reasonable).
377+
# See Cargo.toml.orig for the original contents.
378+
379+
cargo-features = ["multiple-build-scripts"]
380+
381+
[package]
382+
edition = "2024"
383+
name = "dep"
384+
version = "0.1.0"
385+
authors = []
386+
build = [
387+
"build1.rs",
388+
"build2.rs",
389+
]
390+
include = [
391+
"src/main.rs",
392+
"build1.rs",
393+
]
394+
autolib = false
395+
autobins = false
396+
autoexamples = false
397+
autotests = false
398+
autobenches = false
399+
description = "dependency of foo"
400+
documentation = "docs.rs/dep"
401+
readme = false
402+
license = "MIT"
403+
404+
[[bin]]
405+
name = "dep"
406+
path = "src/main.rs"
407+
408+
"##]],
409+
);
410+
411+
p.cargo("check")
412+
.masquerade_as_nightly_cargo(&["multiple-build-scripts"])
413+
.run();
208414
}
209415

210416
#[cargo_test]

0 commit comments

Comments
 (0)