unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
blob 80a43a6a6089f2b0c60485c7c046b81260044831 6421 bytes (raw)
name: guix/build/go-mod-build-system.scm 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
 
;;; GNU Guix --- Functional package management for GNU
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (guix build go-mod-build-system)
  #:use-module ((guix build gnu-build-system) #:prefix gnu:)
  #:use-module (guix build utils)
  #:use-module (ice-9 match)
  #:export (%standard-phases
            go-build))

;; Commentary:
;;
;; Build procedures for Go packages, using go modules.  This is the
;; builder-side code.
;;
;; Software written in Go is either a 'package' (i.e. library) or 'command'
;; (i.e. executable).  The module approach is currently heavily biased towards
;; building executables.
;;
;; Unlike the go build system, this builder does not rely on the workspace
;; or GOPATH, but instead assumes all modules are a part of the input source
;; (otherwise, go build tries to download it which would fail).  Go projects
;; rigidly specify dependencies which is handled by the sources being resolved
;; and downloaded together.  The compiler is fast enough that building
;; everything from source on a per-package basis is not a great speed loss,
;; and the benefit from precompiling libraries is reduced by go statically
;; linking everything anyway.
;;
;; TODO:
;; * Re-use compiled packages
;;
;; Code:

(define* (setup-go-environment #:key inputs outputs import-path goos goarch
                               #:allow-other-keys)
  "Prepare a Go build environment.  We need to tell go to use the specific
toolchain even if a module specifies a (slightly) newer one.  We must also
tell the go build command where to find downloaded packages (go/pkg) and
where executables (\"commands\") are installed to."
  (let* ((mod-cache (string-append (getcwd) "/go/pkg"))
        (src-dir (string-append (getcwd) "/source"))
        (go-dir (assoc-ref inputs "go"))
        (out-dir (assoc-ref outputs "out")))

    ;; TODO: Get toolchain from the program itself or package.version, not the
    ;; store path
    (setenv "GOTOOLCHAIN" (string-delete #\- (strip-store-file-name go-dir)))
    (setenv "GOMODCACHE" mod-cache)
    (setenv "GOCACHE" (string-append (getcwd) "/go/cache"))
    (setenv "GOBIN" (string-append out-dir "/bin"))
    (setenv "GO111MODULE" "on")
    (setenv "GOARCH" (or goarch (getenv "GOHOSTARCH")))
    (setenv "GOOS" (or goos (getenv "GOHOSTOS")))
    (match goarch
      ("arm"
       (setenv "GOARM" "7"))
      ((or "mips" "mipsel")
       (setenv "GOMIPS" "hardfloat"))
      ((or "mips64" "mips64le")
       (setenv "GOMIPS64" "hardfloat"))
      ((or "ppc64" "ppc64le")
       (setenv "GOPPC64" "power8"))
      (_ #t))))

(define* (build #:key import-path build-flags (parallel-build? #t)
                #:allow-other-keys)
  "Build the package named by IMPORT-PATH."
  (let* ((njobs (if parallel-build? (parallel-job-count) 1)))
    (setenv "GOMAXPROCS" (number->string njobs)))

  (with-throw-handler
    #t
    (lambda _
      ;; TODO: This should maybe support list to install multiple commands
      ;; from the same project in the same package
      (with-directory-excursion (string-append "source/" import-path)
        (apply invoke "go" "build"
               "-v" ; print the name of packages as they are compiled
               "-x" ; print each command as it is invoked
               ;; Respectively, strip the symbol table and debug
               ;; information, and the DWARF symbol table.
               "-ldflags=-s -w"
               `(,@build-flags))))
    (lambda (key . args)
      (display (string-append "Building '" import-path "' failed.\n"
                              "Here are the results of `go env`:\n"))
      (invoke "go" "env"))))

(define* (check #:key tests? import-path (parallel-tests? #t)
                #:allow-other-keys)
  "Run the tests for the package named by IMPORT-PATH."
  (when tests?
    (let* ((njobs (if parallel-tests? (parallel-job-count) 1)))
      (setenv "GOMAXPROCS" (number->string njobs)))
    (with-directory-excursion (string-append "source/" import-path)
      (invoke "go" "test")))
  #t)

(define* (install #:key install-source? source outputs import-path
                  #:allow-other-keys)
  (with-directory-excursion (string-append "source/" import-path)
    (display "INSTALLING PROGRAM\n")
    (invoke "go" "install"
               "-v" ; print the name of packages as they are compiled
               "-x" ; print each command as it is invoked
               ;; Respectively, strip the symbol table and debug
               ;; information, and the DWARF symbol table.
               "-ldflags=-s -w"))

  ;; TODO: This is probably less interesting when using the go-mod builder
  (when install-source?
    (let* ((out (assoc-ref outputs "out"))
           (src (string-append source "/source"))
           (dest (string-append out "/src")))
      (mkdir-p dest)
      (copy-recursively src dest #:keep-mtime? #t)))
  #t)

(define* (install-license-files #:rest args)
  "Install license files matching LICENSE-FILE-REGEXP to 'share/doc'.  Adjust
the standard install-license-files phase to first enter the correct directory."
  (with-directory-excursion "source"
    (apply (assoc-ref gnu:%standard-phases 'install-license-files) args)))


(define %standard-phases
  (modify-phases gnu:%standard-phases
    (delete 'bootstrap)
    (delete 'configure)
    (delete 'patch-generated-file-shebangs)
    (add-before 'build 'setup-go-environment setup-go-environment)
    (replace 'build build)
    (replace 'check check)
    (replace 'install install)
    (replace 'install-license-files install-license-files)))

(define* (go-build #:key inputs (phases %standard-phases)
                      #:allow-other-keys #:rest args)
  "Build the given Go package, applying all of PHASES in order."
  (apply gnu:gnu-build #:inputs inputs #:phases phases args))

debug log:

solving 80a43a6a60 ...
found 80a43a6a60 in https://yhetil.org/guix-patches/20241115211106.2759121-3-j@lambda.is/

applying [1/1] https://yhetil.org/guix-patches/20241115211106.2759121-3-j@lambda.is/
diff --git a/guix/build/go-mod-build-system.scm b/guix/build/go-mod-build-system.scm
new file mode 100644
index 0000000000..80a43a6a60

Checking patch guix/build/go-mod-build-system.scm...
Applied patch guix/build/go-mod-build-system.scm cleanly.

index at:
100644 80a43a6a6089f2b0c60485c7c046b81260044831	guix/build/go-mod-build-system.scm

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).