haskell.scm 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015 Federico Beffa <beffa@fbengineering.ch>
  3. ;;; Copyright © 2020 Timothy Sample <samplet@ngyro.com>
  4. ;;; Copyright © 2020 Simon Tournier <zimon.toutoune@gmail.com>
  5. ;;; Copyright © 2021 Ludovic Courtès <ludo@gnu.org>
  6. ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
  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-system haskell)
  23. #:use-module (guix store)
  24. #:use-module (guix utils)
  25. #:use-module (guix packages)
  26. #:use-module (guix gexp)
  27. #:use-module (guix monads)
  28. #:use-module (guix download)
  29. #:use-module (guix search-paths)
  30. #:use-module (guix build-system)
  31. #:use-module (guix build-system gnu)
  32. #:use-module (ice-9 match)
  33. #:use-module (srfi srfi-1)
  34. #:export (hackage-uri
  35. %haskell-build-system-modules
  36. haskell-build
  37. haskell-build-system))
  38. ;; Commentary:
  39. ;;
  40. ;; Standard build procedure for Haskell packages using 'Setup.hs'. This is
  41. ;; implemented as an extension of 'gnu-build-system'.
  42. ;;
  43. ;; Code:
  44. (define (hackage-uri name version)
  45. "Return a URI string for the Haskell package hosted on Hackage corresponding
  46. to NAME and VERSION."
  47. (string-append "https://hackage.haskell.org/package/" name "/"
  48. name "-" version ".tar.gz"))
  49. (define %haskell-build-system-modules
  50. ;; Build-side modules imported by default.
  51. `((guix build haskell-build-system)
  52. ,@%gnu-build-system-modules))
  53. (define (default-haskell)
  54. "Return the default Haskell package."
  55. ;; Lazily resolve the binding to avoid a circular dependency.
  56. (let ((haskell (resolve-interface '(gnu packages haskell))))
  57. (module-ref haskell 'ghc)))
  58. (define (source-url->revision-url url revision)
  59. "Convert URL (a Hackage source URL) to the URL for the Cabal file at
  60. version REVISION."
  61. (let* ((last-slash (string-rindex url #\/))
  62. (next-slash (string-rindex url #\/ 0 last-slash)))
  63. (string-append (substring url 0 next-slash)
  64. (substring url last-slash (- (string-length url)
  65. (string-length ".tar.gz")))
  66. "/revision/" revision ".cabal")))
  67. (define* (lower name
  68. #:key source inputs native-inputs outputs system target
  69. (haskell (default-haskell))
  70. cabal-revision
  71. #:allow-other-keys
  72. #:rest arguments)
  73. "Return a bag for NAME."
  74. (define private-keywords
  75. '(#:target #:haskell #:cabal-revision #:inputs #:native-inputs #:outputs))
  76. (define (cabal-revision->origin cabal-revision)
  77. (match cabal-revision
  78. ((revision hash)
  79. (origin
  80. (method url-fetch)
  81. (uri (source-url->revision-url (origin-uri source) revision))
  82. (sha256 (base32 hash))
  83. (file-name (string-append name "-" revision ".cabal"))))
  84. (#f #f)))
  85. (and (not target) ;XXX: no cross-compilation
  86. (bag
  87. (name name)
  88. (system system)
  89. (host-inputs `(,@(if source
  90. `(("source" ,source))
  91. '())
  92. ,@(match (cabal-revision->origin cabal-revision)
  93. (#f '())
  94. (revision `(("cabal-revision" ,revision))))
  95. ,@inputs
  96. ;; Keep the standard inputs of 'gnu-build-system'.
  97. ,@(standard-packages)))
  98. (build-inputs `(("haskell" ,haskell)
  99. ,@native-inputs))
  100. (outputs outputs)
  101. (build haskell-build)
  102. (arguments
  103. (substitute-keyword-arguments
  104. (strip-keyword-arguments private-keywords arguments)
  105. ((#:extra-directories extra-directories)
  106. `(list ,@(append-map
  107. (lambda (name)
  108. (match (assoc name inputs)
  109. ((_ pkg)
  110. (match (package-transitive-propagated-inputs pkg)
  111. (((propagated-names . _) ...)
  112. (cons name propagated-names))))))
  113. extra-directories))))))))
  114. (define* (haskell-build name inputs
  115. #:key source
  116. (haddock? #t)
  117. (haddock-flags ''())
  118. (tests? #t)
  119. (test-target "test")
  120. ;; FIXME: Parallel builds lead to indeterministic
  121. ;; results, see <http://issues.guix.gnu.org/43843#3>.
  122. (parallel-build? #f)
  123. (configure-flags ''())
  124. (extra-directories ''())
  125. (phases '%standard-phases)
  126. (outputs '("out" "static"))
  127. (search-paths '())
  128. (system (%current-system))
  129. (guile #f)
  130. (imported-modules %haskell-build-system-modules)
  131. (modules '((guix build haskell-build-system)
  132. (guix build utils))))
  133. "Build SOURCE using HASKELL, and with INPUTS. This assumes that SOURCE
  134. provides a 'Setup.hs' file as its build system."
  135. (define builder
  136. (with-imported-modules imported-modules
  137. #~(begin
  138. (use-modules #$@(sexp->gexp modules))
  139. #$(with-build-variables inputs outputs
  140. #~(haskell-build #:name #$name
  141. #:source #+source
  142. ;; XXX: INPUTS contains <gexp-input> records as
  143. ;; opposed to raw lowerable objects, hence the
  144. ;; use of ungexp-splicing.
  145. #:cabal-revision
  146. #$@(match (assoc-ref inputs "cabal-revision")
  147. (#f '(#f))
  148. (lst lst))
  149. #:configure-flags #$configure-flags
  150. #:extra-directories #$extra-directories
  151. #:extra-directories #$extra-directories
  152. #:haddock-flags #$haddock-flags
  153. #:system #$system
  154. #:test-target #$test-target
  155. #:tests? #$tests?
  156. #:parallel-build? #$parallel-build?
  157. #:haddock? #$haddock?
  158. #:phases #$phases
  159. #:outputs #$(outputs->gexp outputs)
  160. #:search-paths '#$(sexp->gexp
  161. (map search-path-specification->sexp
  162. search-paths))
  163. #:inputs #$(input-tuples->gexp inputs))))))
  164. (mlet %store-monad ((guile (package->derivation (or guile (default-guile))
  165. system #:graft? #f)))
  166. (gexp->derivation name builder
  167. #:system system
  168. #:guile-for-build guile)))
  169. (define haskell-build-system
  170. (build-system
  171. (name 'haskell)
  172. (description "The standard Haskell build system")
  173. (lower lower)))
  174. ;;; haskell.scm ends here