go.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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-2022 Ludovic Courtès <ludo@gnu.org>
  6. ;;; Copyright © 2021 Efraim Flashner <efraim@flashner.co.il>
  7. ;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
  8. ;;;
  9. ;;; This file is part of GNU Guix.
  10. ;;;
  11. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  12. ;;; under the terms of the GNU General Public License as published by
  13. ;;; the Free Software Foundation; either version 3 of the License, or (at
  14. ;;; your option) any later version.
  15. ;;;
  16. ;;; GNU Guix is distributed in the hope that it will be useful, but
  17. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;;; GNU General Public License for more details.
  20. ;;;
  21. ;;; You should have received a copy of the GNU General Public License
  22. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  23. (define-module (guix build-system go)
  24. #:use-module (guix utils)
  25. #:use-module (guix gexp)
  26. #:use-module (guix store)
  27. #:use-module (guix monads)
  28. #:use-module (guix search-paths)
  29. #:use-module (guix build-system)
  30. #:use-module (guix build-system gnu)
  31. #:use-module (guix packages)
  32. #:use-module (ice-9 match)
  33. #:use-module (ice-9 regex)
  34. #:use-module (srfi srfi-1)
  35. #:export (%go-build-system-modules
  36. go-build
  37. go-build-system
  38. go-pseudo-version?
  39. go-version->git-ref))
  40. ;; Commentary:
  41. ;;
  42. ;; Standard build procedure for packages using the Go build system. It is
  43. ;; implemented as an extension of 'gnu-build-system'.
  44. ;;
  45. ;; Code:
  46. (define %go-pseudo-version-rx
  47. ;; Match only the end of the version string; this is so that matching the
  48. ;; more complex leading semantic version pattern is not required.
  49. (make-regexp (string-append
  50. "([0-9]{14}-)" ;timestamp
  51. "([0-9A-Fa-f]{12})" ;commit hash
  52. "(\\+incompatible)?$"))) ;optional +incompatible tag
  53. (define (go-version->git-ref version)
  54. "Parse VERSION, a \"pseudo-version\" as defined at
  55. <https://golang.org/ref/mod#pseudo-versions>, and extract the commit hash from
  56. it, defaulting to full VERSION (stripped from the \"+incompatible\" suffix if
  57. present) if a pseudo-version pattern is not recognized."
  58. ;; A module version like v1.2.3 is introduced by tagging a revision in the
  59. ;; underlying source repository. Untagged revisions can be referred to
  60. ;; using a "pseudo-version" like v0.0.0-yyyymmddhhmmss-abcdefabcdef, where
  61. ;; the time is the commit time in UTC and the final suffix is the prefix of
  62. ;; the commit hash (see: https://golang.org/ref/mod#pseudo-versions).
  63. (let* ((version
  64. ;; If a source code repository has a v2.0.0 or later tag for a file
  65. ;; tree with no go.mod, the version is considered to be part of the
  66. ;; v1 module's available versions and is given an +incompatible
  67. ;; suffix
  68. ;; (see:https://golang.org/cmd/go/#hdr-Module_compatibility_and_semantic_versioning).
  69. (if (string-suffix? "+incompatible" version)
  70. (string-drop-right version 13)
  71. version))
  72. (match (regexp-exec %go-pseudo-version-rx version)))
  73. (if match
  74. (match:substring match 2)
  75. version)))
  76. (define (go-pseudo-version? version)
  77. "True if VERSION is a Go pseudo-version, i.e., a version string made of a
  78. commit hash and its date rather than a proper release tag."
  79. (regexp-exec %go-pseudo-version-rx version))
  80. (define (go-target target)
  81. ;; Parse the nix-system equivalent of the target and set the
  82. ;; target for compilation accordingly.
  83. (match (string-split (gnu-triplet->nix-system target) #\-)
  84. ((arch os)
  85. (list (match arch
  86. ("aarch64" "arm64")
  87. ("armhf" "arm")
  88. ("powerpc64le" "ppc64le")
  89. ("powerpc64" "ppc64")
  90. ("i686" "386")
  91. ("x86_64" "amd64")
  92. ("mips64el" "mips64le")
  93. (_ arch))
  94. (match os
  95. ((or "mingw32" "cygwin") "windows")
  96. (_ os))))))
  97. (define %go-build-system-modules
  98. ;; Build-side modules imported and used by default.
  99. `((guix build go-build-system)
  100. (guix build union)
  101. ,@%gnu-build-system-modules))
  102. (define (default-go)
  103. ;; Lazily resolve the binding to avoid a circular dependency.
  104. (let ((go (resolve-interface '(gnu packages golang))))
  105. (module-ref go 'go)))
  106. (define (make-go-std)
  107. (module-ref (resolve-interface '(gnu packages golang)) 'make-go-std))
  108. (define* (lower name
  109. #:key source inputs native-inputs outputs system target
  110. (go (default-go))
  111. #:allow-other-keys
  112. #:rest arguments)
  113. "Return a bag for NAME."
  114. (define private-keywords
  115. '(#:target #:go #:inputs #:native-inputs))
  116. (define inputs-with-cache
  117. ;; XXX: Avoid a circular dependency. This should be rewritten with
  118. ;; 'package-mapping' or similar.
  119. (let ((go-std-name (string-append (package-name go) "-std")))
  120. (if (string-prefix? go-std-name name)
  121. inputs
  122. (cons `(,go-std-name ,((make-go-std) go)) inputs))))
  123. (bag
  124. (name name)
  125. (system system)
  126. (target target)
  127. (build-inputs `(,@(if source
  128. `(("source" ,source))
  129. '())
  130. ,@`(("go" ,go))
  131. ,@native-inputs
  132. ,@(if target '() inputs-with-cache)
  133. ,@(if target
  134. ;; Use the standard cross inputs of
  135. ;; 'gnu-build-system'.
  136. (standard-cross-packages target 'host)
  137. '())
  138. ;; Keep the standard inputs of 'gnu-build-system'.
  139. ,@(standard-packages)))
  140. (host-inputs (if target inputs-with-cache '()))
  141. ;; The cross-libc is really a target package, but for bootstrapping
  142. ;; reasons, we can't put it in 'host-inputs'. Namely, 'cross-gcc' is a
  143. ;; native package, so it would end up using a "native" variant of
  144. ;; 'cross-libc' (built with 'gnu-build'), whereas all the other packages
  145. ;; would use a target variant (built with 'gnu-cross-build'.)
  146. (target-inputs (if target
  147. (standard-cross-packages target 'target)
  148. '()))
  149. (outputs outputs)
  150. (build (if target go-cross-build go-build))
  151. (arguments (strip-keyword-arguments private-keywords arguments))))
  152. (define* (go-build name inputs
  153. #:key
  154. source
  155. (phases '%standard-phases)
  156. (outputs '("out"))
  157. (search-paths '())
  158. (install-source? #t)
  159. (import-path "")
  160. (unpack-path "")
  161. (build-flags ''())
  162. (tests? #t)
  163. (allow-go-reference? #f)
  164. (system (%current-system))
  165. (goarch #f)
  166. (goos #f)
  167. (guile #f)
  168. (imported-modules %go-build-system-modules)
  169. (modules '((guix build go-build-system)
  170. (guix build union)
  171. (guix build utils)))
  172. (substitutable? #t))
  173. (define builder
  174. (with-imported-modules imported-modules
  175. #~(begin
  176. (use-modules #$@modules)
  177. (go-build #:name #$name
  178. #:source #+source
  179. #:system #$system
  180. #:phases #$phases
  181. #:outputs #$(outputs->gexp outputs)
  182. #:substitutable? #$substitutable?
  183. #:goarch #$goarch
  184. #:goos #$goos
  185. #:search-paths '#$(sexp->gexp
  186. (map search-path-specification->sexp
  187. search-paths))
  188. #:install-source? #$install-source?
  189. #:import-path #$import-path
  190. #:unpack-path #$unpack-path
  191. #:build-flags #$build-flags
  192. #:tests? #$tests?
  193. #:allow-go-reference? #$allow-go-reference?
  194. #:inputs #$(input-tuples->gexp inputs)))))
  195. (mlet %store-monad ((guile (package->derivation (or guile (default-guile))
  196. system #:graft? #f)))
  197. (gexp->derivation name builder
  198. #:system system
  199. #:guile-for-build guile)))
  200. (define* (go-cross-build name
  201. #:key
  202. source target
  203. build-inputs target-inputs host-inputs
  204. (phases '%standard-phases)
  205. (outputs '("out"))
  206. (search-paths '())
  207. (native-search-paths '())
  208. (install-source? #t)
  209. (import-path "")
  210. (unpack-path "")
  211. (build-flags ''())
  212. (tests? #f) ; nothing can be done
  213. (allow-go-reference? #f)
  214. (system (%current-system))
  215. (goarch (first (go-target target)))
  216. (goos (last (go-target target)))
  217. (guile #f)
  218. (imported-modules %go-build-system-modules)
  219. (modules '((guix build go-build-system)
  220. (guix build union)
  221. (guix build utils)))
  222. (substitutable? #t))
  223. "Cross-build NAME using GO, where TARGET is a GNU triplet and with INPUTS."
  224. (define builder
  225. #~(begin
  226. (use-modules #$@(sexp->gexp modules))
  227. (define %build-host-inputs
  228. #+(input-tuples->gexp build-inputs))
  229. (define %build-target-inputs
  230. (append #$(input-tuples->gexp host-inputs)
  231. #+(input-tuples->gexp target-inputs)))
  232. (define %build-inputs
  233. (append %build-host-inputs %build-target-inputs))
  234. (define %outputs
  235. #$(outputs->gexp outputs))
  236. (go-build #:name #$name
  237. #:source #+source
  238. #:system #$system
  239. #:phases #$phases
  240. #:outputs %outputs
  241. #:target #$target
  242. #:goarch #$goarch
  243. #:goos #$goos
  244. #:inputs %build-target-inputs
  245. #:native-inputs %build-host-inputs
  246. #:search-paths '#$(map search-path-specification->sexp
  247. search-paths)
  248. #:native-search-paths '#$(map
  249. search-path-specification->sexp
  250. native-search-paths)
  251. #:install-source? #$install-source?
  252. #:import-path #$import-path
  253. #:unpack-path #$unpack-path
  254. #:build-flags #$build-flags
  255. #:tests? #$tests?
  256. #:make-dynamic-linker-cache? #f ;cross-compiling
  257. #:allow-go-reference? #$allow-go-reference?
  258. #:inputs %build-inputs)))
  259. (mlet %store-monad ((guile (package->derivation (or guile (default-guile))
  260. system #:graft? #f)))
  261. (gexp->derivation name builder
  262. #:system system
  263. #:target target
  264. #:graft? #f
  265. #:substitutable? substitutable?
  266. #:guile-for-build guile)))
  267. (define go-build-system
  268. (build-system
  269. (name 'go)
  270. (description
  271. "Build system for Go programs")
  272. (lower lower)))