From 07849e749abb46b48db7123de8b40cb40bc08724 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Thu, 13 Oct 2016 07:58:54 -0500
Subject: [PATCH 1/2] cmake: When there is no stdenv.glibc, still fix search
 paths.

Apply all of the fixups from preConfigure as well!

For testing purposes I added the following assert:

assert (stdenv ? glibc) -> (stdenv.glibc == stdenv.cc.libc);

To ensure behavior in the case there is a 'stdenv.glibc' (linux-only?)
wasn't changed, which passes in the configurations I could think of.

Not including that since it seems like a bad requirement moving forward.
---
 .../tools/build-managers/cmake/2.8.nix        | 25 +++++++++++--------
 .../tools/build-managers/cmake/default.nix    | 17 ++++++-------
 .../cmake/search-path-3.2.patch               | 12 ++++-----
 .../build-managers/cmake/search-path.patch    | 12 ++++-----
 4 files changed, 34 insertions(+), 32 deletions(-)

diff --git a/pkgs/development/tools/build-managers/cmake/2.8.nix b/pkgs/development/tools/build-managers/cmake/2.8.nix
index d824c3e2c964..52c577aedb35 100644
--- a/pkgs/development/tools/build-managers/cmake/2.8.nix
+++ b/pkgs/development/tools/build-managers/cmake/2.8.nix
@@ -5,6 +5,8 @@
 with stdenv.lib;
 
 assert wantPS -> (ps != null);
+assert stdenv ? cc;
+assert stdenv.cc ? libc;
 
 let
   os = stdenv.lib.optionalString;
@@ -31,9 +33,8 @@ stdenv.mkDerivation rec {
       url = "http://www.cmake.org/Bug/file_download.php?file_id=4660&type=bug";
       sha256 = "136z63ff83hnwd247cq4m8m8164pklzyl5i2csf5h6wd8p01pdkj";
     })] ++
-    # Don't search in non-Nix locations such as /usr, but do search in
-    # Nixpkgs' Glibc.
-    optional (stdenv ? glibc) ./search-path.patch ++
+    # Don't search in non-Nix locations such as /usr, but do search in our libc.
+    [ ./search-path.patch ] ++
     optional (stdenv ? cross) (fetchurl {
       name = "fix-darwin-cross-compile.patch";
       url = "http://public.kitware.com/Bug/file_download.php?"
@@ -50,22 +51,24 @@ stdenv.mkDerivation rec {
   CMAKE_PREFIX_PATH = concatStringsSep ":"
     (concatMap (p: [ (p.dev or p) (p.out or p) ]) buildInputs);
 
-  configureFlags =
-    "--docdir=/share/doc/${name} --mandir=/share/man --system-libs --no-system-libarchive"
-    + stdenv.lib.optionalString useQt4 " --qt-gui";
+  configureFlags = [
+    "--docdir=/share/doc/${name}"
+    "--mandir=/share/man"
+    "--system-libs"
+    "--no-system-libarchive"
+   ] ++ stdenv.lib.optional useQt4 "--qt-gui";
 
   setupHook = ./setup-hook.sh;
 
   dontUseCmakeConfigure = true;
 
-  preConfigure = with stdenv; optionalString (stdenv ? glibc)
-    ''
+  preConfigure = with stdenv; ''
       source $setupHook
       fixCmakeFiles .
       substituteInPlace Modules/Platform/UnixPaths.cmake \
-        --subst-var-by glibc_bin ${getBin glibc} \
-        --subst-var-by glibc_dev ${getDev glibc} \
-        --subst-var-by glibc_lib ${getLib glibc}
+        --subst-var-by libc_bin ${getBin cc.libc} \
+        --subst-var-by libc_dev ${getDev cc.libc} \
+        --subst-var-by libc_lib ${getLib cc.libc}
     '';
 
   meta = {
diff --git a/pkgs/development/tools/build-managers/cmake/default.nix b/pkgs/development/tools/build-managers/cmake/default.nix
index 52822178c023..012ece041b21 100644
--- a/pkgs/development/tools/build-managers/cmake/default.nix
+++ b/pkgs/development/tools/build-managers/cmake/default.nix
@@ -7,6 +7,8 @@
 with stdenv.lib;
 
 assert wantPS -> (ps != null);
+assert stdenv ? cc;
+assert stdenv.cc ? libc;
 
 let
   os = stdenv.lib.optionalString;
@@ -25,10 +27,8 @@ stdenv.mkDerivation rec {
     sha256 = "0w3n2i02jpbgai4dxsigm1c1i1qb5v70wyxckzwrxvs0ri0fs1gx";
   };
 
-  patches =
-    # Don't search in non-Nix locations such as /usr, but do search in
-    # Nixpkgs' Glibc.
-    optional (stdenv ? glibc) ./search-path-3.2.patch
+  # Don't search in non-Nix locations such as /usr, but do search in our libc.
+  patches = [ ./search-path-3.2.patch ]
     ++ optional stdenv.isCygwin ./3.2.2-cygwin.patch;
 
   outputs = [ "out" ];
@@ -43,13 +43,12 @@ stdenv.mkDerivation rec {
 
   propagatedBuildInputs = optional wantPS ps;
 
-  preConfigure = with stdenv; optionalString (stdenv ? glibc)
-    ''
+  preConfigure = with stdenv; ''
       fixCmakeFiles .
       substituteInPlace Modules/Platform/UnixPaths.cmake \
-        --subst-var-by glibc_bin ${getBin glibc} \
-        --subst-var-by glibc_dev ${getDev glibc} \
-        --subst-var-by glibc_lib ${getLib glibc}
+        --subst-var-by libc_bin ${getBin cc.libc} \
+        --subst-var-by libc_dev ${getDev cc.libc} \
+        --subst-var-by libc_lib ${getLib cc.libc}
       substituteInPlace Modules/FindCxxTest.cmake \
         --replace "$""{PYTHON_EXECUTABLE}" ${stdenv.shell}
     '';
diff --git a/pkgs/development/tools/build-managers/cmake/search-path-3.2.patch b/pkgs/development/tools/build-managers/cmake/search-path-3.2.patch
index b61982efb9a7..ba7438d2c0f9 100644
--- a/pkgs/development/tools/build-managers/cmake/search-path-3.2.patch
+++ b/pkgs/development/tools/build-managers/cmake/search-path-3.2.patch
@@ -25,7 +25,7 @@ diff -ru3 cmake-3.4.3/Modules/Platform/UnixPaths.cmake cmake-3.4.3-new/Modules/P
 -  /usr/pkg/include
 -  /opt/csw/include /opt/include
 -  /usr/openwin/include
-+  @glibc_dev@/include
++  @libc_dev@/include
    )
 -
  list(APPEND CMAKE_SYSTEM_LIBRARY_PATH
@@ -39,26 +39,26 @@ diff -ru3 cmake-3.4.3/Modules/Platform/UnixPaths.cmake cmake-3.4.3-new/Modules/P
 -  /usr/pkg/lib
 -  /opt/csw/lib /opt/lib
 -  /usr/openwin/lib
-+  @glibc_lib@/lib
++  @libc_lib@/lib
    )
  
  list(APPEND CMAKE_SYSTEM_PROGRAM_PATH
 -  /usr/pkg/bin
-+  @glibc_bin@/bin
++  @libc_bin@/bin
    )
  
  list(APPEND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES
 -  /lib /lib32 /lib64 /usr/lib /usr/lib32 /usr/lib64
-+  @glibc_lib@/lib
++  @libc_lib@/lib
    )
  
  list(APPEND CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES
 -  /usr/include
-+  @glibc_dev@/include
++  @libc_dev@/include
    )
  list(APPEND CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES
 -  /usr/include
-+  @glibc_dev@/include
++  @libc_dev@/include
    )
  
  # Enable use of lib64 search path variants by default.
diff --git a/pkgs/development/tools/build-managers/cmake/search-path.patch b/pkgs/development/tools/build-managers/cmake/search-path.patch
index 6d6ca74ccadc..9fc949661686 100644
--- a/pkgs/development/tools/build-managers/cmake/search-path.patch
+++ b/pkgs/development/tools/build-managers/cmake/search-path.patch
@@ -53,7 +53,7 @@ diff -ru3 cmake-2.8.12.2/Modules/Platform/UnixPaths.cmake cmake-2.8.12.2-new/Mod
 -  /usr/pkg/include
 -  /opt/csw/include /opt/include
 -  /usr/openwin/include
-+  @glibc_dev@/include
++  @libc_dev@/include
    )
  
  list(APPEND CMAKE_SYSTEM_LIBRARY_PATH
@@ -67,26 +67,26 @@ diff -ru3 cmake-2.8.12.2/Modules/Platform/UnixPaths.cmake cmake-2.8.12.2-new/Mod
 -  /usr/pkg/lib
 -  /opt/csw/lib /opt/lib
 -  /usr/openwin/lib
-+  @glibc_lib@/lib
++  @libc_lib@/lib
    )
  
  list(APPEND CMAKE_SYSTEM_PROGRAM_PATH
 -  /usr/pkg/bin
-+  @glibc_bin@/bin
++  @libc_bin@/bin
    )
  
  list(APPEND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES
 -  /lib /usr/lib /usr/lib32 /usr/lib64
-+  @glibc_lib@/lib
++  @libc_lib@/lib
    )
  
  list(APPEND CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES
 -  /usr/include
-+  @glibc_dev@/include
++  @libc_dev@/include
    )
  list(APPEND CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES
 -  /usr/include
-+  @glibc_dev@/include
++  @libc_dev@/include
    )
  
  # Enable use of lib64 search path variants by default.

From 48d7c7751a4a320a8bc68a54f9c528aa9bea1482 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Thu, 13 Oct 2016 08:07:58 -0500
Subject: [PATCH 2/2] cmake: bootstrap in parallel

Builds on previous commit so this happens without needing stdenv.glibc.
---
 pkgs/development/tools/build-managers/cmake/2.8.nix     | 1 +
 pkgs/development/tools/build-managers/cmake/default.nix | 1 +
 2 files changed, 2 insertions(+)

diff --git a/pkgs/development/tools/build-managers/cmake/2.8.nix b/pkgs/development/tools/build-managers/cmake/2.8.nix
index 52c577aedb35..a197c69edff5 100644
--- a/pkgs/development/tools/build-managers/cmake/2.8.nix
+++ b/pkgs/development/tools/build-managers/cmake/2.8.nix
@@ -69,6 +69,7 @@ stdenv.mkDerivation rec {
         --subst-var-by libc_bin ${getBin cc.libc} \
         --subst-var-by libc_dev ${getDev cc.libc} \
         --subst-var-by libc_lib ${getLib cc.libc}
+      configureFlags="--parallel=''${NIX_BUILD_CORES:-1} $configureFlags"
     '';
 
   meta = {
diff --git a/pkgs/development/tools/build-managers/cmake/default.nix b/pkgs/development/tools/build-managers/cmake/default.nix
index 012ece041b21..9cc8d9875708 100644
--- a/pkgs/development/tools/build-managers/cmake/default.nix
+++ b/pkgs/development/tools/build-managers/cmake/default.nix
@@ -51,6 +51,7 @@ stdenv.mkDerivation rec {
         --subst-var-by libc_lib ${getLib cc.libc}
       substituteInPlace Modules/FindCxxTest.cmake \
         --replace "$""{PYTHON_EXECUTABLE}" ${stdenv.shell}
+      configureFlags="--parallel=''${NIX_BUILD_CORES:-1} $configureFlags"
     '';
   configureFlags =
     [ "--docdir=share/doc/${name}"