-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
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
pkgsStatic.llvmPackages.libcxxStdenv does not produce static c++ binaries #111010
Comments
@dtzWill Is there an LLVM version where this is known to work? |
#118313 should fix this for the most obvious cases (can you check your reproducer against that PR?). |
I marked this as stale due to inactivity. → More info |
cc @Ericson2314 @sternenseemann @Mic92 I think I've found a reason why static+llvm stdenvs are broken. Another issue here is that;
nixpkgs/pkgs/stdenv/adapters.nix Line 64 in b8960cd
☝️ This one, built with the gcc stdenv, does not have a dynamic linker set. 2a) LLVM sets a dynamic linker anyway even if you pass
☝️ This fails during the check phase, because the produced executable segfault on startup. 👇 The reason they segfault is because the dynamic linker is running on the (static) binary, which does not work.
nixpkgs/pkgs/build-support/cc-wrapper/cc-wrapper.sh Lines 176 to 177 in b8960cd
nixpkgs/pkgs/build-support/wrapper-common/utils.bash Lines 133 to 144 in a1bcfbc
I think if this is fixed more things will work. I wonder if GCC and/or LLVM should refuse the combination of |
Without this, pkgsStatic.pkgsLLVM.hello fails with segfaulting binaries because of the issue described at [0]. In summary, llvm's linker has a different behaviour to GCC's when supplied with both -static and -Wl,-dynamic-linker=...; GCC copes with it, but LLVM produces a binary which segfaults on startup. It appears to be necessary to omit the dynamic linker in this case. nixpkgs' static adaptor passes -static via NIX_CFLAGS_LINK which was not accounted for prior to this commit in the checkLinkType logic. For good measure I put the other NIX_ flags affecting link in the same logic. Additionally, $NIX_CFLAGS_LINK_@suffixSalt@ is not available until later than it was originally set, so set $linkType close to its point of use. I checked for earlier uses by studying the shell trace output and couldn't find any. [0] #111010 (comment) Signed-off-by: Peter Waller <[email protected]>
@Artturin I think it should be. I just tried to build the example but I hit #177129 in the build of libcxxabi-static-x86_64-unknown-linux-musl, during the configure step to check the compiler is working. That is, that clang specifies gcc_eh to link, when it shouldn't, but this doesn't exist in a static-target gcc build. This is a clang issue, but clang doesn't have a way to know it shouldn't try to link gcc_eh. A workaround is to create an empty gcc_eh. For libcxxabi, it could be worked around via CMAKE_C_COMPILER_WORKS, but I think a more general solution is needed. Perhaps to provide an empty gcc_eh in static builds so that clang's linker driver can function. |
There is an additional problem. -lc++abi is appearing on the link line before -lc++ which is the root cause of the original messages in this thread. Additionally, -lc needs to appear after both of these. With those changes, I get a binary I can run.
So as a workaround, I can get a functioning binary by compiling the original example with |
Describe the bug
Using
pkgs.pkgsStatic
, it is easily possible to compile static c++ binaries using-static
.The same does not seem to work if
stdenv = pkgsStatic.llvmPackages.libcxxStdenv;
.To Reproduce
Minimal working example:
the gcc derivation works well:
$ ldd $(nix-build static.nix -A hello-static-gcc)/bin/hello not a dynamic executable
With clang, the following happens:
Looking at
pkgs/development/compilers/llvm/11/libc++/default.nix
andpkgs/development/compilers/llvm/11/libc++abi.nix
i see that there is someenableShared
parametrization which hints at static binaries being possible, but from looking at the code i don't know if that is already supposed to work out of the box or still needs some tweaking.Expected behavior
Using clang with libcxx on c++ programs with
-static
should result in successful compilation of static binaries.Notify maintainers
@vlstill @lovek323 @dtzWill @primeos
Metadata
Please run
nix-shell -p nix-info --run "nix-info -m"
and paste the result.The text was updated successfully, but these errors were encountered: