From 10cf701de7e6da17787e5dd25819a1777cfa7d1d Mon Sep 17 00:00:00 2001 From: Travis Finkenauer Date: Tue, 26 Apr 2022 01:09:38 -0700 Subject: [PATCH 1/2] doc: discuss build script instruction order --- src/doc/src/reference/build-scripts.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/doc/src/reference/build-scripts.md b/src/doc/src/reference/build-scripts.md index 8c7729d52c2..8bf231f2883 100644 --- a/src/doc/src/reference/build-scripts.md +++ b/src/doc/src/reference/build-scripts.md @@ -77,6 +77,15 @@ Build scripts communicate with Cargo by printing to stdout. Cargo will interpret each line that starts with `cargo:` as an instruction that will influence compilation of the package. All other lines are ignored. +> Note: The order of `cargo:` instructions printed by the build script *may* +> affect the order of arguments that `cargo` passes to `rustc`. In turn, the +> order of arguments passed to `rustc` may affect the order of arguments passed +> to the linker. Therefore, you will want to pay attention to the order of the +> build script's instructions. For example, if object `foo` needs to link against +> library `bar`, you may want to make sure that library `bar`'s +> [`cargo:rustc-link-lib`](#rustc-link-lib) instruction appears *after* +> instructions to link object `foo`. + The output of the script is hidden from the terminal during normal compilation. If you would like to see the output directly in your terminal, invoke Cargo as "very verbose" with the `-vv` flag. This only happens when the From 43ce1e7a98c3bfb9cb1314c3a248e481d4a05234 Mon Sep 17 00:00:00 2001 From: Travis Finkenauer Date: Sun, 1 May 2022 15:54:20 -0700 Subject: [PATCH 2/2] Test that link argument order is maintained Patch provided by @ehuss --- tests/testsuite/build_script.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/testsuite/build_script.rs b/tests/testsuite/build_script.rs index d0ef3a2e727..8ca259d1e36 100644 --- a/tests/testsuite/build_script.rs +++ b/tests/testsuite/build_script.rs @@ -1789,10 +1789,13 @@ fn output_separate_lines_new() { fn main() { println!("cargo:rustc-link-search=foo"); println!("cargo:rustc-link-lib=static=foo"); + println!("cargo:rustc-link-lib=bar"); + println!("cargo:rustc-link-search=bar"); } "#, ) .build(); + // The order of the arguments passed to rustc is important. p.cargo("build -v") .with_status(101) .with_stderr_contains( @@ -1800,7 +1803,7 @@ fn output_separate_lines_new() { [COMPILING] foo v0.5.0 ([CWD]) [RUNNING] `rustc [..] build.rs [..]` [RUNNING] `[..]/foo-[..]/build-script-build` -[RUNNING] `rustc --crate-name foo [..] -L foo -l static=foo` +[RUNNING] `rustc --crate-name foo [..] -L foo -L bar -l static=foo -l bar` [ERROR] could not find native static library [..] ", )