1 ################################################################################
2 # Golang package infrastructure
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
8 # See the Buildroot documentation for details on the usage of this
12 # In terms of implementation, this golang infrastructure requires the .mk file
13 # to only specify metadata information about the package: name, version,
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
22 ################################################################################
24 GO_BIN = $(HOST_DIR)/bin/go
26 # We pass an empty GOBIN, otherwise "go install: cannot install
27 # cross-compiled binaries when GOBIN is set"
29 $(HOST_GO_TARGET_ENV) \
32 CGO_ENABLED=$(HOST_GO_CGO_ENABLED)
34 ################################################################################
35 # inner-golang-package -- defines how the configuration, compilation and
36 # installation of a Go package should be done, implements a few hooks to tune
37 # the build process for Go specificities and calls the generic package
38 # infrastructure to generate the necessary make targets
40 # argument 1 is the lowercase package name
41 # argument 2 is the uppercase package name, including a HOST_ prefix for host
43 # argument 3 is the uppercase package name, without the HOST_ prefix for host
45 # argument 4 is the type (target or host)
47 # NOTE Only type target is supported at the moment
48 ################################################################################
50 define inner-golang-package
52 $(2)_WORKSPACE ?= _gopath
54 ifeq ($(BR2_STATIC_LIBS),y)
55 $(2)_LDFLAGS += -extldflags '-static'
58 $(2)_BUILD_OPTS += -ldflags "$$($(2)_LDFLAGS)"
59 $(2)_BUILD_OPTS += -tags "$$($(2)_TAGS)"
61 # Target packages need the Go compiler on the host.
62 $(2)_DEPENDENCIES += host-go
64 $(2)_BUILD_TARGETS ?= .
66 # If the build target is just ".", then we assume the binary to be
67 # produced is named after the package. If however, a build target has
68 # been specified, we assume that the binaries to be produced are named
69 # after each build target building them (below in <pkg>_BUILD_CMDS).
70 ifeq ($$($(2)_BUILD_TARGETS),.)
74 $(2)_INSTALL_BINS ?= $(1)
76 # Source files in Go should be extracted in a precise folder in the hierarchy
77 # of GOPATH. It usually resolves around domain/vendor/software. By default, we
78 # derive domain/vendor/software from the upstream URL of the project, but we
79 # allow $(2)_SRC_SUBDIR to be overridden if needed.
80 $(2)_SRC_DOMAIN = $$(call domain,$($(2)_SITE))
81 $(2)_SRC_VENDOR = $$(word 1,$$(subst /, ,$$(call notdomain,$($(2)_SITE))))
82 $(2)_SRC_SOFTWARE = $$(word 2,$$(subst /, ,$$(call notdomain,$($(2)_SITE))))
84 $(2)_SRC_SUBDIR ?= $$($(2)_SRC_DOMAIN)/$$($(2)_SRC_VENDOR)/$$($(2)_SRC_SOFTWARE)
85 $(2)_SRC_PATH = $$(@D)/$$($(2)_WORKSPACE)/src/$$($(2)_SRC_SUBDIR)
87 # Configure step. Only define it if not already defined by the package .mk
89 ifndef $(2)_CONFIGURE_CMDS
90 define $(2)_CONFIGURE_CMDS
91 mkdir -p $$(dir $$($(2)_SRC_PATH))
92 ln -sf $$(@D) $$($(2)_SRC_PATH)
96 # Build step. Only define it if not already defined by the package .mk
98 ifndef $(2)_BUILD_CMDS
99 define $(2)_BUILD_CMDS
100 $$(foreach d,$$($(2)_BUILD_TARGETS),\
101 cd $$($(2)_SRC_PATH); \
103 GOPATH="$$(@D)/$$($(2)_WORKSPACE)" \
105 $$(GO_BIN) build -v $$($(2)_BUILD_OPTS) \
106 -o $$(@D)/bin/$$(or $$($(2)_BIN_NAME),$$(notdir $$(d))) \
112 # Target installation step. Only define it if not already defined by the
114 ifndef $(2)_INSTALL_TARGET_CMDS
115 define $(2)_INSTALL_TARGET_CMDS
116 $$(foreach d,$$($(2)_INSTALL_BINS),\
117 $(INSTALL) -D -m 0755 $$(@D)/bin/$$(d) $(TARGET_DIR)/usr/bin/$$(d)
122 # Call the generic package infrastructure to generate the necessary make
124 $(call inner-generic-package,$(1),$(2),$(3),$(4))
126 endef # inner-golang-package
128 ################################################################################
129 # golang-package -- the target generator macro for Go packages
130 ################################################################################
132 golang-package = $(call inner-golang-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)