@@ -0,0 +1,283 @@
1+# generic builder for Cabal packages
2+
3+{ stdenv, fetchurl, lib, pkgconfig, ghcjs, ghc, Cabal, jailbreakCabal, glibcLocales
4+, gnugrep, coreutils, hscolour # hscolour is unused
5+, enableLibraryProfiling ? false
6+, enableSharedLibraries ? false
7+, enableSharedExecutables ? false
8+, enableStaticLibraries ? true
9+, enableCheckPhase ? stdenv.lib.versionOlder "7.4" ghc.version
10+, enableHyperlinkSource ? false
11+, extension ? (self : super : {})
12+}:
13+
14+let
15+ enableFeature = stdenv.lib.enableFeature;
16+ versionOlder = stdenv.lib.versionOlder;
17+ optional = stdenv.lib.optional;
18+ optionals = stdenv.lib.optionals;
19+ optionalString = stdenv.lib.optionalString;
20+ filter = stdenv.lib.filter;
21+
22+ defaultSetupHs = builtins.toFile "Setup.hs" ''
23+ import Distribution.Simple
24+ main = defaultMain
25+ '';
26+in
27+
28+{
29+ mkDerivation =
30+ args : # arguments for the individual package, can modify the defaults
31+ let # These attributes are removed in the end. This is in order not to spoil the build
32+ # environment overly, but also to keep hash-backwards-compatible with the old cabal.nix.
33+ internalAttrs = [
34+ "internalAttrs" "buildDepends" "buildTools" "extraLibraries" "pkgconfigDepends"
35+ "isLibrary" "isExecutable" "testDepends"
36+ ];
37+
38+ # Stuff happening after the user preferences have been processed. We remove
39+ # internal attributes and strip null elements from the dependency lists, all
40+ # in the interest of keeping hashes stable.
41+ postprocess =
42+ x : (removeAttrs x internalAttrs) // {
43+ buildInputs = filter (y : ! (y == null)) x.buildInputs;
44+ propagatedBuildInputs = filter (y : ! (y == null)) x.propagatedBuildInputs;
45+ propagatedUserEnvPkgs = filter (y : ! (y == null)) x.propagatedUserEnvPkgs;
46+ doCheck = enableCheckPhase && x.doCheck;
47+ hyperlinkSource = enableHyperlinkSource && x.hyperlinkSource;
48+ };
49+
50+ defaults =
51+ self : { # self is the final version of the attribute set
52+
53+ # pname should be defined by the client to be the package basename
54+ # version should be defined by the client to be the package version
55+
56+ # fname is the internal full name of the package
57+ fname = "${self.pname}-${self.version}";
58+
59+ # name is the external full name of the package; usually we prefix
60+ # all packages with haskell- to avoid name clashes for libraries;
61+ # if that is not desired (for applications), name can be set to
62+ # fname.
63+ name = if self.isLibrary then
64+ if enableLibraryProfiling && self.enableSharedLibraries then
65+ "haskell-${self.pname}-ghcjs${ghc.ghc.version}-${self.version}-profiling-shared"
66+ else if enableLibraryProfiling && !self.enableSharedLibraries then
67+ "haskell-${self.pname}-ghcjs${ghc.ghc.version}-${self.version}-profiling"
68+ else if !enableLibraryProfiling && self.enableSharedLibraries then
69+ "haskell-${self.pname}-ghcjs${ghc.ghc.version}-${self.version}-shared"
70+ else
71+ "haskell-${self.pname}-ghcjs${ghc.ghc.version}-${self.version}"
72+ else
73+ "${self.pname}-${self.version}";
74+
75+ # the default download location for Cabal packages is Hackage,
76+ # you still have to specify the checksum
77+ src = fetchurl {
78+ url = "mirror://hackage/${self.pname}/${self.fname}.tar.gz";
79+ inherit (self) sha256;
80+ };
81+
82+ # default buildInputs are just ghc, if more buildInputs are required
83+ # buildInputs can be extended by the client by using extraBuildInputs,
84+ # but often propagatedBuildInputs is preferable anyway
85+ buildInputs = [ghc ghc.ghc.parent.Cabal_HEAD] ++ self.extraBuildInputs;
86+ extraBuildInputs = self.buildTools ++
87+ (optionals self.doCheck self.testDepends) ++
88+ (if self.pkgconfigDepends == [] then [] else [pkgconfig]) ++
89+ (if self.isLibrary then [] else self.buildDepends ++ self.extraLibraries ++ self.pkgconfigDepends);
90+
91+ # we make sure that propagatedBuildInputs is defined, so that we don't
92+ # have to check for its existence
93+ propagatedBuildInputs = if self.isLibrary then self.buildDepends ++ self.extraLibraries ++ self.pkgconfigDepends else [];
94+
95+ # By default, also propagate all dependencies to the user environment. This is required, otherwise packages would be broken, because
96+ # GHC also needs all dependencies to be available.
97+ propagatedUserEnvPkgs = if self.isLibrary then self.buildDepends else [];
98+
99+ # library directories that have to be added to the Cabal files
100+ extraLibDirs = [];
101+
102+ # build-depends Cabal field
103+ buildDepends = [];
104+
105+ # target(s) passed to the cabal build phase as an argument
106+ buildTarget = "";
107+
108+ # build-depends Cabal fields stated in test-suite stanzas
109+ testDepends = [];
110+
111+ # target(s) passed to the cabal test phase as an argument
112+ testTarget = "";
113+
114+ # build-tools Cabal field
115+ buildTools = [];
116+
117+ # extra-libraries Cabal field
118+ extraLibraries = [];
119+
120+ # pkgconfig-depends Cabal field
121+ pkgconfigDepends = [];
122+
123+ isLibrary = ! self.isExecutable;
124+ isExecutable = false;
125+
126+ # ignore version restrictions on the build inputs that the cabal file might specify
127+ jailbreak = false;
128+
129+ # pass the '--enable-split-objs' flag to cabal in the configure stage
130+ enableSplitObjs = false; # !stdenv.isDarwin; # http://hackage.haskell.org/trac/ghc/ticket/4013
131+
132+ # pass the '--enable-tests' flag to cabal in the configure stage
133+ # and run any regression test suites the package might have
134+ doCheck = false; #enableCheckPhase;
135+
136+ # pass the '--hyperlink-source' flag to ./Setup haddock
137+ hyperlinkSource = enableHyperlinkSource;
138+
139+ # abort the build if the configure phase detects that the package
140+ # depends on multiple versions of the same build input
141+ strictConfigurePhase = true;
142+
143+ # pass the '--enable-library-vanilla' flag to cabal in the
144+ # configure stage to enable building shared libraries
145+ inherit enableStaticLibraries;
146+
147+ # pass the '--enable-shared' flag to cabal in the configure
148+ # stage to enable building shared libraries
149+ inherit enableSharedLibraries;
150+
151+ # pass the '--enable-executable-dynamic' flag to cabal in
152+ # the configure stage to enable linking shared libraries
153+ inherit enableSharedExecutables;
154+
155+ extraConfigureFlags = [
156+ (enableFeature self.enableSplitObjs "split-objs")
157+ (enableFeature enableLibraryProfiling "library-profiling")
158+ (enableFeature true "shared")
159+ (optional (versionOlder "7" ghc.version) (enableFeature self.enableStaticLibraries "library-vanilla"))
160+ (optional (versionOlder "7.4" ghc.version) (enableFeature self.enableSharedExecutables "executable-dynamic"))
161+ (optional (versionOlder "7" ghc.version) (enableFeature self.doCheck "tests"))
162+ ];
163+
164+ # GHC needs the locale configured during the Haddock phase.
165+ LANG = "en_US.UTF-8";
166+ LOCALE_ARCHIVE = optionalString stdenv.isLinux "${glibcLocales}/lib/locale/locale-archive";
167+
168+ # compiles Setup and configures
169+ configurePhase = ''
170+ eval "$preConfigure"
171+
172+ ${optionalString self.jailbreak "${ghc.ghc.parent.jailbreakCabal}/bin/jailbreak-cabal ${self.pname}.cabal"}
173+
174+ PATH=$PATH:${ghc.ghc.ghc}/bin
175+
176+ for i in Setup.hs Setup.lhs ${defaultSetupHs}; do
177+ test -f $i && break
178+ done
179+ ghc --make -o Setup -odir $TMPDIR -hidir $TMPDIR $i
180+
181+ for p in $extraBuildInputs $propagatedBuildInputs $propagatedNativeBuildInputs; do
182+ PkgDir="$p/lib/ghcjs-${ghc.ghc.version}_ghc-${ghc.ghc.ghc.version}/package.conf.d"
183+ if [ -f "$PkgDir/package.cache" ]; then
184+ extraConfigureFlags+=" --package-db=$PkgDir"
185+ continue;
186+ fi
187+ if [ -d "$p/include" ]; then
188+ extraConfigureFlags+=" --extra-include-dirs=$p/include"
189+ fi
190+ for d in lib{,64}; do
191+ if [ -d "$p/$d" ]; then
192+ extraConfigureFlags+=" --extra-lib-dirs=$p/$d"
193+ fi
194+ done
195+ done
196+
197+ configureFlags+=" --package-db=${ghc.ghc}/${ghc.ghc.libDir}/package.conf.d"
198+
199+ ${optionalString (self.enableSharedExecutables && self.stdenv.isLinux) ''
200+ configureFlags+=" --ghc-option=-optl=-Wl,-rpath=$out/lib/${ghc.ghc.name}/${self.pname}-${self.version}";
201+ ''}
202+ ${optionalString (self.enableSharedExecutables && self.stdenv.isDarwin) ''
203+ configureFlags+=" --ghc-option=-optl=-Wl,-headerpad_max_install_names";
204+ ''}
205+
206+ echo "configure flags: $extraConfigureFlags $configureFlags"
207+ ./Setup configure --ghcjs --verbose --prefix="$out" --libdir='$prefix/lib/$compiler' \
208+ --libsubdir='$pkgid' $extraConfigureFlags $configureFlags 2>&1 \
209+ ${optionalString self.strictConfigurePhase ''
210+ | ${coreutils}/bin/tee "$NIX_BUILD_TOP/cabal-configure.log"
211+ if ${gnugrep}/bin/egrep -q '^Warning:.*depends on multiple versions' "$NIX_BUILD_TOP/cabal-configure.log"; then
212+ echo >&2 "*** abort because of serious configure-time warning from Cabal"
213+ exit 1
214+ fi
215+ ''}
216+
217+ eval "$postConfigure"
218+ '';
219+
220+ # builds via Cabal
221+ buildPhase = ''
222+ eval "$preBuild"
223+
224+ ./Setup build ${self.buildTarget}
225+
226+ export GHC_PACKAGE_PATH=$(${ghc.GHCPackages})
227+ #test -n "$noHaddock" || ./Setup haddock --html --hoogle \
228+ # ${optionalString self.hyperlinkSource "--hyperlink-source"}
229+
230+ eval "$postBuild"
231+ '';
232+
233+ checkPhase = optional self.doCheck ''
234+ eval "$preCheck"
235+
236+ ./Setup test ${self.testTarget}
237+
238+ eval "$postCheck"
239+ '';
240+
241+ # installs via Cabal; creates a registration file for nix-support
242+ # so that the package can be used in other Haskell-builds; also
243+ # adds all propagated build inputs to the user environment packages
244+ installPhase = ''
245+ eval "$preInstall"
246+
247+ ./Setup copy
248+
249+ mkdir -p $out/bin # necessary to get it added to PATH
250+
251+ local confDir=$out/lib/ghcjs-${ghc.ghc.version}_ghc-${ghc.ghc.ghc.version}/package.conf.d
252+ local installedPkgConf=$confDir/${self.fname}.installedconf
253+ local pkgConf=$confDir/${self.fname}.conf
254+ mkdir -p $confDir
255+ ./Setup register --gen-pkg-config=$pkgConf
256+ if test -f $pkgConf; then
257+ echo '[]' > $installedPkgConf
258+ GHC_PACKAGE_PATH=$installedPkgConf ghcjs-pkg --global register $pkgConf --force --package-db=$confDir || true
259+ ghcjs-pkg recache --package-db=$confDir
260+ fi
261+
262+ if test -f $out/nix-support/propagated-native-build-inputs; then
263+ ln -s $out/nix-support/propagated-native-build-inputs $out/nix-support/propagated-user-env-packages
264+ fi
265+
266+ ${optionalString (self.enableSharedExecutables && self.isExecutable && self.stdenv.isDarwin) ''
267+ for exe in $out/bin/* ; do
268+ install_name_tool -add_rpath $out/lib/${ghc.ghc.name}/${self.pname}-${self.version} $exe || true # Ignore failures, which seem to be due to hitting bash scripts rather than binaries
269+ done
270+ ''}
271+
272+ eval "$postInstall"
273+ '';
274+
275+ # We inherit stdenv and ghc so that they can be used
276+ # in Cabal derivations.
277+ inherit stdenv ghc;
278+ };
279+ in
280+ stdenv.mkDerivation (postprocess (let super = defaults self // args self;
281+ self = super // extension self super;
282+ in self));
283+}