go.scm 6.7 KB

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