GO_LICENSE_FILES = LICENSE
HOST_GO_DEPENDENCIES = host-go-bootstrap
+HOST_GO_GOPATH = $(HOST_DIR)/usr/share/go-path
HOST_GO_HOST_CACHE = $(HOST_DIR)/usr/share/host-go-cache
HOST_GO_ROOT = $(HOST_DIR)/lib/go
HOST_GO_TARGET_CACHE = $(HOST_DIR)/usr/share/go-cache
# We pass an empty GOBIN, otherwise "go install: cannot install
# cross-compiled binaries when GOBIN is set"
HOST_GO_COMMON_ENV = \
- GO111MODULE=off \
+ GO111MODULE=on \
+ GOFLAGS=-mod=vendor \
GOROOT="$(HOST_GO_ROOT)" \
+ GOPATH="$(HOST_GO_GOPATH)" \
+ GOPROXY=off \
PATH=$(BR_PATH) \
GOBIN= \
CGO_ENABLED=$(HOST_GO_CGO_ENABLED)
define inner-golang-package
-$(2)_WORKSPACE ?= _gopath
-
$(2)_BUILD_OPTS += \
-ldflags "$$($(2)_LDFLAGS)" \
-tags "$$($(2)_TAGS)" \
$(2)_INSTALL_BINS ?= $(1)
-# Source files in Go should be extracted in a precise folder in the hierarchy
-# of GOPATH. It usually resolves around domain/vendor/software. By default, we
-# derive domain/vendor/software from the upstream URL of the project, but we
-# allow $(2)_SRC_SUBDIR to be overridden if needed.
+# Source files in Go usually use an import path resolved around
+# domain/vendor/software. We infer domain/vendor/software from the upstream URL
+# of the project.
$(2)_SRC_DOMAIN = $$(call domain,$$($(2)_SITE))
$(2)_SRC_VENDOR = $$(word 1,$$(subst /, ,$$(call notdomain,$$($(2)_SITE))))
$(2)_SRC_SOFTWARE = $$(word 2,$$(subst /, ,$$(call notdomain,$$($(2)_SITE))))
-$(2)_SRC_SUBDIR ?= $$($(2)_SRC_DOMAIN)/$$($(2)_SRC_VENDOR)/$$($(2)_SRC_SOFTWARE)
-$(2)_SRC_PATH = $$(@D)/$$($(2)_WORKSPACE)/src/$$($(2)_SRC_SUBDIR)
+# $(2)_GOMOD is the root Go module path for the project, inferred if not set.
+# 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)
-# Configure step. Only define it if not already defined by the package .mk
-# file.
-ifndef $(2)_CONFIGURE_CMDS
-define $(2)_CONFIGURE_CMDS
- mkdir -p $$(dir $$($(2)_SRC_PATH))
- ln -sf $$(@D) $$($(2)_SRC_PATH)
+# 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
+ if [ ! -f $$(@D)/go.mod ]; then \
+ printf "module $$($(2)_GOMOD)\n" > $$(@D)/go.mod; \
+ fi
endef
-endif
+$(2)_POST_PATCH_HOOKS += $(2)_GEN_GOMOD
# Build step. Only define it if not already defined by the package .mk
# file.
# Build package for target
define $(2)_BUILD_CMDS
$$(foreach d,$$($(2)_BUILD_TARGETS),\
- cd $$($(2)_SRC_PATH); \
+ cd $$(@D); \
$$(HOST_GO_TARGET_ENV) \
- GOPATH="$$(@D)/$$($(2)_WORKSPACE)" \
$$($(2)_GO_ENV) \
$$(GO_BIN) build -v $$($(2)_BUILD_OPTS) \
-o $$(@D)/bin/$$(or $$($(2)_BIN_NAME),$$(notdir $$(d))) \
- ./$$(d)
+ $$($(2)_GOMOD)/$$(d)
)
endef
else
# Build package for host
define $(2)_BUILD_CMDS
$$(foreach d,$$($(2)_BUILD_TARGETS),\
- cd $$($(2)_SRC_PATH); \
+ cd $$(@D); \
$$(HOST_GO_HOST_ENV) \
- GOPATH="$$(@D)/$$($(2)_WORKSPACE)" \
$$($(2)_GO_ENV) \
$$(GO_BIN) build -v $$($(2)_BUILD_OPTS) \
-o $$(@D)/bin/$$(or $$($(2)_BIN_NAME),$$(notdir $$(d))) \
- ./$$(d)
+ $$($(2)_GOMOD)/$$(d)
)
endef
endif