haskell-build-system.scm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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, 2020 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. (define-module (guix build haskell-build-system)
  23. #:use-module ((guix build gnu-build-system) #:prefix gnu:)
  24. #:use-module (guix build utils)
  25. #:use-module (srfi srfi-1)
  26. #:use-module (srfi srfi-26)
  27. #:use-module (ice-9 rdelim)
  28. #:use-module (ice-9 regex)
  29. #:use-module (ice-9 match)
  30. #:use-module (ice-9 vlist)
  31. #:use-module (ice-9 ftw)
  32. #:export (%standard-phases
  33. haskell-build))
  34. ;; Commentary:
  35. ;;
  36. ;; Builder-side code of the standard Haskell package build procedure.
  37. ;;
  38. ;; The Haskell compiler, to find libraries, relies on a library database with
  39. ;; a binary cache. For GHC the cache has to be named 'package.cache'. If every
  40. ;; library would generate the cache at build time, then they would clash in
  41. ;; profiles. For this reason we do not generate the cache when we generate
  42. ;; libraries substitutes. Instead:
  43. ;;
  44. ;; - At build time we use the 'setup-compiler' phase to generate a temporary
  45. ;; library database and its cache.
  46. ;;
  47. ;; - We generate the cache when a profile is created.
  48. ;;
  49. ;; Code:
  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. (define (run-setuphs command params)
  56. (let ((setup-file (cond
  57. ((file-exists? "Setup.hs")
  58. "Setup.hs")
  59. ((file-exists? "Setup.lhs")
  60. "Setup.lhs")
  61. (else
  62. #f))))
  63. (if setup-file
  64. (begin
  65. (format #t "running \"runhaskell Setup.hs\" with command ~s \
  66. and parameters ~s~%"
  67. command params)
  68. (apply invoke "runhaskell" setup-file command params))
  69. (error "no Setup.hs nor Setup.lhs found"))))
  70. (define* (configure #:key outputs inputs tests? (configure-flags '())
  71. (extra-directories '()) #:allow-other-keys)
  72. "Configure a given Haskell package."
  73. (let* ((out (assoc-ref outputs "out"))
  74. (doc (assoc-ref outputs "doc"))
  75. (lib (assoc-ref outputs "lib"))
  76. (name-version (strip-store-file-name out))
  77. (extra-dirs (filter-map (cut assoc-ref inputs <>) extra-directories))
  78. (ghc-path (getenv "GHC_PACKAGE_PATH"))
  79. (params `(,(string-append "--prefix=" out)
  80. ,(string-append "--libdir=" (or lib out) "/lib")
  81. ,(string-append "--docdir=" (or doc out)
  82. "/share/doc/" name-version)
  83. "--libsubdir=$compiler/$pkg-$version"
  84. ,(string-append "--package-db=" %tmp-db-dir)
  85. "--global"
  86. ,@(map (cut string-append "--extra-include-dirs=" <>)
  87. (search-path-as-list '("include") extra-dirs))
  88. ,@(map (cut string-append "--extra-lib-dirs=" <>)
  89. (search-path-as-list '("lib") extra-dirs))
  90. ,@(if tests?
  91. '("--enable-tests")
  92. '())
  93. ;; Build and link with shared libraries
  94. "--enable-shared"
  95. "--enable-executable-dynamic"
  96. "--ghc-option=-fPIC"
  97. ,(string-append "--ghc-option=-optl=-Wl,-rpath=" (or lib out)
  98. "/lib/$compiler/$pkg-$version")
  99. ,@configure-flags)))
  100. ;; Cabal errors if GHC_PACKAGE_PATH is set during 'configure', so unset
  101. ;; and restore it.
  102. (unsetenv "GHC_PACKAGE_PATH")
  103. ;; For packages where the Cabal build-type is set to "Configure",
  104. ;; ./configure will be executed. In these cases, the following
  105. ;; environment variable is needed to be able to find the shell executable.
  106. ;; For other package types, the configure script isn't present. For more
  107. ;; information, see the Build Information section of
  108. ;; <https://www.haskell.org/cabal/users-guide/developing-packages.html>.
  109. (when (file-exists? "configure")
  110. (setenv "CONFIG_SHELL" "sh"))
  111. (run-setuphs "configure" params)
  112. (setenv "GHC_PACKAGE_PATH" ghc-path)
  113. #t))
  114. (define* (build #:key parallel-build? #:allow-other-keys)
  115. "Build a given Haskell package."
  116. (run-setuphs "build"
  117. (if parallel-build?
  118. `(,(string-append "--ghc-option=-j" (number->string (parallel-job-count))))
  119. '())))
  120. (define* (install #:key outputs #:allow-other-keys)
  121. "Install a given Haskell package."
  122. (run-setuphs "copy" '())
  123. (when (assoc-ref outputs "static")
  124. (let ((static (assoc-ref outputs "static"))
  125. (lib (or (assoc-ref outputs "lib")
  126. (assoc-ref outputs "out"))))
  127. (for-each (lambda (static-lib)
  128. (let* ((subdir (string-drop static-lib (string-length lib)))
  129. (new (string-append static subdir)))
  130. (mkdir-p (dirname new))
  131. (rename-file static-lib new)))
  132. (find-files lib "\\.a$"))))
  133. #t)
  134. (define (grep rx port)
  135. "Given a regular-expression RX including a group, read from PORT until the
  136. first match and return the content of the group."
  137. (let ((line (read-line port)))
  138. (if (eof-object? line)
  139. #f
  140. (let ((rx-result (regexp-exec rx line)))
  141. (if rx-result
  142. (match:substring rx-result 1)
  143. (grep rx port))))))
  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 (strip-store-file-name 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. ;;; 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. (define (make-ghc-package-database system inputs outputs)
  160. "Generate the GHC package database."
  161. (let* ((haskell (assoc-ref inputs "haskell"))
  162. (name-version (strip-store-file-name haskell))
  163. (input-dirs (match inputs
  164. (((_ . dir) ...)
  165. dir)
  166. (_ '())))
  167. ;; Silence 'find-files' (see 'evaluate-search-paths')
  168. (conf-dirs (with-null-error-port
  169. (search-path-as-list
  170. `(,(string-append "lib/" name-version))
  171. input-dirs #:pattern ".*\\.conf.d$")))
  172. (conf-files (append-map (cut find-files <> "\\.conf$") conf-dirs)))
  173. (mkdir-p %tmp-db-dir)
  174. (for-each (lambda (file)
  175. (let ((dest (string-append %tmp-db-dir "/" (basename file))))
  176. (unless (file-exists? dest)
  177. (copy-file file dest))))
  178. conf-files)
  179. (invoke "ghc-pkg"
  180. (string-append "--package-db=" %tmp-db-dir)
  181. "recache")
  182. #t))
  183. (define* (register #:key name system inputs outputs #:allow-other-keys)
  184. "Generate the compiler registration and binary package database files for a
  185. given Haskell package."
  186. (define (conf-depends conf-file)
  187. ;; Return a list of pkg-ids from the "depends" field in CONF-FILE
  188. (let ((port (open-input-file conf-file))
  189. (field-rx (make-regexp "^(.*):")))
  190. (let loop ((collecting #f)
  191. (deps '()))
  192. (let* ((line (read-line port))
  193. (field (and=> (and (not (eof-object? line))
  194. (regexp-exec field-rx line))
  195. (cut match:substring <> 1))))
  196. (cond
  197. ((and=> field (cut string=? <> "depends"))
  198. ;; The first dependency is listed on the same line as "depends:",
  199. ;; so drop those characters. A line may list more than one .conf.
  200. (let ((d (string-tokenize (string-drop line 8))))
  201. (loop #t (append d deps))))
  202. ((or (eof-object? line) (and collecting field))
  203. (begin
  204. (close-port port)
  205. (reverse! deps)))
  206. (collecting
  207. (loop #t (append (string-tokenize line) deps)))
  208. (else (loop #f deps)))))))
  209. (define (install-transitive-deps conf-file src dest)
  210. ;; Copy .conf files from SRC to DEST for dependencies in CONF-FILE, and
  211. ;; their dependencies, etc.
  212. (let loop ((seen vlist-null)
  213. (lst (conf-depends conf-file)))
  214. (match lst
  215. (() #t) ;done
  216. ((id . tail)
  217. (if (not (vhash-assoc id seen))
  218. (let ((dep-conf (string-append src "/" id ".conf"))
  219. (dep-conf* (string-append dest "/" id ".conf")))
  220. (copy-file dep-conf dep-conf*) ;XXX: maybe symlink instead?
  221. (loop (vhash-cons id #t seen)
  222. (append lst (conf-depends dep-conf))))
  223. (loop seen tail))))))
  224. (let* ((out (assoc-ref outputs "out"))
  225. (doc (assoc-ref outputs "doc"))
  226. (haskell (assoc-ref inputs "haskell"))
  227. (name-verion (strip-store-file-name haskell))
  228. (lib (string-append (or (assoc-ref outputs "lib") out) "/lib"))
  229. (config-dir (string-append lib
  230. "/" name-verion
  231. "/" name ".conf.d"))
  232. (id-rx (make-regexp "^id: *(.*)$"))
  233. (config-file (string-append out "/" name ".conf"))
  234. (params
  235. (list (string-append "--gen-pkg-config=" config-file))))
  236. (run-setuphs "register" params)
  237. ;; The conf file is created only when there is a library to register.
  238. (when (file-exists? config-file)
  239. (mkdir-p config-dir)
  240. (let ((config-file-name+id
  241. (call-with-ascii-input-file config-file (cut grep id-rx <>))))
  242. ;; Remove reference to "doc" output from "lib" (or "out") by rewriting the
  243. ;; "haddock-interfaces" field and removing the optional "haddock-html"
  244. ;; field in the generated .conf file.
  245. (when doc
  246. (substitute* config-file
  247. (("^haddock-html: .*") "\n")
  248. (((format #f "^haddock-interfaces: ~a" doc))
  249. (string-append "haddock-interfaces: " lib)))
  250. ;; Move the referenced file to the "lib" (or "out") output.
  251. (match (find-files doc "\\.haddock$")
  252. ((haddock-file . rest)
  253. (let* ((subdir (string-drop haddock-file (string-length doc)))
  254. (new (string-append lib subdir)))
  255. (mkdir-p (dirname new))
  256. (rename-file haddock-file new)))
  257. (_ #f)))
  258. (install-transitive-deps config-file %tmp-db-dir config-dir)
  259. (rename-file config-file
  260. (string-append config-dir "/"
  261. config-file-name+id ".conf"))
  262. (invoke "ghc-pkg"
  263. (string-append "--package-db=" config-dir)
  264. "recache")))
  265. #t))
  266. (define* (check #:key tests? test-target #:allow-other-keys)
  267. "Run the test suite of a given Haskell package."
  268. (if tests?
  269. (run-setuphs test-target '())
  270. (format #t "test suite not run~%"))
  271. #t)
  272. (define* (haddock #:key outputs haddock? haddock-flags #:allow-other-keys)
  273. "Generate the Haddock documentation of a given Haskell package."
  274. (when haddock?
  275. (run-setuphs "haddock" haddock-flags))
  276. #t)
  277. (define* (patch-cabal-file #:key cabal-revision #:allow-other-keys)
  278. (when cabal-revision
  279. ;; Cabal requires there to be a single file with the suffix ".cabal".
  280. (match (scandir "." (cut string-suffix? ".cabal" <>))
  281. ((original)
  282. (format #t "replacing ~s with ~s~%" original cabal-revision)
  283. (copy-file cabal-revision original))
  284. (_ (error "Could not find a Cabal file to patch."))))
  285. #t)
  286. (define* (generate-setuphs #:rest empty)
  287. "Generate a default Setup.hs if needed."
  288. (when (not (or (file-exists? "Setup.hs")
  289. (file-exists? "Setup.lhs")))
  290. (format #t "generating missing Setup.hs~%")
  291. (with-output-to-file "Setup.hs"
  292. (lambda ()
  293. (format #t "import Distribution.Simple~%")
  294. (format #t "main = defaultMain~%"))))
  295. #t)
  296. (define %standard-phases
  297. (modify-phases gnu:%standard-phases
  298. (add-after 'unpack 'patch-cabal-file patch-cabal-file)
  299. (add-after 'unpack 'generate-setuphs generate-setuphs)
  300. (delete 'bootstrap)
  301. (add-before 'configure 'setup-compiler setup-compiler)
  302. (add-before 'install 'haddock haddock)
  303. (add-after 'install 'register register)
  304. (replace 'install install)
  305. (replace 'check check)
  306. (replace 'build build)
  307. (replace 'configure configure)))
  308. (define* (haskell-build #:key inputs (phases %standard-phases)
  309. #:allow-other-keys #:rest args)
  310. "Build the given Haskell package, applying all of PHASES in order."
  311. (apply gnu:gnu-build
  312. #:inputs inputs #:phases phases
  313. args))
  314. ;;; haskell-build-system.scm ends here