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