summary refs log tree commit diff
path: root/pkgs/development/haskell-modules/generic-builder.nix
diff options
context:
space:
mode:
authorPeter Simons <simons@cryp.to>2015-01-16 20:54:35 +0100
committerPeter Simons <simons@cryp.to>2015-01-17 20:29:00 +0100
commit54bbfd544087dcaf422899191db2a0ce4425b67f (patch)
treea5c3f7858c1c463774c90d1c0550138c44438e8c /pkgs/development/haskell-modules/generic-builder.nix
parente6ecb1fb83a9fda089c6fa23617185145d54b80f (diff)
downloadnixlib-54bbfd544087dcaf422899191db2a0ce4425b67f.tar
nixlib-54bbfd544087dcaf422899191db2a0ce4425b67f.tar.gz
nixlib-54bbfd544087dcaf422899191db2a0ce4425b67f.tar.bz2
nixlib-54bbfd544087dcaf422899191db2a0ce4425b67f.tar.lz
nixlib-54bbfd544087dcaf422899191db2a0ce4425b67f.tar.xz
nixlib-54bbfd544087dcaf422899191db2a0ce4425b67f.tar.zst
nixlib-54bbfd544087dcaf422899191db2a0ce4425b67f.zip
haskell-generic-builder: re-factor for improved modularity
Diffstat (limited to 'pkgs/development/haskell-modules/generic-builder.nix')
-rw-r--r--pkgs/development/haskell-modules/generic-builder.nix128
1 files changed, 77 insertions, 51 deletions
diff --git a/pkgs/development/haskell-modules/generic-builder.nix b/pkgs/development/haskell-modules/generic-builder.nix
index 0f98ded5eb14..a4368838fe43 100644
--- a/pkgs/development/haskell-modules/generic-builder.nix
+++ b/pkgs/development/haskell-modules/generic-builder.nix
@@ -7,6 +7,7 @@
 , sha256 ? null
 , src ? fetchurl { url = "mirror://hackage/${pname}-${version}.tar.gz"; inherit sha256; }
 , buildDepends ? []
+, buildTarget ? ""
 , buildTools ? []
 , configureFlags ? []
 , description ? ""
@@ -50,6 +51,11 @@ let
   inherit (stdenv.lib) optional optionals optionalString versionOlder
                        concatStringsSep enableFeature optionalAttrs;
 
+  newCabalFile = fetchurl {
+    url = "http://hackage.haskell.org/package/${pname}-${version}/${pname}.cabal";
+    sha256 = editedCabalFile;
+  };
+
   defaultSetupHs = builtins.toFile "Setup.hs" ''
                      import Distribution.Simple
                      main = defaultMain
@@ -58,7 +64,18 @@ let
   ghc76xOrLater = stdenv.lib.versionOlder "7.6" ghc.version;
   packageDbFlag = if ghc76xOrLater then "package-db" else "package-conf";
 
+  hasActiveLibrary = isLibrary && (enableStaticLibraries || enableSharedLibraries || enableLibraryProfiling);
+
+  enableParallelBuilding = versionOlder "7.8" ghc.version && !hasActiveLibrary;
+
   defaultConfigureFlags = [
+    "--verbose" "--prefix=$out" "--libdir=\\$prefix/lib/\\$compiler" "--libsubdir=\\$pkgid"
+    "--with-gcc=$CC"            # Clang won't work without that extra information.
+    "--package-db=$packageConfDir"
+    (optionalString (enableSharedExecutables && stdenv.isLinux) "--ghc-option=-optl=-Wl,-rpath=$out/lib/${ghc.name}/${pname}-${version}")
+    (optionalString (enableSharedExecutables && stdenv.isDarwin) "--ghc-option=-optl=-Wl,-headerpad_max_install_names")
+    (optionalString enableParallelBuilding "--ghc-option=-j$NIX_BUILD_CORES")
+    (optionalString (useCpphs) "--with-cpphs=${cpphs}/bin/cpphs --ghc-options=-cpp --ghc-options=-pgmP${cpphs}/bin/cpphs --ghc-options=-optP--cpp")
     (enableFeature enableSplitObjs "split-objs")
     (enableFeature enableLibraryProfiling "library-profiling")
     (enableFeature enableSharedLibraries "shared")
@@ -67,21 +84,21 @@ let
     (optionalString (versionOlder "7" ghc.version) (enableFeature doCheck "tests"))
   ];
 
-  hasActiveLibrary = isLibrary && (enableStaticLibraries || enableSharedLibraries);
-
-  newCabalFile = fetchurl {
-    url = "http://hackage.haskell.org/package/${pname}-${version}/${pname}.cabal";
-    sha256 = editedCabalFile;
-  };
+  setupCompileFlags = [
+    (optionalString (!coreSetup) "-${packageDbFlag}=$packageConfDir")
+    (optionalString (versionOlder "7.8" ghc.version) "-j$NIX_BUILD_CORES")
+  ];
 
-  isHaskellPkg = x: (x ? pname) && (x ? version);
+  isHaskellPkg = x: (x ? pname) && (x ? version) && (x ? env);
   isSystemPkg = x: !isHaskellPkg x;
 
-  allBuildInputs = stdenv.lib.filter (x: x != null) (
-                     buildDepends ++ extraLibraries ++ buildTools ++
+  propagatedBuildInputs = buildDepends;
+  otherBuildInputs = extraLibraries ++
+                     buildTools ++
                      optionals (pkgconfigDepends != []) ([pkgconfig] ++ pkgconfigDepends) ++
-                     optionals doCheck testDepends
-                   );
+                     optionals doCheck testDepends;
+  allBuildInputs = propagatedBuildInputs ++ otherBuildInputs;
+
   haskellBuildInputs = stdenv.lib.filter isHaskellPkg allBuildInputs;
   systemBuildInputs = stdenv.lib.filter isSystemPkg allBuildInputs;
 
@@ -91,46 +108,31 @@ in
 stdenv.mkDerivation ({
   name = "${optionalString hasActiveLibrary "haskell-"}${pname}-${version}";
 
+  prePhases = ["setupCompilerEnvironmentPhase"];
+  preConfigurePhases = ["jailbreakPhase" "compileBuildDriverPhase"];
+  preInstallPhases = ["haddockPhase"];
+
   inherit src;
 
-  nativeBuildInputs = extraLibraries ++ buildTools ++
-    optionals (pkgconfigDepends != []) ([pkgconfig] ++ pkgconfigDepends) ++
-    optionals doCheck testDepends ++
-    optionals (!hasActiveLibrary) buildDepends;
-  propagatedNativeBuildInputs = optionals hasActiveLibrary buildDepends;
+  nativeBuildInputs = otherBuildInputs ++ optionals (!hasActiveLibrary) propagatedBuildInputs;
+  propagatedNativeBuildInputs = optionals hasActiveLibrary propagatedBuildInputs;
 
-  # GHC needs the locale configured during the Haddock phase.
-  LANG = "en_US.UTF-8";
+  LANG = "en_US.UTF-8";         # GHC needs the locale configured during the Haddock phase.
   LOCALE_ARCHIVE = optionalString stdenv.isLinux "${glibcLocales}/lib/locale/locale-archive";
 
-  configurePhase = ''
-    runHook preConfigure
+  setupCompilerEnvironmentPhase = ''
+    runHook preSetupCompilerEnvironment
 
     echo "Building with ${ghc}."
     export PATH="${ghc}/bin:$PATH"
     ${optionalString (hasActiveLibrary && hyperlinkSource) "export PATH=${hscolour}/bin:$PATH"}
 
-    configureFlags="--verbose --prefix=$out --libdir=\$prefix/lib/\$compiler --libsubdir=\$pkgid $configureFlags"
-    configureFlags+=' ${concatStringsSep " " defaultConfigureFlags}'
-    ${optionalString (enableSharedExecutables && stdenv.isLinux) ''
-      configureFlags+=" --ghc-option=-optl=-Wl,-rpath=$out/lib/${ghc.name}/${pname}-${version}"
-    ''}
-    ${optionalString (enableSharedExecutables && stdenv.isDarwin) ''
-      configureFlags+=" --ghc-option=-optl=-Wl,-headerpad_max_install_names"
-    ''}
-    ${optionalString (versionOlder "7.8" ghc.version && !isLibrary) ''
-      configureFlags+=" --ghc-option=-j$NIX_BUILD_CORES"
-      setupCompileFlags="-j$NIX_BUILD_CORES"
-    ''}${optionalString stdenv.isDarwin ''
-      configureFlags+=" --with-gcc=$CC"  # Cabal won't find clang without help.
-    ''}${optionalString useCpphs ''
-      configureFlags+=" --with-cpphs=${cpphs}/bin/cpphs"
-      configureFlags+=" --ghc-options=-cpp --ghc-options=-pgmP${cpphs}/bin/cpphs --ghc-options=-optP--cpp"
-    ''}
-
     packageConfDir="$TMP/package.conf.d"
     mkdir -p $packageConfDir
 
+    setupCompileFlags="${concatStringsSep " " setupCompileFlags}"
+    configureFlags="${concatStringsSep " " defaultConfigureFlags} $configureFlags"
+
     local inputClosure=""
     for i in $propagatedNativeBuildInputs $nativeBuildInputs; do
       findInputs $i inputClosure propagated-native-build-inputs
@@ -150,25 +152,43 @@ stdenv.mkDerivation ({
       done
     done
     ghc-pkg --${packageDbFlag}="$packageConfDir" recache
-    configureFlags+=" --package-db=$packageConfDir"
+
+    runHook postSetupCompilerEnvironment
+  '';
+
+  jailbreakPhase = ''
+    runHook preJailbreak
 
     ${optionalString (editedCabalFile != null) ''
       echo "Replacing Cabal file with edited version ${newCabalFile}."
       cp ${newCabalFile} ${pname}.cabal
-    ''}
-
-    ${optionalString jailbreak ''
+    ''}${optionalString jailbreak ''
       echo "Running jailbreak-cabal to lift version restrictions on build inputs."
       ${jailbreak-cabal}/bin/jailbreak-cabal ${pname}.cabal
     ''}
 
+    runHook postJailbreak
+  '';
+
+  compileBuildDriverPhase = ''
+    runHook preCompileBuildDriver
+
     for i in Setup.hs Setup.lhs ${defaultSetupHs}; do
       test -f $i && break
     done
-    ghc ${optionalString (! coreSetup) "-${packageDbFlag}=$packageConfDir "}$setupCompileFlags --make -o Setup -odir $TMPDIR -hidir $TMPDIR $i
 
-    echo configureFlags: $configureFlags
+    echo setupCompileFlags: $setupCompileFlags
+    ghc $setupCompileFlags --make -o Setup -odir $TMPDIR -hidir $TMPDIR $i
+
+    runHook postCompileBuildDriver
+  '';
+
+  configurePhase = ''
+    runHook preConfigure
+
     unset GHC_PACKAGE_PATH      # Cabal complains if this variable is set during configure.
+
+    echo configureFlags: $configureFlags
     ./Setup configure $configureFlags 2>&1 | ${coreutils}/bin/tee "$NIX_BUILD_TOP/cabal-configure.log"
     if ${gnugrep}/bin/egrep -q '^Warning:.*depends on multiple versions' "$NIX_BUILD_TOP/cabal-configure.log"; then
       echo >&2 "*** abort because of serious configure-time warning from Cabal"
@@ -182,12 +202,7 @@ stdenv.mkDerivation ({
 
   buildPhase = ''
     runHook preBuild
-    ./Setup build
-    ${optionalString (!noHaddock && hasActiveLibrary) ''
-      ./Setup haddock --html \
-        ${optionalString doHoogle "--hoogle"} \
-        ${optionalString (hasActiveLibrary && hyperlinkSource) "--hyperlink-source"}
-    ''}
+    ./Setup build ${buildTarget}
     runHook postBuild
   '';
 
@@ -197,6 +212,16 @@ stdenv.mkDerivation ({
     runHook postCheck
   '';
 
+  haddockPhase = ''
+    runHook preHaddock
+    ${optionalString (!noHaddock && hasActiveLibrary) ''
+      ./Setup haddock --html \
+        ${optionalString doHoogle "--hoogle"} \
+        ${optionalString (hasActiveLibrary && hyperlinkSource) "--hyperlink-source"}
+    ''}
+    runHook postHaddock
+  '';
+
   installPhase = ''
     runHook preInstall
 
@@ -242,10 +267,11 @@ stdenv.mkDerivation ({
 
   };
 
-  meta = { inherit homepage license platforms hydraPlatforms; }
+  meta = { inherit homepage license platforms; }
          // optionalAttrs broken               { inherit broken; }
          // optionalAttrs (description != "")  { inherit description; }
          // optionalAttrs (maintainers != [])  { inherit maintainers; }
+         // optionalAttrs (hydraPlatforms != platforms) { inherit hydraPlatforms; }
          ;
 
 }