haskell.scm 7.8 KB

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