From 5f31774f54402f07c1d83aa407430db5152e6de3 Mon Sep 17 00:00:00 2001 From: Thomas Petazzoni Date: Thu, 19 Nov 2020 22:36:50 +0100 Subject: [PATCH] support/download/go-post-process: implement Go vendoring support This commit introduces the download post-process script support/download/go-post-process, and hooks it into the Go package infrastructure. Signed-off-by: Thomas Petazzoni Skiff fixup: - run "go mod init" just before "go mod vendor" - this fixes the case when go.mod does not exist - use -modcacherw to fix "make clean" permissions errors - proposal was e-mailed to the buildroot mailing list Signed-off-by: Christian Stewart Signed-off-by: Christian Stewart --- package/pkg-download.mk | 1 + package/pkg-golang.mk | 8 +++++++- support/download/dl-wrapper | 6 ++++-- support/download/go-post-process | 35 ++++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100755 support/download/go-post-process diff --git a/package/pkg-download.mk b/package/pkg-download.mk index c914d016e28..29462ebaa43 100644 --- a/package/pkg-download.mk +++ b/package/pkg-download.mk @@ -112,6 +112,7 @@ define DOWNLOAD -o '$($(2)_DL_DIR)/$(notdir $(1))' \ $(if $($(2)_DOWNLOAD_POST_PROCESS),-p '$($(2)_DOWNLOAD_POST_PROCESS)') \ $(if $($(2)_GIT_SUBMODULES),-r) \ + $(if $($(2)_GOMOD),-g '$($(2)_GOMOD)') \ $(foreach uri,$(call DOWNLOAD_URIS,$(1),$(2)),-u $(uri)) \ $(QUIET) \ -- \ diff --git a/package/pkg-golang.mk b/package/pkg-golang.mk index 3813e1c4061..b22720add6a 100644 --- a/package/pkg-golang.mk +++ b/package/pkg-golang.mk @@ -42,12 +42,13 @@ define inner-golang-package $(2)_BUILD_OPTS += \ -ldflags "$$($(2)_LDFLAGS)" \ + -modcacherw \ -tags "$$($(2)_TAGS)" \ -trimpath \ -p $(PARALLEL_JOBS) # Target packages need the Go compiler on the host. -$(2)_DEPENDENCIES += host-go +$(2)_DOWNLOAD_DEPENDENCIES += host-go $(2)_BUILD_TARGETS ?= . @@ -72,6 +73,11 @@ $(2)_SRC_SOFTWARE = $$(word 2,$$(subst /, ,$$(call notdomain,$$($(2)_SITE)))) # If the go.mod file does not exist, one is written with this root path. $(2)_GOMOD ?= $$($(2)_SRC_DOMAIN)/$$($(2)_SRC_VENDOR)/$$($(2)_SRC_SOFTWARE) +$(2)_DOWNLOAD_POST_PROCESS = go +$(2)_DL_ENV = \ + $(HOST_GO_COMMON_ENV) \ + GOPROXY=direct + # Generate a go.mod file if it doesn't exist. Note: Go is configured # to use the "vendor" dir and not make network calls. define $(2)_GEN_GOMOD diff --git a/support/download/dl-wrapper b/support/download/dl-wrapper index 2d74554213a..2fc530f24f9 100755 --- a/support/download/dl-wrapper +++ b/support/download/dl-wrapper @@ -17,7 +17,7 @@ # We want to catch any unexpected failure, and exit immediately. set -e -export BR_BACKEND_DL_GETOPTS=":hc:d:o:n:N:H:ru:qf:e" +export BR_BACKEND_DL_GETOPTS=":hc:d:g:o:n:N:H:ru:qf:e" main() { local OPT OPTARG @@ -25,11 +25,12 @@ main() { local -a uris # Parse our options; anything after '--' is for the backend - while getopts ":c:d:D:o:n:N:H:rf:u:qp:" OPT; do + while getopts ":c:d:D:g:o:n:N:H:rf:u:qp:" OPT; do case "${OPT}" in c) cset="${OPTARG}";; d) dl_dir="${OPTARG}";; D) old_dl_dir="${OPTARG}";; + g) gomod_init="${OPTARG}";; o) output="${OPTARG}";; n) raw_base_name="${OPTARG}";; N) base_name="${OPTARG}";; @@ -138,6 +139,7 @@ main() { if [ -n "${post_process}" ] ; then ${OLDPWD}/support/download/${post_process}-post-process \ + -g "${gomod_init}" \ -o "${tmpf}" \ -n "${raw_base_name}" fi diff --git a/support/download/go-post-process b/support/download/go-post-process new file mode 100755 index 00000000000..1e5441f6e6c --- /dev/null +++ b/support/download/go-post-process @@ -0,0 +1,35 @@ +#!/usr/bin/env bash + +set -e + +. $(dirname $0)/post-process-helpers + +# Parse our options +while getopts "n:g:o:" OPT; do + case "${OPT}" in + g) gomod_init="${OPTARG}";; + o) output="${OPTARG}";; + n) base_name="${OPTARG}";; + :) error "option '%s' expects a mandatory argument\n" "${OPTARG}";; + \?) error "unknown option '%s'\n" "${OPTARG}";; + esac +done + +# Already vendored tarball, nothing to do +if tar tf ${output} | grep -q "^[^/]*/vendor" ; then + exit 0 +fi + +unpack ${base_name} ${output} + +# Do the Go vendoring +pushd ${base_name} > /dev/null +# modcacherw option leaves directories in the module cache at their default +# permissions rather than making them read-only. +if [ ! -f go.mod ] && [ -n "${gomod_init}" ]; then + go mod init -modcacherw ${gomod_init} +fi +go mod vendor -modcacherw -v +popd > /dev/null + +repack ${base_name} ${output}