haskell-build-system.scm 14 KB

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