go-build-system.scm 15 KB

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