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

llvm/16: Fix compiler-rt missing sanitizers when using useLLVM #267814

Closed
wants to merge 14 commits into from
15 changes: 11 additions & 4 deletions pkgs/development/compilers/llvm/16/compiler-rt/default.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{ lib, stdenv, llvm_meta, version
, monorepoSrc, runCommand
, cmake, ninja, python3, xcbuild, libllvm, linuxHeaders, libxcrypt
, cmake, ninja, python3, xcbuild, libllvm, libcxx, linuxHeaders, libxcrypt
, doFakeLibgcc ? stdenv.hostPlatform.isFreeBSD
}:

Expand Down Expand Up @@ -50,14 +50,21 @@ stdenv.mkDerivation {
"-DCMAKE_ASM_COMPILER_TARGET=${stdenv.hostPlatform.config}"
] ++ lib.optionals (haveLibc && stdenv.hostPlatform.libc == "glibc") [
"-DSANITIZER_COMMON_CFLAGS=-I${libxcrypt}/include"
] ++ lib.optionals (useLLVM || bareMetal || isMusl || isDarwinStatic) [
] ++ lib.optionals (useLLVM && stdenv.cc.libcxx == libcxx) [
"-DSANITIZER_CXX_ABI=libcxxabi"
"-DSANITIZER_CXX_ABI_LIBNAME=libcxxabi"
"-DCOMPILER_RT_USE_BUILTINS_LIBRARY=ON"
] ++ lib.optionals (useLLVM && haveLibc) [
"-DCOMPILER_RT_BUILD_SANITIZERS=ON"
"-DCOMPILER_RT_BUILD_PROFILE=ON"
] ++ lib.optionals ((useLLVM && !haveLibc) || bareMetal || isMusl || isDarwinStatic) [
"-DCOMPILER_RT_BUILD_SANITIZERS=OFF"
"-DCOMPILER_RT_BUILD_PROFILE=OFF"
] ++ lib.optionals (useLLVM || bareMetal || isMusl || isDarwinStatic) [
"-DCOMPILER_RT_BUILD_XRAY=OFF"
"-DCOMPILER_RT_BUILD_LIBFUZZER=OFF"
"-DCOMPILER_RT_BUILD_MEMPROF=OFF"
"-DCOMPILER_RT_BUILD_ORC=OFF" # may be possible to build with musl if necessary
] ++ lib.optionals (useLLVM || bareMetal) [
"-DCOMPILER_RT_BUILD_PROFILE=OFF"
] ++ lib.optionals ((useLLVM && !haveLibc) || bareMetal || isDarwinStatic ) [
"-DCMAKE_CXX_COMPILER_WORKS=ON"
] ++ lib.optionals ((useLLVM && !haveLibc) || bareMetal) [
Expand Down
139 changes: 85 additions & 54 deletions pkgs/development/compilers/llvm/16/default.nix
Copy link
Member

@Artturin Artturin Nov 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't reformat the file, it needs to be similar to the other llvm versions for diffing.

It's impossible to tell what changes you've made in it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry. it was accidental. I will fix it and push, but it'll have to be sunday.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{ lowPrio, newScope, pkgs, lib, stdenv, cmake, ninja
, preLibcCrossHeaders
, libxml2, python3, fetchFromGitHub, overrideCC, wrapCCWith, wrapBintoolsWith
, libxcrypt
, buildLlvmTools # tools, but from the previous stage, for cross
, targetLlvmLibraries # libraries, but from the next stage, for cross
, targetLlvm
Expand Down Expand Up @@ -74,9 +75,13 @@ in let
ln -s "${cc.lib}/lib/clang/${major}/include" "$rsrc"
echo "-resource-dir=$rsrc" >> $out/nix-support/cc-cflags
'';
mkExtraBuildCommandsNoLibc = cc: mkExtraBuildCommands0 cc + ''
ln -s "${targetLlvmLibraries.compiler-rt-no-libc.out}/lib" "$rsrc/lib"
ln -s "${targetLlvmLibraries.compiler-rt-no-libc.out}/share" "$rsrc/share"
'';
mkExtraBuildCommands = cc: mkExtraBuildCommands0 cc + ''
ln -s "${targetLlvmLibraries.compiler-rt.out}/lib" "$rsrc/lib"
ln -s "${targetLlvmLibraries.compiler-rt.out}/share" "$rsrc/share"
ln -s "${targetLlvmLibraries.compiler-rt-libc.out}/lib" "$rsrc/lib"
ln -s "${targetLlvmLibraries.compiler-rt-libc.out}/share" "$rsrc/share"
'';

bintoolsNoLibc' =
Expand All @@ -88,7 +93,7 @@ in let
then tools.bintools
else bootBintools;

in {
in rec {

libllvm = callPackage ./llvm {
inherit llvm_meta;
Expand Down Expand Up @@ -153,7 +158,7 @@ in let
};

lldb = callPackage ../common/lldb.nix {
src = callPackage ({ runCommand }: runCommand "lldb-src-${version}" {} ''
src = callPackage ({ runCommand }: runCommand "lldb-src-${version}" { } ''
mkdir -p "$out"
cp -r ${monorepoSrc}/cmake "$out"
cp -r ${monorepoSrc}/lldb "$out"
Expand Down Expand Up @@ -183,7 +188,7 @@ in let
# See here for some context:
# https://github.com/NixOS/nixpkgs/pull/194634#issuecomment-1272129132
++ lib.optional (
stdenv.targetPlatform.isDarwin
stdenv.targetPlatform.isDarwin
&& !stdenv.targetPlatform.isAarch64
&& (lib.versionOlder darwin.apple_sdk.sdk.version "11.0")
) ./lldb/cpu_subtype_arm64e_replacement.patch;
Expand Down Expand Up @@ -213,15 +218,15 @@ in let
libcxx = targetLlvmLibraries.libcxx;
bintools = bintools';
extraPackages = [
targetLlvmLibraries.compiler-rt
targetLlvmLibraries.compiler-rt-libc
] ++ lib.optionals (!stdenv.targetPlatform.isWasm) [
targetLlvmLibraries.libunwind
];
extraBuildCommands = mkExtraBuildCommands cc;
nixSupport.cc-cflags =
[ "-rtlib=compiler-rt"
"-Wno-unused-command-line-argument"
"-B${targetLlvmLibraries.compiler-rt}/lib"
"-B${targetLlvmLibraries.compiler-rt-libc}/lib"
]
++ lib.optional (!stdenv.targetPlatform.isWasm) "--unwindlib=libunwind"
++ lib.optional
Expand All @@ -231,62 +236,80 @@ in let
nixSupport.cc-ldflags = lib.optionals (!stdenv.targetPlatform.isWasm) [ "-L${targetLlvmLibraries.libunwind}/lib" ];
};

clangNoLibcxx = wrapCCWith rec {
clangWithLibcAndBasicRtAndLibcxx = wrapCCWith rec {
cc = tools.clang-unwrapped;
libcxx = null;
libcxx = targetLlvmLibraries.libcxx;
bintools = bintools';
extraPackages = [
targetLlvmLibraries.compiler-rt
targetLlvmLibraries.compiler-rt-no-libc
] ++ lib.optionals (!stdenv.targetPlatform.isWasm) [
targetLlvmLibraries.libunwind
];
extraBuildCommands = mkExtraBuildCommands cc;
nixSupport.cc-cflags =
[
"-rtlib=compiler-rt"
"-B${targetLlvmLibraries.compiler-rt}/lib"
"-nostdlib++"
]
++ lib.optional stdenv.targetPlatform.isWasm "-fno-exceptions";
extraBuildCommands = mkExtraBuildCommandsNoLibc cc;
nixSupport.cc-cflags = [
"-rtlib=compiler-rt"
"-Wno-unused-command-line-argument"
"-B${targetLlvmLibraries.compiler-rt-no-libc}/lib"
]
++ lib.optional (!stdenv.targetPlatform.isWasm) "--unwindlib=libunwind"
++ lib.optional
(!stdenv.targetPlatform.isWasm && stdenv.targetPlatform.useLLVM or false)
"-lunwind"
++ lib.optional stdenv.targetPlatform.isWasm "-fno-exceptions";
};

clangNoLibc = wrapCCWith rec {
clangWithLibcAndBasicRt = wrapCCWith rec {
cc = tools.clang-unwrapped;
libcxx = null;
bintools = bintoolsNoLibc';
bintools = bintools';
extraPackages = [
targetLlvmLibraries.compiler-rt
targetLlvmLibraries.compiler-rt-no-libc
];
extraBuildCommands = mkExtraBuildCommandsNoLibc cc;
nixSupport.cc-cflags = [
"-rtlib=compiler-rt"
"-Wno-unused-command-line-argument"
"-B${targetLlvmLibraries.compiler-rt-no-libc}/lib"
"-nostdlib++"
];
extraBuildCommands = mkExtraBuildCommands cc;
nixSupport.cc-cflags =
[
"-rtlib=compiler-rt"
"-B${targetLlvmLibraries.compiler-rt}/lib"
]
++ lib.optional stdenv.targetPlatform.isWasm "-fno-exceptions";
};

clangNoCompilerRt = wrapCCWith rec {
clangNoLibcWithBasicRt = wrapCCWith rec {
cc = tools.clang-unwrapped;
libcxx = null;
bintools = bintoolsNoLibc';
extraPackages = [ ];
extraBuildCommands = mkExtraBuildCommands0 cc;
nixSupport.cc-cflags =
[
"-nostartfiles"
]
++ lib.optional stdenv.targetPlatform.isWasm "-fno-exceptions";
extraPackages = [ targetLlvmLibraries.compiler-rt-no-libc ];
extraBuildCommands = mkExtraBuildCommandsNoLibc cc;
nixSupport.cc-cflags = [
"-rtlib=compiler-rt"
"-B${targetLlvmLibraries.compiler-rt-no-libc}/lib"
"-nostdlib++"
];
};

clangNoCompilerRtWithLibc = wrapCCWith (rec {
clangWithLibcAndNoRt = wrapCCWith rec {
cc = tools.clang-unwrapped;
libcxx = null;
bintools = bintools';
extraBuildCommands = mkExtraBuildCommands0 cc;
nixSupport.cc-cflags = [
"-nostdlib++"
];
};

clangNoLibcNoRt = wrapCCWith rec {
cc = tools.clang-unwrapped;
libcxx = null;
bintools = bintoolsNoLibc';
extraPackages = [ ];
extraBuildCommands = mkExtraBuildCommands0 cc;
} // lib.optionalAttrs stdenv.targetPlatform.isWasm {
nixSupport.cc-cflags = [ "-fno-exceptions" ];
});
nixSupport.cc-cflags = [
"-nostartfiles"
];
};

clangNoLibc = clangNoLibcNoRt;
clangNoCompilerRtWithLibc = clangWithLibcAndNoRt;
# Has to be in tools despite mostly being a library,
# because we use a native helper executable from a
# non-cross build in cross builds.
Expand All @@ -297,42 +320,50 @@ in let

libraries = lib.makeExtensible (libraries: let
callPackage = newScope (libraries // buildLlvmTools // { inherit stdenv cmake ninja libxml2 python3 release_version version monorepoSrc; });
in {
in
{

compiler-rt-libc = callPackage ./compiler-rt {
inherit llvm_meta;
stdenv = if stdenv.hostPlatform.useLLVM or false || (stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isStatic)
then overrideCC stdenv buildLlvmTools.clangNoCompilerRtWithLibc
else stdenv;
libxcrypt = libxcrypt.override {
stdenv = overrideCC stdenv buildLlvmTools.clangWithLibcAndBasicRtAndLibcxx;
};
stdenv =
if stdenv.hostPlatform.useLLVM or false
then overrideCC stdenv buildLlvmTools.clangWithLibcAndBasicRtAndLibcxx
else if (stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isStatic)
then overrideCC stdenv buildLlvmTools.clangWithLibcAndNoRt
else stdenv;
};

compiler-rt-no-libc = callPackage ./compiler-rt {
inherit llvm_meta;
stdenv = if stdenv.hostPlatform.useLLVM or false
then overrideCC stdenv buildLlvmTools.clangNoCompilerRt
else stdenv;
stdenv =
if stdenv.hostPlatform.useLLVM or false
then overrideCC stdenv buildLlvmTools.clangNoLibcNoRt
else stdenv;
};

# N.B. condition is safe because without useLLVM both are the same.
compiler-rt = if stdenv.hostPlatform.isAndroid || stdenv.hostPlatform.isDarwin
then libraries.compiler-rt-libc
else libraries.compiler-rt-no-libc;
compiler-rt = libraries.compiler-rt-libc;

stdenv = overrideCC stdenv buildLlvmTools.clang;

libcxxStdenv = overrideCC stdenv buildLlvmTools.libcxxClang;

# `libcxx` requires a fairly modern C++ compiler,
# so: we use the clang from this LLVM package set instead of the regular
# stdenv's compiler.
libcxx = callPackage ./libcxx {
inherit llvm_meta;
stdenv = overrideCC stdenv buildLlvmTools.clangNoLibcxx;
stdenv =
if stdenv.hostPlatform.isDarwin
then overrideCC stdenv buildLlvmTools.clangWithLibcAndNoRt else overrideCC stdenv buildLlvmTools.clangWithLibcAndBasicRt;
};

libunwind = callPackage ./libunwind {
inherit llvm_meta;
stdenv = overrideCC stdenv buildLlvmTools.clangNoLibcxx;
stdenv =
if stdenv.hostPlatform.isDarwin
then overrideCC stdenv buildLlvmTools.clangWithLibcAndNoRt else overrideCC stdenv buildLlvmTools.clangWithLibcAndBasicRt;
};

openmp = callPackage ./openmp {
Expand Down
15 changes: 11 additions & 4 deletions pkgs/development/compilers/llvm/17/compiler-rt/default.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{ lib, stdenv, llvm_meta, version
, monorepoSrc, runCommand
, cmake, ninja, python3, xcbuild, libllvm, linuxHeaders, libxcrypt
, cmake, ninja, python3, xcbuild, libllvm, linuxHeaders, libcxx, libxcrypt
, doFakeLibgcc ? stdenv.hostPlatform.isFreeBSD
}:

Expand Down Expand Up @@ -50,14 +50,21 @@ stdenv.mkDerivation {
"-DCMAKE_ASM_COMPILER_TARGET=${stdenv.hostPlatform.config}"
] ++ lib.optionals (haveLibc && stdenv.hostPlatform.libc == "glibc") [
"-DSANITIZER_COMMON_CFLAGS=-I${libxcrypt}/include"
] ++ lib.optionals (useLLVM || bareMetal || isMusl || isDarwinStatic) [
] ++ lib.optionals (useLLVM && stdenv.cc.libcxx == libcxx) [
"-DSANITIZER_CXX_ABI=libcxxabi"
"-DSANITIZER_CXX_ABI_LIBNAME=libcxxabi"
"-DCOMPILER_RT_USE_BUILTINS_LIBRARY=ON"
] ++ lib.optionals (useLLVM && haveLibc) [
"-DCOMPILER_RT_BUILD_SANITIZERS=ON"
"-DCOMPILER_RT_BUILD_PROFILE=ON"
] ++ lib.optionals ((useLLVM && !haveLibc) || bareMetal || isMusl || isDarwinStatic) [
"-DCOMPILER_RT_BUILD_SANITIZERS=OFF"
"-DCOMPILER_RT_BUILD_PROFILE=OFF"
] ++ lib.optionals (useLLVM || bareMetal || isMusl || isDarwinStatic) [
"-DCOMPILER_RT_BUILD_XRAY=OFF"
"-DCOMPILER_RT_BUILD_LIBFUZZER=OFF"
"-DCOMPILER_RT_BUILD_MEMPROF=OFF"
"-DCOMPILER_RT_BUILD_ORC=OFF" # may be possible to build with musl if necessary
] ++ lib.optionals (useLLVM || bareMetal) [
"-DCOMPILER_RT_BUILD_PROFILE=OFF"
] ++ lib.optionals ((useLLVM && !haveLibc) || bareMetal || isDarwinStatic ) [
"-DCMAKE_CXX_COMPILER_WORKS=ON"
] ++ lib.optionals ((useLLVM && !haveLibc) || bareMetal) [
Expand Down
Loading