Skip to content
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

rustc: fix building with llvm #320432

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions pkgs/development/compilers/rust/1_79.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,45 @@
, wrapRustcWith
, llvmPackages_18, llvm_18
} @ args:

let
llvmSharedFor = pkgSet: pkgSet.llvmPackages_18.libllvm.override ({
enableSharedLibraries = true;
} // lib.optionalAttrs (stdenv.targetPlatform.useLLVM or false) {
# Force LLVM to compile using clang + LLVM libs when targeting pkgsLLVM
stdenv = pkgSet.stdenv.override { allowedRequisites = null; cc = pkgSet.llvmPackages_18.clangUseLLVM; };
});
in
import ./default.nix {
rustcVersion = "1.79.0";
rustcSha256 = "sha256-Fy7PPH0fnZ+xbNKmKIaXgmcEFt7QEp5SSoZ1H5YUSMA=";

llvmSharedForBuild = pkgsBuildBuild.llvmPackages_18.libllvm.override { enableSharedLibraries = true; };
llvmSharedForHost = pkgsBuildHost.llvmPackages_18.libllvm.override { enableSharedLibraries = true; };
llvmSharedForTarget = pkgsBuildTarget.llvmPackages_18.libllvm.override { enableSharedLibraries = true; };
llvmSharedForBuild = llvmSharedFor pkgsBuildBuild;
llvmSharedForHost = llvmSharedFor pkgsBuildHost;
llvmSharedForTarget = llvmSharedFor pkgsBuildTarget;

# For use at runtime
llvmShared = llvm_18.override { enableSharedLibraries = true; };
llvmShared = llvmSharedFor { inherit llvmPackages_18 stdenv; };

# Expose llvmPackages used for rustc from rustc via passthru for LTO in Firefox
llvmPackages = llvmPackages_18;
llvmPackages = if (stdenv.targetPlatform.useLLVM or false) then (let
setStdenv = pkg: pkg.override {
stdenv = stdenv.override { allowedRequisites = null; cc = llvmPackages_18.clangUseLLVM; };
};
in rec {
libunwind = setStdenv llvmPackages_18.libunwind;
llvm = setStdenv llvmPackages_18.llvm;

libcxx = llvmPackages_18.libcxx.override {
stdenv = stdenv.override {
allowedRequisites = null;
cc = llvmPackages_18.clangNoLibcxx;
hostPlatform = stdenv.hostPlatform // {
useLLVM = !stdenv.isDarwin;
};
};
inherit libunwind;
};
}) else llvmPackages_18;

# Note: the version MUST be one version prior to the version we're
# building
Expand Down
29 changes: 23 additions & 6 deletions pkgs/development/compilers/rust/rustc.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{ lib, stdenv, removeReferencesTo, pkgsBuildBuild, pkgsBuildHost, pkgsBuildTarget, targetPackages
, llvmShared, llvmSharedForBuild, llvmSharedForHost, llvmSharedForTarget, llvmPackages
, fetchurl, file, python3
, runCommandLocal, fetchurl, file, python3
, darwin, cargo, cmake, rustc, rustfmt
, pkg-config, openssl, xz
, libiconv
Expand All @@ -24,6 +24,7 @@
let
inherit (lib) optionals optional optionalString concatStringsSep;
inherit (darwin.apple_sdk.frameworks) Security;
useLLVM = stdenv.targetPlatform.useLLVM or false;
in stdenv.mkDerivation (finalAttrs: {
pname = "${targetPackages.stdenv.cc.targetPrefix}rustc";
inherit version;
Expand Down Expand Up @@ -66,14 +67,16 @@ in stdenv.mkDerivation (finalAttrs: {
# when linking stage1 libstd: cc: undefined reference to `__cxa_begin_catch'
# This doesn't apply to cross-building for FreeBSD because the host
# uses libstdc++, but the target (used for building std) uses libc++
optional (stdenv.isLinux && !withBundledLLVM && !stdenv.targetPlatform.isFreeBSD) "--push-state --as-needed -lstdc++ --pop-state"
optional (stdenv.isLinux && !withBundledLLVM && !stdenv.targetPlatform.isFreeBSD && !useLLVM)
"--push-state --as-needed -lstdc++ --pop-state"
++ optional (stdenv.isLinux && !withBundledLLVM && !stdenv.targetPlatform.isFreeBSD && useLLVM)
"--push-state --as-needed -L${llvmPackages.libcxx}/lib -lc++ -lc++abi -lLLVM-${lib.versions.major llvmPackages.llvm.version} --pop-state"
++ optional (stdenv.isDarwin && !withBundledLLVM) "-lc++ -lc++abi"
++ optional stdenv.isFreeBSD "-rpath ${llvmPackages.libunwind}/lib"
++ optional stdenv.isDarwin "-rpath ${llvmSharedForHost}/lib");
++ optional stdenv.isDarwin "-rpath ${llvmSharedForHost.lib}/lib");

# Increase codegen units to introduce parallelism within the compiler.
RUSTFLAGS = "-Ccodegen-units=10";

RUSTDOCFLAGS = "-A rustdoc::broken-intra-doc-links";

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

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

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

outputs = [ "out" "man" "doc" ];
setOutputFlags = false;
Expand Down