@@ -1,4 +1,4 @@
1−{ stdenv, buildPackages, ghc, lib, pkgconfig, writeText, runCommand, haskellLib, nonReinstallablePkgs, ghcForComponent, hsPkgs }:
1+{ stdenv, buildPackages, ghc, lib, pkgconfig, haskellLib, makeConfigFiles, ghcForComponent, hsPkgs }:
22
33{ componentId
44, component
@@ -39,115 +39,10 @@ let
3939 then "${name}-all"
4040 else "${name}-${componentId.ctype}-${componentId.cname}";
4141
42− flagsAndConfig = field: xs: lib.optionalString (xs != []) ''
43− echo ${lib.concatStringsSep " " (map (x: "--${field}=${x}") xs)} >> $out/configure-flags
44− echo "${field}: ${lib.concatStringsSep " " xs}" >> $out/cabal.config
45− '';
46−
47− flatDepends =
48− let
49− makePairs = map (p: rec { key="${val}"; val=(p.components.library or p); });
50− closure = builtins.genericClosure {
51− startSet = makePairs component.depends;
52− operator = {val,...}: makePairs val.config.depends;
53− };
54− in map ({val,...}: val) closure;
55−
56− exactDep = pdbArg: p: ''
57− if id=$(target-pkg ${pdbArg} field ${p} id --simple-output); then
58− echo "--dependency=${p}=$id" >> $out/configure-flags
59− fi
60− if ver=$(target-pkg ${pdbArg} field ${p} version --simple-output); then
61− echo "constraint: ${p} == $ver" >> $out/cabal.config
62− echo "constraint: ${p} installed" >> $out/cabal.config
63− fi
64− '';
65−
66− envDep = pdbArg: p: ''
67− if id=$(target-pkg ${pdbArg} field ${p} id --simple-output); then
68− echo "package-id $id" >> $out/ghc-environment
69− fi
70− '';
71−
72− configFiles = runCommand "${fullName}-config" { nativeBuildInputs = [ghc]; } (''
73− mkdir -p $out
74−
75− # Calls ghc-pkg for the target platform
76− target-pkg() {
77− ${ghc.targetPrefix}ghc-pkg "$@"
78− }
79−
80− target-pkg init $out/package.conf.d
81−
82− ${lib.concatStringsSep "\n" (lib.mapAttrsToList flagsAndConfig {
83− "extra-lib-dirs" = map (p: "${lib.getLib p}/lib") component.libs;
84− "extra-include-dirs" = map (p: "${lib.getDev p}/include") component.libs;
85− "extra-framework-dirs" = map (p: "${p}/Library/Frameworks") component.frameworks;
86− })}
87−
88− # Copy over the nonReinstallablePkgs from the global package db.
89− # Note: we need to use --global-package-db with ghc-pkg to prevent it
90− # from looking into the implicit global package db when registering the package.
91− ${lib.concatMapStringsSep "\n" (p: ''
92− target-pkg describe ${p} | target-pkg --force --global-package-db $out/package.conf.d register - || true
93− '') nonReinstallablePkgs}
94−
95− ${lib.concatMapStringsSep "\n" (p: ''
96− target-pkg --package-db ${p}/package.conf.d dump | target-pkg --force --package-db $out/package.conf.d register -
97− '') flatDepends}
98−
99− # Note: we pass `clear` first to ensure that we never consult the implicit global package db.
100− ${flagsAndConfig "package-db" ["clear" "$out/package.conf.d"]}
101−
102− echo ${lib.concatStringsSep " " (lib.mapAttrsToList (fname: val: "--flags=${lib.optionalString (!val) "-" + fname}") flags)} >> $out/configure-flags
103−
104− # Provide a GHC environment file
105− cat > $out/ghc-environment <<EOF
106− package-db $out/package.conf.d
107− EOF
108− ${lib.concatMapStringsSep "\n" (p: envDep "--package-db ${p.components.library or p}/package.conf.d" p.identifier.name) component.depends}
109− ${lib.concatMapStringsSep "\n" (envDep "") (lib.remove "ghc" nonReinstallablePkgs)}
110−
111− '' + lib.optionalString component.doExactConfig ''
112− echo "--exact-configuration" >> $out/configure-flags
113− echo "allow-newer: ${package.identifier.name}:*" >> $out/cabal.config
114− echo "allow-older: ${package.identifier.name}:*" >> $out/cabal.config
115−
116− ${lib.concatMapStringsSep "\n" (p: exactDep "--package-db ${p.components.library}/package.conf.d" p.identifier.name) component.depends}
117− ${lib.concatMapStringsSep "\n" (exactDep "") nonReinstallablePkgs}
118−
119− ''
120− # This code originates in the `generic-builder.nix` from nixpkgs. However GHC has been fixed
121− # to drop unused libraries referneced from libraries; and this patch is usually included in the
122− # nixpkgs's GHC builds. This doesn't sadly make this stupid hack unnecessary. It resurfes in
123− # the form of Cabal trying to be smart. Cabal when linking a library figures out that you likely
124− # need those `rpath` entries, and passes `-optl-Wl,-rpath,...` for each dynamic library path to
125− # GHC, thus subverting the linker and forcing it to insert all those RPATHs weather or not they
126− # are needed. We therfore reuse the linker hack here to move all al dynamic lirbaries into a
127− # common folder (as links) and thus prevent Cabal from going nuts.
128− #
129− # TODO: Fix Cabal.
130− # TODO: this is only needed if we do dynamic libraries.
131− + lib.optionalString stdenv.isDarwin ''
132− # Work around a limit in the macOS Sierra linker on the number of paths
133− # referenced by any one dynamic library:
134− #
135− # Create a local directory with symlinks of the *.dylib (macOS shared
136− # libraries) from all the dependencies.
137− local dynamicLinksDir="$out/lib/links"
138− mkdir -p $dynamicLinksDir
139− for d in $(grep dynamic-library-dirs "$out/package.conf.d/"*|awk '{print $2}'|sort -u); do
140− ln -s "$d/"*.dylib $dynamicLinksDir
141− done
142− # Edit the local package DB to reference the links directory.
143− for f in "$out/package.conf.d/"*.conf; do
144− sed -i "s,dynamic-library-dirs: .*,dynamic-library-dirs: $dynamicLinksDir," $f
145− done
146− '' + ''
147− target-pkg --package-db $out/package.conf.d recache
148− '' + ''
149− target-pkg --package-db $out/package.conf.d check
150− '');
42+ configFiles = makeConfigFiles {
43+ inherit (package) identifier;
44+ inherit component fullName flags;
45+ };
15146
15247 finalConfigureFlags = lib.concatStringsSep " " (
15348 [ "--prefix=$out"