elm.scm 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2022 Philip McGrath <philip@philipmcgrath.com>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (guix build-system elm)
  19. #:use-module (guix store)
  20. #:use-module (guix utils)
  21. #:use-module (guix packages)
  22. #:use-module (guix gexp)
  23. #:use-module (guix monads)
  24. #:use-module (guix search-paths)
  25. #:use-module (guix git-download)
  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. #:export (elm->package-name
  31. guix-package->elm-name
  32. infer-elm-package-name
  33. elm-package-origin
  34. %elm-build-system-modules
  35. %elm-default-modules
  36. elm-build
  37. elm-build-system))
  38. (define (elm->package-name name)
  39. "Given the NAME of an Elm package, return a Guix-style package name."
  40. (let ((converted
  41. (string-join (string-split (string-downcase name) #\/) "-")))
  42. (if (string-prefix? "elm-" converted)
  43. converted
  44. (string-append "elm-" converted))))
  45. (define (guix-package->elm-name package)
  46. "Given an Elm PACKAGE, return the possibly-inferred upstream name, or #f the
  47. upstream name is not specified and can't be inferred."
  48. (or (assoc-ref (package-properties package) 'upstream-name)
  49. (infer-elm-package-name (package-name package))))
  50. (define (infer-elm-package-name guix-name)
  51. "Given the GUIX-NAME of an Elm package, return the inferred upstream name,
  52. or #f if it can't be inferred. If the result is not #f, supplying it to
  53. 'elm->package-name' would produce GUIX-NAME.
  54. See also 'guix-package->elm-name', which respects the 'upstream-name'
  55. property."
  56. (define (parts-join part0 parts)
  57. (string-join (cons part0 parts) "-"))
  58. (match (string-split guix-name #\-)
  59. (("elm" "explorations" part0 parts ...)
  60. (string-append "elm-explorations/"
  61. (parts-join part0 parts)))
  62. (("elm" owner part0 parts ...)
  63. (string-append owner "/" (parts-join part0 parts)))
  64. (("elm" repo)
  65. (string-append "elm/" repo))
  66. (_
  67. #f)))
  68. (define (elm-package-origin elm-name version hash)
  69. "Return an origin for the Elm package with upstream name ELM-NAME at the
  70. given VERSION with sha256 checksum HASH."
  71. ;; elm requires this very specific repository structure and tagging regime
  72. (origin
  73. (method git-fetch)
  74. (uri (git-reference
  75. (url (string-append "https://github.com/" elm-name))
  76. (commit version)))
  77. (file-name (git-file-name (elm->package-name elm-name) version))
  78. (sha256 hash)))
  79. (define %elm-build-system-modules
  80. ;; Build-side modules imported by default.
  81. `((guix build elm-build-system)
  82. (guix build json)
  83. (guix build union)
  84. ,@%gnu-build-system-modules))
  85. (define %elm-default-modules
  86. ;; Modules in scope in the build-side environment.
  87. '((guix build elm-build-system)
  88. (guix build utils)
  89. (guix build json)
  90. (guix build union)))
  91. (define (default-elm)
  92. "Return the default Elm package for builds."
  93. ;; Lazily resolve the binding to avoid a circular dependency.
  94. (let ((elm (resolve-interface '(gnu packages elm))))
  95. (module-ref elm 'elm-sans-reactor)))
  96. (define (default-elm-core)
  97. "Return the default elm-core package."
  98. ;; Lazily resolve the binding to avoid a circular dependency.
  99. (let ((elm (resolve-interface '(gnu packages elm))))
  100. (module-ref elm 'elm-core)))
  101. (define (default-elm-json)
  102. "Return the default elm-json package."
  103. ;; Lazily resolve the binding to avoid a circular dependency.
  104. (let ((elm (resolve-interface '(gnu packages elm))))
  105. (module-ref elm 'elm-json)))
  106. (define* (lower name
  107. #:key source inputs native-inputs outputs system target
  108. (implicit-elm-package-inputs? #t)
  109. (elm (default-elm))
  110. #:allow-other-keys
  111. #:rest arguments)
  112. "Return a bag for NAME."
  113. (define private-keywords
  114. '(#:target #:implicit-elm-package-inputs? #:elm #:inputs #:native-inputs))
  115. (cond
  116. (target
  117. ;; Cross-compilation is not yet supported. It should be easy, though,
  118. ;; since the build products are all platform-independent.
  119. #f)
  120. (else
  121. (bag
  122. (name name)
  123. (system system)
  124. (host-inputs
  125. `(,@(if source
  126. `(("source" ,source))
  127. '())
  128. ,@inputs
  129. ("elm" ,elm)
  130. ,@(cond
  131. (implicit-elm-package-inputs?
  132. ;; These are needed for elm-build-system even if not actually
  133. ;; needed by the package being built. But "elm/json" is often
  134. ;; present in practice, and "elm/core" always is: only add the
  135. ;; default packages if no suitable inputs have been given
  136. ;; explicitly.
  137. (filter-map
  138. (match-lambda
  139. ((name get-default)
  140. (cond
  141. ((find (match-lambda
  142. ((_ pkg . _)
  143. (equal? name (guix-package->elm-name pkg))))
  144. inputs)
  145. #f)
  146. (else
  147. `(,name ,(get-default))))))
  148. `(("elm/core" ,default-elm-core)
  149. ("elm/json" ,default-elm-json))))
  150. (else
  151. '()))
  152. ;; TODO: probably don't need most of (standard-packages)
  153. ,@(standard-packages)))
  154. (outputs outputs)
  155. (build elm-build)
  156. (arguments (strip-keyword-arguments private-keywords arguments))))))
  157. (define* (elm-build name inputs
  158. #:key
  159. source
  160. (tests? #t)
  161. (phases '%standard-phases)
  162. (outputs '("out"))
  163. (search-paths '())
  164. (system (%current-system))
  165. (guile #f)
  166. (imported-modules %elm-build-system-modules)
  167. (modules %elm-default-modules))
  168. "Build SOURCE using ELM."
  169. (define builder
  170. (with-imported-modules imported-modules
  171. #~(begin
  172. (use-modules #$@(sexp->gexp modules))
  173. (elm-build #:name #$name
  174. #:source #+source
  175. #:system #$system
  176. #:tests? #$tests?
  177. #:phases #$phases
  178. #:outputs #$(outputs->gexp outputs)
  179. #:search-paths '#$(sexp->gexp
  180. (map search-path-specification->sexp
  181. search-paths))
  182. #:inputs #$(input-tuples->gexp inputs)))))
  183. (mlet %store-monad ((guile (package->derivation (or guile (default-guile))
  184. system #:graft? #f)))
  185. (gexp->derivation name builder
  186. #:system system
  187. #:guile-for-build guile)))
  188. (define elm-build-system
  189. (build-system
  190. (name 'elm)
  191. (description "The Elm build system")
  192. (lower lower)))