Skip to content

Commit 39ad103

Browse files
committed
Auto merge of #10600 - tmfink:doc-build-script-link-order, r=ehuss
doc: discuss build script instruction order ### What does this PR try to resolve? It is currently not documented that the order of build script `cargo:` instructions may be relevant to linking. This has caused issues such as: rust-lang/rust#96328 ### How should we test and review this PR? Build/view documentation. ### Additional information - Cargo maintainers should fact check my wording. - We may need to discuss if this should also be documented for `rustc` - Maintainers should ensure that this change does not prevent a change in what is currently unspecified behavior. Perhaps `cargo` will want to rearrange link arguments itself to resolve issues in the future?
2 parents e7c8ba7 + 43ce1e7 commit 39ad103

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/doc/src/reference/build-scripts.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ Build scripts communicate with Cargo by printing to stdout. Cargo will
7777
interpret each line that starts with `cargo:` as an instruction that will
7878
influence compilation of the package. All other lines are ignored.
7979

80+
> Note: The order of `cargo:` instructions printed by the build script *may*
81+
> affect the order of arguments that `cargo` passes to `rustc`. In turn, the
82+
> order of arguments passed to `rustc` may affect the order of arguments passed
83+
> to the linker. Therefore, you will want to pay attention to the order of the
84+
> build script's instructions. For example, if object `foo` needs to link against
85+
> library `bar`, you may want to make sure that library `bar`'s
86+
> [`cargo:rustc-link-lib`](#rustc-link-lib) instruction appears *after*
87+
> instructions to link object `foo`.
88+
8089
The output of the script is hidden from the terminal during normal
8190
compilation. If you would like to see the output directly in your terminal,
8291
invoke Cargo as "very verbose" with the `-vv` flag. This only happens when the

tests/testsuite/build_script.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1789,18 +1789,21 @@ fn output_separate_lines_new() {
17891789
fn main() {
17901790
println!("cargo:rustc-link-search=foo");
17911791
println!("cargo:rustc-link-lib=static=foo");
1792+
println!("cargo:rustc-link-lib=bar");
1793+
println!("cargo:rustc-link-search=bar");
17921794
}
17931795
"#,
17941796
)
17951797
.build();
1798+
// The order of the arguments passed to rustc is important.
17961799
p.cargo("build -v")
17971800
.with_status(101)
17981801
.with_stderr_contains(
17991802
"\
18001803
[COMPILING] foo v0.5.0 ([CWD])
18011804
[RUNNING] `rustc [..] build.rs [..]`
18021805
[RUNNING] `[..]/foo-[..]/build-script-build`
1803-
[RUNNING] `rustc --crate-name foo [..] -L foo -l static=foo`
1806+
[RUNNING] `rustc --crate-name foo [..] -L foo -L bar -l static=foo -l bar`
18041807
[ERROR] could not find native static library [..]
18051808
",
18061809
)

0 commit comments

Comments
 (0)