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
2424let
2525 inherit ( lib ) optionals optional optionalString concatStringsSep ;
2626 inherit ( darwin . apple_sdk . frameworks ) Security ;
27+ useLLVM = stdenv . targetPlatform . useLLVM or false ;
2728in 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