-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathpkg-golang.mk
More file actions
209 lines (183 loc) · 7.07 KB
/
pkg-golang.mk
File metadata and controls
209 lines (183 loc) · 7.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
################################################################################
# Golang package infrastructure
#
# This file implements an infrastructure that eases development of package .mk
# files for Go packages. It should be used for all packages that are written in
# go.
#
# See the Buildroot documentation for details on the usage of this
# infrastructure
#
#
# In terms of implementation, this golang infrastructure requires the .mk file
# to only specify metadata information about the package: name, version,
# download URL, etc.
#
# We still allow the package .mk file to override what the different steps are
# doing, if needed. For example, if <PKG>_BUILD_CMDS is already defined, it is
# used as the list of commands to perform to build the package, instead of the
# default golang behavior. The package can also define some post operation
# hooks.
#
################################################################################
GO_BIN = $(HOST_DIR)/bin/go
################################################################################
# inner-golang-package -- defines how the configuration, compilation and
# installation of a Go package should be done, implements a few hooks to tune
# the build process for Go specificities and calls the generic package
# infrastructure to generate the necessary make targets
#
# argument 1 is the lowercase package name
# argument 2 is the uppercase package name, including a HOST_ prefix for host
# packages
# argument 3 is the uppercase package name, without the HOST_ prefix for host
# packages
# argument 4 is the type (target or host)
#
################################################################################
define inner-golang-package
# Legacy
ifneq ($$($(2)_INSTALL_BINS),)
$$(error Package $(1) sets $(2)_INSTALL_BINS, which is no longer supported; \
see the manual: https://buildroot.org/manual.html#migrating-golang-package )
endif
$(2)_BUILD_OPTS += \
-ldflags "$$($(2)_LDFLAGS)" \
-modcacherw \
-tags "$$($(2)_TAGS)" \
-trimpath \
-p $$(PARALLEL_JOBS) \
-buildvcs=false
# Target packages need the Go compiler on the host at download time (for
# vendoring), and at build and install time.
$(2)_DOWNLOAD_DEPENDENCIES += host-go
$(2)_DEPENDENCIES += host-go
$(2)_BUILD_TARGETS ?= .
# If the build target is just ".", then we assume the binary to be
# produced is named after the package. If however, a build target has
# been specified, we assume that the binaries to be produced are named
# after each build target building them (below in <pkg>_BUILD_CMDS).
ifeq ($$($(2)_BUILD_TARGETS),.)
$(2)_BIN_NAME ?= $$($(2)_RAWNAME)
$(2)_INSTALL_BINS ?= $$($(2)_BIN_NAME)
else ifeq ($$(words $$($(2)_BUILD_TARGETS)),1)
$(2)_BIN_NAME ?= $$(notdir $$($(2)_BUILD_TARGETS))
$(2)_INSTALL_BINS ?= $$($(2)_BIN_NAME)
else
ifneq ($$($(2)_BIN_NAME),)
$$(error $(1) sets $(2)_BIN_NAME while there are multiple targets in $(2)_BUILD_TARGETS)
endif
$(2)_INSTALL_BINS ?= $$(notdir $$($(2)_BUILD_TARGETS))
endif
# 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)_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)
# 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)/$$($(2)_SUBDIR)/go.mod ]; then \
printf "module $$($(2)_GOMOD)\n" > $$(@D)/$$($(2)_SUBDIR)/go.mod; \
fi
endef
$(2)_POST_PATCH_HOOKS += $(2)_GEN_GOMOD
$(2)_DOWNLOAD_POST_PROCESS = go
$(2)_DL_ENV += \
$$(HOST_GO_COMMON_ENV) \
GOPROXY=direct \
$$($(2)_GO_ENV)
# If building in a sub directory, do the vendoring in there
ifneq ($$($(2)_SUBDIR),)
$(2)_DOWNLOAD_POST_PROCESS_OPTS += -s$$($(2)_SUBDIR)
endif
# Because we append vendored info, we can't rely on the values being empty
# once we eventually get into the generic-package infra. So, we duplicate
# the heuristics here
ifndef $(2)_LICENSE
ifdef $(3)_LICENSE
$(2)_LICENSE = $$($(3)_LICENSE)
endif
endif
# Due to vendoring, it is pretty likely that not all licenses are
# listed in <pkg>_LICENSE. If the license is unset, it is "unknown"
# so adding unknowns to some unknown is still some other unknown,
# so don't append the blurb in that case.
ifneq ($$($(2)_LICENSE),)
$(2)_LICENSE += , vendored dependencies licenses probably not listed
endif
# Build step. Only define it if not already defined by the package .mk
# file.
ifndef $(2)_BUILD_CMDS
ifeq ($(4),target)
ifeq ($(BR2_STATIC_LIBS),y)
$(2)_EXTLDFLAGS += -static
$(2)_TAGS += osusergo netgo
endif
ifeq ($(BR2_aarch64),y)
# Go forces use of the Gold linker on aarch64 due to a bug in BFD that
# is fixed in Binutils >= 2.41 (that includes all versions provided by
# Buildroot). Forcing Gold will break with toolchains that don't
# provide it (like the Buildroot toolchains), so override the flag and
# use BFD.
# See: https://github.com/golang/go/issues/22040
$(2)_EXTLDFLAGS += -fuse-ld=bfd
endif
ifneq ($$($(2)_EXTLDFLAGS),)
$(2)_LDFLAGS += -extldflags '$$($(2)_EXTLDFLAGS)'
endif
# Build package for target
define $(2)_BUILD_CMDS
$$(foreach d,$$($(2)_BUILD_TARGETS),\
cd $$(@D)/$$($(2)_SUBDIR); \
$$(HOST_GO_TARGET_ENV) \
$$($(2)_GO_ENV) \
$$(GO_BIN) build -v $$($(2)_BUILD_OPTS) \
-o $$(@D)/bin/$$(or $$($(2)_BIN_NAME),$$(notdir $$(d))) \
$$($(2)_GOMOD)/$$(d)
)
endef
else
# Build package for host
define $(2)_BUILD_CMDS
$$(foreach d,$$($(2)_BUILD_TARGETS),\
cd $$(@D)/$$($(2)_SUBDIR); \
$$(HOST_GO_HOST_ENV) \
$$($(2)_GO_ENV) \
$$(GO_BIN) build -v $$($(2)_BUILD_OPTS) \
-o $$(@D)/bin/$$(or $$($(2)_BIN_NAME),$$(notdir $$(d))) \
$$($(2)_GOMOD)/$$(d)
)
endef
endif
endif
# Target installation step. Only define it if not already defined by the
# package .mk file.
ifndef $(2)_INSTALL_TARGET_CMDS
define $(2)_INSTALL_TARGET_CMDS
$$(foreach d,$$($(2)_INSTALL_BINS),\
$$(INSTALL) -D -m 0755 $$(@D)/bin/$$(d) $$(TARGET_DIR)/usr/bin/$$(d)
)
endef
endif
# Host installation step
ifndef $(2)_INSTALL_CMDS
define $(2)_INSTALL_CMDS
$$(foreach d,$$($(2)_INSTALL_BINS),\
$$(INSTALL) -D -m 0755 $$(@D)/bin/$$(d) $$(HOST_DIR)/bin/$$(d)
)
endef
endif
# Call the generic package infrastructure to generate the necessary make
# targets
$(call inner-generic-package,$(1),$(2),$(3),$(4))
endef # inner-golang-package
################################################################################
# golang-package -- the target generator macro for Go packages
################################################################################
golang-package = $(call inner-golang-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)
host-golang-package = $(call inner-golang-package,host-$(pkgname),$(call UPPERCASE,host-$(pkgname)),$(call UPPERCASE,$(pkgname)),host)