Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doc test link failure with native dependencies #1401

Closed
dcuddeback opened this issue Mar 10, 2015 · 3 comments · Fixed by #1405
Closed

Doc test link failure with native dependencies #1401

dcuddeback opened this issue Mar 10, 2015 · 3 comments · Fixed by #1405

Comments

@dcuddeback
Copy link

This is reopening #681, which was closed by the reporter without a response from the Rust team. In #681, the reporter was able to work around it by forcing static linking, but I don't think that should be a requirement for writing examples in documentation.

When building a crate with native dependencies (such as libusb), the doc tests fail to link because the flags from the build script are not included in the linker command. I distilled it down to a minimal example, which is hosted here: https://github.com/dcuddeback/rust-link-bug

The doc example can be anything. It doesn't have to be related to the native library:

/// Returns the string "hello".
///
/// ```
/// bug::hello(); // returns "hello"
/// ```
pub fn hello() -> &'static str {
  "hello"
}

It seems the only requirement for triggering this bug is to have a links property in Cargo.toml for a library that requires additional linker flags. For this example, I'm linking to libusb-1.0:

[package]

name = "bug"
version = "0.0.1"
authors = ["David Cuddeback <david.cuddeback@gmail.com>"]

build = "build.rs"
links = "usb-1.0"

I wrote a simple build script to provide the correct flags on my system. This is also the output from using pkg-config-rs on my system, but I wanted to minimize the moving parts in my example.

fn main() {
  println!("cargo:rustc-flags=-L native=/usr/local/Cellar/libusb/1.0.19/lib");
  println!("cargo:rustc-flags=-l usb-1.0");
}

Compiling with cargo build works fine. Running cargo test produces this link error:

failures:

---- hello_0 stdout ----
    error: linking with `cc` failed: exit code: 1
note: "cc" "-m64" "-L" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib" "-o" "/var/folders/7f/z9y9c0gj52gb38jlrsr2pzkc0000gn/T/rustdoctest.uFWruUs3fErH/rust-out" "/var/folders/7f/z9y9c0gj52gb38jlrsr2pzkc0000gn/T/rustdoctest.uFWruUs3fErH/rust_out.o" "-Wl,-force_load,/usr/local/lib/rustlib/x86_64-apple-darwin/lib/libmorestack.a" "-Wl,-dead_strip" "-nodefaultlibs" "/Users/david/src/bug/target/libbug-a0a1f90974336c26.rlib" "-L" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib" "-lstd-4e7c5e5c" "-L" "/Users/david/src/bug/target" "-L" "/Users/david/src/bug/target/deps" "-L" "/usr/local/lib/rustlib/x86_64-apple-darwin/lib" "-L" "/Users/david/src/bug/.rust/lib/x86_64-apple-darwin" "-L" "/Users/david/src/bug/lib/x86_64-apple-darwin" "-lusb-1.0" "-lSystem" "-lpthread" "-lc" "-lm" "-lcompiler-rt"
note: ld: warning: directory not found for option '-L/Users/david/src/bug/.rust/lib/x86_64-apple-darwin'
ld: warning: directory not found for option '-L/Users/david/src/bug/lib/x86_64-apple-darwin'
ld: library not found for -lusb-1.0
clang: error: linker command failed with exit code 1 (use -v to see invocation)

error: aborting due to previous error
thread 'hello_0' panicked at 'Box<Any>', /Users/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-mac/build/src/libsyntax/diagnostic.rs:151

The directory /usr/local/Cellar/libusb/1.0.19/lib is missing from the linker flags.

@alexcrichton
Copy link
Member

Ah yes looks like we're not propagating the -L flags all the way through to the rustdoc invocation itself. Could you run cargo test -v to confirm that rustdoc is not receiving the -L flag?

@dcuddeback
Copy link
Author

@alexcrichton Confirmed, it gets target and target/deps, but not /usr/local/Cellar/libusb/1.0.19/lib:

Running `rustdoc --test /Users/david/src/bug/src/lib.rs --crate-name bug -L /Users/david/src/bug/target -L /Users/david/src/bug/target/deps --extern bug=/Users/david/src/bug/target/libbug-a0a1f90974336c26.rlib`

Not sure if you need this, too, but my Cargo version is about a week old:

cargo 0.0.1-pre-nightly (dd7c7bd 2015-03-04) (built 2015-03-04)

@alexcrichton
Copy link
Member

I think I fixed this in #1441, but feel free to reopen if it still reproduces!

alexcrichton added a commit to alexcrichton/cargo that referenced this issue Mar 26, 2015
bors added a commit that referenced this issue Mar 26, 2015
github-merge-queue bot pushed a commit that referenced this issue Feb 28, 2025
…15245)

### What does this PR try to resolve?

GitHub Runner Images 20250224.5.0+ ship Windows 11 SDK 10.0.26100+
compared to the previous Windows 11 SDK 10.0.22621, which bumped the
UCRT headers. The new UCRT headers use SSE2 types. However, `cc`
versions <= 1.2.15 emit `/arch:IA32` for `x86` Windows targets for
`clang-cl`, which causes compilation errors since `clang-cl` can't find
SSE2 types without `/arch:SSE2` specified (or defaulted). (Note that
MSVC at the time of writing silently accepts and emits instruments for
code using SSE2 types, as opposed to `clang-cl` hard error-ing).

`cc` 1.2.16 contains a fix for this problem,
rust-lang/cc-rs#1425, to correctly emit
`/arch:SSE2` instead of `/arch:IA32` to enable `clang-cl` to find the
SSE2 types. However, cargo's `cc` currently is still on 1.2.13.

To fix this for rust-lang/rust CI, we need to bump anything that
transitively relies on `cc` and tries to use `clang-cl` on `x86` Windows
targets to compile any C/C++ code that transitively use functions or
types that require SSE2 types, such as `<wchar.h>`.

### How should we test and review this PR?

The fix was initially intended for `rustc_{codegen_ssa,llvm}` `cc`, and
based on testing in rust-lang/rust#137724, I was
able to successfully build `rustc_{codegen_ssa,llvm}` with a forked `cc`
based on 1.2.15 which contains the fix from
rust-lang/cc-rs#1425. Note that in the same PR,
while the compiler build succeeded, the build of cargo itself failed
since it transitively used a `cc` *without* the fix to build
`curl-sys`[^dep-chain], which failed as one might expect (`curl-sys`
tries to build C code that uses `<wchar.h>` which runs into the same
problem). Hence, this PR is opened to bump cargo's `cc` to a `cc`
version containing the fix.

### Additional information

This `x86` Windows CI problem is:

- Discussed in
https://rust-lang.zulipchat.com/#narrow/channel/242791-t-infra/topic/spurious.20.28.3F.29.20i686.20msvc.20errors.
- Tracked by rust-lang/rust#137733.

#### `cc` changelog between 1.2.13 and 1.2.16

<details>
<summary>`cc` changes since 1.2.13 up to and including 1.2.16</summary>

#####
[1.2.16](rust-lang/cc-rs@cc-v1.2.15...cc-v1.2.16)
- 2025-02-28

###### Fixed

- force windows compiler to run in `out_dir` to prevent artifacts in cwd
(#1415)

###### Other

- use `/arch:SSE2` for `x86` target arch (#1425)
- Regenerate windows-sys binding
([#1422](rust-lang/cc-rs#1422))
- Regenerate target info
([#1418](rust-lang/cc-rs#1418))
- Add LIB var when compiling flag_check (#1417)
- Change flag ordering
([#1403](rust-lang/cc-rs#1403))
- Fix archiver detection for musl cross compilation
([#1404](rust-lang/cc-rs#1404))

#####
[1.2.15](rust-lang/cc-rs@cc-v1.2.14...cc-v1.2.15)
- 2025-02-21

###### Other

- Regenerate target info
([#1406](rust-lang/cc-rs#1406))
- Always read from all `CFLAGS`-style flags
([#1401](rust-lang/cc-rs#1401))
- Simplify the error output on failed `Command` invocation
([#1397](rust-lang/cc-rs#1397))

#####
[1.2.14](rust-lang/cc-rs@cc-v1.2.13...cc-v1.2.14)
- 2025-02-14

###### Other

- Regenerate target info
([#1398](rust-lang/cc-rs#1398))
- Add support for setting `-gdwarf-{version}` based on RUSTFLAGS
([#1395](rust-lang/cc-rs#1395))
- Add support for alternative network stack io-sock on QNX 7.1 aarch64
and x86_64 ([#1312](rust-lang/cc-rs#1312))

</details>

[^dep-chain]: I think the dep chain is something like git2 ->
libgit2-sys -> curl -> curl-sys?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants