go.scm 6.8 KB

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