1################################################################################ 2# Golang package infrastructure 3# 4# This file implements an infrastructure that eases development of package .mk 5# files for Go packages. It should be used for all packages that are written in 6# go. 7# 8# See the Buildroot documentation for details on the usage of this 9# infrastructure 10# 11# 12# In terms of implementation, this golang infrastructure requires the .mk file 13# to only specify metadata information about the package: name, version, 14# download URL, etc. 15# 16# We still allow the package .mk file to override what the different steps are 17# doing, if needed. For example, if <PKG>_BUILD_CMDS is already defined, it is 18# used as the list of commands to perform to build the package, instead of the 19# default golang behavior. The package can also define some post operation 20# hooks. 21# 22################################################################################ 23 24GO_BIN = $(HOST_DIR)/bin/go 25 26################################################################################ 27# inner-golang-package -- defines how the configuration, compilation and 28# installation of a Go package should be done, implements a few hooks to tune 29# the build process for Go specificities and calls the generic package 30# infrastructure to generate the necessary make targets 31# 32# argument 1 is the lowercase package name 33# argument 2 is the uppercase package name, including a HOST_ prefix for host 34# packages 35# argument 3 is the uppercase package name, without the HOST_ prefix for host 36# packages 37# argument 4 is the type (target or host) 38# 39################################################################################ 40 41define inner-golang-package 42 43$(2)_BUILD_OPTS += \ 44 -ldflags "$$($(2)_LDFLAGS)" \ 45 -tags "$$($(2)_TAGS)" \ 46 -trimpath \ 47 -p $(PARALLEL_JOBS) 48 49# Target packages need the Go compiler on the host. 50$(2)_DEPENDENCIES += host-go 51 52$(2)_BUILD_TARGETS ?= . 53 54# If the build target is just ".", then we assume the binary to be 55# produced is named after the package. If however, a build target has 56# been specified, we assume that the binaries to be produced are named 57# after each build target building them (below in <pkg>_BUILD_CMDS). 58ifeq ($$($(2)_BUILD_TARGETS),.) 59$(2)_BIN_NAME ?= $(1) 60endif 61 62$(2)_INSTALL_BINS ?= $(1) 63 64# Source files in Go usually use an import path resolved around 65# domain/vendor/software. We infer domain/vendor/software from the upstream URL 66# of the project. 67$(2)_SRC_DOMAIN = $$(call domain,$$($(2)_SITE)) 68$(2)_SRC_VENDOR = $$(word 1,$$(subst /, ,$$(call notdomain,$$($(2)_SITE)))) 69$(2)_SRC_SOFTWARE = $$(word 2,$$(subst /, ,$$(call notdomain,$$($(2)_SITE)))) 70 71# $(2)_GOMOD is the root Go module path for the project, inferred if not set. 72# If the go.mod file does not exist, one is written with this root path. 73$(2)_GOMOD ?= $$($(2)_SRC_DOMAIN)/$$($(2)_SRC_VENDOR)/$$($(2)_SRC_SOFTWARE) 74 75# Generate a go.mod file if it doesn't exist. Note: Go is configured 76# to use the "vendor" dir and not make network calls. 77define $(2)_GEN_GOMOD 78 if [ ! -f $$(@D)/go.mod ]; then \ 79 printf "module $$($(2)_GOMOD)\n" > $$(@D)/go.mod; \ 80 fi 81endef 82$(2)_POST_PATCH_HOOKS += $(2)_GEN_GOMOD 83 84# Build step. Only define it if not already defined by the package .mk 85# file. 86ifndef $(2)_BUILD_CMDS 87ifeq ($(4),target) 88 89ifeq ($(BR2_STATIC_LIBS),y) 90$(2)_LDFLAGS += -extldflags '-static' 91endif 92 93# Build package for target 94define $(2)_BUILD_CMDS 95 $$(foreach d,$$($(2)_BUILD_TARGETS),\ 96 cd $$(@D); \ 97 $$(HOST_GO_TARGET_ENV) \ 98 $$($(2)_GO_ENV) \ 99 $$(GO_BIN) build -v $$($(2)_BUILD_OPTS) \ 100 -o $$(@D)/bin/$$(or $$($(2)_BIN_NAME),$$(notdir $$(d))) \ 101 $$($(2)_GOMOD)/$$(d) 102 ) 103endef 104else 105# Build package for host 106define $(2)_BUILD_CMDS 107 $$(foreach d,$$($(2)_BUILD_TARGETS),\ 108 cd $$(@D); \ 109 $$(HOST_GO_HOST_ENV) \ 110 $$($(2)_GO_ENV) \ 111 $$(GO_BIN) build -v $$($(2)_BUILD_OPTS) \ 112 -o $$(@D)/bin/$$(or $$($(2)_BIN_NAME),$$(notdir $$(d))) \ 113 $$($(2)_GOMOD)/$$(d) 114 ) 115endef 116endif 117endif 118 119# Target installation step. Only define it if not already defined by the 120# package .mk file. 121ifndef $(2)_INSTALL_TARGET_CMDS 122define $(2)_INSTALL_TARGET_CMDS 123 $$(foreach d,$$($(2)_INSTALL_BINS),\ 124 $(INSTALL) -D -m 0755 $$(@D)/bin/$$(d) $$(TARGET_DIR)/usr/bin/$$(d) 125 ) 126endef 127endif 128 129# Host installation step 130ifndef $(2)_INSTALL_CMDS 131define $(2)_INSTALL_CMDS 132 $$(foreach d,$$($(2)_INSTALL_BINS),\ 133 $(INSTALL) -D -m 0755 $$(@D)/bin/$$(d) $$(HOST_DIR)/bin/$$(d) 134 ) 135endef 136endif 137 138# Call the generic package infrastructure to generate the necessary make 139# targets 140$(call inner-generic-package,$(1),$(2),$(3),$(4)) 141 142endef # inner-golang-package 143 144################################################################################ 145# golang-package -- the target generator macro for Go packages 146################################################################################ 147 148golang-package = $(call inner-golang-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target) 149host-golang-package = $(call inner-golang-package,host-$(pkgname),$(call UPPERCASE,host-$(pkgname)),$(call UPPERCASE,$(pkgname)),host) 150