haskell-build-system.scm 14 KB

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