Skip to content

Commit

Permalink
treewide: Try to make a few bootstrapping things more consistent
Browse files Browse the repository at this point in the history
- Introduce `preLibcCrossHeaders` to bootstrap libgcc and compiler-rt
  the same way.

- Organize LLVM bintools as `bintools{-unwrapped,,NoLibc}` for
  consistency with GNU Binutils and Apple's cctools.

- Do Android changes for all `llvmPackages` for consistency.

- Improve the way the default GCC and LLVM versions are selected.
  • Loading branch information
Ericson2314 committed May 12, 2021
1 parent e0d3c9d commit 37194a3
Show file tree
Hide file tree
Showing 19 changed files with 301 additions and 169 deletions.
14 changes: 7 additions & 7 deletions pkgs/development/compilers/llvm/10/compiler-rt/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ let

useLLVM = stdenv.hostPlatform.useLLVM or false;
bareMetal = stdenv.hostPlatform.parsed.kernel.name == "none";
haveLibc = stdenv.cc.libc != null;
inherit (stdenv.hostPlatform) isMusl;

in

stdenv.mkDerivation rec {
pname = "compiler-rt";
stdenv.mkDerivation {
pname = "compiler-rt" + lib.optionalString (haveLibc) "-libc";
inherit version;
src = fetch pname "1yjqjri753w0fzmxcyz687nvd97sbc9rsqrxzpq720na47hwh3fr";
src = fetch "compiler-rt" "1yjqjri753w0fzmxcyz687nvd97sbc9rsqrxzpq720na47hwh3fr";

nativeBuildInputs = [ cmake python3 llvm.dev ];
buildInputs = lib.optional stdenv.hostPlatform.isDarwin libcxxabi;
Expand All @@ -29,14 +30,15 @@ stdenv.mkDerivation rec {
"-DCOMPILER_RT_BUILD_XRAY=OFF"
"-DCOMPILER_RT_BUILD_LIBFUZZER=OFF"
"-DCOMPILER_RT_BUILD_PROFILE=OFF"
] ++ lib.optionals (useLLVM || bareMetal) [
] ++ lib.optionals ((useLLVM || bareMetal) && !haveLibc) [
"-DCMAKE_C_COMPILER_WORKS=ON"
"-DCMAKE_CXX_COMPILER_WORKS=ON"
"-DCOMPILER_RT_BAREMETAL_BUILD=ON"
"-DCMAKE_SIZEOF_VOID_P=${toString (stdenv.hostPlatform.parsed.cpu.bits / 8)}"
] ++ lib.optionals (useLLVM && !haveLibc) [
"-DCMAKE_C_FLAGS=-nodefaultlibs"
] ++ lib.optionals (useLLVM) [
"-DCOMPILER_RT_BUILD_BUILTINS=ON"
"-DCMAKE_C_FLAGS=-nodefaultlibs"
#https://stackoverflow.com/questions/53633705/cmake-the-c-compiler-is-not-able-to-compile-a-simple-test-program
"-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY"
] ++ lib.optionals (bareMetal) [
Expand All @@ -58,7 +60,6 @@ stdenv.mkDerivation rec {
]# ++ lib.optional stdenv.hostPlatform.isMusl ./sanitizers-nongnu.patch
++ lib.optional stdenv.hostPlatform.isAarch32 ./armv7l.patch;


# TSAN requires XPC on Darwin, which we have no public/free source files for. We can depend on the Apple frameworks
# to get it, but they're unfree. Since LLVM is rather central to the stdenv, we patch out TSAN support so that Hydra
# can build this. If we didn't do it, basically the entire nixpkgs on Darwin would have an unfree dependency and we'd
Expand Down Expand Up @@ -88,5 +89,4 @@ stdenv.mkDerivation rec {
ln -s $out/lib/*/clang_rt.crtbegin_shared-*.o $out/lib/crtbeginS.o
ln -s $out/lib/*/clang_rt.crtend_shared-*.o $out/lib/crtendS.o
'';

}
58 changes: 41 additions & 17 deletions pkgs/development/compilers/llvm/10/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{ lowPrio, newScope, pkgs, lib, stdenv, cmake, gccForLibs
{ lowPrio, newScope, pkgs, lib, stdenv, cmake
, gccForLibs, preLibcCrossHeaders
, libxml2, python3, isl, fetchurl, overrideCC, wrapCCWith, wrapBintoolsWith
, buildLlvmTools # tools, but from the previous stage, for cross
, targetLlvmLibraries # libraries, but from the next stage, for cross
Expand Down Expand Up @@ -93,7 +94,16 @@ let
# doesn’t support like LLVM. Probably we should move to some other
# file.

bintools = callPackage ./bintools.nix {};
bintools-unwrapped = callPackage ./bintools {};

bintoolsNoLibc = wrapBintoolsWith {
bintools = tools.bintools-unwrapped;
libc = preLibcCrossHeaders;
};

bintools = wrapBintoolsWith {
bintools = tools.bintools-unwrapped;
};

lldClang = wrapCCWith rec {
cc = tools.clang-unwrapped;
Expand All @@ -112,6 +122,8 @@ let
echo "-B${targetLlvmLibraries.compiler-rt}/lib" >> $out/nix-support/cc-cflags
'' + lib.optionalString (!stdenv.targetPlatform.isWasm) ''
echo "--unwindlib=libunwind" >> $out/nix-support/cc-cflags
'' + lib.optionalString (stdenv.targetPlatform.isAndroid && stdenv.targetPlatform.useLLVM) ''
echo "-lunwind" >> $out/nix-support/cc-ldflags
'' + lib.optionalString stdenv.targetPlatform.isWasm ''
echo "-fno-exceptions" >> $out/nix-support/cc-cflags
'' + mkExtraBuildCommands cc;
Expand All @@ -120,9 +132,7 @@ let
lldClangNoLibcxx = wrapCCWith rec {
cc = tools.clang-unwrapped;
libcxx = null;
bintools = wrapBintoolsWith {
inherit (tools) bintools;
};
inherit (tools) bintools;
extraPackages = [
targetLlvmLibraries.compiler-rt
];
Expand All @@ -136,10 +146,7 @@ let
lldClangNoLibc = wrapCCWith rec {
cc = tools.clang-unwrapped;
libcxx = null;
bintools = wrapBintoolsWith {
inherit (tools) bintools;
libc = null;
};
bintools = tools.bintoolsNoLibc;
extraPackages = [
targetLlvmLibraries.compiler-rt
];
Expand All @@ -152,26 +159,43 @@ let
lldClangNoCompilerRt = wrapCCWith rec {
cc = tools.clang-unwrapped;
libcxx = null;
bintools = wrapBintoolsWith {
inherit (tools) bintools;
libc = null;
};
bintools = tools.bintoolsNoLibc;
extraPackages = [ ];
extraBuildCommands = ''
echo "-nostartfiles" >> $out/nix-support/cc-cflags
'' + mkExtraBuildCommands0 cc;
};

lldClangNoCompilerRtWithLibc = wrapCCWith rec {
cc = tools.clang-unwrapped;
libcxx = null;
inherit (tools) bintools;
extraPackages = [ ];
extraBuildCommands = mkExtraBuildCommands0 cc;
};

});

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

compiler-rt = callPackage ./compiler-rt ({} //
(lib.optionalAttrs (stdenv.hostPlatform.useLLVM or false) {
stdenv = overrideCC stdenv buildLlvmTools.lldClangNoCompilerRt;
}));
compiler-rt-libc = callPackage ./compiler-rt {
stdenv = if stdenv.hostPlatform.useLLVM or false
then overrideCC stdenv buildLlvmTools.lldClangNoCompilerRtWithLibc
else stdenv;
};

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

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

stdenv = overrideCC stdenv buildLlvmTools.clang;

Expand Down
14 changes: 7 additions & 7 deletions pkgs/development/compilers/llvm/11/compiler-rt/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ let

useLLVM = stdenv.hostPlatform.useLLVM or false;
bareMetal = stdenv.hostPlatform.parsed.kernel.name == "none";
haveLibc = stdenv.cc.libc != null;
inherit (stdenv.hostPlatform) isMusl;

in

stdenv.mkDerivation rec {
pname = "compiler-rt";
stdenv.mkDerivation {
pname = "compiler-rt" + lib.optionalString (haveLibc) "-libc";
inherit version;
src = fetch pname "0x1j8ngf1zj63wlnns9vlibafq48qcm72p4jpaxkmkb4qw0grwfy";
src = fetch "compiler-rt" "0x1j8ngf1zj63wlnns9vlibafq48qcm72p4jpaxkmkb4qw0grwfy";

nativeBuildInputs = [ cmake python3 llvm.dev ];
buildInputs = lib.optional stdenv.hostPlatform.isDarwin libcxxabi;
Expand All @@ -29,14 +30,15 @@ stdenv.mkDerivation rec {
"-DCOMPILER_RT_BUILD_XRAY=OFF"
"-DCOMPILER_RT_BUILD_LIBFUZZER=OFF"
"-DCOMPILER_RT_BUILD_PROFILE=OFF"
] ++ lib.optionals (useLLVM || bareMetal) [
] ++ lib.optionals ((useLLVM || bareMetal) && !haveLibc) [
"-DCMAKE_C_COMPILER_WORKS=ON"
"-DCMAKE_CXX_COMPILER_WORKS=ON"
"-DCOMPILER_RT_BAREMETAL_BUILD=ON"
"-DCMAKE_SIZEOF_VOID_P=${toString (stdenv.hostPlatform.parsed.cpu.bits / 8)}"
] ++ lib.optionals (useLLVM && !haveLibc) [
"-DCMAKE_C_FLAGS=-nodefaultlibs"
] ++ lib.optionals (useLLVM) [
"-DCOMPILER_RT_BUILD_BUILTINS=ON"
"-DCMAKE_C_FLAGS=-nodefaultlibs"
#https://stackoverflow.com/questions/53633705/cmake-the-c-compiler-is-not-able-to-compile-a-simple-test-program
"-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY"
] ++ lib.optionals (bareMetal) [
Expand All @@ -59,7 +61,6 @@ stdenv.mkDerivation rec {
]# ++ lib.optional stdenv.hostPlatform.isMusl ./sanitizers-nongnu.patch
++ lib.optional stdenv.hostPlatform.isAarch32 ./armv7l.patch;


# TSAN requires XPC on Darwin, which we have no public/free source files for. We can depend on the Apple frameworks
# to get it, but they're unfree. Since LLVM is rather central to the stdenv, we patch out TSAN support so that Hydra
# can build this. If we didn't do it, basically the entire nixpkgs on Darwin would have an unfree dependency and we'd
Expand Down Expand Up @@ -91,5 +92,4 @@ stdenv.mkDerivation rec {
ln -s $out/lib/*/clang_rt.crtbegin_shared-*.o $out/lib/crtbeginS.o
ln -s $out/lib/*/clang_rt.crtend_shared-*.o $out/lib/crtendS.o
'';

}
58 changes: 41 additions & 17 deletions pkgs/development/compilers/llvm/11/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{ lowPrio, newScope, pkgs, lib, stdenv, cmake, gccForLibs
{ lowPrio, newScope, pkgs, lib, stdenv, cmake
, gccForLibs, preLibcCrossHeaders
, libxml2, python3, isl, fetchurl, overrideCC, wrapCCWith, wrapBintoolsWith
, buildLlvmTools # tools, but from the previous stage, for cross
, targetLlvmLibraries # libraries, but from the next stage, for cross
Expand Down Expand Up @@ -94,7 +95,16 @@ let
# doesn’t support like LLVM. Probably we should move to some other
# file.

bintools = callPackage ./bintools.nix {};
bintools-unwrapped = callPackage ./bintools {};

bintoolsNoLibc = wrapBintoolsWith {
bintools = tools.bintools-unwrapped;
libc = preLibcCrossHeaders;
};

bintools = wrapBintoolsWith {
bintools = tools.bintools-unwrapped;
};

lldClang = wrapCCWith rec {
cc = tools.clang-unwrapped;
Expand All @@ -113,6 +123,8 @@ let
echo "-B${targetLlvmLibraries.compiler-rt}/lib" >> $out/nix-support/cc-cflags
'' + lib.optionalString (!stdenv.targetPlatform.isWasm) ''
echo "--unwindlib=libunwind" >> $out/nix-support/cc-cflags
'' + lib.optionalString (stdenv.targetPlatform.isAndroid && stdenv.targetPlatform.useLLVM) ''
echo "-lunwind" >> $out/nix-support/cc-ldflags
'' + lib.optionalString stdenv.targetPlatform.isWasm ''
echo "-fno-exceptions" >> $out/nix-support/cc-cflags
'' + mkExtraBuildCommands cc;
Expand All @@ -121,9 +133,7 @@ let
lldClangNoLibcxx = wrapCCWith rec {
cc = tools.clang-unwrapped;
libcxx = null;
bintools = wrapBintoolsWith {
inherit (tools) bintools;
};
inherit (tools) bintools;
extraPackages = [
targetLlvmLibraries.compiler-rt
];
Expand All @@ -137,10 +147,7 @@ let
lldClangNoLibc = wrapCCWith rec {
cc = tools.clang-unwrapped;
libcxx = null;
bintools = wrapBintoolsWith {
inherit (tools) bintools;
libc = null;
};
bintools = tools.bintoolsNoLibc;
extraPackages = [
targetLlvmLibraries.compiler-rt
];
Expand All @@ -153,26 +160,43 @@ let
lldClangNoCompilerRt = wrapCCWith rec {
cc = tools.clang-unwrapped;
libcxx = null;
bintools = wrapBintoolsWith {
inherit (tools) bintools;
libc = null;
};
bintools = tools.bintoolsNoLibc;
extraPackages = [ ];
extraBuildCommands = ''
echo "-nostartfiles" >> $out/nix-support/cc-cflags
'' + mkExtraBuildCommands0 cc;
};

lldClangNoCompilerRtWithLibc = wrapCCWith rec {
cc = tools.clang-unwrapped;
libcxx = null;
inherit (tools) bintools;
extraPackages = [ ];
extraBuildCommands = mkExtraBuildCommands0 cc;
};

});

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

compiler-rt = callPackage ./compiler-rt ({} //
(lib.optionalAttrs (stdenv.hostPlatform.useLLVM or false) {
stdenv = overrideCC stdenv buildLlvmTools.lldClangNoCompilerRt;
}));
compiler-rt-libc = callPackage ./compiler-rt {
stdenv = if stdenv.hostPlatform.useLLVM or false
then overrideCC stdenv buildLlvmTools.lldClangNoCompilerRtWithLibc
else stdenv;
};

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

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

stdenv = overrideCC stdenv buildLlvmTools.clang;

Expand Down
3 changes: 1 addition & 2 deletions pkgs/development/compilers/llvm/12/compiler-rt/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let

in

stdenv.mkDerivation rec {
stdenv.mkDerivation {
pname = "compiler-rt" + lib.optionalString (haveLibc) "-libc";
inherit version;
src = fetch "compiler-rt" "0d444qihq9jhqnfv003cr704v363va72zl6qaw2algj1c85cva45";
Expand Down Expand Up @@ -61,7 +61,6 @@ stdenv.mkDerivation rec {
]# ++ lib.optional stdenv.hostPlatform.isMusl ./sanitizers-nongnu.patch
++ lib.optional stdenv.hostPlatform.isAarch32 ./armv7l.patch;


# TSAN requires XPC on Darwin, which we have no public/free source files for. We can depend on the Apple frameworks
# to get it, but they're unfree. Since LLVM is rather central to the stdenv, we patch out TSAN support so that Hydra
# can build this. If we didn't do it, basically the entire nixpkgs on Darwin would have an unfree dependency and we'd
Expand Down
Loading

0 comments on commit 37194a3

Please sign in to comment.