Makefile 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #####################################################
  2. REPO_PREFIX := yunion.io/x/onecloud
  3. VERSION_PKG := yunion.io/x/pkg/util/version
  4. ROOT_DIR := $(CURDIR)
  5. BUILD_DIR := $(ROOT_DIR)/_output
  6. BIN_DIR := $(BUILD_DIR)/bin
  7. BUILD_SCRIPT := $(ROOT_DIR)/build/build.sh
  8. DEB_BUILD_SCRIPT := $(ROOT_DIR)/build/build_deb.sh
  9. ifeq ($(ONECLOUD_CI_BUILD),)
  10. GIT_COMMIT := $(shell git rev-parse --short HEAD)
  11. GIT_BRANCH := $(shell git branch -r --contains | head -1 | sed -E -e "s%(HEAD ->|origin|upstream)/?%%g" | xargs)
  12. GIT_VERSION := $(shell git describe --always --tags --abbrev=14 $(GIT_COMMIT)^{commit})
  13. GIT_TREE_STATE := $(shell s=`git status --porcelain 2>/dev/null`; if [ -z "$$s" ]; then echo "clean"; else echo "dirty"; fi)
  14. BUILD_DATE := $(shell date -u +'%Y-%m-%dT%H:%M:%SZ')
  15. else
  16. GIT_COMMIT:=x
  17. GIT_BRANCH:=x
  18. GIT_VERSION:=x
  19. BUILD_TREE_STATE:=clean
  20. BUILD_DATE=2099-07-01T07:11:09Z
  21. endif
  22. LDFLAGS := "-w \
  23. -X $(VERSION_PKG).gitVersion=$(GIT_VERSION) \
  24. -X $(VERSION_PKG).gitCommit=$(GIT_COMMIT) \
  25. -X $(VERSION_PKG).gitBranch=$(GIT_BRANCH) \
  26. -X $(VERSION_PKG).buildDate=$(BUILD_DATE) \
  27. -X $(VERSION_PKG).gitTreeState=$(GIT_TREE_STATE) \
  28. -X $(VERSION_PKG).gitMajor=0 \
  29. -X $(VERSION_PKG).gitMinor=0"
  30. #####################################################
  31. ifneq ($(DLV),)
  32. GO_BUILD_FLAGS += -gcflags "all=-N -l"
  33. LDFLAGS = ""
  34. endif
  35. GO_BUILD_FLAGS+=-mod vendor -ldflags $(LDFLAGS)
  36. GO_BUILD := go build $(GO_BUILD_FLAGS)
  37. GO_INSTALL := go install -ldflags $(LDFLAGS)
  38. GO_TEST := go test
  39. PKGS := go list ./...
  40. CGO_CFLAGS_ENV = $(shell go env CGO_CFLAGS)
  41. CGO_LDFLAGS_ENV = $(shell go env CGO_LDFLAGS)
  42. export GOOS ?= linux
  43. export GO111MODULE:=on
  44. export CGO_CFLAGS = ${X_CGO_CFLAGS}
  45. export CGO_LDFLAGS = ${X_CGO_LDFLAGS}
  46. UNAME := $(shell uname)
  47. ifeq ($(UNAME), Linux)
  48. XARGS_FLAGS = --no-run-if-empty
  49. endif
  50. cmdTargets:=$(wildcard cmd/*)
  51. rpmTargets:=$(foreach b,$(patsubst cmd/%,%,$(cmdTargets)),$(if $(shell [ -f "$(CURDIR)/build/$(b)/vars" ] && echo 1),rpm/$(b)))
  52. debTargets:=$(foreach b,$(patsubst cmd/%,%,$(cmdTargets)),$(if $(shell [ -f "$(CURDIR)/build/$(b)/vars" ] && echo 1),deb/$(b)))
  53. all: build
  54. install: prepare_dir
  55. @for PKG in $$( $(PKGS) | grep -w "$(filter-out $@,$(MAKECMDGOALS))" ); do \
  56. echo $$PKG; \
  57. $(GO_INSTALL) $$PKG; \
  58. done
  59. gencopyright:
  60. @bash scripts/gencopyright.sh pkg cmd
  61. test:
  62. @go test $(GO_BUILD_FLAGS) $(shell go list ./... | egrep -v 'host-image|hostimage|torrent')
  63. vet:
  64. go vet ./...
  65. # cmd/esxi-agent: prepare_dir
  66. # CGO_ENABLED=0 $(GO_BUILD) -o $(BIN_DIR)/$(shell basename $@) $(REPO_PREFIX)/$@
  67. cmd/%: prepare_dir
  68. CGO_ENABLED=0 $(GO_BUILD) -o $(BIN_DIR)/$(shell basename $@) $(REPO_PREFIX)/$@
  69. rpm/%: cmd/%
  70. $(BUILD_SCRIPT) $*
  71. deb/%: cmd/%
  72. $(DEB_BUILD_SCRIPT) $*
  73. pkg/%: prepare_dir
  74. $(GO_INSTALL) $(REPO_PREFIX)/$@
  75. build:
  76. $(MAKE) $(cmdTargets)
  77. rpm:
  78. $(MAKE) $(rpmTargets)
  79. deb:
  80. $(MAKE) $(debTargets)
  81. rpmclean:
  82. rm -fr $(BUILD_DIR)/rpms
  83. prepare_dir: bin_dir
  84. bin_dir: output_dir
  85. @mkdir -p $(BUILD_DIR)/bin
  86. output_dir:
  87. @mkdir -p $(BUILD_DIR)
  88. .PHONY: all build prepare_dir clean rpm
  89. clean:
  90. @rm -fr $(BUILD_DIR)
  91. fmt:
  92. @git ls-files --exclude '*' '*.go' \
  93. | grep -v '^vendor/' \
  94. | while read f; do \
  95. if ! grep -m1 -q '^// Code generated .* DO NOT EDIT\.$$' "$$f"; then \
  96. echo "$$f"; \
  97. fi ; \
  98. done \
  99. | xargs $(XARGS_FLAGS) gofmt -w
  100. fmt-check: fmt
  101. @if git status --short | grep -E '^.M .*/[^.]+.go'; then \
  102. git diff | cat; \
  103. echo "$@: working tree modified (possibly by gofmt)" >&2 ; \
  104. false ; \
  105. fi
  106. .PHONY: fmt fmt-check
  107. gendocgo:
  108. @sh build/gendoc.sh
  109. adddocgo:
  110. @git ls-files --others '*/doc.go' | xargs $(XARGS_FLAGS) -- git add
  111. gendocgo-check: gendocgo
  112. @n="$$(git ls-files --others '*/doc.go' | wc -l)"; \
  113. if test "$$n" -gt 0; then \
  114. git ls-files --others '*/doc.go' | sed -e 's/^/ /'; \
  115. echo "$@: untracked doc.go file(s) exist in working directory" >&2 ; \
  116. false ; \
  117. fi
  118. .PHONY: gendocgo adddocgo gendocgo-check
  119. goimports-check:
  120. @goimports -w -local "yunion.io/x/:yunion.io/x/onecloud" pkg cmd; \
  121. if git status --short | grep -E '^.M .*/[^.]+.go'; then \
  122. git diff | cat; \
  123. echo "$@: working tree modified (possibly by goimports)" >&2 ; \
  124. echo "$@: " >&2 ; \
  125. echo "$@: import spec should be grouped in order: std, 3rd-party, yunion.io/x, yunion.io/x/onecloud" >&2 ; \
  126. echo "$@: goimports should be installed by: \
  127. echo "$@: git clone --depth 4 https://github.com/yunionio/tools && cd tools && go install ./cmd/goimports ; \
  128. false ; \
  129. fi
  130. .PHONY: goimports-check
  131. vet-check:
  132. ./scripts/vet.sh gen
  133. ./scripts/vet.sh chk
  134. .PHONY: vet-check
  135. comma:=,
  136. space:=$(space) $(space)
  137. # NOTE: keep y18n-packages in alphabetical order
  138. y18n-src-lang := en-US
  139. y18n-lang := en-US,zh-CN
  140. y18n-packages := \
  141. yunion.io/x/onecloud/cmd/apigateway \
  142. yunion.io/x/onecloud/cmd/keystone \
  143. yunion.io/x/onecloud/cmd/monitor \
  144. yunion.io/x/onecloud/cmd/region \
  145. yunion.io/x/onecloud/cmd/yunionconf \
  146. define y18n-gen
  147. set -o errexit; \
  148. set -o pipefail; \
  149. export GO111MODULE=off; \
  150. y18n \
  151. -chdir $(CURDIR) \
  152. -dir ./locales/ \
  153. -out ./locales/locales.go \
  154. -lang $(y18n-lang) \
  155. $(y18n-packages) \
  156. ; \
  157. $(foreach lang,$(filter-out $(y18n-src-lang),$(subst $(comma), ,$(y18n-lang))),cp ./locales/$(lang)/{out,messages}.gotext.json;) \
  158. endef
  159. y18n-gen:
  160. $(y18n-gen)
  161. $(y18n-gen)
  162. .PHONY: y18n-gen
  163. y18n-check:
  164. $(y18n-gen)
  165. if git status --short ./locales | sed 's/^/$@: /' | grep .; then \
  166. echo "$@: Locales content needs care" >&2 ; \
  167. false; \
  168. fi
  169. .PHONY: y18n-check
  170. define hostdeployer-grpc-gen
  171. set -o errexit; \
  172. set -o pipefail; \
  173. protoc -I pkg/hostman/hostdeployer/apis \
  174. --go_out=pkg/hostman/hostdeployer/apis \
  175. --go_opt=paths=source_relative \
  176. --go-grpc_out=pkg/hostman/hostdeployer/apis \
  177. --go-grpc_opt=paths=source_relative \
  178. pkg/hostman/hostdeployer/apis/deploy.proto
  179. endef
  180. hostdeployer-grpc-gen:
  181. $(hostdeployer-grpc-gen)
  182. check: fmt-check
  183. check: gendocgo-check
  184. #check: goimports-check
  185. #check: vet-check
  186. #check: y18n-check
  187. .PHONY: check
  188. define depDeprecated
  189. OneCloud now requires using go-mod for dependency management. dep target,
  190. vendor files will be removed in future versions
  191. Follow the following link to find out more about go-mod
  192. - https://blog.golang.org/using-go-modules
  193. - https://github.com/golang/go/wiki/Modules
  194. Switching to "make mod"...
  195. endef
  196. dep: export depDeprecated:=$(depDeprecated)
  197. dep:
  198. @echo "$$depDeprecated"
  199. @$(MAKE) mod
  200. RELEASE_BRANCH:=master
  201. GOPROXY ?= direct
  202. mod:
  203. GOPROXY=$(GOPROXY) GONOSUMDB=yunion.io/x go get yunion.io/x/cloudmux@$(RELEASE_BRANCH)
  204. GOPROXY=$(GOPROXY) GONOSUMDB=yunion.io/x go get $(patsubst %,%@master,$(shell GO111MODULE=on go mod edit -print | sed -n -e 's|.*\(yunion.io/x/[a-z].*\) v.*|\1|p' | grep -v '/cloudmux$$'))
  205. GOPROXY=$(GOPROXY) GONOSUMDB=yunion.io/x go mod tidy
  206. GOPROXY=$(GOPROXY) GONOSUMDB=yunion.io/x go mod vendor -v
  207. define helpText
  208. Build with docker
  209. make docker-alpine-build F='-j4'
  210. make docker-alpine-build F='-j4 cmd/host cmd/host-deployer'
  211. make docker-alpine-build-stop
  212. Tidy up go modules and vendor directory
  213. make mod
  214. endef
  215. help: export helpText:=$(helpText)
  216. help:
  217. @echo "$$helpText"
  218. .PHONY: help
  219. gen-model-api-check:
  220. which model-api-gen || (GO111MODULE=off go get -u yunion.io/x/code-generator/cmd/model-api-gen)
  221. gen-model-api: gen-model-api-check
  222. $(ROOT_DIR)/scripts/codegen.py model-api
  223. gen-swagger-check:
  224. which swagger || (GO111MODULE=off go get -u github.com/go-swagger/go-swagger/cmd/swagger)
  225. which swagger-gen || (GO111MODULE=off go get -u yunion.io/x/code-generator/cmd/swagger-gen)
  226. which swagger-serve || (GO111MODULE=off go get -u yunion.io/x/code-generator/cmd/swagger-serve)
  227. gen-swagger: gen-swagger-check
  228. $(ROOT_DIR)/scripts/codegen.py swagger-code
  229. $(ROOT_DIR)/scripts/codegen.py swagger-yaml
  230. swagger-serve-only:
  231. $(ROOT_DIR)/scripts/codegen.py swagger-serve
  232. swagger-serve: gen-model-api gen-swagger swagger-serve-only
  233. swagger-site: gen-model-api gen-swagger
  234. $(ROOT_DIR)/scripts/codegen.py swagger-site
  235. .PHONY: gen-model-api-check gen-model-api gen-swagger-check gen-swagger swagger-serve swagger-site
  236. REGISTRY ?= "registry.cn-beijing.aliyuncs.com/yunionio"
  237. VERSION ?= $(shell git describe --exact-match 2> /dev/null || \
  238. git describe --match=$(git rev-parse --short=8 HEAD) --always --dirty --abbrev=8)
  239. image:
  240. mkdir -p $(ROOT_DIR)/_output
  241. DEBUG=$(DEBUG) ARCH=$(ARCH) TAG=$(VERSION) REGISTRY=$(REGISTRY) $(ROOT_DIR)/scripts/docker_push.sh $(filter-out $@,$(MAKECMDGOALS))
  242. .PHONY: image
  243. image-telegraf-raid-plugin:
  244. VERSION=v4.0-20260115.0 GOOS=linux ARCH=all make image telegraf-raid-plugin
  245. %:
  246. @:
  247. ModName:=yunion.io/x/onecloud
  248. include $(CURDIR)/Makefile.common.mk