go.scm 12 KB

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