on e94de40 help sign in
haskell.nix/builder/haddock-builder.nix
156 lines 4.9 kB history raw
1{ stdenv, buildPackages, lib, haskellLib, ghc, ghcForComponent, nonReinstallablePkgs, runCommand, writeText, writeScript, makeConfigFiles }:
3{ componentId
4, component
5, package
6, flags
7, revision
8, commonAttrs
9, preHaddock
10, postHaddock
11, pkgconfig
12, commonConfigureFlags
14, doHaddock
15, doHoogle
16, hyperlinkSource
17, quickjump
18, setupHaddockFlags
19, setupGhcOptions
21, needsProfiling
22, componentDrv
23, configFiles # component config files
24}:
26let
27 doHaddock' = doHaddock
28 && (haskellLib.isLibrary componentId)
29 && !haskellLib.isCrossHost;
31 # the target dir for haddock documentation
32 docdir = docoutput: docoutput + "/share/doc/" + componentId.cname;
34 packageCfgDir = configFiles.packageCfgDir;
36 fullName = "${componentDrv.name}-haddock";
38 # These config files are like the one used in the build derivation,
39 # but `chooseDrv` will be used to map all the references to libraries
40 # to their haddock derivation.
41 docsConfigFiles = makeConfigFiles {
42 inherit (package) identifier;
43 inherit component fullName flags needsProfiling;
44 chooseDrv = p: p.haddock;
45 };
47 finalConfigureFlags = lib.concatStringsSep " " (
48 [ "--prefix=${componentDrv}"
49 "${haskellLib.componentTarget componentId}"
50 "$(cat ${docsConfigFiles}/configure-flags)"
51 ]
52 ++ commonConfigureFlags
53 ++ lib.optional doHaddock' " --docdir=${docdir "$doc"}");
55 shellWrappers = ghcForComponent {
56 componentName = fullName;
57 configFiles = docsConfigFiles;
58 };
60 drv = stdenv.mkDerivation (commonAttrs // {
61 name = fullName;
63 passthru = {
64 configFiles = docsConfigFiles;
66 # The directory containing the haddock documentation.
67 haddockDir = if doHaddock' then "${docdir drv.doc}/html" else null;
68 };
70 # `out` contains the `package.conf.d` files used for building the
71 # haddock files.
72 # `doc` contains just the haddock output files.
73 outputs = ["out"]
74 ++ lib.optional doHaddock' "doc";
76 propagatedBuildInputs = builtins.concatLists pkgconfig;
78 buildInputs = component.libs
79 ++ map (d: d.components.library.haddock or d) component.depends;
81 nativeBuildInputs =
82 [ shellWrappers buildPackages.removeReferencesTo ]
83 ++ componentDrv.executableToolDepends;
85 configurePhase = ''
86 runHook preConfigure
87 echo Configure flags:
88 printf "%q " ${finalConfigureFlags}
89 echo
90 $SETUP_HS configure ${finalConfigureFlags}
91 runHook postConfigure
92 '';
94 buildPhase = ''
95 mkdir -p $out
96 '' + lib.optionalString doHaddock' ''
97 runHook preHaddock
99 docdir="${docdir "$doc"}"
100 # This mkdir needed for packages like base-noprelude and bytestring-builder
101 # (which is also empty when `bytestring >= 0.10.4`)
102 mkdir -p "$docdir"
104 # If we don't have any source files, no need to run haddock
105 [[ -n $(find . -name "*.hs" -o -name "*.lhs") ]] && {
106 $SETUP_HS haddock \
107 "--html" \
108 ${lib.optionalString doHoogle "--hoogle"} \
109 ${lib.optionalString hyperlinkSource "--hyperlink-source"} \
110 ${lib.optionalString quickjump "--quickjump"} \
111 ${lib.concatStringsSep " " (setupHaddockFlags ++ setupGhcOptions)}
112 }
113 runHook postHaddock
114 '';
116 installPhase =
117 let
118 target-pkg-and-db = "${ghc.targetPrefix}ghc-pkg -v0 --package-db $out/package.conf.d";
119 in ''
120 html="dist/doc/html/${package.identifier.name}"
122 if [ -d "$html" ]; then
123 # Ensure that libraries are not pulled into the docs closure.
124 # As an example, the prettified source code of a
125 # Paths_package module will contain store paths of the library package.
126 for x in "$html/src/"*.html; do
127 remove-references-to -t $out $x
128 remove-references-to -t ${componentDrv} $x
129 done
131 docdir="${docdir "$doc"}"
132 mkdir -p "$docdir"
134 cp -R "$html" "$docdir"/html
135 fi
137 ${ghc.targetPrefix}ghc-pkg -v0 init $out/package.conf.d
139 for i in "${componentDrv}/package.conf.d"/*.conf; do
140 # Copy the .conf files from the build derivation, but replace the `haddock-intefaces:`
141 # field with correct location for haddocks (now it is known). This will insure that
142 # the other haddock derivations build to reference this one will have the correct
143 # working hyper links.
144 pkg=$(basename "$i")
145 sed -e "s|haddock-interfaces:.*|haddock-interfaces: $docdir/html/${componentId.cname}.haddock|" -e "s|haddock-html:.*|haddock-html: $docdir/html/|" "$i" > "$pkg"
146 ${ghc.targetPrefix}ghc-pkg -v0 --package-db ${docsConfigFiles}/${configFiles.packageCfgDir} -f $out/package.conf.d register "$pkg"
147 done
149 ln -s ${componentDrv}/exactDep $out/exactDep
150 ln -s ${componentDrv}/envDep $out/envDep
151 '';
152 }
153 // haskellLib.optionalHooks {
154 inherit preHaddock postHaddock;
155 });
156in drv