haskell.scm 7.2 KB

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