Merge branch 'core-updates'
[jackhill/guix/guix.git] / gnu / packages / golang.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016 Efraim Flashner <efraim@flashner.co.il>
3 ;;; Copyright © 2016 Matthew Jordan <matthewjordandevops@yandex.com>
4 ;;; Copyright © 2016 Andy Wingo <wingo@igalia.com>
5 ;;; Copyright © 2016 Ludovic Courtès <ludo@gnu.org>
6 ;;; Copyright © 2016, 2017 Petter <petter@mykolab.ch>
7 ;;; Copyright © 2016, 2017 Leo Famulari <leo@famulari.name>
8 ;;; Copyright © 2017 Sergei Trofimovich <slyfox@inbox.ru>
9 ;;; Copyright © 2017 Alex Vong <alexvong1995@gmail.com>
10 ;;;
11 ;;; This file is part of GNU Guix.
12 ;;;
13 ;;; GNU Guix is free software; you can redistribute it and/or modify it
14 ;;; under the terms of the GNU General Public License as published by
15 ;;; the Free Software Foundation; either version 3 of the License, or (at
16 ;;; your option) any later version.
17 ;;;
18 ;;; GNU Guix is distributed in the hope that it will be useful, but
19 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;;; GNU General Public License for more details.
22 ;;;
23 ;;; You should have received a copy of the GNU General Public License
24 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
25
26 (define-module (gnu packages golang)
27 #:use-module ((guix licenses) #:prefix license:)
28 #:use-module (guix utils)
29 #:use-module (guix download)
30 #:use-module (guix packages)
31 #:use-module (guix build-system gnu)
32 #:use-module (gnu packages admin)
33 #:use-module (gnu packages gcc)
34 #:use-module (gnu packages base)
35 #:use-module (gnu packages perl)
36 #:use-module (gnu packages pkg-config)
37 #:use-module (gnu packages pcre)
38 #:use-module (ice-9 match)
39 #:use-module (srfi srfi-1))
40
41 ;; According to https://golang.org/doc/install/gccgo, gccgo-4.8.2 includes a
42 ;; complete go-1.1.2 implementation, gccgo-4.9 includes a complete go-1.2
43 ;; implementation, and gccgo-5 a complete implementation of go-1.4. Ultimately
44 ;; we hope to build go-1.5+ with a bootstrap process using gccgo-5. As of
45 ;; go-1.5, go cannot be bootstrapped without go-1.4, so we need to use go-1.4 or
46 ;; gccgo-5. Mips is not officially supported, but it should work if it is
47 ;; bootstrapped.
48
49 (define-public go-1.4
50 (package
51 (name "go")
52 (version "1.4.3")
53 (source (origin
54 (method url-fetch)
55 (uri (string-append "https://storage.googleapis.com/golang/"
56 name version ".src.tar.gz"))
57 (sha256
58 (base32
59 "0na9yqilzpvq0bjndbibfp07wr796gf252y471cip10bbdqgqiwr"))))
60 (build-system gnu-build-system)
61 (outputs '("out"
62 "doc"
63 "tests"))
64 (arguments
65 `(#:modules ((ice-9 match)
66 (guix build gnu-build-system)
67 (guix build utils)
68 (srfi srfi-1))
69 #:tests? #f ; Tests are run by the all.bash script.
70 #:phases
71 (modify-phases %standard-phases
72 (delete 'configure)
73 (add-after 'patch-generated-file-shebangs 'chdir
74 (lambda _
75 (chdir "src")))
76 (add-before 'build 'prebuild
77 (lambda* (#:key inputs outputs #:allow-other-keys)
78 (let* ((gcclib (string-append (assoc-ref inputs "gcc:lib") "/lib"))
79 (ld (string-append (assoc-ref inputs "libc") "/lib"))
80 (loader (car (find-files ld "^ld-linux.+")))
81 (net-base (assoc-ref inputs "net-base"))
82 (tzdata-path
83 (string-append (assoc-ref inputs "tzdata") "/share/zoneinfo"))
84 (output (assoc-ref outputs "out")))
85
86 ;; Removing net/ tests, which fail when attempting to access
87 ;; network resources not present in the build container.
88 (for-each delete-file
89 '("net/multicast_test.go" "net/parse_test.go"
90 "net/port_test.go"))
91
92 ;; Add libgcc to the RUNPATH.
93 (substitute* "cmd/go/build.go"
94 (("cgoldflags := \\[\\]string\\{\\}")
95 (string-append "cgoldflags := []string{"
96 "\"-rpath=" gcclib "\"}"))
97 (("ldflags := buildLdflags")
98 (string-append
99 "ldflags := buildLdflags\n"
100 "ldflags = append(ldflags, \"-r\")\n"
101 "ldflags = append(ldflags, \"" gcclib "\")\n")))
102
103 (substitute* "os/os_test.go"
104 (("/usr/bin") (getcwd))
105 (("/bin/pwd") (which "pwd")))
106
107 ;; Disable failing tests: these tests attempt to access
108 ;; commands or network resources which are neither available or
109 ;; necessary for the build to succeed.
110 (for-each
111 (match-lambda
112 ((file regex)
113 (substitute* file
114 ((regex all before test_name)
115 (string-append before "Disabled" test_name)))))
116 '(("net/net_test.go" "(.+)(TestShutdownUnix.+)")
117 ("net/dial_test.go" "(.+)(TestDialTimeout.+)")
118 ("os/os_test.go" "(.+)(TestHostname.+)")
119 ("time/format_test.go" "(.+)(TestParseInSydney.+)")
120
121 ;; Tzdata 2016g changed the name of the time zone used in this
122 ;; test, and the patch for Go 1.7 does not work for 1.4.3:
123 ;; https://github.com/golang/go/issues/17545
124 ;; https://github.com/golang/go/issues/17276
125 ("time/time_test.go" "(.+)(TestLoadFixed.+)")
126 ("time/format_test.go" "(.+)(TestParseInLocation.+)")
127
128 ("os/exec/exec_test.go" "(.+)(TestEcho.+)")
129 ("os/exec/exec_test.go" "(.+)(TestCommandRelativeName.+)")
130 ("os/exec/exec_test.go" "(.+)(TestCatStdin.+)")
131 ("os/exec/exec_test.go" "(.+)(TestCatGoodAndBadFile.+)")
132 ("os/exec/exec_test.go" "(.+)(TestExitStatus.+)")
133 ("os/exec/exec_test.go" "(.+)(TestPipes.+)")
134 ("os/exec/exec_test.go" "(.+)(TestStdinClose.+)")
135 ("syscall/syscall_unix_test.go" "(.+)(TestPassFD\\(.+)")
136 ("os/exec/exec_test.go" "(.+)(TestExtraFiles.+)")))
137
138 (substitute* "net/lookup_unix.go"
139 (("/etc/protocols") (string-append net-base "/etc/protocols")))
140 (substitute* "time/zoneinfo_unix.go"
141 (("/usr/share/zoneinfo/") tzdata-path))
142 (substitute* (find-files "cmd" "asm.c")
143 (("/lib/ld-linux.*\\.so\\.[0-9]") loader))
144 #t)))
145
146 (replace 'build
147 (lambda* (#:key inputs outputs #:allow-other-keys)
148 ;; FIXME: Some of the .a files are not bit-reproducible.
149 (let* ((output (assoc-ref outputs "out")))
150 (setenv "CC" (which "gcc"))
151 (setenv "GOOS" "linux")
152 (setenv "GOROOT" (dirname (getcwd)))
153 (setenv "GOROOT_FINAL" output)
154 ;; Go 1.4's cgo will not work with binutils >= 2.27:
155 ;; https://github.com/golang/go/issues/16906
156 (setenv "CGO_ENABLED" "0")
157 (zero? (system* "sh" "all.bash")))))
158
159 (replace 'install
160 (lambda* (#:key outputs inputs #:allow-other-keys)
161 (let* ((output (assoc-ref outputs "out"))
162 (doc_out (assoc-ref outputs "doc"))
163 (bash (string-append (assoc-ref inputs "bash") "bin/bash"))
164 (docs (string-append doc_out "/share/doc/" ,name "-" ,version))
165 (tests (string-append
166 (assoc-ref outputs "tests") "/share/" ,name "-" ,version)))
167 (mkdir-p tests)
168 (copy-recursively "../test" (string-append tests "/test"))
169 (delete-file-recursively "../test")
170 (mkdir-p docs)
171 (copy-recursively "../api" (string-append docs "/api"))
172 (delete-file-recursively "../api")
173 (copy-recursively "../doc" (string-append docs "/doc"))
174 (delete-file-recursively "../doc")
175
176 (for-each (lambda (file)
177 (let ((file (string-append "../" file)))
178 (install-file file docs)
179 (delete-file file)))
180 '("README" "CONTRIBUTORS" "AUTHORS" "PATENTS"
181 "LICENSE" "VERSION" "robots.txt"))
182 (copy-recursively "../" output)
183 #t))))))
184 (inputs
185 `(("tzdata" ,tzdata)
186 ("pcre" ,pcre)
187 ("gcc:lib" ,gcc "lib")))
188 (native-inputs
189 `(("pkg-config" ,%pkg-config)
190 ("which" ,which)
191 ("net-base" ,net-base)
192 ("perl" ,perl)))
193
194 (home-page "https://golang.org/")
195 (synopsis "Compiler and libraries for Go, a statically-typed language")
196 (description "Go, also commonly referred to as golang, is an imperative
197 programming language. Designed primarily for systems programming, it is a
198 compiled, statically typed language in the tradition of C and C++, with
199 garbage collection, various safety features and in the style of communicating
200 sequential processes (CSP) concurrent programming features added.")
201 (license license:bsd-3)))
202
203 (define-public go-1.9
204 (package
205 (inherit go-1.4)
206 (name "go")
207 (version "1.9")
208 (source
209 (origin
210 (method url-fetch)
211 (uri (string-append "https://storage.googleapis.com/golang/"
212 name version ".src.tar.gz"))
213 (sha256
214 (base32
215 "14z9azh8pk5cwyl2qdk893j68lk0cca7a9b8k2hpn5pd52825ax4"))))
216 (arguments
217 (substitute-keyword-arguments (package-arguments go-1.4)
218 ((#:phases phases)
219 `(modify-phases ,phases
220 (replace 'prebuild
221 ;; TODO: Most of this could be factorized with Go 1.4.
222 (lambda* (#:key inputs outputs #:allow-other-keys)
223 (let* ((gcclib (string-append (assoc-ref inputs "gcc:lib") "/lib"))
224 (ld (string-append (assoc-ref inputs "libc") "/lib"))
225 (loader (car (find-files ld "^ld-linux.+")))
226 (net-base (assoc-ref inputs "net-base"))
227 (tzdata-path
228 (string-append (assoc-ref inputs "tzdata") "/share/zoneinfo"))
229 (output (assoc-ref outputs "out")))
230
231 ;; Removing net/ tests, which fail when attempting to access
232 ;; network resources not present in the build container.
233 (for-each delete-file
234 '("net/listen_test.go"
235 "net/parse_test.go"
236 "net/cgo_unix_test.go"))
237
238 (substitute* "os/os_test.go"
239 (("/usr/bin") (getcwd))
240 (("/bin/pwd") (which "pwd"))
241 (("/bin/sh") (which "sh")))
242
243 ;; Add libgcc to runpath
244 (substitute* "cmd/link/internal/ld/lib.go"
245 (("!rpath.set") "true"))
246 (substitute* "cmd/go/internal/work/build.go"
247 (("cgoldflags := \\[\\]string\\{\\}")
248 (string-append "cgoldflags := []string{"
249 "\"-rpath=" gcclib "\""
250 "}"))
251 (("ldflags = setextld\\(ldflags, compiler\\)")
252 (string-append
253 "ldflags = setextld(ldflags, compiler)\n"
254 "ldflags = append(ldflags, \"-r\")\n"
255 "ldflags = append(ldflags, \"" gcclib "\")\n"))
256 (("\"-lgcc_s\", ")
257 (string-append
258 "\"-Wl,-rpath=" gcclib "\", \"-lgcc_s\", ")))
259
260 ;; Disable failing tests: these tests attempt to access
261 ;; commands or network resources which are neither available
262 ;; nor necessary for the build to succeed.
263 (for-each
264 (match-lambda
265 ((file regex)
266 (substitute* file
267 ((regex all before test_name)
268 (string-append before "Disabled" test_name)))))
269 '(("net/net_test.go" "(.+)(TestShutdownUnix.+)")
270 ("net/dial_test.go" "(.+)(TestDialTimeout.+)")
271 ("os/os_test.go" "(.+)(TestHostname.+)")
272 ("time/format_test.go" "(.+)(TestParseInSydney.+)")
273 ("time/format_test.go" "(.+)(TestParseInLocation.+)")
274 ("os/exec/exec_test.go" "(.+)(TestEcho.+)")
275 ("os/exec/exec_test.go" "(.+)(TestCommandRelativeName.+)")
276 ("os/exec/exec_test.go" "(.+)(TestCatStdin.+)")
277 ("os/exec/exec_test.go" "(.+)(TestCatGoodAndBadFile.+)")
278 ("os/exec/exec_test.go" "(.+)(TestExitStatus.+)")
279 ("os/exec/exec_test.go" "(.+)(TestPipes.+)")
280 ("os/exec/exec_test.go" "(.+)(TestStdinClose.+)")
281 ("os/exec/exec_test.go" "(.+)(TestIgnorePipeErrorOnSuccess.+)")
282 ("syscall/syscall_unix_test.go" "(.+)(TestPassFD\\(.+)")
283 ("os/exec/exec_test.go" "(.+)(TestExtraFiles/areturn.+)")
284 ("cmd/go/go_test.go" "(.+)(TestCoverageWithCgo.+)")
285 ("os/exec/exec_test.go" "(.+)(TestOutputStderrCapture.+)")
286 ("os/exec/exec_test.go" "(.+)(TestExtraFiles.+)")
287 ("os/exec/exec_test.go" "(.+)(TestExtraFilesRace.+)")
288 ("net/lookup_test.go" "(.+)(TestLookupPort.+)")
289 ("syscall/exec_linux_test.go"
290 "(.+)(TestCloneNEWUSERAndRemapNoRootDisableSetgroups.+)")))
291
292 (substitute* "../misc/cgo/testsanitizers/test.bash"
293 (("(CC=)cc" all var) (string-append var "gcc")))
294
295 ;; fix shebang for testar script
296 ;; note the target script is generated at build time.
297 (substitute* "../misc/cgo/testcarchive/carchive_test.go"
298 (("#!/usr/bin/env") (string-append "#!" (which "env"))))
299
300 ;; Escape braces in test data to workaround test failure. For
301 ;; more information:
302 ;; https://github.com/golang/go/issues/20007
303 ;; FIXME: remove this once we upgrade to 1.9
304 (substitute* "cmd/vet/testdata/copylock_func.go"
305 (("struct\\{lock sync.Mutex\\}") "struct\\{lock sync.Mutex\\}"))
306
307 (substitute* "net/lookup_unix.go"
308 (("/etc/protocols") (string-append net-base "/etc/protocols")))
309 (substitute* "net/port_unix.go"
310 (("/etc/services") (string-append net-base "/etc/services")))
311 (substitute* "time/zoneinfo_unix.go"
312 (("/usr/share/zoneinfo/") tzdata-path))
313 (substitute* (find-files "cmd" "\\.go")
314 (("/lib(64)?/ld-linux.*\\.so\\.[0-9]") loader))
315 #t)))
316 (add-before 'build 'set-bootstrap-variables
317 (lambda* (#:key outputs inputs #:allow-other-keys)
318 ;; Tell the build system where to find the bootstrap Go.
319 (let ((go (assoc-ref inputs "go"))
320 (out (assoc-ref outputs "out")))
321 (setenv "GOROOT_BOOTSTRAP" go)
322 (setenv "PATH"
323 (string-append out "/bin:"
324 (dirname (getcwd)) "/bin:"
325 (getenv "PATH")))
326
327 ;; XXX: The following variables seem unrelated.
328 (setenv "GOGC" "400")
329 (setenv "GO_TEST_TIMEOUT_SCALE" "9999")
330 #t)))
331
332 (replace 'build
333 (lambda* (#:key inputs outputs #:allow-other-keys)
334 ;; FIXME: Some of the .a files are not bit-reproducible.
335 (let* ((output (assoc-ref outputs "out")))
336 (setenv "CC" (which "gcc"))
337 (setenv "GOOS" "linux")
338 (setenv "GOROOT" (dirname (getcwd)))
339 (setenv "GOROOT_FINAL" output)
340 (setenv "CGO_ENABLED" "1")
341 (zero? (system* "sh" "all.bash")))))
342
343 (replace 'install
344 ;; TODO: Most of this could be factorized with Go 1.4.
345 (lambda* (#:key outputs #:allow-other-keys)
346 (let* ((output (assoc-ref outputs "out"))
347 (doc_out (assoc-ref outputs "doc"))
348 (docs (string-append doc_out "/share/doc/" ,name "-" ,version))
349 (src (string-append
350 (assoc-ref outputs "tests") "/share/" ,name "-" ,version)))
351 (delete-file-recursively "../pkg/bootstrap")
352
353 (mkdir-p src)
354 (copy-recursively "../test" (string-append src "/test"))
355 (delete-file-recursively "../test")
356 (mkdir-p docs)
357 (copy-recursively "../api" (string-append docs "/api"))
358 (delete-file-recursively "../api")
359 (copy-recursively "../doc" (string-append docs "/doc"))
360 (delete-file-recursively "../doc")
361
362 (for-each
363 (lambda (file)
364 (let* ((filein (string-append "../" file))
365 (fileout (string-append docs "/" file)))
366 (copy-file filein fileout)
367 (delete-file filein)))
368 ;; Note the slightly different file names compared to 1.4.
369 '("README.md" "CONTRIBUTORS" "AUTHORS" "PATENTS"
370 "LICENSE" "VERSION" "CONTRIBUTING.md" "robots.txt"))
371
372 (copy-recursively "../" output))))))))
373 (native-inputs
374 `(("go" ,go-1.4)
375 ,@(package-native-inputs go-1.4)))))
376
377 (define-public go go-1.9)