Skip to content

Commit

Permalink
support/download/go-post-process: implement Go vendoring support
Browse files Browse the repository at this point in the history
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 <thomas.petazzoni@bootlin.com>

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 <christian@paral.in>

Signed-off-by: Christian Stewart <christian@paral.in>
  • Loading branch information
tpetazzoni authored and paralin committed Nov 25, 2020
1 parent 626fc0a commit 5f31774
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
1 change: 1 addition & 0 deletions package/pkg-download.mk
Expand Up @@ -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) \
-- \
Expand Down
8 changes: 7 additions & 1 deletion package/pkg-golang.mk
Expand Up @@ -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 ?= .

Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions support/download/dl-wrapper
Expand Up @@ -17,19 +17,20 @@
# 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
local backend output hfile recurse quiet rc
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}";;
Expand Down Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions 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}

0 comments on commit 5f31774

Please sign in to comment.