go-build-system.scm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016 Petter <petter@mykolab.ch>
  3. ;;; Copyright © 2017, 2019 Leo Famulari <leo@famulari.name>
  4. ;;; Copyright © 2019 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  5. ;;; Copyright © 2020 Jack Hill <jackhill@jackhill.us>
  6. ;;; Copyright © 2020 Jakub Kądziołka <kuba@kadziolka.net>
  7. ;;;
  8. ;;; This file is part of GNU Guix.
  9. ;;;
  10. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  11. ;;; under the terms of the GNU General Public License as published by
  12. ;;; the Free Software Foundation; either version 3 of the License, or (at
  13. ;;; your option) any later version.
  14. ;;;
  15. ;;; GNU Guix is distributed in the hope that it will be useful, but
  16. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;;; GNU General Public License for more details.
  19. ;;;
  20. ;;; You should have received a copy of the GNU General Public License
  21. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  22. (define-module (guix build go-build-system)
  23. #:use-module ((guix build gnu-build-system) #:prefix gnu:)
  24. #:use-module (guix build union)
  25. #:use-module (guix build utils)
  26. #:use-module (ice-9 match)
  27. #:use-module (ice-9 ftw)
  28. #:use-module (srfi srfi-1)
  29. #:use-module (rnrs io ports)
  30. #:use-module (rnrs bytevectors)
  31. #:export (%standard-phases
  32. go-build))
  33. ;; Commentary:
  34. ;;
  35. ;; Build procedures for Go packages. This is the builder-side code.
  36. ;;
  37. ;; Software written in Go is either a 'package' (i.e. library) or 'command'
  38. ;; (i.e. executable). Both types can be built with either the `go build` or `go
  39. ;; install` commands. However, `go build` discards the result of the build
  40. ;; process for Go libraries, so we use `go install`, which preserves the
  41. ;; results. [0]
  42. ;; Go software is developed and built within a particular file system hierarchy
  43. ;; structure called a 'workspace' [1]. This workspace can be found by Go via
  44. ;; the GOPATH environment variable. Typically, all Go source code and compiled
  45. ;; objects are kept in a single workspace, but GOPATH may be a list of
  46. ;; directories [2]. In this go-build-system we create a file system union of
  47. ;; the Go-language dependencies. Previously, we made GOPATH a list of store
  48. ;; directories, but stopped because Go programs started keeping references to
  49. ;; these directories in Go 1.11:
  50. ;; <https://bugs.gnu.org/33620>.
  51. ;;
  52. ;; Go software, whether a package or a command, is uniquely named using an
  53. ;; 'import path'. The import path is based on the URL of the software's source.
  54. ;; Because most source code is provided over the internet, the import path is
  55. ;; typically a combination of the remote URL and the source repository's file
  56. ;; system structure. For example, the Go port of the common `du` command is
  57. ;; hosted on github.com, at <https://github.com/calmh/du>. Thus, the import
  58. ;; path is <github.com/calmh/du>. [3]
  59. ;;
  60. ;; It may be possible to automatically guess a package's import path based on
  61. ;; the source URL, but we don't try that in this revision of the
  62. ;; go-build-system.
  63. ;;
  64. ;; Modules of modular Go libraries are named uniquely with their
  65. ;; file system paths. For example, the supplemental but "standardized"
  66. ;; libraries developed by the Go upstream developers are available at
  67. ;; <https://golang.org/x/{net,text,crypto, et cetera}>. The Go IPv4
  68. ;; library's import path is <golang.org/x/net/ipv4>. The source of
  69. ;; such modular libraries must be unpacked at the top-level of the
  70. ;; file system structure of the library. So the IPv4 library should be
  71. ;; unpacked to <golang.org/x/net>. This is handled in the
  72. ;; go-build-system with the optional #:unpack-path key.
  73. ;;
  74. ;; In general, Go software is built using a standardized build mechanism
  75. ;; that does not require any build scripts like Makefiles. This means
  76. ;; that all modules of modular libraries cannot be built with a single
  77. ;; command. Each module must be built individually. This complicates
  78. ;; certain cases, and these issues are currently resolved by creating a
  79. ;; file system union of the required modules of such libraries. I think
  80. ;; this could be improved in future revisions of the go-build-system.
  81. ;;
  82. ;; TODO:
  83. ;; * Avoid copying dependencies into the build environment and / or avoid using
  84. ;; a tmpdir when creating the inputs union.
  85. ;; * Use Go modules [4]
  86. ;; * Re-use compiled packages [5]
  87. ;; * Avoid the go-inputs hack
  88. ;; * Stop needing remove-go-references (-trimpath ? )
  89. ;; * Remove module packages, only offering the full Git repos? This is
  90. ;; more idiomatic, I think, because Go downloads Git repos, not modules.
  91. ;; What are the trade-offs?
  92. ;;
  93. ;; [0] `go build`:
  94. ;; https://golang.org/cmd/go/#hdr-Compile_packages_and_dependencies
  95. ;; `go install`:
  96. ;; https://golang.org/cmd/go/#hdr-Compile_and_install_packages_and_dependencies
  97. ;; [1] Go workspace example, from <https://golang.org/doc/code.html#Workspaces>:
  98. ;; bin/
  99. ;; hello # command executable
  100. ;; outyet # command executable
  101. ;; pkg/
  102. ;; linux_amd64/
  103. ;; github.com/golang/example/
  104. ;; stringutil.a # package object
  105. ;; src/
  106. ;; github.com/golang/example/
  107. ;; .git/ # Git repository metadata
  108. ;; hello/
  109. ;; hello.go # command source
  110. ;; outyet/
  111. ;; main.go # command source
  112. ;; main_test.go # test source
  113. ;; stringutil/
  114. ;; reverse.go # package source
  115. ;; reverse_test.go # test source
  116. ;; golang.org/x/image/
  117. ;; .git/ # Git repository metadata
  118. ;; bmp/
  119. ;; reader.go # package source
  120. ;; writer.go # package source
  121. ;; ... (many more repositories and packages omitted) ...
  122. ;;
  123. ;; [2] https://golang.org/doc/code.html#GOPATH
  124. ;; [3] https://golang.org/doc/code.html#ImportPaths
  125. ;; [4] https://golang.org/cmd/go/#hdr-Modules__module_versions__and_more
  126. ;; [5] https://bugs.gnu.org/32919
  127. ;;
  128. ;; Code:
  129. (define* (setup-go-environment #:key inputs outputs #:allow-other-keys)
  130. "Prepare a Go build environment for INPUTS and OUTPUTS. Build a file system
  131. union of INPUTS. Export GOPATH, which helps the compiler find the source code
  132. of the package being built and its dependencies, and GOBIN, which determines
  133. where executables (\"commands\") are installed to. This phase is sometimes used
  134. by packages that use (guix build-system gnu) but have a handful of Go
  135. dependencies, so it should be self-contained."
  136. ;; The Go cache is required starting in Go 1.12. We don't actually use it but
  137. ;; we need it to be a writable directory.
  138. (setenv "GOCACHE" "/tmp/go-cache")
  139. ;; Using the current working directory as GOPATH makes it easier for packagers
  140. ;; who need to manipulate the unpacked source code.
  141. (setenv "GOPATH" (getcwd))
  142. ;; Go 1.13 uses go modules by default. The go build system does not
  143. ;; currently support modules, so turn modules off to continue using the old
  144. ;; GOPATH behavior.
  145. (setenv "GO111MODULE" "off")
  146. (setenv "GOBIN" (string-append (assoc-ref outputs "out") "/bin"))
  147. (let ((tmpdir (tmpnam)))
  148. (match (go-inputs inputs)
  149. (((names . directories) ...)
  150. (union-build tmpdir (filter directory-exists? directories)
  151. #:create-all-directories? #t
  152. #:log-port (%make-void-port "w"))))
  153. ;; XXX A little dance because (guix build union) doesn't use mkdir-p.
  154. (copy-recursively tmpdir
  155. (string-append (getenv "GOPATH"))
  156. #:keep-mtime? #t)
  157. (delete-file-recursively tmpdir))
  158. #t)
  159. (define* (unpack #:key source import-path unpack-path #:allow-other-keys)
  160. "Relative to $GOPATH, unpack SOURCE in UNPACK-PATH, or IMPORT-PATH when
  161. UNPACK-PATH is unset. If the SOURCE archive has a single top level directory,
  162. it is stripped so that the sources appear directly under UNPACK-PATH. When
  163. SOURCE is a directory, copy its content into UNPACK-PATH instead of
  164. unpacking."
  165. (define (unpack-maybe-strip source dest)
  166. (let* ((scratch-dir (string-append (or (getenv "TMPDIR") "/tmp")
  167. "/scratch-dir"))
  168. (out (mkdir-p scratch-dir)))
  169. (with-directory-excursion scratch-dir
  170. (if (string-suffix? ".zip" source)
  171. (invoke "unzip" source)
  172. (invoke "tar" "-xvf" source))
  173. (let ((top-level-files (remove (lambda (x)
  174. (member x '("." "..")))
  175. (scandir "."))))
  176. (match top-level-files
  177. ((top-level-file)
  178. (when (file-is-directory? top-level-file)
  179. (copy-recursively top-level-file dest #:keep-mtime? #t)))
  180. (_
  181. (copy-recursively "." dest #:keep-mtime? #t)))))
  182. (delete-file-recursively scratch-dir)))
  183. (when (string-null? import-path)
  184. (display "WARNING: The Go import path is unset.\n"))
  185. (when (string-null? unpack-path)
  186. (set! unpack-path import-path))
  187. (let ((dest (string-append (getenv "GOPATH") "/src/" unpack-path)))
  188. (mkdir-p dest)
  189. (if (file-is-directory? source)
  190. (copy-recursively source dest #:keep-mtime? #t)
  191. (unpack-maybe-strip source dest)))
  192. #t)
  193. (define (go-package? name)
  194. (string-prefix? "go-" name))
  195. (define (go-inputs inputs)
  196. "Return the alist of INPUTS that are Go software."
  197. ;; XXX This should not check the file name of the store item. Instead we
  198. ;; should pass, from the host side, the list of inputs that are packages using
  199. ;; the go-build-system.
  200. (alist-delete "go" ; Exclude the Go compiler
  201. (alist-delete "source" ; Exclude the source code of the package being built
  202. (filter (match-lambda
  203. ((label . directory)
  204. (go-package? ((compose package-name->name+version
  205. strip-store-file-name)
  206. directory)))
  207. (_ #f))
  208. inputs))))
  209. (define* (build #:key import-path build-flags #:allow-other-keys)
  210. "Build the package named by IMPORT-PATH."
  211. (with-throw-handler
  212. #t
  213. (lambda _
  214. (apply invoke "go" "install"
  215. "-v" ; print the name of packages as they are compiled
  216. "-x" ; print each command as it is invoked
  217. ;; Respectively, strip the symbol table and debug
  218. ;; information, and the DWARF symbol table.
  219. "-ldflags=-s -w"
  220. `(,@build-flags ,import-path)))
  221. (lambda (key . args)
  222. (display (string-append "Building '" import-path "' failed.\n"
  223. "Here are the results of `go env`:\n"))
  224. (invoke "go" "env"))))
  225. ;; Can this also install commands???
  226. (define* (check #:key tests? import-path #:allow-other-keys)
  227. "Run the tests for the package named by IMPORT-PATH."
  228. (when tests?
  229. (invoke "go" "test" import-path))
  230. #t)
  231. (define* (install #:key install-source? outputs import-path unpack-path #:allow-other-keys)
  232. "Install the source code of IMPORT-PATH to the primary output directory.
  233. Compiled executable files (Go \"commands\") should have already been installed
  234. to the store based on $GOBIN in the build phase.
  235. XXX We can't make use of compiled libraries (Go \"packages\")."
  236. (when install-source?
  237. (if (string-null? import-path)
  238. ((display "WARNING: The Go import path is unset.\n")))
  239. (let* ((out (assoc-ref outputs "out"))
  240. (source (string-append (getenv "GOPATH") "/src/" import-path))
  241. (dest (string-append out "/src/" import-path)))
  242. (mkdir-p dest)
  243. (copy-recursively source dest #:keep-mtime? #t)))
  244. #t)
  245. (define* (remove-store-reference file file-name
  246. #:optional (store (%store-directory)))
  247. "Remove from FILE occurrences of FILE-NAME in STORE; return #t when FILE-NAME
  248. is encountered in FILE, #f otherwise. This implementation reads FILE one byte at
  249. a time, which is slow. Instead, we should use the Boyer-Moore string search
  250. algorithm; there is an example in (guix build grafts)."
  251. (define pattern
  252. (string-take file-name
  253. (+ 34 (string-length (%store-directory)))))
  254. (with-fluids ((%default-port-encoding #f))
  255. (with-atomic-file-replacement file
  256. (lambda (in out)
  257. ;; We cannot use `regexp-exec' here because it cannot deal with
  258. ;; strings containing NUL characters.
  259. (format #t "removing references to `~a' from `~a'...~%" file-name file)
  260. (setvbuf in 'block 65536)
  261. (setvbuf out 'block 65536)
  262. (fold-port-matches (lambda (match result)
  263. (put-bytevector out (string->utf8 store))
  264. (put-u8 out (char->integer #\/))
  265. (put-bytevector out
  266. (string->utf8
  267. "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-"))
  268. #t)
  269. #f
  270. pattern
  271. in
  272. (lambda (char result)
  273. (put-u8 out (char->integer char))
  274. result))))))
  275. (define* (remove-go-references #:key allow-go-reference?
  276. inputs outputs #:allow-other-keys)
  277. "Remove any references to the Go compiler from the compiled Go executable
  278. files in OUTPUTS."
  279. ;; We remove this spurious reference to save bandwidth when installing Go
  280. ;; executables. It would be better to not embed the reference in the first
  281. ;; place, but I'm not sure how to do that. The subject was discussed at:
  282. ;; <https://lists.gnu.org/archive/html/guix-devel/2017-10/msg00207.html>
  283. (if allow-go-reference?
  284. #t
  285. (let ((go (assoc-ref inputs "go"))
  286. (bin "/bin"))
  287. (for-each (lambda (output)
  288. (when (file-exists? (string-append (cdr output)
  289. bin))
  290. (for-each (lambda (file)
  291. (remove-store-reference file go))
  292. (find-files (string-append (cdr output) bin)))))
  293. outputs)
  294. #t)))
  295. (define %standard-phases
  296. (modify-phases gnu:%standard-phases
  297. (delete 'bootstrap)
  298. (delete 'configure)
  299. (delete 'patch-generated-file-shebangs)
  300. (add-before 'unpack 'setup-go-environment setup-go-environment)
  301. (replace 'unpack unpack)
  302. (replace 'build build)
  303. (replace 'check check)
  304. (replace 'install install)
  305. (add-after 'install 'remove-go-references remove-go-references)))
  306. (define* (go-build #:key inputs (phases %standard-phases)
  307. #:allow-other-keys #:rest args)
  308. "Build the given Go package, applying all of PHASES in order."
  309. (apply gnu:gnu-build #:inputs inputs #:phases phases args))