@@ -0,0 +1,129 @@
1+{ stdenv, targetPackages
2+, buildPlatform, hostPlatform, targetPlatform
3+, selfPkgs, cross ? null
4+
5+# build-tools
6+, bootPkgs, alex, happy
7+, autoconf, automake, coreutils, fetchgit, perl, python3
8+
9+, libiconv ? null, ncurses
10+
11+, # If enabled, GHC will be built with the GPL-free but slower integer-simple
12+ # library instead of the faster but GPLed integer-gmp library.
13+ enableIntegerSimple ? false, gmp ? null
14+
15+, version ? "8.4.20180115"
16+}:
17+
18+assert !enableIntegerSimple -> gmp != null;
19+
20+let
21+ inherit (bootPkgs) ghc;
22+
23+ rev = "3e3a096885c0fcd0703edbeffb4e47f5cbd8f4cc";
24+
25+ # TODO(@Ericson2314) Make unconditional
26+ targetPrefix = stdenv.lib.optionalString
27+ (targetPlatform != hostPlatform)
28+ "${targetPlatform.config}-";
29+in
30+stdenv.mkDerivation (rec {
31+ inherit version rev;
32+ name = "${targetPrefix}ghc-${version}";
33+
34+ src = fetchgit {
35+ url = "git://git.haskell.org/ghc.git";
36+ inherit rev;
37+ sha256 = "06slymbsd7vsfp4hh40v7cxf7nmp0kvlni2wfq7ag5wlqh04slgs";
38+ };
39+
40+ postPatch = "patchShebangs .";
41+
42+ preConfigure = ''
43+ echo ${version} >VERSION
44+ echo ${rev} >GIT_COMMIT_ID
45+ ./boot
46+ sed -i -e 's|-isysroot /Developer/SDKs/MacOSX10.5.sdk||' configure
47+ '' + stdenv.lib.optionalString (!stdenv.isDarwin) ''
48+ export NIX_LDFLAGS="$NIX_LDFLAGS -rpath $out/lib/ghc-${version}"
49+ '' + stdenv.lib.optionalString stdenv.isDarwin ''
50+ export NIX_LDFLAGS+=" -no_dtrace_dof"
51+ '' + stdenv.lib.optionalString enableIntegerSimple ''
52+ echo "INTEGER_LIBRARY=integer-simple" > mk/build.mk
53+ '' + stdenv.lib.optionalString (targetPlatform != hostPlatform) ''
54+ sed 's|#BuildFlavour = quick-cross|BuildFlavour = perf-cross|' mk/build.mk.sample > mk/build.mk
55+ '';
56+
57+ buildInputs = [ ghc perl autoconf automake happy alex python3 ];
58+
59+ enableParallelBuilding = true;
60+
61+ configureFlags = [
62+ "CC=${stdenv.cc}/bin/cc"
63+ "--with-curses-includes=${ncurses.dev}/include" "--with-curses-libraries=${ncurses.out}/lib"
64+ "--datadir=$doc/share/doc/ghc"
65+ ] ++ stdenv.lib.optional (! enableIntegerSimple) [
66+ "--with-gmp-includes=${gmp.dev}/include" "--with-gmp-libraries=${gmp.out}/lib"
67+ ] ++ stdenv.lib.optional stdenv.isDarwin [
68+ "--with-iconv-includes=${libiconv}/include" "--with-iconv-libraries=${libiconv}/lib"
69+ ];
70+
71+ # required, because otherwise all symbols from HSffi.o are stripped, and
72+ # that in turn causes GHCi to abort
73+ stripDebugFlags = [ "-S" ] ++ stdenv.lib.optional (!targetPlatform.isDarwin) "--keep-file-symbols";
74+
75+ checkTarget = "test";
76+
77+ postInstall = ''
78+ paxmark m $out/lib/${name}/bin/${if targetPlatform != hostPlatform then "ghc" else "{ghc,haddock}"}
79+
80+ # Install the bash completion file.
81+ install -D -m 444 utils/completion/ghc.bash $out/share/bash-completion/completions/${targetPrefix}ghc
82+
83+ # Patch scripts to include "readelf" and "cat" in $PATH.
84+ for i in "$out/bin/"*; do
85+ test ! -h $i || continue
86+ egrep --quiet '^#!' <(head -n 1 $i) || continue
87+ sed -i -e '2i export PATH="$PATH:${stdenv.lib.makeBinPath [ targetPackages.stdenv.cc.bintools coreutils ]}"' $i
88+ done
89+ '';
90+
91+ outputs = [ "out" "doc" ];
92+
93+ passthru = {
94+ inherit bootPkgs targetPrefix;
95+ } // stdenv.lib.optionalAttrs (targetPlatform != buildPlatform) {
96+ crossCompiler = selfPkgs.ghc.override {
97+ cross = targetPlatform;
98+ bootPkgs = selfPkgs;
99+ };
100+ };
101+
102+ meta = {
103+ homepage = http://haskell.org/ghc;
104+ description = "The Glasgow Haskell Compiler";
105+ maintainers = with stdenv.lib.maintainers; [ marcweber andres peti ];
106+ inherit (ghc.meta) license platforms;
107+ };
108+
109+} // stdenv.lib.optionalAttrs (cross != null) {
110+ configureFlags = [
111+ "CC=${stdenv.cc}/bin/${cross.config}-cc"
112+ "LD=${stdenv.cc.bintools}/bin/${cross.config}-ld"
113+ "AR=${stdenv.cc.bintools}/bin/${cross.config}-ar"
114+ "NM=${stdenv.cc.bintools}/bin/${cross.config}-nm"
115+ "RANLIB=${stdenv.cc.bintools}/bin/${cross.config}-ranlib"
116+ "--target=${cross.config}"
117+ "--enable-bootstrap-with-devel-snapshot"
118+ ] ++
119+ # fix for iOS: https://www.reddit.com/r/haskell/comments/4ttdz1/building_an_osxi386_to_iosarm64_cross_compiler/d5qvd67/
120+ stdenv.lib.optional (cross.config or null == "aarch64-apple-darwin14") "--disable-large-address-space";
121+
122+ configurePlatforms = [];
123+
124+ passthru = {
125+ inherit bootPkgs cross;
126+ cc = "${stdenv.cc}/bin/${cross.config}-cc";
127+ ld = "${stdenv.cc}/bin/${cross.config}-ld";
128+ };
129+})