|
| 1 | +# Copied from nixpkgs and adapted to work with the package database of |
| 2 | +# a single component-builder package. |
| 3 | +{ lib, stdenv, ghc, llvmPackages, package, runCommand, lndir, makeWrapper |
| 4 | +, withLLVM ? false |
| 5 | +, postBuild ? "" |
| 6 | +, ghcLibdir ? null # only used by ghcjs, when resolving plugins |
| 7 | +}: |
| 8 | + |
| 9 | +assert ghcLibdir != null -> (ghc.isGhcjs or false); |
| 10 | + |
| 11 | +# This wrapper works only with GHC 6.12 or later. |
| 12 | +assert lib.versionOlder "6.12" ghc.version || ghc.isGhcjs || ghc.isHaLVM; |
| 13 | + |
| 14 | +# It's probably a good idea to include the library "ghc-paths" in the |
| 15 | +# compiler environment, because we have a specially patched version of |
| 16 | +# that package in Nix that honors these environment variables |
| 17 | +# |
| 18 | +# NIX_GHC |
| 19 | +# NIX_GHCPKG |
| 20 | +# NIX_GHC_DOCDIR |
| 21 | +# NIX_GHC_LIBDIR |
| 22 | +# |
| 23 | +# instead of hard-coding the paths. The wrapper sets these variables |
| 24 | +# appropriately to configure ghc-paths to point back to the wrapper |
| 25 | +# instead of to the pristine GHC package, which doesn't know any of the |
| 26 | +# additional libraries. |
| 27 | +# |
| 28 | +# A good way to import the environment set by the wrapper below into |
| 29 | +# your shell is to add the following snippet to your ~/.bashrc: |
| 30 | +# |
| 31 | +# if [ -e ~/.nix-profile/bin/ghc ]; then |
| 32 | +# eval $(grep export ~/.nix-profile/bin/ghc) |
| 33 | +# fi |
| 34 | + |
| 35 | +let |
| 36 | + isGhcjs = ghc.isGhcjs or false; |
| 37 | + isHaLVM = ghc.isHaLVM or false; |
| 38 | + ghc761OrLater = isGhcjs || isHaLVM || lib.versionOlder "7.6.1" ghc.version; |
| 39 | + packageDBFlag = if ghc761OrLater then "--global-package-db" else "--global-conf"; |
| 40 | + ghcCommand' = if isGhcjs then "ghcjs" else "ghc"; |
| 41 | + ghcCommand = "${ghc.targetPrefix}${ghcCommand'}"; |
| 42 | + ghcCommandCaps= lib.toUpper ghcCommand'; |
| 43 | + libDir = if isHaLVM then "$out/lib/HaLVM-${ghc.version}" |
| 44 | + else "$out/lib/${ghcCommand}-${ghc.version}"; |
| 45 | + docDir = "$out/share/doc/ghc/html"; |
| 46 | + packageCfgDir = "${libDir}/package.conf.d"; |
| 47 | + # CLang is needed on Darwin for -fllvm to work: |
| 48 | + # https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/codegens.html#llvm-code-generator-fllvm |
| 49 | + llvm = lib.makeBinPath |
| 50 | + ([ llvmPackages.llvm ] |
| 51 | + ++ lib.optional stdenv.targetPlatform.isDarwin llvmPackages.clang); |
| 52 | + |
| 53 | +in runCommand "${ghc.name}-with-${package.identifier.name}" { |
| 54 | + preferLocalBuild = true; |
| 55 | + passthru = { |
| 56 | + inherit (ghc) version meta; |
| 57 | + baseGhc = ghc; |
| 58 | + inherit package; |
| 59 | + }; |
| 60 | +} ( |
| 61 | + '' |
| 62 | + . ${makeWrapper}/nix-support/setup-hook |
| 63 | +
|
| 64 | + # Start with a ghc... |
| 65 | + mkdir -p $out |
| 66 | + ${lndir}/bin/lndir -silent ${ghc} $out |
| 67 | + # ...and replace package database with the one from target package config. |
| 68 | + rm -rf ${packageCfgDir} |
| 69 | + cp -R --no-preserve=mode ${package.configFiles}/package.conf.d ${packageCfgDir} |
| 70 | +
|
| 71 | + # wrap compiler executables with correct env variables |
| 72 | +
|
| 73 | + for prg in ${ghcCommand} ${ghcCommand}i ${ghcCommand}-${ghc.version} ${ghcCommand}i-${ghc.version}; do |
| 74 | + if [[ -x "${ghc}/bin/$prg" ]]; then |
| 75 | + rm -f $out/bin/$prg |
| 76 | + makeWrapper ${ghc}/bin/$prg $out/bin/$prg \ |
| 77 | + --add-flags '"-B$NIX_${ghcCommandCaps}_LIBDIR"' \ |
| 78 | + --set "NIX_${ghcCommandCaps}" "$out/bin/${ghcCommand}" \ |
| 79 | + --set "NIX_${ghcCommandCaps}PKG" "$out/bin/${ghcCommand}-pkg" \ |
| 80 | + --set "NIX_${ghcCommandCaps}_DOCDIR" "${docDir}" \ |
| 81 | + --set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}" \ |
| 82 | + ${lib.optionalString (ghc.isGhcjs or false) |
| 83 | + ''--set NODE_PATH "${ghc.socket-io}/lib/node_modules"'' |
| 84 | + } \ |
| 85 | + ${lib.optionalString withLLVM ''--prefix "PATH" ":" "${llvm}"''} |
| 86 | + fi |
| 87 | + done |
| 88 | +
|
| 89 | + for prg in runghc runhaskell; do |
| 90 | + if [[ -x "${ghc}/bin/$prg" ]]; then |
| 91 | + rm -f $out/bin/$prg |
| 92 | + makeWrapper ${ghc}/bin/$prg $out/bin/$prg \ |
| 93 | + --add-flags "-f $out/bin/${ghcCommand}" \ |
| 94 | + --set "NIX_${ghcCommandCaps}" "$out/bin/${ghcCommand}" \ |
| 95 | + --set "NIX_${ghcCommandCaps}PKG" "$out/bin/${ghcCommand}-pkg" \ |
| 96 | + --set "NIX_${ghcCommandCaps}_DOCDIR" "${docDir}" \ |
| 97 | + --set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}" |
| 98 | + fi |
| 99 | + done |
| 100 | +
|
| 101 | + for prg in ${ghcCommand}-pkg ${ghcCommand}-pkg-${ghc.version}; do |
| 102 | + if [[ -x "${ghc}/bin/$prg" ]]; then |
| 103 | + rm -f $out/bin/$prg |
| 104 | + makeWrapper ${ghc}/bin/$prg $out/bin/$prg --add-flags "${packageDBFlag}=${packageCfgDir}" |
| 105 | + fi |
| 106 | + done |
| 107 | +
|
| 108 | + # haddock was referring to the base ghc, https://github.com/NixOS/nixpkgs/issues/36976 |
| 109 | + if [[ -x "${ghc}/bin/haddock" ]]; then |
| 110 | + rm -f $out/bin/haddock |
| 111 | + makeWrapper ${ghc}/bin/haddock $out/bin/haddock \ |
| 112 | + --add-flags '"-B$NIX_${ghcCommandCaps}_LIBDIR"' \ |
| 113 | + --set "NIX_${ghcCommandCaps}_LIBDIR" "${libDir}" |
| 114 | + fi |
| 115 | +
|
| 116 | + '' + (lib.optionalString (stdenv.targetPlatform.isDarwin && !isGhcjs && !stdenv.targetPlatform.isiOS) '' |
| 117 | + # Work around a linker limit in macOS Sierra (see generic-builder.nix): |
| 118 | + local packageConfDir="$out/lib/${ghc.name}/package.conf.d"; |
| 119 | + local dynamicLinksDir="$out/lib/links" |
| 120 | + mkdir -p $dynamicLinksDir |
| 121 | + # Clean up the old links that may have been (transitively) included by |
| 122 | + # symlinkJoin: |
| 123 | + rm -f $dynamicLinksDir/* |
| 124 | + for d in $(grep dynamic-library-dirs $packageConfDir/*|awk '{print $2}'|sort -u); do |
| 125 | + ln -s $d/*.dylib $dynamicLinksDir |
| 126 | + done |
| 127 | + for f in $packageConfDir/*.conf; do |
| 128 | + # Initially, $f is a symlink to a read-only file in one of the inputs |
| 129 | + # (as a result of this symlinkJoin derivation). |
| 130 | + # Replace it with a copy whose dynamic-library-dirs points to |
| 131 | + # $dynamicLinksDir |
| 132 | + cp $f $f-tmp |
| 133 | + rm $f |
| 134 | + sed "s,dynamic-library-dirs: .*,dynamic-library-dirs: $dynamicLinksDir," $f-tmp > $f |
| 135 | + rm $f-tmp |
| 136 | + done |
| 137 | + '') + '' |
| 138 | + $out/bin/${ghcCommand}-pkg recache |
| 139 | + ${# ghcjs will read the ghc_libdir file when resolving plugins. |
| 140 | + lib.optionalString (isGhcjs && ghcLibdir != null) '' |
| 141 | + mkdir -p "${libDir}" |
| 142 | + rm -f "${libDir}/ghc_libdir" |
| 143 | + printf '%s' '${ghcLibdir}' > "${libDir}/ghc_libdir" |
| 144 | + ''} |
| 145 | +
|
| 146 | + # fixme: the check doesn't work |
| 147 | + $out/bin/${ghcCommand}-pkg check || true |
| 148 | + '' + postBuild |
| 149 | +) |
0 commit comments