Skip to content

Commit 56fcbcd

Browse files
llvmPackages_11: darwin cross compilation and bootstrapping
1 parent c31591c commit 56fcbcd

5 files changed

Lines changed: 33 additions & 14 deletions

File tree

pkgs/development/compilers/llvm/11/compiler-rt/default.nix

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
let
44

55
useLLVM = stdenv.hostPlatform.useLLVM or false;
6+
isDarwin = stdenv.hostPlatform.isDarwin;
67
bareMetal = stdenv.hostPlatform.parsed.kernel.name == "none";
78
haveLibc = stdenv.cc.libc != null;
89
inherit (stdenv.hostPlatform) isMusl;
@@ -15,7 +16,6 @@ stdenv.mkDerivation {
1516
src = fetch "compiler-rt" "0x1j8ngf1zj63wlnns9vlibafq48qcm72p4jpaxkmkb4qw0grwfy";
1617

1718
nativeBuildInputs = [ cmake python3 llvm.dev ];
18-
buildInputs = lib.optional stdenv.hostPlatform.isDarwin libcxxabi;
1919

2020
NIX_CFLAGS_COMPILE = [
2121
"-DSCUDO_DEFAULT_OPTIONS=DeleteSizeMismatch=0:DeallocationTypeMismatch=0"
@@ -25,19 +25,19 @@ stdenv.mkDerivation {
2525
"-DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON"
2626
"-DCMAKE_C_COMPILER_TARGET=${stdenv.hostPlatform.config}"
2727
"-DCMAKE_ASM_COMPILER_TARGET=${stdenv.hostPlatform.config}"
28-
] ++ lib.optionals (useLLVM || bareMetal || isMusl) [
28+
] ++ lib.optionals (useLLVM || isDarwin || bareMetal || isMusl) [
2929
"-DCOMPILER_RT_BUILD_SANITIZERS=OFF"
3030
"-DCOMPILER_RT_BUILD_XRAY=OFF"
3131
"-DCOMPILER_RT_BUILD_LIBFUZZER=OFF"
3232
"-DCOMPILER_RT_BUILD_PROFILE=OFF"
33-
] ++ lib.optionals ((useLLVM || bareMetal) && !haveLibc) [
33+
] ++ lib.optionals ((useLLVM || isDarwin || bareMetal) && !haveLibc) [
3434
"-DCMAKE_C_COMPILER_WORKS=ON"
3535
"-DCMAKE_CXX_COMPILER_WORKS=ON"
3636
"-DCOMPILER_RT_BAREMETAL_BUILD=ON"
3737
"-DCMAKE_SIZEOF_VOID_P=${toString (stdenv.hostPlatform.parsed.cpu.bits / 8)}"
38-
] ++ lib.optionals (useLLVM && !haveLibc) [
38+
] ++ lib.optionals ((useLLVM || isDarwin) && !haveLibc) [
3939
"-DCMAKE_C_FLAGS=-nodefaultlibs"
40-
] ++ lib.optionals (useLLVM) [
40+
] ++ lib.optionals (useLLVM || isDarwin) [
4141
"-DCOMPILER_RT_BUILD_BUILTINS=ON"
4242
#https://stackoverflow.com/questions/53633705/cmake-the-c-compiler-is-not-able-to-compile-a-simple-test-program
4343
"-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY"
@@ -61,6 +61,11 @@ stdenv.mkDerivation {
6161
]# ++ lib.optional stdenv.hostPlatform.isMusl ./sanitizers-nongnu.patch
6262
++ lib.optional stdenv.hostPlatform.isAarch32 ./armv7l.patch;
6363

64+
65+
preConfigure = lib.optionalString isDarwin ''
66+
cmakeFlagsArray+=("-DCMAKE_LIPO=$(command -v ${stdenv.cc.targetPrefix}lipo)")
67+
'';
68+
6469
# TSAN requires XPC on Darwin, which we have no public/free source files for. We can depend on the Apple frameworks
6570
# to get it, but they're unfree. Since LLVM is rather central to the stdenv, we patch out TSAN support so that Hydra
6671
# can build this. If we didn't do it, basically the entire nixpkgs on Darwin would have an unfree dependency and we'd
@@ -71,7 +76,7 @@ stdenv.mkDerivation {
7176
--replace 'set(X86 i386)' 'set(X86 i386 i486 i586 i686)'
7277
'' + lib.optionalString stdenv.isDarwin ''
7378
substituteInPlace cmake/builtin-config-ix.cmake \
74-
--replace 'set(ARM64 arm64 arm64e)' 'set(ARM64)'
79+
--replace 'foreach(arch ''${ARM64})' 'foreach(arch)'
7580
substituteInPlace cmake/config-ix.cmake \
7681
--replace 'set(COMPILER_RT_HAS_TSAN TRUE)' 'set(COMPILER_RT_HAS_TSAN FALSE)'
7782
'' + lib.optionalString (useLLVM) ''

pkgs/development/compilers/llvm/11/default.nix

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,20 +213,20 @@ let
213213

214214
compiler-rt-libc = callPackage ./compiler-rt {
215215
inherit llvm_meta;
216-
stdenv = if stdenv.hostPlatform.useLLVM or false
216+
stdenv = if (stdenv.hostPlatform.useLLVM or false) || stdenv.hostPlatform.isDarwin
217217
then overrideCC stdenv buildLlvmTools.clangNoCompilerRtWithLibc
218218
else stdenv;
219219
};
220220

221221
compiler-rt-no-libc = callPackage ./compiler-rt {
222222
inherit llvm_meta;
223-
stdenv = if stdenv.hostPlatform.useLLVM or false
223+
stdenv = if (stdenv.hostPlatform.useLLVM or false) || stdenv.hostPlatform.isDarwin
224224
then overrideCC stdenv buildLlvmTools.clangNoCompilerRt
225225
else stdenv;
226226
};
227227

228228
# N.B. condition is safe because without useLLVM both are the same.
229-
compiler-rt = if stdenv.hostPlatform.isAndroid
229+
compiler-rt = if stdenv.hostPlatform.isAndroid || (stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isAarch64)
230230
then libraries.compiler-rt-libc
231231
else libraries.compiler-rt-no-libc;
232232

@@ -236,21 +236,21 @@ let
236236

237237
libcxx = callPackage ./libcxx {
238238
inherit llvm_meta;
239-
stdenv = if stdenv.hostPlatform.useLLVM or false
239+
stdenv = if (stdenv.hostPlatform.useLLVM or false) || stdenv.hostPlatform.isDarwin
240240
then overrideCC stdenv buildLlvmTools.clangNoLibcxx
241241
else stdenv;
242242
};
243243

244244
libcxxabi = callPackage ./libcxxabi {
245245
inherit llvm_meta;
246-
stdenv = if stdenv.hostPlatform.useLLVM or false
246+
stdenv = if (stdenv.hostPlatform.useLLVM or false) || stdenv.hostPlatform.isDarwin
247247
then overrideCC stdenv buildLlvmTools.clangNoLibcxx
248248
else stdenv;
249249
};
250250

251251
libunwind = callPackage ./libunwind {
252252
inherit llvm_meta;
253-
stdenv = if stdenv.hostPlatform.useLLVM or false
253+
stdenv = if (stdenv.hostPlatform.useLLVM or false) || stdenv.hostPlatform.isDarwin
254254
then overrideCC stdenv buildLlvmTools.clangNoLibcxx
255255
else stdenv;
256256
};

pkgs/development/compilers/llvm/11/libcxx/default.nix

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,20 @@ stdenv.mkDerivation {
4646
"-DLIBCXX_ENABLE_THREADS=OFF"
4747
"-DLIBCXX_ENABLE_FILESYSTEM=OFF"
4848
"-DLIBCXX_ENABLE_EXCEPTIONS=OFF"
49-
] ++ lib.optional (!enableShared) "-DLIBCXX_ENABLE_SHARED=OFF";
49+
] ++ lib.optional (!enableShared) "-DLIBCXX_ENABLE_SHARED=OFF"
50+
51+
# TODO: this is a bit of a hack to cross compile to Apple Silicon. libcxx
52+
# starting with 11 enables CMAKE_BUILD_WITH_INSTALL_NAME_DIR which requires
53+
# platform setup for rpaths. In cmake, this is enabled when macos is newer
54+
# than 10.5. However CMAKE_SYSTEM_VERSION is set to empty (TODO: why?)
55+
# which prevents the conditional configuration, and configure fails. The
56+
# value here corresponds to `uname -r`. If stdenv.hostPlatform.release is
57+
# not null, then this property will be set via mkDerivation (TODO: how can
58+
# we set this?).
59+
++ lib.optional (
60+
stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isAarch64 &&
61+
stdenv.hostPlatform != stdenv.buildPlatform
62+
) "-DCMAKE_SYSTEM_VERSION=20.1.0";
5063

5164
passthru = {
5265
isLLVM = true;

pkgs/development/compilers/llvm/11/libcxxabi/default.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ stdenv.mkDerivation {
4848
# the magic combination of necessary CMake variables
4949
# if you fancy a try, take a look at
5050
# https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/RPATH-handling
51-
install_name_tool -id $out/$file $file
51+
${stdenv.cc.targetPrefix}install_name_tool -id $out/$file $file
5252
done
5353
make install
5454
install -d 755 $out/include

pkgs/top-level/all-packages.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14848,6 +14848,7 @@ in
1484814848
inherit (stdenv.targetPlatform) libc;
1484914849
in if libc == "msvcrt" then targetPackages.windows.mingw_w64_headers or windows.mingw_w64_headers
1485014850
else if libc == "nblibc" then targetPackages.netbsdCross.headers or netbsdCross.headers
14851+
else if libc == "libSystem" && stdenv.targetPlatform.isAarch64 then targetPackages.darwin.LibsystemCross or darwin.LibsystemCross
1485114852
else null;
1485214853

1485314854
# We can choose:

0 commit comments

Comments
 (0)