-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGo.include
More file actions
134 lines (120 loc) · 5.29 KB
/
Go.include
File metadata and controls
134 lines (120 loc) · 5.29 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
# Extract args from platform definition.
# Here PLATFORM is os:arch:arm, usually arm is "" unless arch=="arm" when it
# is then one of "5","6" or "7"
GO-OS =$(word 1,$(subst :, ,$(PLATFORM)))
GO-ARCH =$(word 2,$(subst :, ,$(PLATFORM)))
GO-ARM =$(word 3,$(subst :, ,$(PLATFORM)))
GO-ARCH-DIR =$(call GO-OS,$1)/$(call GO-ARCH,$1)$(call GO-ARM,$1)
# $(call GO-BUILD,platform,destination,src)
GO-BUILD = $(call cmd,"GO BUILD","$(subst /, ,$(call GO-ARCH-DIR,$1)) $(shell basename $2)");\
mkdir -p $(shell dirname $2);\
CGO_ENABLED=0 GOOS=$(call GO-OS,$1) GOARCH=$(call GO-ARCH,$1) GOARM=$(GO-ARM,$1) \
go build \
-ldflags="-X '$(PACKAGE_PREFIX).Version=$(shell basename $2) ($(VERSION) $(subst /, ,$(call GO-ARCH-DIR,$1)) $(shell id -u -n) $(shell date))'" \
-o $2 \
$3
GO-CLEAN = $(call cmd,"GO CLEAN",$1);go clean $1
GO-MOD = $(call cmd,"GO MOD",$1);go mod $1
# Append -test.v to GO_TEST to show status of each test.
# Without it, only shows total time per module if they pass
GO-TEST = $(GO_TEST)$(call cmd,"GO TEST",$1);(cd $1;go test ./...)
targets-clean += go-clean
.PHONY: go-clean
go-clean:
$(call GO-CLEAN,-testcache)
# Init goInit
targets-init += go-init
.PHONY: go-init
go-init:
$(call GO-MOD,download)
# Target to run all tests, results into builds directory
targets-test += go-test
.PHONY: go-test
go-test:
$(MKDIR) $(BUILDS)
$(call cmd,"GO TEST",$(BUILDS)/go-test.txt);go test ./... >$(BUILDS)/go-test.txt 2>&1 || cat $(BUILDS)/go-test.txt
targets-tools += go-tools
.PHONY: go-tools
go-tools: $(subst /bin/main.go,,$(subst tools,$(BUILDS),$(shell ls tools/*/bin/main.go)))
# Rule to build a go application
# For this to work: the main function is in tools/<toolName>/bin/main.go
# The compiled binary will be placed in $(BUILDS)/<os>/<arch>/
# This takes precedence of the main one, it allows us to ignore a tool if the .donotbuild file exists
$(BUILDS)/%: tools/%/bin/main.go tools/%/.donotbuild
$(call cmd,"IGNORE",$(shell basename $@))
$(BUILDS)/%: tools/%/bin/main.go
$(foreach PLATFORM,$(PLATFORMS),\
$(call GO-BUILD,$(PLATFORM),$(BUILDS)/$(call GO-ARCH-DIR,$(PLATFORM))/$(BINDIR)$(shell basename $@),$<)${\n}\
)
# Validates the installed version of go against the version declared in go.mod
MINIMUM_SUPPORTED_GO_MAJOR_VERSION = $(shell grep "^go" go.mod | cut -f2 -d' ' | cut -f1 -d'.')
MINIMUM_SUPPORTED_GO_MINOR_VERSION = $(shell grep "^go" go.mod | cut -f2 -d' ' | cut -f2 -d'.')
GO_MAJOR_VERSION = $(shell go version | cut -f3 -d' ' | cut -c 3- | cut -f1 -d' ' | cut -f1 -d'.')
GO_MINOR_VERSION = $(shell go version | cut -f3 -d' ' | cut -c 3- | cut -f1 -d' ' | cut -f2 -d'.')
GO_VERSION_VALIDATION_ERR_MSG = Golang version $(GO_MAJOR_VERSION).$(GO_MINOR_VERSION) is not supported, please update to at least $(MINIMUM_SUPPORTED_GO_MAJOR_VERSION).$(MINIMUM_SUPPORTED_GO_MINOR_VERSION)
targets-validate += validate-go-version
.PHONY: validate-go-version
validate-go-version:
$(call cmd,"CHECK_VERSN","go")
@if [ $(GO_MAJOR_VERSION) -gt $(MINIMUM_SUPPORTED_GO_MAJOR_VERSION) ]; then \
exit 0 ;\
elif [ $(GO_MAJOR_VERSION) -lt $(MINIMUM_SUPPORTED_GO_MAJOR_VERSION) ]; then \
$(ECHO) '$(GO_VERSION_VALIDATION_ERR_MSG)';\
exit 1; \
elif [ $(GO_MINOR_VERSION) -lt $(MINIMUM_SUPPORTED_GO_MINOR_VERSION) ] ; then \
$(ECHO) '$(GO_VERSION_VALIDATION_ERR_MSG)';\
exit 1; \
fi
# This discovers all platforms supported by the locally installed go compiler.
# This will only expand then if the PLATFORMS environment variable was not set
# when invoking make
#
# For now filter out various platforms due to:
# incompatibilities with bbolt (missing syscalls)
resolve-platforms = resolve-go-platforms
.PHONY: resolve-go-platforms
resolve-go-platforms:
ifeq ("$(PLATFORMS)","")
$(eval DISC_PLATFORMS=)
$(foreach DISC_PLATFORM,$(shell go tool dist list), \
$(eval GOOS=$(word 1,$(subst /, ,$(DISC_PLATFORM)))) \
$(if $(filter android,$(GOOS)),,\
$(if $(filter ios,$(GOOS)),,\
$(eval GOARCH=$(word 2,$(subst /, ,$(DISC_PLATFORM)))) \
$(if $(filter loong64,$(GOARCH)),,\
$(if $(filter aix,$(GOOS)),,\
$(if $(filter js,$(GOOS)),,\
$(if $(filter plan9,$(GOOS)),,\
$(foreach GOARM, \
$(if $(filter arm,$(GOARCH)),6 7,:), \
$(eval DISC_PLATFORMS=$(DISC_PLATFORMS) $(GOOS):$(GOARCH):$(GOARM)) \
)\
)\
)\
)\
) \
)\
)\
)
$(eval export PLATFORMS=$(DISC_PLATFORMS))
endif
# Generates platforms.md based on the local go installation.
# This does nothing other than keep that page in sync with what is currently
# supported by go and the build system.
platforms.md: resolve-platforms
$(shell ( \
echo "# Supported Platforms"; \
echo; \
echo "The following platforms are supported by virtue of how the build system works:"; \
echo; \
echo "| Operating System | CPU Architectures |"; \
echo "| ---------------- | ----------------- |"; \
$(foreach OS, $(shell ls $(BUILDS)), echo "| $(OS) | $(foreach ARCH,$(shell ls $(BUILDS)/$(OS)),$(ARCH)) |"; ) \
echo; \
echo "Operating Systems: $(shell ls $(BUILDS)|wc -l) CPU Architectures: $(shell ls -d $(BUILDS)/*/*| cut -f3 -d'/' | sort |uniq | wc -l)"; \
echo; \
echo "This is all non-mobile platforms supported by go version \`$(GO_MAJOR_VERSION).$(GO_MINOR_VERSION)\`" ;\
echo; \
echo "This page is automatically generated from the output of \`go tool dist list\`"; \
) >$@ \
)