haskell.scm 8.0 KB

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