gnu: Add nxbelld.
[guix.git] / gnu / packages / golang.scm
blobb8f86ac5fd1c1ae16363c138a11580b1436b27eb
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016, 2017 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/>.
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))
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.
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")))
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"))
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")))
103                (substitute* "os/os_test.go"
104                  (("/usr/bin") (getcwd))
105                  (("/bin/pwd") (which "pwd")))
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.+)")
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.+)")
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.+)")))
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)))
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")))))
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")
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)))
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     (supported-systems '("x86_64-linux" "i686-linux" "armhf-linux"))
202     (license license:bsd-3)))
204 (define-public go-1.9
205   (package
206     (inherit go-1.4)
207     (name "go")
208     (version "1.9.2")
209     (source
210      (origin
211        (method url-fetch)
212        (uri (string-append "https://storage.googleapis.com/golang/"
213                            name version ".src.tar.gz"))
214        (sha256
215         (base32
216          "1p23n4xzbknl3bbhlckbvxbhpxknd5rn0i2szmn9i2dcz15ihpv6"))))
217     (arguments
218      (substitute-keyword-arguments (package-arguments go-1.4)
219        ((#:phases phases)
220         `(modify-phases ,phases
221            (replace 'prebuild
222              ;; TODO: Most of this could be factorized with Go 1.4.
223              (lambda* (#:key inputs outputs #:allow-other-keys)
224                (let* ((gcclib (string-append (assoc-ref inputs "gcc:lib") "/lib"))
225                       (ld (string-append (assoc-ref inputs "libc") "/lib"))
226                       (loader (car (find-files ld "^ld-linux.+")))
227                       (net-base (assoc-ref inputs "net-base"))
228                       (tzdata-path
229                        (string-append (assoc-ref inputs "tzdata") "/share/zoneinfo"))
230                       (output (assoc-ref outputs "out")))
232                  ;; Removing net/ tests, which fail when attempting to access
233                  ;; network resources not present in the build container.
234                  (for-each delete-file
235                            '("net/listen_test.go"
236                              "net/parse_test.go"
237                              "net/cgo_unix_test.go"))
239                  (substitute* "os/os_test.go"
240                    (("/usr/bin") (getcwd))
241                    (("/bin/pwd") (which "pwd"))
242                    (("/bin/sh") (which "sh")))
244                  ;; Add libgcc to runpath
245                  (substitute* "cmd/link/internal/ld/lib.go"
246                    (("!rpath.set") "true"))
247                  (substitute* "cmd/go/internal/work/build.go"
248                    (("cgoldflags := \\[\\]string\\{\\}")
249                     (string-append "cgoldflags := []string{"
250                                    "\"-rpath=" gcclib "\""
251                                    "}"))
252                    (("ldflags = setextld\\(ldflags, compiler\\)")
253                     (string-append
254                      "ldflags = setextld(ldflags, compiler)\n"
255                      "ldflags = append(ldflags, \"-r\")\n"
256                      "ldflags = append(ldflags, \"" gcclib "\")\n"))
257                    (("\"-lgcc_s\", ")
258                     (string-append
259                      "\"-Wl,-rpath=" gcclib "\", \"-lgcc_s\", ")))
261                  ;; Disable failing tests: these tests attempt to access
262                  ;; commands or network resources which are neither available
263                  ;; nor necessary for the build to succeed.
264                  (for-each
265                   (match-lambda
266                     ((file regex)
267                      (substitute* file
268                        ((regex all before test_name)
269                         (string-append before "Disabled" test_name)))))
270                   '(("net/net_test.go" "(.+)(TestShutdownUnix.+)")
271                     ("net/dial_test.go" "(.+)(TestDialTimeout.+)")
272                     ("os/os_test.go" "(.+)(TestHostname.+)")
273                     ("time/format_test.go" "(.+)(TestParseInSydney.+)")
274                     ("time/format_test.go" "(.+)(TestParseInLocation.+)")
275                     ("os/exec/exec_test.go" "(.+)(TestEcho.+)")
276                     ("os/exec/exec_test.go" "(.+)(TestCommandRelativeName.+)")
277                     ("os/exec/exec_test.go" "(.+)(TestCatStdin.+)")
278                     ("os/exec/exec_test.go" "(.+)(TestCatGoodAndBadFile.+)")
279                     ("os/exec/exec_test.go" "(.+)(TestExitStatus.+)")
280                     ("os/exec/exec_test.go" "(.+)(TestPipes.+)")
281                     ("os/exec/exec_test.go" "(.+)(TestStdinClose.+)")
282                     ("os/exec/exec_test.go" "(.+)(TestIgnorePipeErrorOnSuccess.+)")
283                     ("syscall/syscall_unix_test.go" "(.+)(TestPassFD\\(.+)")
284                     ("os/exec/exec_test.go" "(.+)(TestExtraFiles/areturn.+)")
285                     ("cmd/go/go_test.go" "(.+)(TestCoverageWithCgo.+)")
286                     ("os/exec/exec_test.go" "(.+)(TestOutputStderrCapture.+)")
287                     ("os/exec/exec_test.go" "(.+)(TestExtraFiles.+)")
288                     ("os/exec/exec_test.go" "(.+)(TestExtraFilesRace.+)")
289                     ("net/lookup_test.go" "(.+)(TestLookupPort.+)")
290                     ("syscall/exec_linux_test.go"
291                      "(.+)(TestCloneNEWUSERAndRemapNoRootDisableSetgroups.+)")))
293                  (substitute* "../misc/cgo/testsanitizers/test.bash"
294                    (("(CC=)cc" all var) (string-append var "gcc")))
296                  ;; fix shebang for testar script
297                  ;; note the target script is generated at build time.
298                  (substitute* "../misc/cgo/testcarchive/carchive_test.go"
299                    (("#!/usr/bin/env") (string-append "#!" (which "env"))))
301                  ;; Escape braces in test data to workaround test failure. For
302                  ;; more information:
303                  ;; https://github.com/golang/go/issues/20007
304                  ;; FIXME: remove this once we upgrade to 1.9
305                  (substitute* "cmd/vet/testdata/copylock_func.go"
306                    (("struct\\{lock sync.Mutex\\}") "struct\\{lock sync.Mutex\\}"))
308                  (substitute* "net/lookup_unix.go"
309                    (("/etc/protocols") (string-append net-base "/etc/protocols")))
310                  (substitute* "net/port_unix.go"
311                    (("/etc/services") (string-append net-base "/etc/services")))
312                  (substitute* "time/zoneinfo_unix.go"
313                    (("/usr/share/zoneinfo/") tzdata-path))
314                  (substitute* (find-files "cmd" "\\.go")
315                    (("/lib(64)?/ld-linux.*\\.so\\.[0-9]") loader))
316                  #t)))
317            (add-before 'build 'set-bootstrap-variables
318              (lambda* (#:key outputs inputs #:allow-other-keys)
319                ;; Tell the build system where to find the bootstrap Go.
320                (let ((go  (assoc-ref inputs "go"))
321                      (out (assoc-ref outputs "out")))
322                  (setenv "GOROOT_BOOTSTRAP" go)
323                  (setenv "PATH"
324                          (string-append out "/bin:"
325                                         (dirname (getcwd)) "/bin:"
326                                         (getenv "PATH")))
328                  ;; XXX: The following variables seem unrelated.
329                  (setenv "GOGC" "400")
330                  (setenv "GO_TEST_TIMEOUT_SCALE" "9999")
331                  #t)))
333            (replace 'build
334              (lambda* (#:key inputs outputs #:allow-other-keys)
335                ;; FIXME: Some of the .a files are not bit-reproducible.
336                (let* ((output (assoc-ref outputs "out")))
337                  (setenv "CC" (which "gcc"))
338                  (setenv "GOOS" "linux")
339                  (setenv "GOROOT" (dirname (getcwd)))
340                  (setenv "GOROOT_FINAL" output)
341                  (setenv "CGO_ENABLED" "1")
342                  (zero? (system* "sh" "all.bash")))))
344            (replace 'install
345              ;; TODO: Most of this could be factorized with Go 1.4.
346              (lambda* (#:key outputs #:allow-other-keys)
347                (let* ((output (assoc-ref outputs "out"))
348                       (doc_out (assoc-ref outputs "doc"))
349                       (docs (string-append doc_out "/share/doc/" ,name "-" ,version))
350                       (src (string-append
351                             (assoc-ref outputs "tests") "/share/" ,name "-" ,version)))
352                  (delete-file-recursively "../pkg/bootstrap")
354                  (mkdir-p src)
355                  (copy-recursively "../test" (string-append src "/test"))
356                  (delete-file-recursively "../test")
357                  (mkdir-p docs)
358                  (copy-recursively "../api" (string-append docs "/api"))
359                  (delete-file-recursively "../api")
360                  (copy-recursively "../doc" (string-append docs "/doc"))
361                  (delete-file-recursively "../doc")
363                  (for-each
364                   (lambda (file)
365                     (let* ((filein (string-append "../" file))
366                            (fileout (string-append docs "/" file)))
367                       (copy-file filein fileout)
368                       (delete-file filein)))
369                   ;; Note the slightly different file names compared to 1.4.
370                   '("README.md" "CONTRIBUTORS" "AUTHORS" "PATENTS"
371                     "LICENSE" "VERSION" "CONTRIBUTING.md" "robots.txt"))
373                  (copy-recursively "../" output))))))))
374     (native-inputs
375      `(("go" ,go-1.4)
376        ,@(package-native-inputs go-1.4)))
377     (supported-systems %supported-systems)))
379 (define-public go go-1.9)