btrbk: add mainProgram (#356350)
[NixPkgs.git] / pkgs / development / compilers / ghc / 8.10.7-binary.nix
blob20ad72462401d4df16369d7ac6f44c6e01090437
1 { lib, stdenv
2 , fetchurl, perl, gcc
3 , ncurses5
4 , ncurses6, gmp, libiconv, numactl
5 , llvmPackages
6 , coreutils
7 , rcodesign
8 , targetPackages
10   # minimal = true; will remove files that aren't strictly necessary for
11   # regular builds and GHC bootstrapping.
12   # This is "useful" for staying within hydra's output limits for at least the
13   # aarch64-linux architecture.
14 , minimal ? false
17 # Prebuilt only does native
18 assert stdenv.targetPlatform == stdenv.hostPlatform;
20 let
21   downloadsUrl = "https://downloads.haskell.org/ghc";
23   # Copy sha256 from https://downloads.haskell.org/~ghc/8.10.7/SHA256SUMS
24   version = "8.10.7";
26   # Information about available bindists that we use in the build.
27   #
28   # # Bindist library checking
29   #
30   # The field `archSpecificLibraries` also provides a way for us get notified
31   # early when the upstream bindist changes its dependencies (e.g. because a
32   # newer Debian version is used that uses a new `ncurses` version).
33   #
34   # Usage:
35   #
36   # * You can find the `fileToCheckFor` of libraries by running `readelf -d`
37   #   on the compiler binary (`exePathForLibraryCheck`).
38   # * To skip library checking for an architecture,
39   #   set `exePathForLibraryCheck = null`.
40   # * To skip file checking for a specific arch specfic library,
41   #   set `fileToCheckFor = null`.
42   ghcBinDists = {
43     # Binary distributions for the default libc (e.g. glibc, or libSystem on Darwin)
44     # nixpkgs uses for the respective system.
45     defaultLibc = {
46       i686-linux = {
47         variantSuffix = "";
48         src = {
49           url = "${downloadsUrl}/${version}/ghc-${version}-i386-deb9-linux.tar.xz";
50           sha256 = "fbfc1ef194f4e7a4c0da8c11cc69b17458a4b928b609b3622c97acc4acd5c5ab";
51         };
52         exePathForLibraryCheck = "ghc/stage2/build/tmp/ghc-stage2";
53         archSpecificLibraries = [
54           { nixPackage = gmp; fileToCheckFor = null; }
55           # The i686-linux bindist provided by GHC HQ is currently built on Debian 9,
56           # which link it against `libtinfo.so.5` (ncurses 5).
57           # Other bindists are linked `libtinfo.so.6` (ncurses 6).
58           { nixPackage = ncurses5; fileToCheckFor = "libtinfo.so.5"; }
59         ];
60       };
61       x86_64-linux = {
62         variantSuffix = "";
63         src = {
64           url = "${downloadsUrl}/${version}/ghc-${version}-x86_64-deb10-linux.tar.xz";
65           sha256 = "a13719bca87a0d3ac0c7d4157a4e60887009a7f1a8dbe95c4759ec413e086d30";
66         };
67         exePathForLibraryCheck = "ghc/stage2/build/tmp/ghc-stage2";
68         archSpecificLibraries = [
69           { nixPackage = gmp; fileToCheckFor = null; }
70           { nixPackage = ncurses6; fileToCheckFor = "libtinfo.so.6"; }
71         ];
72       };
73       armv7l-linux = {
74         variantSuffix = "";
75         src = {
76           url = "${downloadsUrl}/${version}/ghc-${version}-armv7-deb10-linux.tar.xz";
77           sha256 = "3949c31bdf7d3b4afb765ea8246bca4ca9707c5d988d9961a244f0da100956a2";
78         };
79         exePathForLibraryCheck = "ghc/stage2/build/tmp/ghc-stage2";
80         archSpecificLibraries = [
81           { nixPackage = gmp; fileToCheckFor = null; }
82           { nixPackage = ncurses6; fileToCheckFor = "libtinfo.so.6"; }
83         ];
84       };
85       aarch64-linux = {
86         variantSuffix = "";
87         src = {
88           url = "${downloadsUrl}/${version}/ghc-${version}-aarch64-deb10-linux.tar.xz";
89           sha256 = "fad2417f9b295233bf8ade79c0e6140896359e87be46cb61cd1d35863d9d0e55";
90         };
91         exePathForLibraryCheck = "ghc/stage2/build/tmp/ghc-stage2";
92         archSpecificLibraries = [
93           { nixPackage = gmp; fileToCheckFor = null; }
94           { nixPackage = ncurses6; fileToCheckFor = "libtinfo.so.6"; }
95           { nixPackage = numactl; fileToCheckFor = null; }
96         ];
97       };
98       x86_64-darwin = {
99         variantSuffix = "";
100         src = {
101           url = "${downloadsUrl}/${version}/ghc-${version}-x86_64-apple-darwin.tar.xz";
102           sha256 = "287db0f9c338c9f53123bfa8731b0996803ee50f6ee847fe388092e5e5132047";
103         };
104         exePathForLibraryCheck = null; # we don't have a library check for darwin yet
105         archSpecificLibraries = [
106           { nixPackage = gmp; fileToCheckFor = null; }
107           { nixPackage = ncurses6; fileToCheckFor = null; }
108           { nixPackage = libiconv; fileToCheckFor = null; }
109         ];
110       };
111       aarch64-darwin = {
112         variantSuffix = "";
113         src = {
114           url = "${downloadsUrl}/${version}/ghc-${version}-aarch64-apple-darwin.tar.xz";
115           sha256 = "dc469fc3c35fd2a33a5a575ffce87f13de7b98c2d349a41002e200a56d9bba1c";
116         };
117         exePathForLibraryCheck = null; # we don't have a library check for darwin yet
118         archSpecificLibraries = [
119           { nixPackage = gmp; fileToCheckFor = null; }
120           { nixPackage = ncurses6; fileToCheckFor = null; }
121           { nixPackage = libiconv; fileToCheckFor = null; }
122         ];
123       };
124     };
125     # Binary distributions for the musl libc for the respective system.
126     musl = {
127       x86_64-linux = {
128         variantSuffix = "-musl-integer-simple";
129         src = {
130           url = "${downloadsUrl}/${version}/ghc-${version}-x86_64-alpine3.10-linux-integer-simple.tar.xz";
131           sha256 = "16903df850ef73d5246f2ff169cbf57ecab76c2ac5acfa9928934282cfad575c";
132         };
133         exePathForLibraryCheck = "bin/ghc";
134         archSpecificLibraries = [
135           # No `gmp` here, since this is an `integer-simple` bindist.
137           # In contrast to glibc builds, the musl-bindist uses `libncursesw.so.*`
138           # instead of `libtinfo.so.*.`
139           { nixPackage = ncurses6; fileToCheckFor = "libncursesw.so.6"; }
140         ];
141         isHadrian = true;
142       };
143     };
144   };
146   distSetName = if stdenv.hostPlatform.isMusl then "musl" else "defaultLibc";
148   binDistUsed = ghcBinDists.${distSetName}.${stdenv.hostPlatform.system}
149     or (throw "cannot bootstrap GHC on this platform ('${stdenv.hostPlatform.system}' with libc '${distSetName}')");
151   useLLVM = !stdenv.targetPlatform.isx86;
153   libPath =
154     lib.makeLibraryPath (
155       # Add arch-specific libraries.
156       map ({ nixPackage, ... }: nixPackage) binDistUsed.archSpecificLibraries
157     );
159   libEnvVar = lib.optionalString stdenv.hostPlatform.isDarwin "DY"
160     + "LD_LIBRARY_PATH";
162   runtimeDeps = [
163     targetPackages.stdenv.cc
164     targetPackages.stdenv.cc.bintools
165     coreutils # for cat
166   ]
167   ++ lib.optionals useLLVM [
168     (lib.getBin llvmPackages.llvm)
169   ]
170   # On darwin, we need unwrapped bintools as well (for otool)
171   ++ lib.optionals (stdenv.targetPlatform.linker == "cctools") [
172     targetPackages.stdenv.cc.bintools.bintools
173   ];
177 stdenv.mkDerivation rec {
178   inherit version;
179   pname = "ghc-binary${binDistUsed.variantSuffix}";
181   src = fetchurl binDistUsed.src;
183   # Note that for GHC 8.10 versions >= 8.10.6, the GHC HQ musl bindist
184   # uses `integer-simple` and has no `gmp` dependency:
185   # https://gitlab.haskell.org/ghc/ghc/-/commit/8306501020cd66f683ad9c215fa8e16c2d62357d
186   # Related nixpkgs issues:
187   # * https://github.com/NixOS/nixpkgs/pull/130441#issuecomment-922452843
188   # TODO: When this file is copied to `ghc-9.*-binary.nix`, determine whether
189   #       the GHC 9 branch also switched from `gmp` to `integer-simple` via the
190   #       currently-open issue:
191   #           https://gitlab.haskell.org/ghc/ghc/-/issues/20059
192   #       and update this comment accordingly.
194   nativeBuildInputs = [ perl ]
195     # Upstream binaries may not be linker-signed, which invalidates their signatures
196     # because `install_name_tool` will only replace a signature if it is both
197     # an ad hoc signature and the signature is flagged as linker-signed.
198     #
199     # rcodesign is used to replace the signature instead of sigtool because it
200     # supports setting the linker-signed flag, which will ensure future processing
201     # of the binaries does not invalidate their signatures.
202     ++ lib.optionals (stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isAarch64) [ rcodesign ];
204   # Set LD_LIBRARY_PATH or equivalent so that the programs running as part
205   # of the bindist installer can find the libraries they expect.
206   # Cannot patchelf beforehand due to relative RPATHs that anticipate
207   # the final install location.
208   ${libEnvVar} = libPath;
210   postUnpack =
211     # Verify our assumptions of which `libtinfo.so` (ncurses) version is used,
212     # so that we know when ghc bindists upgrade that and we need to update the
213     # version used in `libPath`.
214     lib.optionalString
215       (binDistUsed.exePathForLibraryCheck != null)
216       # Note the `*` glob because some GHCs have a suffix when unpacked, e.g.
217       # the musl bindist has dir `ghc-VERSION-x86_64-unknown-linux/`.
218       # As a result, don't shell-quote this glob when splicing the string.
219       (let buildExeGlob = ''ghc-${version}*/"${binDistUsed.exePathForLibraryCheck}"''; in
220         lib.concatStringsSep "\n" [
221           (''
222             shopt -u nullglob
223             echo "Checking that ghc binary exists in bindist at ${buildExeGlob}"
224             if ! test -e ${buildExeGlob}; then
225               echo >&2 "GHC binary ${binDistUsed.exePathForLibraryCheck} could not be found in the bindist build directory (at ${buildExeGlob}) for arch ${stdenv.hostPlatform.system}, please check that ghcBinDists correctly reflect the bindist dependencies!"; exit 1;
226             fi
227           '')
228           (lib.concatMapStringsSep
229             "\n"
230             ({ fileToCheckFor, nixPackage }:
231               lib.optionalString (fileToCheckFor != null) ''
232                 echo "Checking bindist for ${fileToCheckFor} to ensure that is still used"
233                 if ! readelf -d ${buildExeGlob} | grep "${fileToCheckFor}"; then
234                   echo >&2 "File ${fileToCheckFor} could not be found in ${binDistUsed.exePathForLibraryCheck} for arch ${stdenv.hostPlatform.system}, please check that ghcBinDists correctly reflect the bindist dependencies!"; exit 1;
235                 fi
237                 echo "Checking that the nix package ${nixPackage} contains ${fileToCheckFor}"
238                 if ! test -e "${lib.getLib nixPackage}/lib/${fileToCheckFor}"; then
239                   echo >&2 "Nix package ${nixPackage} did not contain ${fileToCheckFor} for arch ${stdenv.hostPlatform.system}, please check that ghcBinDists correctly reflect the bindist dependencies!"; exit 1;
240                 fi
241               ''
242             )
243             binDistUsed.archSpecificLibraries
244           )
245         ])
246     # GHC has dtrace probes, which causes ld to try to open /usr/lib/libdtrace.dylib
247     # during linking
248     + lib.optionalString stdenv.hostPlatform.isDarwin (''
249       export NIX_LDFLAGS+=" -no_dtrace_dof"
250       # not enough room in the object files for the full path to libiconv :(
251       for exe in $(find . -type f -executable); do
252         isScript $exe && continue
253         ln -fs ${libiconv}/lib/libiconv.dylib $(dirname $exe)/libiconv.dylib
254         install_name_tool -change /usr/lib/libiconv.2.dylib @executable_path/libiconv.dylib -change /usr/local/lib/gcc/6/libgcc_s.1.dylib ${gcc.cc.lib}/lib/libgcc_s.1.dylib $exe
255     '' + lib.optionalString stdenv.hostPlatform.isAarch64 ''
256         # Resign the binary and set the linker-signed flag. Ignore failures when the file is an object file.
257         # Object files don’t have signatures, so ignoring the failures is harmless.
258         rcodesign sign --code-signature-flags linker-signed $exe || true
259     '' + ''
260       done
261     '') +
263     # Some scripts used during the build need to have their shebangs patched
264     ''
265       patchShebangs ghc-${version}/utils/
266       patchShebangs ghc-${version}/configure
267       test -d ghc-${version}/inplace/bin && \
268         patchShebangs ghc-${version}/inplace/bin
269     '' +
270     # We have to patch the GMP paths for the integer-gmp package.
271     # Note that musl bindists do not contain them,
272     # see: https://gitlab.haskell.org/ghc/ghc/-/issues/20073#note_363231
273     # However, musl bindists >= 8.10.6 use `integer-simple`, not `gmp`.
274     ''
275       find . -name integer-gmp.buildinfo \
276           -exec sed -i "s@extra-lib-dirs: @extra-lib-dirs: ${gmp.out}/lib@" {} \;
277     '' + lib.optionalString stdenv.hostPlatform.isDarwin ''
278       find . -name base.buildinfo \
279           -exec sed -i "s@extra-lib-dirs: @extra-lib-dirs: ${libiconv}/lib@" {} \;
280     '' +
281     # aarch64 does HAVE_NUMA so -lnuma requires it in library-dirs in rts/package.conf.in
282     # FFI_LIB_DIR is a good indication of places it must be needed.
283     lib.optionalString (stdenv.hostPlatform.isLinux && stdenv.hostPlatform.isAarch64) ''
284       find . -name package.conf.in \
285           -exec sed -i "s@FFI_LIB_DIR@FFI_LIB_DIR ${numactl.out}/lib@g" {} \;
286     '' +
287     # Rename needed libraries and binaries, fix interpreter
288     lib.optionalString stdenv.hostPlatform.isLinux ''
289       find . -type f -executable -exec patchelf \
290           --interpreter ${stdenv.cc.bintools.dynamicLinker} {} \;
291     '' +
292     # The hadrian install Makefile uses 'xxx' as a temporary placeholder in path
293     # substitution. Which can break the build if the store path / prefix happens
294     # to contain this string. This will be fixed with 9.4 bindists.
295     # https://gitlab.haskell.org/ghc/ghc/-/issues/21402
296     ''
297       # Detect hadrian Makefile by checking for the target that has the problem
298       if grep '^update_package_db' ghc-${version}*/Makefile > /dev/null; then
299         echo Hadrian bindist, applying workaround for xxx path substitution.
300         # based on https://gitlab.haskell.org/ghc/ghc/-/commit/dd5fecb0e2990b192d92f4dfd7519ecb33164fad.patch
301         substituteInPlace ghc-${version}*/Makefile --replace 'xxx' '\0xxx\0'
302       else
303         echo Not a hadrian bindist, not applying xxx path workaround.
304       fi
305     '';
307   # fix for `configure: error: Your linker is affected by binutils #16177`
308   preConfigure = lib.optionalString
309     stdenv.targetPlatform.isAarch32
310     "LD=ld.gold";
312   configurePlatforms = [ ];
313   configureFlags = [
314     "--with-gmp-includes=${lib.getDev gmp}/include"
315     # Note `--with-gmp-libraries` does nothing for GHC bindists:
316     # https://gitlab.haskell.org/ghc/ghc/-/merge_requests/6124
317   ] ++ lib.optional stdenv.hostPlatform.isDarwin "--with-gcc=${./gcc-clang-wrapper.sh}"
318     # From: https://github.com/NixOS/nixpkgs/pull/43369/commits
319     ++ lib.optional stdenv.hostPlatform.isMusl "--disable-ld-override";
321   # No building is necessary, but calling make without flags ironically
322   # calls install-strip ...
323   dontBuild = true;
325   # Patch scripts to include runtime dependencies in $PATH.
326   postInstall = ''
327     for i in "$out/bin/"*; do
328       test ! -h "$i" || continue
329       isScript "$i" || continue
330       sed -i -e '2i export PATH="${lib.makeBinPath runtimeDeps}:$PATH"' "$i"
331     done
332   '';
334   # Apparently necessary for the ghc Alpine (musl) bindist:
335   # When we strip, and then run the
336   #     patchelf --set-rpath "${libPath}:$(patchelf --print-rpath $p)" $p
337   # below, running ghc (e.g. during `installCheckPhase)` gives some apparently
338   # corrupted rpath or whatever makes the loader work on nonsensical strings:
339   #     running install tests
340   #     Error relocating /nix/store/...-ghc-8.10.2-binary/lib/ghc-8.10.5/bin/ghc: : symbol not found
341   #     Error relocating /nix/store/...-ghc-8.10.2-binary/lib/ghc-8.10.5/bin/ghc: ir6zf6c9f86pfx8sr30n2vjy-ghc-8.10.2-binary/lib/ghc-8.10.5/bin/../lib/x86_64-linux-ghc-8.10.5/libHSexceptions-0.10.4-ghc8.10.5.so: symbol not found
342   #     Error relocating /nix/store/...-ghc-8.10.2-binary/lib/ghc-8.10.5/bin/ghc: y/lib/ghc-8.10.5/bin/../lib/x86_64-linux-ghc-8.10.5/libHStemplate-haskell-2.16.0.0-ghc8.10.5.so: symbol not found
343   #     Error relocating /nix/store/...-ghc-8.10.2-binary/lib/ghc-8.10.5/bin/ghc: 8.10.5/libHStemplate-haskell-2.16.0.0-ghc8.10.5.so: symbol not found
344   #     Error relocating /nix/store/...-ghc-8.10.2-binary/lib/ghc-8.10.5/bin/ghc: �: symbol not found
345   #     Error relocating /nix/store/...-ghc-8.10.2-binary/lib/ghc-8.10.5/bin/ghc: �?: symbol not found
346   #     Error relocating /nix/store/...-ghc-8.10.2-binary/lib/ghc-8.10.5/bin/ghc: 64-linux-ghc-8.10.5/libHSexceptions-0.10.4-ghc8.10.5.so: symbol not found
347   # This is extremely bogus and should be investigated.
348   dontStrip = if stdenv.hostPlatform.isMusl then true else false; # `if` for explicitness
350   # On Linux, use patchelf to modify the executables so that they can
351   # find editline/gmp.
352   postFixup = lib.optionalString stdenv.hostPlatform.isLinux
353     (if stdenv.hostPlatform.isAarch64 then
354       # Keep rpath as small as possible on aarch64 for patchelf#244.  All Elfs
355       # are 2 directories deep from $out/lib, so pooling symlinks there makes
356       # a short rpath.
357       ''
358       (cd $out/lib; ln -s ${ncurses6.out}/lib/libtinfo.so.6)
359       (cd $out/lib; ln -s ${gmp.out}/lib/libgmp.so.10)
360       (cd $out/lib; ln -s ${numactl.out}/lib/libnuma.so.1)
361       for p in $(find "$out/lib" -type f -name "*\.so*"); do
362         (cd $out/lib; ln -s $p)
363       done
365       for p in $(find "$out/lib" -type f -executable); do
366         if isELF "$p"; then
367           echo "Patchelfing $p"
368           patchelf --set-rpath "\$ORIGIN:\$ORIGIN/../.." $p
369         fi
370       done
371       ''
372     else
373       ''
374       for p in $(find "$out" -type f -executable); do
375         if isELF "$p"; then
376           echo "Patchelfing $p"
377           patchelf --set-rpath "${libPath}:$(patchelf --print-rpath $p)" $p
378         fi
379       done
380     '') + lib.optionalString stdenv.hostPlatform.isDarwin ''
381     # not enough room in the object files for the full path to libiconv :(
382     for exe in $(find "$out" -type f -executable); do
383       isScript $exe && continue
384       ln -fs ${libiconv}/lib/libiconv.dylib $(dirname $exe)/libiconv.dylib
385       install_name_tool -change /usr/lib/libiconv.2.dylib @executable_path/libiconv.dylib -change /usr/local/lib/gcc/6/libgcc_s.1.dylib ${gcc.cc.lib}/lib/libgcc_s.1.dylib $exe
386     done
388     for file in $(find "$out" -name setup-config); do
389       substituteInPlace $file --replace /usr/bin/ranlib "$(type -P ranlib)"
390     done
391   '' +
392   lib.optionalString minimal ''
393     # Remove profiling files
394     find $out -type f -name '*.p_o' -delete
395     find $out -type f -name '*.p_hi' -delete
396     find $out -type f -name '*_p.a' -delete
397     # `-f` because e.g. musl bindist does not have this file.
398     rm -f $out/lib/ghc-*/bin/ghc-iserv-prof
399     # Hydra will redistribute this derivation, so we have to keep the docs for
400     # legal reasons (retaining the legal notices etc)
401     # As a last resort we could unpack the docs separately and symlink them in.
402     # They're in $out/share/{doc,man}.
403   '';
405   # In nixpkgs, musl based builds currently enable `pie` hardening by default
406   # (see `defaultHardeningFlags` in `make-derivation.nix`).
407   # But GHC cannot currently produce outputs that are ready for `-pie` linking.
408   # Thus, disable `pie` hardening, otherwise `recompile with -fPIE` errors appear.
409   # See:
410   # * https://github.com/NixOS/nixpkgs/issues/129247
411   # * https://gitlab.haskell.org/ghc/ghc/-/issues/19580
412   hardeningDisable = lib.optional stdenv.targetPlatform.isMusl "pie";
414   doInstallCheck = true;
415   installCheckPhase = ''
416     # Sanity check, can ghc create executables?
417     cd $TMP
418     mkdir test-ghc; cd test-ghc
419     cat > main.hs << EOF
420       {-# LANGUAGE TemplateHaskell #-}
421       module Main where
422       main = putStrLn \$([|"yes"|])
423     EOF
424     env -i $out/bin/ghc --make main.hs || exit 1
425     echo compilation ok
426     [ $(./main) == "yes" ]
427   '';
429   passthru = {
430     targetPrefix = "";
431     enableShared = true;
433     inherit llvmPackages;
435     # Our Cabal compiler name
436     haskellCompilerName = "ghc-${version}";
437   }
438   # We duplicate binDistUsed here since we have a sensible default even if no bindist is avaible,
439   # this makes sure that getting the `meta` attribute doesn't throw even on unsupported platforms.
440   // lib.optionalAttrs (ghcBinDists.${distSetName}.${stdenv.hostPlatform.system}.isHadrian or false) {
441     # Normal GHC derivations expose the hadrian derivation used to build them
442     # here. In the case of bindists we just make sure that the attribute exists,
443     # as it is used for checking if a GHC derivation has been built with hadrian.
444     # The isHadrian mechanism will become obsolete with GHCs that use hadrian
445     # exclusively, i.e. 9.6 (and 9.4?).
446     hadrian = null;
447   };
449   meta = rec {
450     homepage = "http://haskell.org/ghc";
451     description = "Glasgow Haskell Compiler";
452     license = lib.licenses.bsd3;
453     # HACK: since we can't encode the libc / abi in platforms, we need
454     # to make the platform list dependent on the evaluation platform
455     # in order to avoid eval errors with musl which supports less
456     # platforms than the default libcs (i. e. glibc / libSystem).
457     # This is done for the benefit of Hydra, so `packagePlatforms`
458     # won't return any platforms that would cause an evaluation
459     # failure for `pkgsMusl.haskell.compiler.ghc8107Binary`, as
460     # long as the evaluator runs on a platform that supports
461     # `pkgsMusl`.
462     platforms = builtins.attrNames ghcBinDists.${distSetName};
463     maintainers = with lib.maintainers; [
464       prusnak
465       domenkozar
466     ] ++ lib.teams.haskell.members;
467   };