@@ -19,6 +19,7 @@ in
1919, buildTarget ? ""
2020, buildTools ? [], libraryToolDepends ? [], executableToolDepends ? [], testToolDepends ? [], benchmarkToolDepends ? []
2121, configureFlags ? []
22+, buildFlags ? []
2223, description ? ""
2324, doCheck ? !isCross && stdenv.lib.versionOlder "7.4" ghc.version
2425, doBenchmark ? false
@@ -31,7 +32,7 @@ in
3132, enableSharedExecutables ? false
3233, enableSharedLibraries ? ((ghc.isGhcjs or false) || stdenv.lib.versionOlder "7.7" ghc.version)
3334, enableDeadCodeElimination ? (!stdenv.isDarwin) # TODO: use -dead_strip for darwin
34−, enableStaticLibraries ? true
35+, enableStaticLibraries ? !hostPlatform.isWindows
3536, enableHsc2hsViaAsm ? hostPlatform.isWindows && stdenv.lib.versionAtLeast ghc.version "8.4"
3637, extraLibraries ? [], librarySystemDepends ? [], executableSystemDepends ? []
3738, homepage ? "http://hackage.haskell.org/package/${pname}"
@@ -68,6 +69,10 @@ in
6869
6970assert editedCabalFile != null -> revision != null;
7071
72+# --enable-static does not work on windows. This is a bug in GHC.
73+# --enable-static will pass -staticlib to ghc, which only works for mach-o and elf.
74+assert hostPlatform.isWindows -> enableStaticLibraries == false;
75+
7176let
7277
7378 inherit (stdenv.lib) optional optionals optionalString versionOlder versionAtLeast
@@ -126,6 +131,8 @@ let
126131 crossCabalFlagsString =
127132 stdenv.lib.optionalString isCross (" " + stdenv.lib.concatStringsSep " " crossCabalFlags);
128133
134+ buildFlagsString = optionalString (buildFlags != []) (" " + concatStringsSep " " buildFlags);
135+
129136 defaultConfigureFlags = [
130137 "--verbose" "--prefix=$out" "--libdir=\\$prefix/lib/\\$compiler" "--libsubdir=\\$pkgid"
131138 (optionalString enableSeparateDataOutput "--datadir=$data/share/${ghc.name}")
@@ -169,18 +176,22 @@ let
169176 optionals doCheck testPkgconfigDepends ++ optionals doBenchmark benchmarkPkgconfigDepends;
170177
171178 nativeBuildInputs = [ ghc nativeGhc removeReferencesTo ] ++ optional (allPkgconfigDepends != []) pkgconfig ++
179+ setupHaskellDepends ++
172180 buildTools ++ libraryToolDepends ++ executableToolDepends;
173181 propagatedBuildInputs = buildDepends ++ libraryHaskellDepends ++ executableHaskellDepends;
174− otherBuildInputs = setupHaskellDepends ++ extraLibraries ++ librarySystemDepends ++ executableSystemDepends ++
182+ otherBuildInputs = extraLibraries ++ librarySystemDepends ++ executableSystemDepends ++
175183 allPkgconfigDepends ++
176184 optionals doCheck (testDepends ++ testHaskellDepends ++ testSystemDepends ++ testToolDepends) ++
177185 optionals doBenchmark (benchmarkDepends ++ benchmarkHaskellDepends ++ benchmarkSystemDepends ++ benchmarkToolDepends);
186+
178187 allBuildInputs = propagatedBuildInputs ++ otherBuildInputs;
179188
180189 haskellBuildInputs = stdenv.lib.filter isHaskellPkg allBuildInputs;
181190 systemBuildInputs = stdenv.lib.filter isSystemPkg allBuildInputs;
182191
183− ghcEnv = ghc.withPackages (p: haskellBuildInputs);
192+ # When not cross compiling, also include Setup.hs dependencies.
193+ ghcEnv = ghc.withPackages (p:
194+ haskellBuildInputs ++ stdenv.lib.optional (!isCross) setupHaskellDepends);
184195
185196 setupCommand = "./Setup";
186197
@@ -190,6 +201,22 @@ let
190201
191202 nativeGhcCommand = "${nativeGhc.targetPrefix}ghc";
192203
204+ buildPkgDb = ghcName: ''
205+ if [ -d "$p/lib/${ghcName}/package.conf.d" ]; then
206+ cp -f "$p/lib/${ghcName}/package.conf.d/"*.conf $packageConfDir/
207+ continue
208+ fi
209+ if [ -d "$p/include" ]; then
210+ configureFlags+=" --extra-include-dirs=$p/include"
211+ fi
212+ if [ -d "$p/lib" ]; then
213+ configureFlags+=" --extra-lib-dirs=$p/lib"
214+ fi
215+ if [[ -d "$p/Library/Frameworks" ]]; then
216+ configureFlags+=" --extra-framework-dirs=$p/Library/Frameworks"
217+ fi
218+ '';
219+
193220in
194221
195222assert allPkgconfigDepends != [] -> pkgconfig != null;
@@ -230,30 +257,37 @@ stdenv.mkDerivation ({
230257 echo "Build with ${ghc}."
231258 ${optionalString (hasActiveLibrary && hyperlinkSource) "export PATH=${hscolour}/bin:$PATH"}
232259
260+ '' + (optionalString (setupHaskellDepends != []) ''
261+ setupPackageConfDir="$TMPDIR/setup-package.conf.d"
262+ mkdir -p $setupPackageConfDir
263+ '') + ''
233264 packageConfDir="$TMPDIR/package.conf.d"
234265 mkdir -p $packageConfDir
235266
236267 setupCompileFlags="${concatStringsSep " " setupCompileFlags}"
237268 configureFlags="${concatStringsSep " " defaultConfigureFlags} $configureFlags"
269+ ''
270+ # We build the Setup.hs on the *build* machine, and as such should only add
271+ # dependencies for the build machine.
272+ #
273+ # pkgs* arrays defined in stdenv/setup.hs
274+ + (optionalString (setupHaskellDepends != []) ''
275+ for p in "''${pkgsBuildBuild[@]}" "''${pkgsBuildHost[@]}" "''${pkgsBuildTarget[@]}"; do
276+ ${buildPkgDb nativeGhc.name}
277+ done
278+ ${nativeGhcCommand}-pkg --${nativePackageDbFlag}="$setupPackageConfDir" recache
279+ '')
238280
239− # host.*Pkgs defined in stdenv/setup.hs
281+ # For normal components
282+ + ''
240283 for p in "''${pkgsHostHost[@]}" "''${pkgsHostTarget[@]}"; do
241− if [ -d "$p/lib/${ghc.name}/package.conf.d" ]; then
242− cp -f "$p/lib/${ghc.name}/package.conf.d/"*.conf $packageConfDir/
243− continue
244− fi
245− if [ -d "$p/include" ]; then
246− configureFlags+=" --extra-include-dirs=$p/include"
247− fi
248− if [ -d "$p/lib" ]; then
249− configureFlags+=" --extra-lib-dirs=$p/lib"
250− fi
284+ ${buildPkgDb ghc.name}
251285 done
252286 ''
253287 # only use the links hack if we're actually building dylibs. otherwise, the
254288 # "dynamic-library-dirs" point to nonexistent paths, and the ln command becomes
255289 # "ln -s $out/lib/links", which tries to recreate the links dir and fails
256− + (optionalString (stdenv.isDarwin && enableSharedLibraries) ''
290+ + (optionalString (stdenv.isDarwin && (enableSharedLibraries || enableSharedExecutables)) ''
257291 # Work around a limit in the macOS Sierra linker on the number of paths
258292 # referenced by any one dynamic library:
259293 #
@@ -282,7 +316,11 @@ stdenv.mkDerivation ({
282316 done
283317
284318 echo setupCompileFlags: $setupCompileFlags
285− ${nativeGhcCommand} $setupCompileFlags --make -o Setup -odir $TMPDIR -hidir $TMPDIR $i
319+ ${optionalString (setupHaskellDepends != [])
320+ ''
321+ echo GHC_PACKAGE_PATH="$setupPackageConfDir:"
322+ GHC_PACKAGE_PATH="$setupPackageConfDir:" ''
323+ }${nativeGhcCommand} $setupCompileFlags --make -o Setup -odir $TMPDIR -hidir $TMPDIR $i
286324
287325 runHook postCompileBuildDriver
288326 '';
@@ -310,7 +348,7 @@ stdenv.mkDerivation ({
310348
311349 buildPhase = ''
312350 runHook preBuild
313− ${setupCommand} build ${buildTarget}${crossCabalFlagsString}
351+ ${setupCommand} build ${buildTarget}${crossCabalFlagsString}${buildFlagsString}
314352 runHook postBuild
315353 '';
316354