From 1a6039cb4575ad13a94163264b73567b51a57f84 Mon Sep 17 00:00:00 2001
From: Peter Simons <simons@cryp.to>
Date: Sun, 8 Aug 2010 18:51:42 +0000
Subject: [PATCH] Enable parallel building of gcc, glibc, gmp, mpfr, ncurses,
 coreutils, perl, python, git, and qt4.

If a build expressions has set "enableParallelBuilding = true", then the
generic builder may utilize more than one CPU core to build that particular
expression. This feature works out of the box for GNU Make. Expressions that
use other build drivers like Boost.Jam or SCons have to specify appropriate
flags such as "-j${NIX_BUILD_CORES}" themselves.

svn path=/nixpkgs/trunk/; revision=23042
---
 .../git-and-tools/git/default.nix             |  4 +++-
 .../development/compilers/gcc-4.3/default.nix |  2 ++
 .../development/compilers/gcc-4.4/default.nix |  2 ++
 .../development/compilers/gcc-4.5/default.nix |  2 ++
 .../compilers/gcc-apple/default.nix           |  2 ++
 .../compilers/gcc-apple64/default.nix         |  2 ++
 .../interpreters/perl-5.10/default.nix        |  2 ++
 .../interpreters/python/2.6/default.nix       |  2 ++
 .../libraries/glibc-2.11/builder.sh           |  2 +-
 .../libraries/glibc-2.11/builder2.sh          |  2 +-
 .../libraries/glibc-2.11/common.nix           |  2 ++
 .../libraries/glibc-2.5/builder.sh            |  2 +-
 .../libraries/glibc-2.5/default.nix           |  2 ++
 pkgs/development/libraries/gmp/4.3.1.nix      |  2 ++
 pkgs/development/libraries/gmp/default.nix    |  2 ++
 pkgs/development/libraries/mpfr/default.nix   |  2 ++
 .../development/libraries/ncurses/default.nix |  4 +++-
 .../libraries/qt-4.x/4.5/default.nix          |  2 ++
 .../libraries/qt-4.x/4.6/default.nix          |  2 ++
 .../libraries/qt-4.x/4.7/default.nix          |  2 ++
 pkgs/stdenv/generic/setup.sh                  | 19 +++++++++++++++++++
 pkgs/tools/misc/coreutils/default.nix         |  2 ++
 22 files changed, 60 insertions(+), 5 deletions(-)

diff --git a/pkgs/applications/version-management/git-and-tools/git/default.nix b/pkgs/applications/version-management/git-and-tools/git/default.nix
index d548f39799a43d..41da12cdf30c25 100644
--- a/pkgs/applications/version-management/git-and-tools/git/default.nix
+++ b/pkgs/applications/version-management/git-and-tools/git/default.nix
@@ -74,7 +74,7 @@ stdenv.mkDerivation rec {
        '')
 
    + ''# Install man pages and Info manual
-       make PERL_PATH="${perl}/bin/perl" cmd-list.made install install-info \
+       make -j $NIX_BUILD_CORES -l $NIX_BUILD_CORES PERL_PATH="${perl}/bin/perl" cmd-list.made install install-info \
          -C Documentation ''
 
    + (if guiSupport then ''
@@ -116,6 +116,8 @@ stdenv.mkDerivation rec {
 
      '';
 
+  enableParallelBuilding = true;
+
   meta = {
     license = "GPLv2";
     homepage = http://git-scm.com/;
diff --git a/pkgs/development/compilers/gcc-4.3/default.nix b/pkgs/development/compilers/gcc-4.3/default.nix
index 73caa156bd0163..b9d0d9bff5df0a 100644
--- a/pkgs/development/compilers/gcc-4.3/default.nix
+++ b/pkgs/development/compilers/gcc-4.3/default.nix
@@ -127,6 +127,8 @@ stdenv.mkDerivation ({
   passthru = { inherit langC langCC langFortran langVhdl langTreelang
       enableMultilib; };
 
+  enableParallelBuilding = true;
+
   meta = {
     homepage = "http://gcc.gnu.org/";
     license = "GPL/LGPL";
diff --git a/pkgs/development/compilers/gcc-4.4/default.nix b/pkgs/development/compilers/gcc-4.4/default.nix
index bdf2e58bcb4a03..e4206cabd8a0d5 100644
--- a/pkgs/development/compilers/gcc-4.4/default.nix
+++ b/pkgs/development/compilers/gcc-4.4/default.nix
@@ -201,6 +201,8 @@ stdenv.mkDerivation ({
   passthru = { inherit langC langCC langAda langFortran langTreelang langVhdl
       enableMultilib version; };
 
+  enableParallelBuilding = true;
+
   meta = {
     homepage = http://gcc.gnu.org/;
     license = "GPLv3+";  # runtime support libraries are typically LGPLv3+
diff --git a/pkgs/development/compilers/gcc-4.5/default.nix b/pkgs/development/compilers/gcc-4.5/default.nix
index 39232c4988ec67..38f4338d4ac234 100644
--- a/pkgs/development/compilers/gcc-4.5/default.nix
+++ b/pkgs/development/compilers/gcc-4.5/default.nix
@@ -289,6 +289,8 @@ stdenv.mkDerivation ({
   passthru = { inherit langC langCC langAda langFortran langTreelang langVhdl
       enableMultilib version; };
 
+  enableParallelBuilding = true;
+
   meta = {
     homepage = http://gcc.gnu.org/;
     license = "GPLv3+";  # runtime support libraries are typically LGPLv3+
diff --git a/pkgs/development/compilers/gcc-apple/default.nix b/pkgs/development/compilers/gcc-apple/default.nix
index 8006bd4426d7bc..3346a4e327a5ac 100644
--- a/pkgs/development/compilers/gcc-apple/default.nix
+++ b/pkgs/development/compilers/gcc-apple/default.nix
@@ -21,6 +21,8 @@ stdenv.mkDerivation ({
       sha256 = "a7d8041e50e110f5a503e188a05cb217f0c99c51f248a0a1387cc07a0b6f167f";
     }) ;
 
+  enableParallelBuilding = true;
+
   sourceRoot = "gcc_42-5574/";
   patches =
     [./pass-cxxcpp.patch ./debug_list.patch]
diff --git a/pkgs/development/compilers/gcc-apple64/default.nix b/pkgs/development/compilers/gcc-apple64/default.nix
index b87cd596a5b3a3..ec16fae7977418 100644
--- a/pkgs/development/compilers/gcc-apple64/default.nix
+++ b/pkgs/development/compilers/gcc-apple64/default.nix
@@ -21,6 +21,8 @@ stdenv.mkDerivation ({
       sha256 = "1fy6j41rhxdsm19sib9wygjl5l54g8pm13c6y5x13f40mavw1mma";
     }) ;
 
+  enableParallelBuilding = true;
+
   libstdcxx = "libstdcxx-39";
   sourceRoot = "gcc-5646/";
   patches =
diff --git a/pkgs/development/interpreters/perl-5.10/default.nix b/pkgs/development/interpreters/perl-5.10/default.nix
index 31168291e3d1f0..76928d10448148 100644
--- a/pkgs/development/interpreters/perl-5.10/default.nix
+++ b/pkgs/development/interpreters/perl-5.10/default.nix
@@ -39,6 +39,8 @@ stdenv.mkDerivation rec {
 
   dontAddPrefix = true;
 
+  enableParallelBuilding = true;
+
   preConfigure =
     ''
       configureFlags="$configureFlags -Dprefix=$out -Dman1dir=$out/share/man/man1 -Dman3dir=$out/share/man/man3"
diff --git a/pkgs/development/interpreters/python/2.6/default.nix b/pkgs/development/interpreters/python/2.6/default.nix
index 8865738b0d75c2..3fd035b09bb82d 100644
--- a/pkgs/development/interpreters/python/2.6/default.nix
+++ b/pkgs/development/interpreters/python/2.6/default.nix
@@ -88,6 +88,8 @@ stdenv.mkDerivation ( {
     libPrefix = "python${majorVersion}";
   };
 
+  enableParallelBuilding = true;
+
   meta = {
     platforms = stdenv.lib.platforms.all;
   };
diff --git a/pkgs/development/libraries/glibc-2.11/builder.sh b/pkgs/development/libraries/glibc-2.11/builder.sh
index f8da3b8bbe2e5c..cbdb55b121b762 100644
--- a/pkgs/development/libraries/glibc-2.11/builder.sh
+++ b/pkgs/development/libraries/glibc-2.11/builder.sh
@@ -17,7 +17,7 @@ postConfigure() {
 
 postInstall() {
     if test -n "$installLocales"; then
-        make localedata/install-locales
+        make -j${NIX_BUILD_CORES:-1} -l${NIX_BUILD_CORES:-1} localedata/install-locales
     fi
     
     test -f $out/etc/ld.so.cache && rm $out/etc/ld.so.cache
diff --git a/pkgs/development/libraries/glibc-2.11/builder2.sh b/pkgs/development/libraries/glibc-2.11/builder2.sh
index f156e647e0b40a..7d1a05131c2ac8 100644
--- a/pkgs/development/libraries/glibc-2.11/builder2.sh
+++ b/pkgs/development/libraries/glibc-2.11/builder2.sh
@@ -20,7 +20,7 @@ postConfigure() {
 
 postInstall() {
     if test -n "$installLocales"; then
-        make localedata/install-locales
+        make -j${NIX_BUILD_CORES:-1} -l${NIX_BUILD_CORES:-1} localedata/install-locales
     fi
     
     test -f $out/etc/ld.so.cache && rm $out/etc/ld.so.cache
diff --git a/pkgs/development/libraries/glibc-2.11/common.nix b/pkgs/development/libraries/glibc-2.11/common.nix
index b792e8447e7496..00d9b99cc519ac 100644
--- a/pkgs/development/libraries/glibc-2.11/common.nix
+++ b/pkgs/development/libraries/glibc-2.11/common.nix
@@ -30,6 +30,8 @@ stdenv.mkDerivation ({
 
   inherit (stdenv) is64bit;
 
+  enableParallelBuilding = true;
+
   patches =
     stdenv.lib.optional (fetchgit == null)
     /* Fix for NIXPKGS-79: when doing host name lookups, when
diff --git a/pkgs/development/libraries/glibc-2.5/builder.sh b/pkgs/development/libraries/glibc-2.5/builder.sh
index 695a3a1ab3daba..4cd0132e37008b 100644
--- a/pkgs/development/libraries/glibc-2.5/builder.sh
+++ b/pkgs/development/libraries/glibc-2.5/builder.sh
@@ -38,7 +38,7 @@ postConfigure() {
 
 postInstall() {
     if test -n "$installLocales"; then
-        make localedata/install-locales
+        make -j${NIX_BUILD_CORES:-1} -l${NIX_BUILD_CORES:-1} localedata/install-locales
     fi
     rm $out/etc/ld.so.cache
     (cd $out/include && ln -s $kernelHeaders/include/* .) || exit 1
diff --git a/pkgs/development/libraries/glibc-2.5/default.nix b/pkgs/development/libraries/glibc-2.5/default.nix
index abfc3d0f43955b..83447d8fe8a975 100644
--- a/pkgs/development/libraries/glibc-2.5/default.nix
+++ b/pkgs/development/libraries/glibc-2.5/default.nix
@@ -30,6 +30,8 @@ stdenv.mkDerivation {
   # the symbol __i686.get_pc_thunk.dx to be mangled.
   NIX_CFLAGS_COMPILE = "-U__i686";
 
+  enableParallelBuilding = true;
+
   meta = {
     homepage = http://www.gnu.org/software/libc/;
     description = "The GNU C Library";
diff --git a/pkgs/development/libraries/gmp/4.3.1.nix b/pkgs/development/libraries/gmp/4.3.1.nix
index 17455b9f1dd49d..5f83d9ae46dc90 100644
--- a/pkgs/development/libraries/gmp/4.3.1.nix
+++ b/pkgs/development/libraries/gmp/4.3.1.nix
@@ -16,6 +16,8 @@ stdenv.mkDerivation rec {
 
   doCheck = true;
 
+  enableParallelBuilding = true;
+
   meta = {
     description = "A free library for arbitrary precision arithmetic, operating on signed integers, rational numbers, and floating point numbers";
     homepage = http://gmplib.org/;
diff --git a/pkgs/development/libraries/gmp/default.nix b/pkgs/development/libraries/gmp/default.nix
index 0b608fb4aadae3..ac20bc68f4d8af 100644
--- a/pkgs/development/libraries/gmp/default.nix
+++ b/pkgs/development/libraries/gmp/default.nix
@@ -16,6 +16,8 @@ stdenv.mkDerivation rec {
 
   doCheck = true;
 
+  enableParallelBuilding = true;
+
   meta = {
     description = "A free library for arbitrary precision arithmetic, operating on signed integers, rational numbers, and floating point numbers";
     homepage = http://gmplib.org/;
diff --git a/pkgs/development/libraries/mpfr/default.nix b/pkgs/development/libraries/mpfr/default.nix
index dea0113cc69f51..1bfd1448200371 100644
--- a/pkgs/development/libraries/mpfr/default.nix
+++ b/pkgs/development/libraries/mpfr/default.nix
@@ -12,6 +12,8 @@ stdenv.mkDerivation rec {
 
   doCheck = true;
 
+  enableParallelBuilding = true;
+
   meta = {
     homepage = http://www.mpfr.org/;
     description = "GNU MPFR, a library for multiple-precision floating-point arithmetic";
diff --git a/pkgs/development/libraries/ncurses/default.nix b/pkgs/development/libraries/ncurses/default.nix
index 344db123801ac2..dd3d60e48129f8 100644
--- a/pkgs/development/libraries/ncurses/default.nix
+++ b/pkgs/development/libraries/ncurses/default.nix
@@ -14,7 +14,9 @@ stdenv.mkDerivation ( rec {
   '';
 
   selfBuildNativeInput = true;
-    
+
+  enableParallelBuilding = true;
+
   preBuild = ''sed -e "s@\([[:space:]]\)sh @\1''${SHELL} @" -i */Makefile Makefile'';
 
   # When building a wide-character (Unicode) build, create backward
diff --git a/pkgs/development/libraries/qt-4.x/4.5/default.nix b/pkgs/development/libraries/qt-4.x/4.5/default.nix
index 35960c4d8ba863..3b29043aeeb46b 100644
--- a/pkgs/development/libraries/qt-4.x/4.5/default.nix
+++ b/pkgs/development/libraries/qt-4.x/4.5/default.nix
@@ -74,6 +74,8 @@ stdenv.mkDerivation {
     ${if keepDocumentation == false then "rm -rf $out/doc" else ""}
   '';
 
+  enableParallelBuilding = true;
+
   meta = {
     homepage = http://www.qtsoftware.com/downloads/opensource/appdev/linux-x11-cpp;
     description = "A cross-platform application framework for C++";
diff --git a/pkgs/development/libraries/qt-4.x/4.6/default.nix b/pkgs/development/libraries/qt-4.x/4.6/default.nix
index 9440765d830db1..3253e58e7c21e4 100644
--- a/pkgs/development/libraries/qt-4.x/4.6/default.nix
+++ b/pkgs/development/libraries/qt-4.x/4.6/default.nix
@@ -89,6 +89,8 @@ stdenv.mkDerivation rec {
 
   postInstall = if useDocs then "rm -rf $out/share/doc/${name}/{html,src}" else "";
 
+  enableParallelBuilding = true;
+
   meta = {
     homepage = http://qt.nokia.com/products;
     description = "A cross-platform application framework for C++";
diff --git a/pkgs/development/libraries/qt-4.x/4.7/default.nix b/pkgs/development/libraries/qt-4.x/4.7/default.nix
index 2ab7c388553b67..cc6d3c8d513e36 100644
--- a/pkgs/development/libraries/qt-4.x/4.7/default.nix
+++ b/pkgs/development/libraries/qt-4.x/4.7/default.nix
@@ -99,6 +99,8 @@ stdenv.mkDerivation rec {
 
   postInstall = if useDocs then "rm -rf $out/share/doc/${name}/{html,src}" else "";
 
+  enableParallelBuilding = true;
+
   meta = with stdenv.lib; {
     homepage = http://qt.nokia.com/products;
     description = "A cross-platform application framework for C++";
diff --git a/pkgs/stdenv/generic/setup.sh b/pkgs/stdenv/generic/setup.sh
index 5e2fc7b24dbe60..948b6ca3ad6678 100644
--- a/pkgs/stdenv/generic/setup.sh
+++ b/pkgs/stdenv/generic/setup.sh
@@ -269,6 +269,23 @@ fi
 export NIX_INDENT_MAKE=1
 
 
+# Normalize the NIX_BUILD_CORES variable. The value might be 0, which
+# means that we're supposed to try and auto-detect the number of
+# available CPU cores at run-time.
+
+if test -z "${NIX_BUILD_CORES:-}"; then
+  NIX_BUILD_CORES="1"
+elif test "$NIX_BUILD_CORES" -le 0; then
+  NIX_BUILD_CORES=$(nproc 2>/dev/null || true)
+  if expr >/dev/null 2>&1 "$NIX_BUILD_CORES" : "^[0-9][0-9]*$"; then
+    :
+  else
+    NIX_BUILD_CORES="1"
+  fi
+fi
+export NIX_BUILD_CORES
+
+
 ######################################################################
 # Misc. helper functions.
 
@@ -603,6 +620,7 @@ buildPhase() {
 
     echo "make flags: $makeFlags ${makeFlagsArray[@]} $buildFlags ${buildFlagsArray[@]}"
     make ${makefile:+-f $makefile} \
+        ${enableParallelBuilding:+-j${NIX_BUILD_CORES} -l${NIX_BUILD_CORES}} \
         $makeFlags "${makeFlagsArray[@]}" \
         $buildFlags "${buildFlagsArray[@]}"
 
@@ -615,6 +633,7 @@ checkPhase() {
 
     echo "check flags: $makeFlags ${makeFlagsArray[@]} $checkFlags ${checkFlagsArray[@]}"
     make ${makefile:+-f $makefile} \
+        ${enableParallelBuilding:+-j${NIX_BUILD_CORES} -l${NIX_BUILD_CORES}} \
         $makeFlags "${makeFlagsArray[@]}" \
         $checkFlags "${checkFlagsArray[@]}" ${checkTarget:-check}
 
diff --git a/pkgs/tools/misc/coreutils/default.nix b/pkgs/tools/misc/coreutils/default.nix
index f4cf9fe98869dc..8052ce9fa56ab1 100644
--- a/pkgs/tools/misc/coreutils/default.nix
+++ b/pkgs/tools/misc/coreutils/default.nix
@@ -24,6 +24,8 @@ stdenv.mkDerivation (rec {
   # and {Open,Free}BSD.
   doCheck = (stdenv ? glibc) && (cross == null);
 
+  enableParallelBuilding = true;
+
   meta = {
     homepage = http://www.gnu.org/software/coreutils/;
     description = "The basic file, shell and text manipulation utilities of the GNU operating system";