Subject: [PATCH] gnu: go-1.16: Clean up installation location and logic.
From: Sarah Morgensen <[email-masked]>
To: [email-masked]
Date: Sat, 17 Jul 2021 20:53:36 -0700
List-Id: <debbugs-submit.debbugs.gnu.org>

* gnu/packages/golang.scm (go-1.16)[arguments]<#:tests>: Fix formatting.
<#:strip-directories>: Avoid stripping standard library which breaks it.
<#:phases>{install}: Install to out/lib/go instead of out.
{build}: Use the new location.
{reset-cwd}: Chdir back to source root before install-license-files.
---
Hello Guix,

Currently the "out" output of Go looks like...

/gnu/store/...-go-1.16.5
|-> bin          [binaries]
|-> lib          [host-independent timezone data]
|-> misc         [various arch-independent resources]
|-> pkg          [compiled Go std library]
|-> src          [Go compiler and std library source]

...which means that after installing Go, all of those directories are polluting
~/.guix-profile. Unfortunately, Go does not have any standardized installation
process, and it expects all these directories to be siblings (yes, it needs its
own source to work). Its install instructions suggest installing everything in
/usr/local/go; Debian installs in /usr/lib/go-X.Y and /usr/share/go-X.Y, with
symlinks in /usr/lib/go-X.Y for anything in /usr/share/go-X.Y.

We could install source to e.g. /share/go (or even in its own output), but we
would still require a symlink like Debian so that Go sees it in its main tree.

(Tests (6MB) and docs (7MB) are currently installed in separate directories in
separate outputs, which makes them difficult to actually use.)

This patch is fairly conservative, and just moves everything to "/lib/go":

/gnu/store/...-go-1.16.5
|-> bin                  [symlinks to "lib/go/bin"]
|-> lib
    |-> go
        |-> bin          [binaries]
        |-> lib          [host-independent timezone data]
        |-> misc         [various arch-independent resources]
        |-> pkg          [compiled Go std library]
        |-> src          [Go compiler and std library source]

But if it were up to me, I'd chuck it all in "out" and symlink like
Debian. WDYT?

--
Sarah

 gnu/packages/golang.scm | 48 ++++++++++++++++++++++++++++++++++++++---
 1 file changed, 45 insertions(+), 3 deletions(-)

diff --git a/gnu/packages/golang.scm b/gnu/packages/golang.scm
index f38f307392..77df7bfbf4 100644
--- a/gnu/packages/golang.scm
+++ b/gnu/packages/golang.scm
@@ -1405,7 +1405,8 @@ in the style of communicating sequential processes (@dfn{CSP}).")
          "19a93p217h5xi2sgh34qzv24pkd4df0sw4fc5z6k47lspjp3vx2l"))))
     (arguments
      (substitute-keyword-arguments (package-arguments go-1.14)
-       ((#:tests? _) #t)
+       ((#:tests? _ #t) #t)
+       ((#:strip-directories _ '()) ''("lib/go/pkg/tool" "lib/go/bin"))
        ((#:phases phases)
         `(modify-phases ,phases
            (add-after 'unpack 'remove-unused-sourcecode-generators
@@ -1523,7 +1524,7 @@ in the style of communicating sequential processes (@dfn{CSP}).")
                  (setenv "GO_LDSO" loader)
                  (setenv "GOOS" "linux")
                  (setenv "GOROOT" (dirname (getcwd)))
-                 (setenv "GOROOT_FINAL" output)
+                 (setenv "GOROOT_FINAL" (string-append output "/lib/go"))
                  (setenv "GOCACHE" "/tmp/go-cache")
                  (invoke "sh" "make.bash" "--no-banner"))))
            (replace 'check
@@ -1537,7 +1538,48 @@ in the style of communicating sequential processes (@dfn{CSP}).")
              (lambda _
                ;; Rewrite references to perl input in test scripts
                (substitute* "net/http/cgi/testdata/test.cgi"
-                 (("^#!.*") "#!/usr/bin/env perl\n"))))))))
+                 (("^#!.*") "#!/usr/bin/env perl\n"))))
+           (replace 'install
+             ;; TODO: Most of this could be factorized with Go 1.4.
+             (lambda* (#:key outputs #:allow-other-keys)
+               (let* ((out (assoc-ref outputs "out"))
+                      (tests (assoc-ref outputs "tests"))
+                      (out-lib (string-append out "/lib/go"))
+                      (tests-share (string-append tests "/share/go"))
+                      (docs (string-append (assoc-ref outputs "doc")
+                                          "/share/doc/go-"
+                                          ,(package-version this-package))))
+                 ;; Prevent installation of the build cache, which contains
+                 ;; store references to most of the tools used to build Go and
+                 ;; would unnecessarily increase the size of Go's closure if it
+                 ;; was installed.
+                 (delete-file-recursively "../pkg/obj")
+
+                 (install-file "../VERSION" out-lib)
+                 (copy-recursively "../pkg" (string-append out-lib "/pkg"))
+                 (copy-recursively "../src" (string-append out-lib "/src"))
+                 (copy-recursively "../bin" (string-append out-lib "/bin"))
+                 (copy-recursively "../lib" (string-append out-lib "/lib"))
+                 (copy-recursively "../misc" (string-append out-lib "/misc"))
+
+                 (mkdir-p (string-append out "/bin"))
+                 (with-directory-excursion (string-append out "/bin")
+                   (symlink "../lib/go/bin/go"    "go")
+                   (symlink "../lib/go/bin/gofmt" "gofmt"))
+
+                 (mkdir-p tests-share)
+                 (copy-recursively "../test" (string-append tests-share "/test"))
+                 (copy-recursively "../api" (string-append tests-share "/api"))
+
+                 (for-each
+                  (lambda (file) (install-file (string-append "../" file) docs))
+                  ;; Note the slightly different file names compared to 1.4.
+                  '("AUTHORS" "CONTRIBUTING.md" "CONTRIBUTORS" "PATENTS"
+                    "README.md" "SECURITY.md" "favicon.ico" "robots.txt"))
+                 (copy-recursively "../doc" (string-append docs "/doc")))))
+           (add-before 'install-license-files 'reset-cwd
+             (lambda _
+               (chdir "..")))))))
     (native-inputs
      `(("go-fix-script-tests.patch" ,(search-patch "go-fix-script-tests.patch"))
        ,@(if (not (member (%current-system) (package-supported-systems go-1.4)))

base-commit: 9cb35c02164d929fcb8929e7f454df215df8cf25
-- 
2.31.1