91f62138d07ffa089e2314a2af3d9ad1c2349039
[jackhill/guix/guix.git] / guix / build / haskell-build-system.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015 Federico Beffa <beffa@fbengineering.ch>
3 ;;; Copyright © 2015 Eric Bavier <bavier@member.fsf.org>
4 ;;; Copyright © 2015 Paul van der Walt <paul@denknerd.org>
5 ;;; Copyright © 2018 Ricardo Wurmus <rekado@elephly.net>
6 ;;; Copyright © 2018 Alex Vong <alexvong1995@gmail.com>
7 ;;;
8 ;;; This file is part of GNU Guix.
9 ;;;
10 ;;; GNU Guix is free software; you can redistribute it and/or modify it
11 ;;; under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 3 of the License, or (at
13 ;;; your option) any later version.
14 ;;;
15 ;;; GNU Guix is distributed in the hope that it will be useful, but
16 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;;; GNU General Public License for more details.
19 ;;;
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
22
23 (define-module (guix build haskell-build-system)
24 #:use-module ((guix build gnu-build-system) #:prefix gnu:)
25 #:use-module (guix build utils)
26 #:use-module (srfi srfi-1)
27 #:use-module (srfi srfi-26)
28 #:use-module (ice-9 rdelim)
29 #:use-module (ice-9 regex)
30 #:use-module (ice-9 match)
31 #:use-module (ice-9 vlist)
32 #:use-module (ice-9 ftw)
33 #:export (%standard-phases
34 haskell-build))
35
36 ;; Commentary:
37 ;;
38 ;; Builder-side code of the standard Haskell package build procedure.
39 ;;
40 ;; The Haskell compiler, to find libraries, relies on a library database with
41 ;; a binary cache. For GHC the cache has to be named 'package.cache'. If every
42 ;; library would generate the cache at build time, then they would clash in
43 ;; profiles. For this reason we do not generate the cache when we generate
44 ;; libraries substitutes. Instead:
45 ;;
46 ;; - At build time we use the 'setup-compiler' phase to generate a temporary
47 ;; library database and its cache.
48 ;;
49 ;; - We generate the cache when a profile is created.
50 ;;
51 ;; Code:
52
53 ;; Directory where we create the temporary libraries database with its cache
54 ;; as required by the compiler.
55 (define %tmp-db-dir
56 (string-append (or (getenv "TMP") "/tmp")
57 "/package.conf.d"))
58
59 (define (run-setuphs command params)
60 (let ((setup-file (cond
61 ((file-exists? "Setup.hs")
62 "Setup.hs")
63 ((file-exists? "Setup.lhs")
64 "Setup.lhs")
65 (else
66 #f))))
67 (if setup-file
68 (begin
69 (format #t "running \"runhaskell Setup.hs\" with command ~s \
70 and parameters ~s~%"
71 command params)
72 (apply invoke "runhaskell" setup-file command params))
73 (error "no Setup.hs nor Setup.lhs found"))))
74
75 (define* (configure #:key outputs inputs tests? (configure-flags '())
76 #:allow-other-keys)
77 "Configure a given Haskell package."
78 (let* ((out (assoc-ref outputs "out"))
79 (doc (assoc-ref outputs "doc"))
80 (lib (assoc-ref outputs "lib"))
81 (bin (assoc-ref outputs "bin"))
82 (name-version (strip-store-file-name out))
83 (input-dirs (match inputs
84 (((_ . dir) ...)
85 dir)
86 (_ '())))
87 (ghc-path (getenv "GHC_PACKAGE_PATH"))
88 (params (append `(,(string-append "--prefix=" out))
89 `(,(string-append "--libdir=" (or lib out) "/lib"))
90 `(,(string-append "--bindir=" (or bin out) "/bin"))
91 `(,(string-append
92 "--docdir=" (or doc out)
93 "/share/doc/" name-version))
94 '("--libsubdir=$compiler/$pkg-$version")
95 `(,(string-append "--package-db=" %tmp-db-dir))
96 '("--global")
97 `(,@(map
98 (cut string-append "--extra-include-dirs=" <>)
99 (search-path-as-list '("include") input-dirs)))
100 `(,@(map
101 (cut string-append "--extra-lib-dirs=" <>)
102 (search-path-as-list '("lib") input-dirs)))
103 (if tests?
104 '("--enable-tests")
105 '())
106 configure-flags)))
107 ;; Cabal errors if GHC_PACKAGE_PATH is set during 'configure', so unset
108 ;; and restore it.
109 (unsetenv "GHC_PACKAGE_PATH")
110
111 ;; For packages where the Cabal build-type is set to "Configure",
112 ;; ./configure will be executed. In these cases, the following
113 ;; environment variable is needed to be able to find the shell executable.
114 ;; For other package types, the configure script isn't present. For more
115 ;; information, see the Build Information section of
116 ;; <https://www.haskell.org/cabal/users-guide/developing-packages.html>.
117 (when (file-exists? "configure")
118 (setenv "CONFIG_SHELL" "sh"))
119 (run-setuphs "configure" params)
120
121 (setenv "GHC_PACKAGE_PATH" ghc-path)
122 #t))
123
124 (define* (build #:rest empty)
125 "Build a given Haskell package."
126 (run-setuphs "build" '()))
127
128 (define* (install #:rest empty)
129 "Install a given Haskell package."
130 (run-setuphs "copy" '()))
131
132 (define (grep rx port)
133 "Given a regular-expression RX including a group, read from PORT until the
134 first match and return the content of the group."
135 (let ((line (read-line port)))
136 (if (eof-object? line)
137 #f
138 (let ((rx-result (regexp-exec rx line)))
139 (if rx-result
140 (match:substring rx-result 1)
141 (grep rx port))))))
142
143 (define* (setup-compiler #:key system inputs outputs #:allow-other-keys)
144 "Setup the compiler environment."
145 (let* ((haskell (assoc-ref inputs "haskell"))
146 (name-version (strip-store-file-name haskell)))
147 (cond
148 ((string-match "ghc" name-version)
149 (make-ghc-package-database system inputs outputs))
150 (else
151 (format #t
152 "Compiler ~a not supported~%" name-version)))))
153
154 ;;; TODO: Move this to (guix build utils)?
155 (define-syntax-rule (with-null-error-port exp)
156 "Evaluate EXP with the error port pointing to the bit bucket."
157 (with-error-to-port (%make-void-port "w")
158 (lambda () exp)))
159
160 (define (make-ghc-package-database system inputs outputs)
161 "Generate the GHC package database."
162 (let* ((haskell (assoc-ref inputs "haskell"))
163 (name-version (strip-store-file-name haskell))
164 (input-dirs (match inputs
165 (((_ . dir) ...)
166 dir)
167 (_ '())))
168 ;; Silence 'find-files' (see 'evaluate-search-paths')
169 (conf-dirs (with-null-error-port
170 (search-path-as-list
171 `(,(string-append "lib/" name-version))
172 input-dirs #:pattern ".*\\.conf.d$")))
173 (conf-files (append-map (cut find-files <> "\\.conf$") conf-dirs)))
174 (mkdir-p %tmp-db-dir)
175 (for-each (lambda (file)
176 (let ((dest (string-append %tmp-db-dir "/" (basename file))))
177 (unless (file-exists? dest)
178 (copy-file file dest))))
179 conf-files)
180 (invoke "ghc-pkg"
181 (string-append "--package-db=" %tmp-db-dir)
182 "recache")
183 #t))
184
185 (define* (register #:key name system inputs outputs #:allow-other-keys)
186 "Generate the compiler registration and binary package database files for a
187 given Haskell package."
188
189 (define (conf-depends conf-file)
190 ;; Return a list of pkg-ids from the "depends" field in CONF-FILE
191 (let ((port (open-input-file conf-file))
192 (field-rx (make-regexp "^(.*):")))
193 (let loop ((collecting #f)
194 (deps '()))
195 (let* ((line (read-line port))
196 (field (and=> (and (not (eof-object? line))
197 (regexp-exec field-rx line))
198 (cut match:substring <> 1))))
199 (cond
200 ((and=> field (cut string=? <> "depends"))
201 ;; The first dependency is listed on the same line as "depends:",
202 ;; so drop those characters. A line may list more than one .conf.
203 (let ((d (string-tokenize (string-drop line 8))))
204 (loop #t (append d deps))))
205 ((or (eof-object? line) (and collecting field))
206 (begin
207 (close-port port)
208 (reverse! deps)))
209 (collecting
210 (loop #t (append (string-tokenize line) deps)))
211 (else (loop #f deps)))))))
212
213 (define (install-transitive-deps conf-file src dest)
214 ;; Copy .conf files from SRC to DEST for dependencies in CONF-FILE, and
215 ;; their dependencies, etc.
216 (let loop ((seen vlist-null)
217 (lst (conf-depends conf-file)))
218 (match lst
219 (() #t) ;done
220 ((id . tail)
221 (if (not (vhash-assoc id seen))
222 (let ((dep-conf (string-append src "/" id ".conf"))
223 (dep-conf* (string-append dest "/" id ".conf")))
224 (copy-file dep-conf dep-conf*) ;XXX: maybe symlink instead?
225 (loop (vhash-cons id #t seen)
226 (append lst (conf-depends dep-conf))))
227 (loop seen tail))))))
228
229 (let* ((out (assoc-ref outputs "out"))
230 (haskell (assoc-ref inputs "haskell"))
231 (name-verion (strip-store-file-name haskell))
232 (lib (string-append out "/lib"))
233 (config-dir (string-append lib
234 "/" name-verion
235 "/" name ".conf.d"))
236 (id-rx (make-regexp "^id: *(.*)$"))
237 (config-file (string-append out "/" name ".conf"))
238 (params
239 (list (string-append "--gen-pkg-config=" config-file))))
240 (run-setuphs "register" params)
241 ;; The conf file is created only when there is a library to register.
242 (when (file-exists? config-file)
243 (mkdir-p config-dir)
244 (let* ((config-file-name+id
245 (call-with-ascii-input-file config-file (cut grep id-rx <>))))
246 (install-transitive-deps config-file %tmp-db-dir config-dir)
247 (rename-file config-file
248 (string-append config-dir "/"
249 config-file-name+id ".conf"))
250 (invoke "ghc-pkg"
251 (string-append "--package-db=" config-dir)
252 "recache")))
253 #t))
254
255 (define* (check #:key tests? test-target #:allow-other-keys)
256 "Run the test suite of a given Haskell package."
257 (if tests?
258 (run-setuphs test-target '())
259 (format #t "test suite not run~%"))
260 #t)
261
262 (define* (haddock #:key outputs haddock? haddock-flags #:allow-other-keys)
263 "Generate the Haddock documentation of a given Haskell package."
264 (when haddock?
265 (run-setuphs "haddock" haddock-flags))
266 #t)
267
268 (define* (patch-cabal-file #:key cabal-revision #:allow-other-keys)
269 (when cabal-revision
270 ;; Cabal requires there to be a single file with the suffix ".cabal".
271 (match (scandir "." (cut string-suffix? ".cabal" <>))
272 ((original)
273 (format #t "replacing ~s with ~s~%" original cabal-revision)
274 (copy-file cabal-revision original))
275 (_ (error "Could not find a Cabal file to patch."))))
276 #t)
277
278 (define* (generate-setuphs #:rest empty)
279 "Generate a default Setup.hs if needed."
280 (when (not (or (file-exists? "Setup.hs")
281 (file-exists? "Setup.lhs")))
282 (format #t "generating missing Setup.hs~%")
283 (with-output-to-file "Setup.hs"
284 (lambda ()
285 (format #t "import Distribution.Simple~%")
286 (format #t "main = defaultMain~%"))))
287 #t)
288
289 (define %standard-phases
290 (modify-phases gnu:%standard-phases
291 (add-after 'unpack 'patch-cabal-file patch-cabal-file)
292 (add-after 'unpack 'generate-setuphs generate-setuphs)
293 (delete 'bootstrap)
294 (add-before 'configure 'setup-compiler setup-compiler)
295 (add-before 'install 'haddock haddock)
296 (add-after 'install 'register register)
297 (replace 'install install)
298 (replace 'check check)
299 (replace 'build build)
300 (replace 'configure configure)))
301
302 (define* (haskell-build #:key inputs (phases %standard-phases)
303 #:allow-other-keys #:rest args)
304 "Build the given Haskell package, applying all of PHASES in order."
305 (apply gnu:gnu-build
306 #:inputs inputs #:phases phases
307 args))
308
309 ;;; haskell-build-system.scm ends here