rustc --crate-type staticlib --print native-static-libs includes gcc_s
Update I was confused about gcc_eh thinking it was the static version of gcc_s. So have that in mind when reading below.
Im trying to build a static binary that links statically with an external library thru a pkg-config generated by cargo-c. But it fails because it tries to link with -lgcc_s
when I think it should link with -lgcc_eh
.
The reason this happens is because cargo-c uses the output from rustc -print native-static-libs
(https://github.com/lu-zero/cargo-c/blob/master/src/static_libs.rs) to determine which libraries to include in the generated .pc-file.
Reproduction:
# setup
$ docker run --rm -ti -v $PWD:$PWD -w $PWD alpine:3.12.0
$ apk add --no-cache build-base rust cargo
$ cargo install cargo-c
$ USER=root cargo new statictest --lib
$ cd statictest
$ echo | rustc --crate-type staticlib --print native-static-libs - --out-dir target --target x86_64-alpine-linux-musl
note: Link against the following native artifacts when linking against this static library. The order and any duplication can be significant on some platforms.
note: native-static-libs: -lssp_nonshared -lgcc_s -lc
# target-feature=+crt-static does not seem to make a difference
$ echo | RUSTFLAGS="-C target-feature=+crt-static" rustc --crate-type staticlib --print native-static-libs - --out-dir target --target x86_64-alpine-linux-musl
note: Link against the following native artifacts when linking against this static library. The order and any duplication can be significant on some platforms.
note: native-static-libs: -lssp_nonshared -lgcc_s -lc
Strangely there is a patch for the alpine version of rust that should address this https://git.alpinelinux.org/aports/tree/community/rust/0006-Prefer-libgcc_eh-over-libunwind-for-musl.patch?h=3.12-stable but it seems to not work in this case.
Also there are alpine packages that uses cargo-c to generate .pc files that might be wrong because of this. For example https://git.alpinelinux.org/aports/tree/testing/rav1e/APKBUILD has a .pc-file with Libs.private: -lssp_nonshared -lgcc_s -lc
.