Skip to content

Commit 6a163e2

Browse files
rustc: fix building with llvm (#320432)
1 parent b204bd7 commit 6a163e2

2 files changed

Lines changed: 54 additions & 12 deletions

File tree

pkgs/development/compilers/rust/1_79.nix

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,45 @@
1717
, wrapRustcWith
1818
, llvmPackages_18, llvm_18
1919
} @ args:
20-
20+
let
21+
llvmSharedFor = pkgSet: pkgSet.llvmPackages_18.libllvm.override ({
22+
enableSharedLibraries = true;
23+
} // lib.optionalAttrs (stdenv.targetPlatform.useLLVM or false) {
24+
# Force LLVM to compile using clang + LLVM libs when targeting pkgsLLVM
25+
stdenv = pkgSet.stdenv.override { allowedRequisites = null; cc = pkgSet.llvmPackages_18.clangUseLLVM; };
26+
});
27+
in
2128
import ./default.nix {
2229
rustcVersion = "1.79.0";
2330
rustcSha256 = "sha256-Fy7PPH0fnZ+xbNKmKIaXgmcEFt7QEp5SSoZ1H5YUSMA=";
2431

25-
llvmSharedForBuild = pkgsBuildBuild.llvmPackages_18.libllvm.override { enableSharedLibraries = true; };
26-
llvmSharedForHost = pkgsBuildHost.llvmPackages_18.libllvm.override { enableSharedLibraries = true; };
27-
llvmSharedForTarget = pkgsBuildTarget.llvmPackages_18.libllvm.override { enableSharedLibraries = true; };
32+
llvmSharedForBuild = llvmSharedFor pkgsBuildBuild;
33+
llvmSharedForHost = llvmSharedFor pkgsBuildHost;
34+
llvmSharedForTarget = llvmSharedFor pkgsBuildTarget;
2835

2936
# For use at runtime
30-
llvmShared = llvm_18.override { enableSharedLibraries = true; };
37+
llvmShared = llvmSharedFor { inherit llvmPackages_18 stdenv; };
3138

3239
# Expose llvmPackages used for rustc from rustc via passthru for LTO in Firefox
33-
llvmPackages = llvmPackages_18;
40+
llvmPackages = if (stdenv.targetPlatform.useLLVM or false) then (let
41+
setStdenv = pkg: pkg.override {
42+
stdenv = stdenv.override { allowedRequisites = null; cc = llvmPackages_18.clangUseLLVM; };
43+
};
44+
in rec {
45+
libunwind = setStdenv llvmPackages_18.libunwind;
46+
llvm = setStdenv llvmPackages_18.llvm;
47+
48+
libcxx = llvmPackages_18.libcxx.override {
49+
stdenv = stdenv.override {
50+
allowedRequisites = null;
51+
cc = llvmPackages_18.clangNoLibcxx;
52+
hostPlatform = stdenv.hostPlatform // {
53+
useLLVM = !stdenv.isDarwin;
54+
};
55+
};
56+
inherit libunwind;
57+
};
58+
}) else llvmPackages_18;
3459

3560
# Note: the version MUST be one version prior to the version we're
3661
# building

pkgs/development/compilers/rust/rustc.nix

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{ lib, stdenv, removeReferencesTo, pkgsBuildBuild, pkgsBuildHost, pkgsBuildTarget, targetPackages
22
, llvmShared, llvmSharedForBuild, llvmSharedForHost, llvmSharedForTarget, llvmPackages
3-
, fetchurl, file, python3
3+
, runCommandLocal, fetchurl, file, python3
44
, darwin, cargo, cmake, rustc, rustfmt
55
, pkg-config, openssl, xz
66
, libiconv
@@ -24,6 +24,7 @@
2424
let
2525
inherit (lib) optionals optional optionalString concatStringsSep;
2626
inherit (darwin.apple_sdk.frameworks) Security;
27+
useLLVM = stdenv.targetPlatform.useLLVM or false;
2728
in stdenv.mkDerivation (finalAttrs: {
2829
pname = "${targetPackages.stdenv.cc.targetPrefix}rustc";
2930
inherit version;
@@ -66,14 +67,16 @@ in stdenv.mkDerivation (finalAttrs: {
6667
# when linking stage1 libstd: cc: undefined reference to `__cxa_begin_catch'
6768
# This doesn't apply to cross-building for FreeBSD because the host
6869
# uses libstdc++, but the target (used for building std) uses libc++
69-
optional (stdenv.isLinux && !withBundledLLVM && !stdenv.targetPlatform.isFreeBSD) "--push-state --as-needed -lstdc++ --pop-state"
70+
optional (stdenv.isLinux && !withBundledLLVM && !stdenv.targetPlatform.isFreeBSD && !useLLVM)
71+
"--push-state --as-needed -lstdc++ --pop-state"
72+
++ optional (stdenv.isLinux && !withBundledLLVM && !stdenv.targetPlatform.isFreeBSD && useLLVM)
73+
"--push-state --as-needed -L${llvmPackages.libcxx}/lib -lc++ -lc++abi -lLLVM-${lib.versions.major llvmPackages.llvm.version} --pop-state"
7074
++ optional (stdenv.isDarwin && !withBundledLLVM) "-lc++ -lc++abi"
7175
++ optional stdenv.isFreeBSD "-rpath ${llvmPackages.libunwind}/lib"
72-
++ optional stdenv.isDarwin "-rpath ${llvmSharedForHost}/lib");
76+
++ optional stdenv.isDarwin "-rpath ${llvmSharedForHost.lib}/lib");
7377

7478
# Increase codegen units to introduce parallelism within the compiler.
7579
RUSTFLAGS = "-Ccodegen-units=10";
76-
7780
RUSTDOCFLAGS = "-A rustdoc::broken-intra-doc-links";
7881

7982
# We need rust to build rust. If we don't provide it, configure will try to download it.
@@ -152,7 +155,7 @@ in stdenv.mkDerivation (finalAttrs: {
152155
# Since fastCross only builds std, it doesn't make sense (and
153156
# doesn't work) to build a linker.
154157
"--disable-llvm-bitcode-linker"
155-
] ++ optionals (stdenv.isLinux && !stdenv.targetPlatform.isRedox) [
158+
] ++ optionals (stdenv.isLinux && !stdenv.targetPlatform.isRedox && !(stdenv.targetPlatform.useLLVM or false)) [
156159
"--enable-profiler" # build libprofiler_builtins
157160
] ++ optionals stdenv.buildPlatform.isMusl [
158161
"${setBuild}.musl-root=${pkgsBuildBuild.targetPackages.stdenv.cc.libc}"
@@ -165,6 +168,10 @@ in stdenv.mkDerivation (finalAttrs: {
165168
] ++ optionals (stdenv.isDarwin && stdenv.isx86_64) [
166169
# https://github.com/rust-lang/rust/issues/92173
167170
"--set rust.jemalloc"
171+
] ++ optionals useLLVM [
172+
# https://github.com/NixOS/nixpkgs/issues/311930
173+
"--llvm-libunwind=${if withBundledLLVM then "in-tree" else "system"}"
174+
"--enable-use-libcxx"
168175
];
169176

170177
# if we already have a rust compiler for build just compile the target std
@@ -189,6 +196,7 @@ in stdenv.mkDerivation (finalAttrs: {
189196
python ./x.py --keep-stage=0 --stage=1 install library/std
190197
mkdir -v $out/bin $doc $man
191198
ln -s ${rustc.unwrapped}/bin/{rustc,rustdoc} $out/bin
199+
rm -rf -v $out/lib/rustlib/{manifest-rust-std-,}${stdenv.hostPlatform.rust.rustcTargetSpec}
192200
ln -s ${rustc.unwrapped}/lib/rustlib/{manifest-rust-std-,}${stdenv.hostPlatform.rust.rustcTargetSpec} $out/lib/rustlib/
193201
echo rust-std-${stdenv.hostPlatform.rust.rustcTargetSpec} >> $out/lib/rustlib/components
194202
lndir ${rustc.doc} $doc
@@ -248,7 +256,16 @@ in stdenv.mkDerivation (finalAttrs: {
248256

249257
buildInputs = [ openssl ]
250258
++ optionals stdenv.isDarwin [ libiconv Security ]
251-
++ optional (!withBundledLLVM) llvmShared;
259+
++ optional (!withBundledLLVM) llvmShared.lib
260+
++ optional (useLLVM && !withBundledLLVM) [
261+
llvmPackages.libunwind
262+
# Hack which is used upstream https://github.com/gentoo/gentoo/blob/master/dev-lang/rust/rust-1.78.0.ebuild#L284
263+
(runCommandLocal "libunwind-libgcc" {} ''
264+
mkdir -p $out/lib
265+
ln -s ${llvmPackages.libunwind}/lib/libunwind.so $out/lib/libgcc_s.so
266+
ln -s ${llvmPackages.libunwind}/lib/libunwind.so $out/lib/libgcc_s.so.1
267+
'')
268+
];
252269

253270
outputs = [ "out" "man" "doc" ];
254271
setOutputFlags = false;

0 commit comments

Comments
 (0)