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