Repository navigation
fix: link the static C++ stdlib with -bundle, once per Build - #1955
Conversation
With `cpp_link_stdlib_static(true)`, cc emitted `rustc-link-lib=static=stdc++`, which makes rustc look up the archive in its own search paths and bundle it into the rlib. That fails when the archive is only in the toolchain's directories, as with MinGW. Emit `static:-bundle=` instead so the linker of the final binary resolves it. rustc rejects a library passed twice when either mention has link modifiers, so the line is emitted once per `Build` (shared with its clones), however many times it compiles. `wasm32` and `pauthtest`, where cc may also link the C++ stdlib through `rustc-flags`, keep `static=`. So do Apple targets, where rustc passes no static hint and ld64 prefers the dylib, so an unbundled stdlib would silently link dynamically. A C++ stdlib value that already names a link kind, such as `CXXSTDLIB=static:-bundle=stdc++`, is now emitted unchanged instead of getting a second `static=` prefix. cc-test now builds `baz` once, statically linked on `*-linux-gnu` targets, and no longer needs the `libstdc++.a` symlink. Its CUDA build sets `cpp_link_stdlib(None)` there, since `baz` already links stdc++. Fixes rust-lang#1856
|
One data point before review. A GitHub code search for Are you fine with that as a documented change, or would you rather dedupe this one line per build script process after all? |
NobodyXu
left a comment
There was a problem hiding this comment.
Thank you! Will cut a new release afterwards today
Hmm I think we'd have to ship the right behaviour instead of bundling stdc++ into rlib |
Problem
With
cpp_link_stdlib_static(true), cc emitscargo:rustc-link-lib=static=stdc++. rustc then looks uplibstdc++.ain its own search paths and bundles it into the rlib. That fails when the archive only lives in the toolchain's directories, as with MinGW. It is also why cc-test needed alibstdc++.asymlink inOUT_DIR. ACXXSTDLIBvalue that already names a kind, such asstatic:-bundle=stdc++, got a secondstatic=prefix.Change
static:-bundle=<stdlib>, so the linker of the final binary finds it.Build. The state lives in theBuild's cache, which its clones share, so severalcompile()calls on oneBuildor its clones emit it once. SeparateBuilds don't share it.wasm32,pauthtestand Apple targets keepstatic=<stdlib>. Onwasm32andpauthtestcc may also pass-lstatic=c++or-lc++throughrustc-flags, and a second mention with modifiers would fail. On Apple targets rustc passes no static hint and ld64 prefers the dylib, so-bundlewould silently link it dynamically.=is emitted unchanged, once perBuild.cpp_link_stdlib_staticdescribe this and what to do when twoBuilds link the same stdlib.baztwice on Linux, linkingstdc++once dynamically and once statically, which now fails in rustc. It now buildsbazonce, with the static stdlib on*-linux-gnutargets. The symlink is removed, since rustc no longer looks up the archive. The check now uses the target instead of the host, so the Apple cross jobs on Linux no longer try to link the host'slibstdc++.a.cpp_link_stdlib(None)on*-linux-gnu, sincebazalready linksstdc++statically there.Validation
Buildand a clone emit the stdlib once. Two separateBuilds emit it twice. A compile with metadata off doesn't count.wasm32,pauthtestandx86_64-apple-darwinkeepstatic=.CXXSTDLIB=static:-bundle=c++andCXXSTDLIB=dylib=stdc++are passed through. The test sets its target and host, so it doesn't depend on the CI host.cargo test,cargo fmt --checkandcargo clippy --workspace --all-targets -- -D warnings(also with--features cc-test/test_cuda) pass on Windows (MSVC), and cc-test builds and passes there. On CI, cc-test links libstdc++ statically without the symlink on x86_64 and i686 Linux, and the CUDA job passes. MinGW, the reporter's setup, is not covered by cc-test.-l static:-bundle=foobuilds an rlib without findingfoo, while-l static:-bundle=foo -l foofails with "overriding linking modifiers from command line is not supported".Limitations
Builds with this option, or links it both statically and dynamically, now fails in rustc. The docs suggest oneBuildor its clones, orcpp_link_stdlib(None)on the others.staticlibno longer contains the C++ stdlib objects. It is listed by--print native-static-libsand has to be linked by the user.cpp_link_stdliboverridingCXXSTDLIB, is not covered here.Thanks @kb-1000 for the report and for handing this over.
Fixes #1856