go.scm 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016 Petter <petter@mykolab.ch>
  3. ;;; Copyright © 2017 Leo Famulari <leo@famulari.name>
  4. ;;; Copyright © 2020 Jakub Kądziołka <kuba@kadziolka.net>
  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 go)
  21. #:use-module (guix utils)
  22. #:use-module (guix derivations)
  23. #:use-module (guix search-paths)
  24. #:use-module (guix build-system)
  25. #:use-module (guix build-system gnu)
  26. #:use-module (guix packages)
  27. #:use-module (ice-9 match)
  28. #:use-module (ice-9 regex)
  29. #:export (%go-build-system-modules
  30. go-build
  31. go-build-system
  32. go-pseudo-version?
  33. go-version->git-ref))
  34. ;; Commentary:
  35. ;;
  36. ;; Standard build procedure for packages using the Go build system. It is
  37. ;; implemented as an extension of 'gnu-build-system'.
  38. ;;
  39. ;; Code:
  40. (define %go-pseudo-version-rx
  41. ;; Match only the end of the version string; this is so that matching the
  42. ;; more complex leading semantic version pattern is not required.
  43. (make-regexp (string-append
  44. "([0-9]{14}-)" ;timestamp
  45. "([0-9A-Fa-f]{12})" ;commit hash
  46. "(\\+incompatible)?$"))) ;optional +incompatible tag
  47. (define (go-version->git-ref version)
  48. "Parse VERSION, a \"pseudo-version\" as defined at
  49. <https://golang.org/ref/mod#pseudo-versions>, and extract the commit hash from
  50. it, defaulting to full VERSION (stripped from the \"+incompatible\" suffix if
  51. present) if a pseudo-version pattern is not recognized."
  52. ;; A module version like v1.2.3 is introduced by tagging a revision in the
  53. ;; underlying source repository. Untagged revisions can be referred to
  54. ;; using a "pseudo-version" like v0.0.0-yyyymmddhhmmss-abcdefabcdef, where
  55. ;; the time is the commit time in UTC and the final suffix is the prefix of
  56. ;; the commit hash (see: https://golang.org/ref/mod#pseudo-versions).
  57. (let* ((version
  58. ;; If a source code repository has a v2.0.0 or later tag for a file
  59. ;; tree with no go.mod, the version is considered to be part of the
  60. ;; v1 module's available versions and is given an +incompatible
  61. ;; suffix
  62. ;; (see:https://golang.org/cmd/go/#hdr-Module_compatibility_and_semantic_versioning).
  63. (if (string-suffix? "+incompatible" version)
  64. (string-drop-right version 13)
  65. version))
  66. (match (regexp-exec %go-pseudo-version-rx version)))
  67. (if match
  68. (match:substring match 2)
  69. version)))
  70. (define (go-pseudo-version? version)
  71. "True if VERSION is a Go pseudo-version, i.e., a version string made of a
  72. commit hash and its date rather than a proper release tag."
  73. (regexp-exec %go-pseudo-version-rx version))
  74. (define %go-build-system-modules
  75. ;; Build-side modules imported and used by default.
  76. `((guix build go-build-system)
  77. (guix build union)
  78. ,@%gnu-build-system-modules))
  79. (define (default-go)
  80. ;; Lazily resolve the binding to avoid a circular dependency.
  81. (let ((go (resolve-interface '(gnu packages golang))))
  82. (module-ref go 'go)))
  83. (define* (lower name
  84. #:key source inputs native-inputs outputs system target
  85. (go (default-go))
  86. #:allow-other-keys
  87. #:rest arguments)
  88. "Return a bag for NAME."
  89. (define private-keywords
  90. '(#:source #:target #:go #:inputs #:native-inputs))
  91. (and (not target) ;XXX: no cross-compilation
  92. (bag
  93. (name name)
  94. (system system)
  95. (host-inputs `(,@(if source
  96. `(("source" ,source))
  97. '())
  98. ,@inputs
  99. ;; Keep the standard inputs of 'gnu-build-system'.
  100. ,@(standard-packages)))
  101. (build-inputs `(("go" ,go)
  102. ,@native-inputs))
  103. (outputs outputs)
  104. (build go-build)
  105. (arguments (strip-keyword-arguments private-keywords arguments)))))
  106. (define* (go-build store name inputs
  107. #:key
  108. (phases '(@ (guix build go-build-system)
  109. %standard-phases))
  110. (outputs '("out"))
  111. (search-paths '())
  112. (install-source? #t)
  113. (import-path "")
  114. (unpack-path "")
  115. (build-flags ''())
  116. (tests? #t)
  117. (allow-go-reference? #f)
  118. (system (%current-system))
  119. (guile #f)
  120. (imported-modules %go-build-system-modules)
  121. (modules '((guix build go-build-system)
  122. (guix build union)
  123. (guix build utils))))
  124. (define builder
  125. `(begin
  126. (use-modules ,@modules)
  127. (go-build #:name ,name
  128. #:source ,(match (assoc-ref inputs "source")
  129. (((? derivation? source))
  130. (derivation->output-path source))
  131. ((source)
  132. source)
  133. (source
  134. source))
  135. #:system ,system
  136. #:phases ,phases
  137. #:outputs %outputs
  138. #:search-paths ',(map search-path-specification->sexp
  139. search-paths)
  140. #:install-source? ,install-source?
  141. #:import-path ,import-path
  142. #:unpack-path ,unpack-path
  143. #:build-flags ,build-flags
  144. #:tests? ,tests?
  145. #:allow-go-reference? ,allow-go-reference?
  146. #:inputs %build-inputs)))
  147. (define guile-for-build
  148. (match guile
  149. ((? package?)
  150. (package-derivation store guile system #:graft? #f))
  151. (#f ; the default
  152. (let* ((distro (resolve-interface '(gnu packages commencement)))
  153. (guile (module-ref distro 'guile-final)))
  154. (package-derivation store guile system
  155. #:graft? #f)))))
  156. (build-expression->derivation store name builder
  157. #:inputs inputs
  158. #:system system
  159. #:modules imported-modules
  160. #:outputs outputs
  161. #:guile-for-build guile-for-build))
  162. (define go-build-system
  163. (build-system
  164. (name 'go)
  165. (description
  166. "Build system for Go programs")
  167. (lower lower)))