haskell.scm 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (guix build-system haskell)
  21. #:use-module (guix store)
  22. #:use-module (guix utils)
  23. #:use-module (guix packages)
  24. #:use-module (guix derivations)
  25. #:use-module (guix download)
  26. #:use-module (guix search-paths)
  27. #:use-module (guix build-system)
  28. #:use-module (guix build-system gnu)
  29. #:use-module (ice-9 match)
  30. #:use-module (srfi srfi-1)
  31. #:use-module (srfi srfi-26)
  32. #:export (%haskell-build-system-modules
  33. haskell-build
  34. haskell-build-system))
  35. ;; Commentary:
  36. ;;
  37. ;; Standard build procedure for Haskell packages using 'Setup.hs'. This is
  38. ;; implemented as an extension of 'gnu-build-system'.
  39. ;;
  40. ;; Code:
  41. (define %haskell-build-system-modules
  42. ;; Build-side modules imported by default.
  43. `((guix build haskell-build-system)
  44. ,@%gnu-build-system-modules))
  45. (define (default-haskell)
  46. "Return the default Haskell package."
  47. ;; Lazily resolve the binding to avoid a circular dependency.
  48. (let ((haskell (resolve-interface '(gnu packages haskell))))
  49. (module-ref haskell 'ghc)))
  50. (define (source-url->revision-url url revision)
  51. "Convert URL (a Hackage source URL) to the URL for the Cabal file at
  52. version REVISION."
  53. (let* ((last-slash (string-rindex url #\/))
  54. (next-slash (string-rindex url #\/ 0 last-slash)))
  55. (string-append (substring url 0 next-slash)
  56. (substring url last-slash (- (string-length url)
  57. (string-length ".tar.gz")))
  58. "/revision/" revision ".cabal")))
  59. (define* (lower name
  60. #:key source inputs native-inputs outputs system target
  61. (haskell (default-haskell))
  62. cabal-revision
  63. #:allow-other-keys
  64. #:rest arguments)
  65. "Return a bag for NAME."
  66. (define private-keywords
  67. '(#:target #:haskell #:cabal-revision #:inputs #:native-inputs #:outputs))
  68. (define (cabal-revision->origin cabal-revision)
  69. (match cabal-revision
  70. ((revision hash)
  71. (origin
  72. (method url-fetch)
  73. (uri (source-url->revision-url (origin-uri source) revision))
  74. (sha256 (base32 hash))
  75. (file-name (string-append name "-" revision ".cabal"))))
  76. (#f #f)))
  77. (and (not target) ;XXX: no cross-compilation
  78. (bag
  79. (name name)
  80. (system system)
  81. (host-inputs `(,@(if source
  82. `(("source" ,source))
  83. '())
  84. ,@(match (cabal-revision->origin cabal-revision)
  85. (#f '())
  86. (revision `(("cabal-revision" ,revision))))
  87. ,@inputs
  88. ;; Keep the standard inputs of 'gnu-build-system'.
  89. ,@(standard-packages)))
  90. (build-inputs `(("haskell" ,haskell)
  91. ,@native-inputs))
  92. ;; XXX: this is a hack to get around issue #41569.
  93. (outputs (match outputs
  94. (("out") (cons "static" outputs))
  95. (_ outputs)))
  96. (build haskell-build)
  97. (arguments
  98. (substitute-keyword-arguments
  99. (strip-keyword-arguments private-keywords arguments)
  100. ((#:extra-directories extra-directories)
  101. `(list ,@(append-map
  102. (lambda (name)
  103. (match (assoc name inputs)
  104. ((_ pkg)
  105. (match (package-transitive-propagated-inputs pkg)
  106. (((propagated-names . _) ...)
  107. (cons name propagated-names))))))
  108. extra-directories))))))))
  109. (define* (haskell-build store name inputs
  110. #:key source
  111. (haddock? #t)
  112. (haddock-flags ''())
  113. (tests? #t)
  114. (test-target "test")
  115. ;; FIXME: Parallel builds lead to indeterministic
  116. ;; results, see <http://issues.guix.gnu.org/43843#3>.
  117. (parallel-build? #f)
  118. (configure-flags ''())
  119. (extra-directories ''())
  120. (phases '(@ (guix build haskell-build-system)
  121. %standard-phases))
  122. (outputs '("out" "static"))
  123. (search-paths '())
  124. (system (%current-system))
  125. (guile #f)
  126. (imported-modules %haskell-build-system-modules)
  127. (modules '((guix build haskell-build-system)
  128. (guix build utils))))
  129. "Build SOURCE using HASKELL, and with INPUTS. This assumes that SOURCE
  130. provides a 'Setup.hs' file as its build system."
  131. (define builder
  132. `(begin
  133. (use-modules ,@modules)
  134. (haskell-build #:name ,name
  135. #:source ,(match (assoc-ref inputs "source")
  136. (((? derivation? source))
  137. (derivation->output-path source))
  138. ((source)
  139. source)
  140. (source
  141. source))
  142. #:cabal-revision ,(match (assoc-ref inputs
  143. "cabal-revision")
  144. (((? derivation? revision))
  145. (derivation->output-path revision))
  146. (revision revision))
  147. #:configure-flags ,configure-flags
  148. #:extra-directories ,extra-directories
  149. #:haddock-flags ,haddock-flags
  150. #:system ,system
  151. #:test-target ,test-target
  152. #:tests? ,tests?
  153. #:parallel-build? ,parallel-build?
  154. #:haddock? ,haddock?
  155. #:phases ,phases
  156. #:outputs %outputs
  157. #:search-paths ',(map search-path-specification->sexp
  158. search-paths)
  159. #:inputs %build-inputs)))
  160. (define guile-for-build
  161. (match guile
  162. ((? package?)
  163. (package-derivation store guile system #:graft? #f))
  164. (#f ; the default
  165. (let* ((distro (resolve-interface '(gnu packages commencement)))
  166. (guile (module-ref distro 'guile-final)))
  167. (package-derivation store guile system #:graft? #f)))))
  168. (build-expression->derivation store name builder
  169. #:inputs inputs
  170. #:system system
  171. #:modules imported-modules
  172. #:outputs outputs
  173. #:guile-for-build guile-for-build))
  174. (define haskell-build-system
  175. (build-system
  176. (name 'haskell)
  177. (description "The standard Haskell build system")
  178. (lower lower)))
  179. ;;; haskell.scm ends here