From 0a32eab91e7a547325958da2581933a98095d50c Mon Sep 17 00:00:00 2001
From: Jude Taylor <me@jude.bio>
Date: Mon, 5 Oct 2015 08:46:49 -0700
Subject: [PATCH] fix LLVM packages to work in darwin stdenv

---
 .../compilers/llvm/3.7/libc++/default.nix     |  9 ----
 .../compilers/llvm/3.7/libc++abi.nix          |  2 +-
 pkgs/development/compilers/llvm/3.7/llvm.nix  |  6 ++-
 .../interpreters/perl/5.22/default.nix        |  7 +++-
 .../interpreters/perl/5.22/no-libutil.patch   | 16 +++----
 pkgs/development/libraries/gmp/6.x.nix        |  2 +
 pkgs/development/libraries/gmp/l0m4_tmp.patch | 27 ++++++++++++
 .../development/libraries/ncurses/clang.patch | 42 -------------------
 .../development/libraries/ncurses/default.nix |  2 -
 .../libraries/openssl/darwin-arch.patch       | 13 +++---
 pkgs/stdenv/pure-darwin/default.nix           |  8 ++--
 pkgs/tools/misc/cloc/default.nix              |  8 +++-
 12 files changed, 68 insertions(+), 74 deletions(-)
 create mode 100644 pkgs/development/libraries/gmp/l0m4_tmp.patch
 delete mode 100644 pkgs/development/libraries/ncurses/clang.patch

diff --git a/pkgs/development/compilers/llvm/3.7/libc++/default.nix b/pkgs/development/compilers/llvm/3.7/libc++/default.nix
index cd985d89098d..db1475cfc14f 100644
--- a/pkgs/development/compilers/llvm/3.7/libc++/default.nix
+++ b/pkgs/development/compilers/llvm/3.7/libc++/default.nix
@@ -12,15 +12,6 @@ stdenv.mkDerivation rec {
   preConfigure = ''
     # Get headers from the cxxabi source so we can see private headers not installed by the cxxabi package
     cmakeFlagsArray=($cmakeFlagsArray -DLIBCXX_CXX_ABI_INCLUDE_PATHS="$NIX_BUILD_TOP/libcxxabi-${version}.src/include")
-  '' +
-  stdenv.lib.optionalString stdenv.isDarwin ''
-    # instead of allowing libc++ to link with /usr/lib/libc++abi.dylib,
-    # force it to link with our copy
-    substituteInPlace lib/CMakeLists.txt \
-      --replace 'OSX_RE_EXPORT_LINE "/usr/lib/libc++abi.dylib' \
-                'OSX_RE_EXPORT_LINE "${libcxxabi}/lib/libc++abi.dylib' \
-      --replace '"''${CMAKE_OSX_SYSROOT}/usr/lib/libc++abi.dylib"' \
-                '"${libcxxabi}/lib/libc++abi.dylib"'
   '';
 
   patches = [ ./darwin.patch ];
diff --git a/pkgs/development/compilers/llvm/3.7/libc++abi.nix b/pkgs/development/compilers/llvm/3.7/libc++abi.nix
index a1b300ffa04f..97f182a9af81 100644
--- a/pkgs/development/compilers/llvm/3.7/libc++abi.nix
+++ b/pkgs/development/compilers/llvm/3.7/libc++abi.nix
@@ -27,7 +27,7 @@ stdenv.mkDerivation {
       done
       make install
       install -d 755 $out/include
-      install -m 644 ../include/cxxabi.h $out/include
+      install -m 644 ../include/*.h $out/include
     ''
     else ''
       install -d -m 755 $out/include $out/lib
diff --git a/pkgs/development/compilers/llvm/3.7/llvm.nix b/pkgs/development/compilers/llvm/3.7/llvm.nix
index 4ff61b278783..9784a64e7c3e 100644
--- a/pkgs/development/compilers/llvm/3.7/llvm.nix
+++ b/pkgs/development/compilers/llvm/3.7/llvm.nix
@@ -11,6 +11,7 @@
 , ncurses
 , version
 , zlib
+, libcxxabi
 , compiler-rt_src
 }:
 
@@ -27,7 +28,8 @@ in stdenv.mkDerivation rec {
     mv compiler-rt-* $sourceRoot/projects/compiler-rt
   '';
 
-  buildInputs = [ perl groff cmake libxml2 python libffi ] /* ++ stdenv.lib.optional stdenv.isLinux valgrind */;
+  buildInputs = [ perl groff cmake libxml2 python libffi ]
+    ++ stdenv.lib.optional stdenv.isDarwin libcxxabi;
 
   propagatedBuildInputs = [ ncurses zlib ];
 
@@ -47,7 +49,7 @@ in stdenv.mkDerivation rec {
     "-DBUILD_SHARED_LIBS=ON"
     "-DLLVM_BINUTILS_INCDIR=${binutils}/include"
   ] ++ stdenv.lib.optionals ( isDarwin) [
-    "-DCMAKE_CXX_FLAGS=-stdlib=libc++"
+    "-DLLVM_ENABLE_LIBCXX=ON"
     "-DCAN_TARGET_i386=false"
   ];
 
diff --git a/pkgs/development/interpreters/perl/5.22/default.nix b/pkgs/development/interpreters/perl/5.22/default.nix
index e0be8610fb7b..4b0f4364ea22 100644
--- a/pkgs/development/interpreters/perl/5.22/default.nix
+++ b/pkgs/development/interpreters/perl/5.22/default.nix
@@ -59,9 +59,14 @@ stdenv.mkDerivation rec {
 
   enableParallelBuilding = true;
 
+  postPatch = ''
+    pwd="$(type -P pwd)"
+    substituteInPlace dist/PathTools/Cwd.pm \
+      --replace "pwd_cmd = 'pwd'" "pwd_cmd = '$pwd'"
+  '';
+
   preConfigure =
     ''
-
       configureFlags="$configureFlags -Dprefix=$out -Dman1dir=$out/share/man/man1 -Dman3dir=$out/share/man/man3"
 
       ${optionalString stdenv.isArm ''
diff --git a/pkgs/development/interpreters/perl/5.22/no-libutil.patch b/pkgs/development/interpreters/perl/5.22/no-libutil.patch
index 68d44612bfe6..d6356a524370 100644
--- a/pkgs/development/interpreters/perl/5.22/no-libutil.patch
+++ b/pkgs/development/interpreters/perl/5.22/no-libutil.patch
@@ -1,12 +1,14 @@
-diff -ru -x '*~' perl-5.14.2-orig/Configure perl-5.14.2/Configure
---- perl-5.14.2-orig/Configure	2011-09-26 11:44:34.000000000 +0200
-+++ perl-5.14.2/Configure	2012-02-16 17:24:50.779839039 +0100
-@@ -1368,7 +1368,7 @@
+diff --git a/Configure b/Configure
+index 0a405d3..1871298 100755
+--- a/Configure
++++ b/Configure
+@@ -1455,7 +1455,7 @@ libswanted_uselargefiles=''
  : List of libraries we want.
  : If anyone needs extra -lxxx, put those in a hint file.
- libswanted="socket bind inet nsl nm ndbm gdbm dbm db malloc dl ld sun"
--libswanted="$libswanted m crypt sec util c cposix posix ucb bsd BSD"
-+libswanted="$libswanted m crypt sec c cposix posix ucb bsd BSD"
+ libswanted="cl pthread socket bind inet nsl nm ndbm gdbm dbm db malloc dl ld"
+-libswanted="$libswanted sun m crypt sec util c cposix posix ucb bsd BSD"
++libswanted="$libswanted sun m crypt sec c cposix posix ucb bsd BSD"
  : We probably want to search /usr/shlib before most other libraries.
  : This is only used by the lib/ExtUtils/MakeMaker.pm routine extliblist.
  glibpth=`echo " $glibpth " | sed -e 's! /usr/shlib ! !'`
+
diff --git a/pkgs/development/libraries/gmp/6.x.nix b/pkgs/development/libraries/gmp/6.x.nix
index 171611e12498..083bfec9b05a 100644
--- a/pkgs/development/libraries/gmp/6.x.nix
+++ b/pkgs/development/libraries/gmp/6.x.nix
@@ -33,6 +33,8 @@ stdenv.mkDerivation rec {
       configureFlagsArray+=("--build=$(./configfsf.guess)")
     '';
 
+  patches = [ ./l0m4_tmp.patch ];
+
   doCheck = true;
 
   dontDisableStatic = withStatic;
diff --git a/pkgs/development/libraries/gmp/l0m4_tmp.patch b/pkgs/development/libraries/gmp/l0m4_tmp.patch
new file mode 100644
index 000000000000..4431106cba62
--- /dev/null
+++ b/pkgs/development/libraries/gmp/l0m4_tmp.patch
@@ -0,0 +1,27 @@
+# HG changeset patch
+# User Torbjorn Granlund <tege@gmplib.org>
+# Date 1396470504 -7200
+# Node ID 1fab0adc5ff7d9ecddcbda96f407da58347bb49c
+# Parent  db645603dcdb41afcf78b19b551ecd5a01c3841c
+Workaround for Darwin assembler quirk.
+
+diff -r db645603dcdb -r 1fab0adc5ff7 mpn/x86_64/k8/redc_1.asm
+--- a/mpn/x86_64/k8/redc_1.asm	Mon Mar 31 23:04:32 2014 +0200
++++ b/mpn/x86_64/k8/redc_1.asm	Wed Apr 02 22:28:24 2014 +0200
+@@ -114,7 +114,7 @@
+ 
+ 	JUMPTABSECT
+ 	ALIGN(8)
+-L(tab):	JMPENT(	L(0m4), L(tab))
++L(tab):	JMPENT(	L(0), L(tab))
+ 	JMPENT(	L(1), L(tab))
+ 	JMPENT(	L(2), L(tab))
+ 	JMPENT(	L(3), L(tab))
+@@ -397,6 +397,7 @@
+ 
+ 
+ 	ALIGN(16)
++L(0):
+ L(0m4):
+ L(lo0):	mov	(mp,nneg,8), %rax
+ 	mov	nneg, i
diff --git a/pkgs/development/libraries/ncurses/clang.patch b/pkgs/development/libraries/ncurses/clang.patch
deleted file mode 100644
index ce33049bf405..000000000000
--- a/pkgs/development/libraries/ncurses/clang.patch
+++ /dev/null
@@ -1,42 +0,0 @@
-diff -ruNp ncurses-5.8.orig/c++/cursesf.h ncurses-5.8/c++/cursesf.h
---- ncurses-5.8.orig/c++/cursesf.h	2005-08-13 21:08:24.000000000 +0300
-+++ ncurses-5.8/c++/cursesf.h	2011-04-03 18:29:29.000000000 +0300
-@@ -681,7 +681,7 @@ public:
- 		   const T* p_UserData = STATIC_CAST(T*)(0),
- 		   bool with_frame=FALSE,
- 		   bool autoDelete_Fields=FALSE)
--    : NCursesForm (Fields, with_frame, autoDelete_Fields) {
-+    : NCursesForm (&Fields, with_frame, autoDelete_Fields) {
-       if (form)
- 	set_user (const_cast<void *>(p_UserData));
-   };
-@@ -694,7 +694,7 @@ public:
- 		   const T* p_UserData = STATIC_CAST(T*)(0),
- 		   bool with_frame=FALSE,
- 		   bool autoDelete_Fields=FALSE)
--    : NCursesForm (Fields, nlines, ncols, begin_y, begin_x,
-+    : NCursesForm (&Fields, nlines, ncols, begin_y, begin_x,
- 		   with_frame, autoDelete_Fields) {
-       if (form)
- 	set_user (const_cast<void *>(p_UserData));
-diff -ruNp ncurses-5.8.orig/c++/cursesm.h ncurses-5.8/c++/cursesm.h
---- ncurses-5.8.orig/c++/cursesm.h	2005-08-13 21:10:36.000000000 +0300
-+++ ncurses-5.8/c++/cursesm.h	2011-04-03 18:31:42.000000000 +0300
-@@ -639,7 +639,7 @@ public:
- 		   const T* p_UserData = STATIC_CAST(T*)(0),
- 		   bool with_frame=FALSE,
- 		   bool autoDelete_Items=FALSE)
--    : NCursesMenu (Items, with_frame, autoDelete_Items) {
-+    : NCursesMenu (&Items, with_frame, autoDelete_Items) {
-       if (menu)
- 	set_user (const_cast<void *>(p_UserData));
-   };
-@@ -651,7 +651,7 @@ public:
- 		   int begin_x = 0,
- 		   const T* p_UserData = STATIC_CAST(T*)(0),
- 		   bool with_frame=FALSE)
--    : NCursesMenu (Items, nlines, ncols, begin_y, begin_x, with_frame) {
-+    : NCursesMenu (&Items, nlines, ncols, begin_y, begin_x, with_frame) {
-       if (menu)
- 	set_user (const_cast<void *>(p_UserData));
-   };
diff --git a/pkgs/development/libraries/ncurses/default.nix b/pkgs/development/libraries/ncurses/default.nix
index 046c7347f863..a76bba24c62f 100644
--- a/pkgs/development/libraries/ncurses/default.nix
+++ b/pkgs/development/libraries/ncurses/default.nix
@@ -14,8 +14,6 @@ stdenv.mkDerivation rec {
     sha256 = "0q3jck7lna77z5r42f13c4xglc7azd19pxfrjrpgp2yf615w4lgm";
   };
 
-  patches = [ ./clang.patch ];
-
   configureFlags = [
     "--with-shared"
     "--with-cxx-shared"
diff --git a/pkgs/development/libraries/openssl/darwin-arch.patch b/pkgs/development/libraries/openssl/darwin-arch.patch
index 63db3efc0846..238dd7d08d88 100644
--- a/pkgs/development/libraries/openssl/darwin-arch.patch
+++ b/pkgs/development/libraries/openssl/darwin-arch.patch
@@ -1,12 +1,13 @@
-diff -ru -x '*~' openssl-1.0.1c-orig/Configure openssl-1.0.1c/Configure
---- openssl-1.0.1c-orig/Configure	2012-03-14 23:20:40.000000000 +0100
-+++ openssl-1.0.1c/Configure	2012-12-18 17:29:30.268090633 +0100
-@@ -579,7 +579,7 @@
+diff --git a/Configure b/Configure
+index d99eed7..341dbd0 100755
+--- a/Configure
++++ b/Configure
+@@ -638,7 +638,7 @@ my %table=(
  "darwin64-ppc-cc","cc:-arch ppc64 -O3 -DB_ENDIAN::-D_REENTRANT:MACOSX:-Wl,-search_paths_first%:SIXTY_FOUR_BIT_LONG RC4_CHAR RC4_CHUNK DES_UNROLL BF_PTR:${ppc64_asm}:osx64:dlfcn:darwin-shared:-fPIC -fno-common:-arch ppc64 -dynamiclib:.\$(SHLIB_MAJOR).\$(SHLIB_MINOR).dylib",
  "darwin-i386-cc","cc:-arch i386 -O3 -fomit-frame-pointer -DL_ENDIAN::-D_REENTRANT:MACOSX:-Wl,-search_paths_first%:BN_LLONG RC4_INT RC4_CHUNK DES_UNROLL BF_PTR:".eval{my $asm=$x86_asm;$asm=~s/cast\-586\.o//;$asm}.":macosx:dlfcn:darwin-shared:-fPIC -fno-common:-arch i386 -dynamiclib:.\$(SHLIB_MAJOR).\$(SHLIB_MINOR).dylib",
  "debug-darwin-i386-cc","cc:-arch i386 -g3 -DL_ENDIAN::-D_REENTRANT:MACOSX:-Wl,-search_paths_first%:BN_LLONG RC4_INT RC4_CHUNK DES_UNROLL BF_PTR:${x86_asm}:macosx:dlfcn:darwin-shared:-fPIC -fno-common:-arch i386 -dynamiclib:.\$(SHLIB_MAJOR).\$(SHLIB_MINOR).dylib",
--"darwin64-x86_64-cc","cc:-arch x86_64 -O3 -DL_ENDIAN -Wall::-D_REENTRANT:MACOSX:-Wl,-search_paths_first%:SIXTY_FOUR_BIT_LONG RC4_CHAR RC4_CHUNK DES_INT DES_UNROLL:".eval{my $asm=$x86_64_asm;$asm=~s/rc4\-[^:]+//;$asm}.":macosx:dlfcn:darwin-shared:-fPIC -fno-common:-arch x86_64 -dynamiclib:.\$(SHLIB_MAJOR).\$(SHLIB_MINOR).dylib",
+-"darwin64-x86_64-cc","cc:-arch x86_64 -O3 -DL_ENDIAN -Wall::-D_REENTRANT:MACOSX:-Wl,-search_paths_first%:SIXTY_FOUR_BIT_LONG RC4_CHUNK DES_INT DES_UNROLL:".eval{my $asm=$x86_64_asm;$asm=~s/rc4\-[^:]+//;$asm}.":macosx:dlfcn:darwin-shared:-fPIC -fno-common:-arch x86_64 -dynamiclib:.\$(SHLIB_MAJOR).\$(SHLIB_MINOR).dylib",
 +"darwin64-x86_64-cc","cc:-O3 -DL_ENDIAN -Wall::-D_REENTRANT:MACOSX:-Wl,-search_paths_first%:SIXTY_FOUR_BIT_LONG RC4_CHAR RC4_CHUNK DES_INT DES_UNROLL:".eval{my $asm=$x86_64_asm;$asm=~s/rc4\-[^:]+//;$asm}.":macosx:dlfcn:darwin-shared:-fPIC -fno-common:-dynamiclib:.\$(SHLIB_MAJOR).\$(SHLIB_MINOR).dylib",
+ "debug-darwin64-x86_64-cc","cc:-arch x86_64 -ggdb -g2 -O0 -DL_ENDIAN -Wall::-D_REENTRANT:MACOSX:-Wl,-search_paths_first%:SIXTY_FOUR_BIT_LONG RC4_CHUNK DES_INT DES_UNROLL:".eval{my $asm=$x86_64_asm;$asm=~s/rc4\-[^:]+//;$asm}.":macosx:dlfcn:darwin-shared:-fPIC -fno-common:-arch x86_64 -dynamiclib:.\$(SHLIB_MAJOR).\$(SHLIB_MINOR).dylib",
  "debug-darwin-ppc-cc","cc:-DBN_DEBUG -DREF_CHECK -DCONF_DEBUG -DCRYPTO_MDEBUG -DB_ENDIAN -g -Wall -O::-D_REENTRANT:MACOSX::BN_LLONG RC4_CHAR RC4_CHUNK DES_UNROLL BF_PTR:${ppc32_asm}:osx32:dlfcn:darwin-shared:-fPIC:-dynamiclib:.\$(SHLIB_MAJOR).\$(SHLIB_MINOR).dylib",
  # iPhoneOS/iOS
- "iphoneos-cross","llvm-gcc:-O3 -isysroot \$(CROSS_TOP)/SDKs/\$(CROSS_SDK) -fomit-frame-pointer -fno-common::-D_REENTRANT:iOS:-Wl,-search_paths_first%:BN_LLONG RC4_CHAR RC4_CHUNK DES_UNROLL BF_PTR:${no_asm}:dlfcn:darwin-shared:-fPIC -fno-common:-dynamiclib:.\$(SHLIB_MAJOR).\$(SHLIB_MINOR).dylib",
diff --git a/pkgs/stdenv/pure-darwin/default.nix b/pkgs/stdenv/pure-darwin/default.nix
index 51780b6f774b..a50a63fbda66 100644
--- a/pkgs/stdenv/pure-darwin/default.nix
+++ b/pkgs/stdenv/pure-darwin/default.nix
@@ -220,11 +220,13 @@ in rec {
   persistent3 = orig: with stage3.pkgs; {
     inherit
       gnumake gzip gnused bzip2 gawk ed xz patch bash
-      libcxxabi libcxx ncurses libffi zlib llvm gmp pcre gnugrep
+      libcxxabi libcxx ncurses libffi zlib gmp pcre gnugrep
       coreutils findutils diffutils patchutils;
 
-    llvmPackages = orig.llvmPackages // {
-      inherit (llvmPackages) llvm clang-unwrapped;
+    llvmPackages = let llvmOverride = llvmPackages.llvm.override { inherit libcxxabi; };
+    in orig.llvmPackages // {
+      llvm = llvmOverride;
+      clang-unwrapped = llvmPackages.clang-unwrapped.override { llvm = llvmOverride; };
     };
 
     darwin = orig.darwin // {
diff --git a/pkgs/tools/misc/cloc/default.nix b/pkgs/tools/misc/cloc/default.nix
index bb278dbe1b19..0b5796d4ac38 100644
--- a/pkgs/tools/misc/cloc/default.nix
+++ b/pkgs/tools/misc/cloc/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, perl, AlgorithmDiff, RegexpCommon }:
+{ stdenv, fetchurl, fetchpatch, perl, AlgorithmDiff, RegexpCommon }:
 
 stdenv.mkDerivation rec {
 
@@ -11,6 +11,12 @@ stdenv.mkDerivation rec {
     sha256 = "1w3mz69h2i7pscvi9q7yp7wimds8g38c5ph78cj5pvjl5wa035rh";
   };
 
+  patches = [ (fetchpatch {
+    name = "perl-5.22.patch";
+    url = "https://bugs.archlinux.org/task/45494?getfile=13174";
+    sha256 = "1xxwqjy2q2fdza7kfp9ld0yzljkdsrgm8a9pwnmx5q4adigcjjsz";
+  }) ];
+
   buildInputs = [ perl AlgorithmDiff RegexpCommon ];
 
   makeFlags = [ "prefix=" "DESTDIR=$(out)" "INSTALL=install" ];