From 25c16f3ebcadcada906eb45a2c732eb12669b476 Mon Sep 17 00:00:00 2001 From: Andrew Childs Date: Fri, 31 Jul 2020 23:03:51 +0900 Subject: [PATCH 1/2] stdenv/darwin: assemble full clang toolchain Adapted from main expression for clang 7. --- pkgs/stdenv/darwin/default.nix | 48 +++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/pkgs/stdenv/darwin/default.nix b/pkgs/stdenv/darwin/default.nix index a040e6ab24245..879e00c11d73a 100644 --- a/pkgs/stdenv/darwin/default.nix +++ b/pkgs/stdenv/darwin/default.nix @@ -24,6 +24,8 @@ assert crossSystem == localSystem; let inherit (localSystem) system platform; + bootstrapClangVersion = "7.1.0"; + commonImpureHostDeps = [ "/bin/sh" "/usr/lib/libSystem.B.dylib" @@ -90,7 +92,18 @@ in rec { inherit shell; inherit (last) stdenvNoCC; - extraPackages = []; + extraPackages = [ + # last.pkgs.llvmPackages_7.libcxxabi # TODO: is this required? if not, why not? + last.pkgs.llvmPackages_7.compiler-rt + ]; + + extraBuildCommands = '' + rsrc="$out/resource-root" + mkdir "$rsrc" + ln -s "${bootstrapTools}/lib/clang/${bootstrapClangVersion}/include" "$rsrc" + ln -s "${last.pkgs.llvmPackages_7.compiler-rt.out}/lib" "$rsrc/lib" + echo "-resource-dir=$rsrc" >> $out/nix-support/cc-cflags + ''; nativeTools = false; nativeLibc = false; @@ -180,6 +193,15 @@ in rec { ln -s ${bootstrapTools}/lib/libc++abi.dylib $out/lib/libc++abi.dylib ''; }; + + compiler-rt = stdenv.mkDerivation { + name = "bootstrap-stage0-compiler-rt"; + buildCommand = '' + mkdir -p $out/lib + ln -s ${bootstrapTools}/lib/libclang_rt* $out/lib + ln -s ${bootstrapTools}/lib/darwin $out/lib/darwin + ''; + }; }; }; @@ -198,6 +220,12 @@ in rec { python3 = super.python3Minimal; ninja = super.ninja.override { buildDocs = false; }; + + llvmPackages_7 = super.llvmPackages_7 // (let + libraries = super.llvmPackages_7.libraries.extend (_: _: { + inherit (llvmPackages_7) compiler-rt; + }); + in { inherit libraries; } // libraries); }; in with prevStage; stageFun 1 prevStage { extraPreHook = "export NIX_CFLAGS_COMPILE+=\" -F${bootstrapTools}/Library/Frameworks\""; @@ -206,7 +234,9 @@ in rec { libcxx = pkgs.libcxx; allowedRequisites = - [ bootstrapTools ] ++ (with pkgs; [ libcxx libcxxabi ]) ++ [ pkgs.darwin.Libsystem ]; + [ bootstrapTools ] ++ + (with pkgs; [ libcxx libcxxabi llvmPackages_7.compiler-rt ]) ++ + (with pkgs.darwin; [ Libsystem ]); overrides = persistent; }; @@ -220,6 +250,12 @@ in rec { findfreetype libssh curl cmake autoconf automake libtool ed cpio coreutils libssh2 nghttp2 libkrb5 ninja; + llvmPackages_7 = super.llvmPackages_7 // (let + libraries = super.llvmPackages_7.libraries.extend (_: _: { + inherit (llvmPackages_7) compiler-rt; + }); + in { inherit libraries; } // libraries); + darwin = super.darwin // { inherit (darwin) dyld Libsystem xnu configd ICU libdispatch libclosure launchd CF; @@ -237,8 +273,8 @@ in rec { allowedRequisites = [ bootstrapTools ] ++ (with pkgs; [ - xz.bin xz.out libcxx libcxxabi zlib libxml2.out curl.out openssl.out libssh2.out - nghttp2.lib libkrb5 + xz.bin xz.out libcxx libcxxabi llvmPackages_7.compiler-rt zlib + libxml2.out curl.out openssl.out libssh2.out nghttp2.lib libkrb5 ]) ++ (with pkgs.darwin; [ dyld Libsystem CF ICU locale ]); @@ -287,8 +323,8 @@ in rec { allowedRequisites = [ bootstrapTools ] ++ (with pkgs; [ - xz.bin xz.out bash libcxx libcxxabi zlib libxml2.out curl.out openssl.out libssh2.out - nghttp2.lib libkrb5 + xz.bin xz.out bash libcxx libcxxabi llvmPackages_7.compiler-rt zlib + libxml2.out curl.out openssl.out libssh2.out nghttp2.lib libkrb5 ]) ++ (with pkgs.darwin; [ dyld ICU Libsystem locale ]); From 1aa04cadc23939512a62c9901e3cced8ef84fa33 Mon Sep 17 00:00:00 2001 From: Andrew Childs Date: Fri, 31 Jul 2020 23:03:51 +0900 Subject: [PATCH 2/2] darwin/stdenv: refactoring Build the llvm support libraries (libcxx, libcxxabi) from scratch without using the existing llvm libraries. This is the same spirit and similar implementation as the "useLLVM" bootstrap in llvm package sets. Critically it avoids having libcxxabi provided by the cc-wrapper when building libcxx, which otherwise results in two libcxxabi instances. $ otool -L /nix/store/vd4vvgs9xngqbjzpg3qc41wl6jh42s9i-libc++-7.1.0/lib/libc++.dylib /nix/store/vd4vvgs9xngqbjzpg3qc41wl6jh42s9i-libc++-7.1.0/lib/libc++.dylib: /nix/store/vd4vvgs9xngqbjzpg3qc41wl6jh42s9i-libc++-7.1.0/lib/libc++.1.0.dylib (compatibility version 1.0.0, current version 1.0.0) /nix/store/gmpwk5fyp3iasppqrrdpswxvid6kcp8r-libc++abi-7.1.0/lib/libc++abi.dylib (compatibility version 1.0.0, current version 1.0.0) /nix/store/3hn7azynqgp2pm5gpdg45gpq0ia72skg-libc++abi-7.1.0/lib/libc++abi.dylib (compatibility version 1.0.0, current version 1.0.0) /nix/store/1nq94scbxs6bk7pimqhvz76q6cfmbv97-Libsystem-osx-10.12.6/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1) Additionally move some utilities (clang, binutils, coreutils, gnugrep) to the stage layers so they can be replaced before the final stdenv. This should cause most of stage4 to be built from the toolchain assembled as of stage3 instead of the bootstrap toolchain. --- .../compilers/llvm/7/libc++abi.nix | 5 +- pkgs/stdenv/darwin/default.nix | 134 ++++++++++++------ 2 files changed, 95 insertions(+), 44 deletions(-) diff --git a/pkgs/development/compilers/llvm/7/libc++abi.nix b/pkgs/development/compilers/llvm/7/libc++abi.nix index 97141a0bd402a..7f2ec528f3c1b 100644 --- a/pkgs/development/compilers/llvm/7/libc++abi.nix +++ b/pkgs/development/compilers/llvm/7/libc++abi.nix @@ -1,4 +1,5 @@ { stdenv, cmake, fetch, libcxx, llvm, version +, standalone ? false # on musl the shared objects don't build , enableShared ? ! stdenv.hostPlatform.isMusl }: @@ -20,7 +21,9 @@ stdenv.mkDerivation { patch -p1 -d $(ls -d libcxx-*) -i ${../libcxx-0001-musl-hacks.patch} ''; - cmakeFlags = stdenv.lib.optional (!enableShared) "-DLIBCXXABI_ENABLE_SHARED=OFF"; + cmakeFlags = + stdenv.lib.optional standalone "-DLLVM_ENABLE_LIBCXX=ON" ++ + stdenv.lib.optional (!enableShared) "-DLIBCXXABI_ENABLE_SHARED=OFF"; installPhase = if stdenv.isDarwin then '' diff --git a/pkgs/stdenv/darwin/default.nix b/pkgs/stdenv/darwin/default.nix index 879e00c11d73a..bfcc2a6945f4a 100644 --- a/pkgs/stdenv/darwin/default.nix +++ b/pkgs/stdenv/darwin/default.nix @@ -74,44 +74,48 @@ in rec { inherit (last) stdenv; }; - coreutils = { name = "${name}-coreutils"; outPath = bootstrapTools; }; - gnugrep = { name = "${name}-gnugrep"; outPath = bootstrapTools; }; - - bintools = import ../../build-support/bintools-wrapper { - inherit shell; - inherit (last) stdenvNoCC; - - nativeTools = false; - nativeLibc = false; - inherit buildPackages coreutils gnugrep; - libc = last.pkgs.darwin.Libsystem; - bintools = { name = "${name}-binutils"; outPath = bootstrapTools; }; - }; - - cc = if last == null then "/dev/null" else import ../../build-support/cc-wrapper { - inherit shell; - inherit (last) stdenvNoCC; - + mkExtraBuildCommands = cc: '' + rsrc="$out/resource-root" + mkdir "$rsrc" + ln -s "${cc}/lib/clang/${cc.version}/include" "$rsrc" + ln -s "${last.pkgs.llvmPackages_7.compiler-rt.out}/lib" "$rsrc/lib" + echo "-resource-dir=$rsrc" >> $out/nix-support/cc-cflags + ''; + + mkCC = overrides: import ../../build-support/cc-wrapper ( + let args = { + inherit shell; + inherit (last) stdenvNoCC; + + nativeTools = false; + nativeLibc = false; + inherit buildPackages libcxx; + inherit (last.pkgs) coreutils gnugrep; + bintools = last.pkgs.darwin.binutils; + libc = last.pkgs.darwin.Libsystem; + isClang = true; + cc = last.pkgs.llvmPackages_7.clang-unwrapped; + }; in args // (overrides args)); + + cc = if last == null then "/dev/null" else mkCC ({ cc, ... }: { extraPackages = [ - # last.pkgs.llvmPackages_7.libcxxabi # TODO: is this required? if not, why not? + last.pkgs.llvmPackages_7.libcxxabi last.pkgs.llvmPackages_7.compiler-rt ]; + extraBuildCommands = mkExtraBuildCommands cc; + }); + ccNoLibcxx = if last == null then "/dev/null" else mkCC ({ cc, ... }: { + libcxx = null; + extraPackages = [ + last.pkgs.llvmPackages_7.compiler-rt + ]; extraBuildCommands = '' - rsrc="$out/resource-root" - mkdir "$rsrc" - ln -s "${bootstrapTools}/lib/clang/${bootstrapClangVersion}/include" "$rsrc" - ln -s "${last.pkgs.llvmPackages_7.compiler-rt.out}/lib" "$rsrc/lib" - echo "-resource-dir=$rsrc" >> $out/nix-support/cc-cflags - ''; - - nativeTools = false; - nativeLibc = false; - inherit buildPackages coreutils gnugrep bintools libcxx; - libc = last.pkgs.darwin.Libsystem; - isClang = true; - cc = { name = "${name}-clang"; outPath = bootstrapTools; }; - }; + echo "-rtlib=compiler-rt" >> $out/nix-support/cc-cflags + echo "-B${last.pkgs.llvmPackages_7.compiler-rt}/lib" >> $out/nix-support/cc-cflags + echo "-nostdlib++" >> $out/nix-support/cc-cflags + '' + mkExtraBuildCommands cc; + }); thisStdenv = import ../generic { name = "${name}-stdenv-darwin"; @@ -150,7 +154,10 @@ in rec { extraAttrs = { inherit macosVersionMin appleSdkVersion platform; }; - overrides = self: super: (overrides self super) // { fetchurl = thisStdenv.fetchurlBoot; }; + overrides = self: super: (overrides self super) // { + inherit ccNoLibcxx; + fetchurl = thisStdenv.fetchurlBoot; + }; }; in { @@ -160,6 +167,9 @@ in rec { stage0 = stageFun 0 null { overrides = self: super: with stage0; { + coreutils = { name = "bootstrap-stage0-coreutils"; outPath = bootstrapTools; }; + gnugrep = { name = "bootstrap-stage0-gnugrep"; outPath = bootstrapTools; }; + darwin = super.darwin // { Libsystem = stdenv.mkDerivation { name = "bootstrap-stage0-Libsystem"; @@ -170,9 +180,26 @@ in rec { ''; }; dyld = bootstrapTools; + + binutils = lib.makeOverridable (import ../../build-support/bintools-wrapper) { + shell = "${bootstrapTools}/bin/bash"; + inherit (self) stdenvNoCC; + + nativeTools = false; + nativeLibc = false; + inherit (self) buildPackages coreutils gnugrep; + libc = self.pkgs.darwin.Libsystem; + bintools = { name = "bootstrap-stag0-binutils"; outPath = bootstrapTools; }; + }; }; llvmPackages_7 = { + clang-unwrapped = { + name = "bootstrap-stage0-clang"; + outPath = bootstrapTools; + version = bootstrapClangVersion; + }; + libcxx = stdenv.mkDerivation { name = "bootstrap-stage0-libcxx"; phases = [ "installPhase" "fixupPhase" ]; @@ -222,10 +249,19 @@ in rec { ninja = super.ninja.override { buildDocs = false; }; llvmPackages_7 = super.llvmPackages_7 // (let + tools = super.llvmPackages_7.tools.extend (_: _: { + inherit (llvmPackages_7) clang-unwrapped; + }); libraries = super.llvmPackages_7.libraries.extend (_: _: { - inherit (llvmPackages_7) compiler-rt; + inherit (llvmPackages_7) compiler-rt libcxx libcxxabi; }); - in { inherit libraries; } // libraries); + in { inherit tools libraries; } // tools // libraries); + + darwin = super.darwin // { + binutils = darwin.binutils.override { + libc = self.darwin.Libsystem; + }; + }; }; in with prevStage; stageFun 1 prevStage { extraPreHook = "export NIX_CFLAGS_COMPILE+=\" -F${bootstrapTools}/Library/Frameworks\""; @@ -251,14 +287,24 @@ in rec { libssh2 nghttp2 libkrb5 ninja; llvmPackages_7 = super.llvmPackages_7 // (let - libraries = super.llvmPackages_7.libraries.extend (_: _: { + tools = super.llvmPackages_7.tools.extend (_: _: { + inherit (llvmPackages_7) clang-unwrapped; + }); + libraries = super.llvmPackages_7.libraries.extend (_: libSuper: { inherit (llvmPackages_7) compiler-rt; + libcxx = libSuper.libcxx.override { + stdenv = overrideCC self.stdenv self.ccNoLibcxx; + }; + libcxxabi = libSuper.libcxxabi.override { + stdenv = overrideCC self.stdenv self.ccNoLibcxx; + standalone = true; + }; }); - in { inherit libraries; } // libraries); + in { inherit tools libraries; } // tools // libraries); darwin = super.darwin // { inherit (darwin) - dyld Libsystem xnu configd ICU libdispatch libclosure launchd CF; + binutils dyld Libsystem xnu configd ICU libdispatch libclosure launchd CF; }; }; in with prevStage; stageFun 2 prevStage { @@ -273,8 +319,9 @@ in rec { allowedRequisites = [ bootstrapTools ] ++ (with pkgs; [ - xz.bin xz.out libcxx libcxxabi llvmPackages_7.compiler-rt zlib - libxml2.out curl.out openssl.out libssh2.out nghttp2.lib libkrb5 + xz.bin xz.out libcxx libcxxabi llvmPackages_7.compiler-rt + zlib libxml2.out curl.out openssl.out libssh2.out + nghttp2.lib libkrb5 coreutils gnugrep pcre.out gmp libiconv ]) ++ (with pkgs.darwin; [ dyld Libsystem CF ICU locale ]); @@ -323,8 +370,9 @@ in rec { allowedRequisites = [ bootstrapTools ] ++ (with pkgs; [ - xz.bin xz.out bash libcxx libcxxabi llvmPackages_7.compiler-rt zlib - libxml2.out curl.out openssl.out libssh2.out nghttp2.lib libkrb5 + xz.bin xz.out bash libcxx libcxxabi llvmPackages_7.compiler-rt + zlib libxml2.out curl.out openssl.out libssh2.out + nghttp2.lib libkrb5 coreutils gnugrep pcre.out gmp libiconv ]) ++ (with pkgs.darwin; [ dyld ICU Libsystem locale ]);