void-packages/common/build-style/go.sh
Noel Cower 43db1c25dc 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]
2019-01-02 20:10:56 -08:00

48 lines
1.5 KiB
Bash

#
# This helper is for templates for Go packages.
#
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
# 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_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
}
do_build() {
go_package=${go_package:-$go_import_path}
# 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() {
find "${GOPATH}/bin" -type f -executable | while read line
do
vbin "${line}"
done
}