From 43db1c25dcf0247f714fa465067c74ab3b265b39 Mon Sep 17 00:00:00 2001 From: Noel Cower Date: Wed, 19 Dec 2018 17:01:04 -0800 Subject: [PATCH] build-styles: Add support for Go modules. Adds support for Go modules by detecting a go.mod file and, if available, using it. In addition, to continue supporting vendoring (and avoid the need for git on all module builds), if a package includes a vendor directory, the module mode will switch to vendor mode if not already set. This will use vendored source code for dependencies instead of downloading that code again using the descriptions under go.mod. go_mod_mode=vendor also skips the go.sum check because nothing is downloaded that isn't already verified, so this fixes packages with vendored code that have checksum mismatches due to Go 1.11.4 module checksum changes. [ci skip] Closes: #6036 [via git-merge-pr] --- Manual.md | 4 ++++ common/build-style/go.sh | 22 ++++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Manual.md b/Manual.md index 902b1defdf5..f39df1611b3 100644 --- a/Manual.md +++ b/Manual.md @@ -1324,6 +1324,10 @@ The following variables influence how Go packages are built: packages; using a versioned distfile is preferred. - `go_build_tags`: An optional, space-separated list of build tags to pass to Go. +- `go_mod_mode`: The module download mode to use. May be `off` to ignore + any go.mod files, `default` to use Go's default behavior, or anything + accepted by `go build -mod MODE`. Defaults to `vendor` if there's + a vendor directory, otherwise `default`. Occasionally it is necessary to perform operations from within the Go source tree. This is usually needed by programs using go-bindata or diff --git a/common/build-style/go.sh b/common/build-style/go.sh index e148417689d..a1f74958e8a 100644 --- a/common/build-style/go.sh +++ b/common/build-style/go.sh @@ -4,6 +4,7 @@ do_configure() { # $go_import_path must be set, or we can't link $PWD into $GOSRCPATH + # nor build from modules if [ -z "$go_import_path" ]; then msg_error "\"\$go_import_path\" not set on $pkgname template.\n" fi @@ -11,7 +12,10 @@ do_configure() { # This isn't really configuration, but its needed by packages # that do unusual things with the build where the expect to be # able to cd into the $GOSRCPATH - if [[ "${go_get}" != "yes" ]]; then + if [[ "${go_mod_mode}" != "off" ]] && [[ -f go.mod ]]; then + # Skip GOPATH symlink for Go modules + msg_normal "Building $pkgname using Go modules.\n" + elif [[ "${go_get}" != "yes" ]]; then mkdir -p "$(dirname ${GOSRCPATH})" ln -fs $PWD "${GOSRCPATH}" fi @@ -19,7 +23,21 @@ do_configure() { do_build() { go_package=${go_package:-$go_import_path} - go get -x -tags "${go_build_tags}" -ldflags "${go_ldflags}" ${go_package} + # Build using Go modules if there's a go.mod file + if [[ "${go_mod_mode}" != "off" ]] && [[ -f go.mod ]]; then + if [[ -z "${go_mod_mode}" ]] && [[ -d vendor ]]; then + msg_normal "Using vendor dir for $pkgname Go dependencies.\n" + go_mod_mode=vendor + elif [[ "${go_mod_mode}" = "default" ]]; then + # Allow templates to explicitly opt into the go tool's + # default behavior. + go_mod_mode= + fi + go build -o "${GOPATH}/bin/$(basename ${go_package})" -mod="${go_mod_mode}" -x -tags "${go_build_tags}" -ldflags "${go_ldflags}" ${go_package} + else + # Otherwise, build using GOPATH + go get -x -tags "${go_build_tags}" -ldflags "${go_ldflags}" ${go_package} + fi } do_install() {