You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Makefile 15 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. # Copyright 2023 OpenIMSDK. All rights reserved.
  2. # Use of this source code is governed by a MIT style
  3. # license that can be found in the LICENSE file.
  4. ###################################=> common commands <=#############################################
  5. # ========================== Capture Environment ===============================
  6. # get the repo root and output path
  7. ROOT_PACKAGE=github.com/OpenIMSDK/OpenKF
  8. OUT_DIR=$(REPO_ROOT)/_output
  9. # ==============================================================================
  10. # define the default goal
  11. #
  12. SHELL := /bin/bash
  13. DIRS=$(shell ls)
  14. GO=go
  15. .DEFAULT_GOAL := help
  16. # include the common makefile
  17. COMMON_SELF_DIR := $(dir $(lastword $(MAKEFILE_LIST)))
  18. # ROOT_DIR: root directory of the code base
  19. ifeq ($(origin ROOT_DIR),undefined)
  20. ROOT_DIR := $(abspath $(shell cd $(COMMON_SELF_DIR)/. && pwd -P))
  21. endif
  22. # OUTPUT_DIR: The directory where the build output is stored.
  23. ifeq ($(origin OUTPUT_DIR),undefined)
  24. OUTPUT_DIR := $(ROOT_DIR)/_output
  25. $(shell mkdir -p $(OUTPUT_DIR))
  26. endif
  27. # BIN_DIR: The directory where the build output is stored.
  28. ifeq ($(origin BIN_DIR),undefined)
  29. BIN_DIR := $(OUTPUT_DIR)/bin
  30. $(shell mkdir -p $(BIN_DIR))
  31. endif
  32. ifeq ($(origin TOOLS_DIR),undefined)
  33. TOOLS_DIR := $(OUTPUT_DIR)/tools
  34. $(shell mkdir -p $(TOOLS_DIR))
  35. endif
  36. ifeq ($(origin TMP_DIR),undefined)
  37. TMP_DIR := $(OUTPUT_DIR)/tmp
  38. $(shell mkdir -p $(TMP_DIR))
  39. endif
  40. ifeq ($(origin VERSION), undefined)
  41. VERSION := $(shell git describe --tags --always --match="v*" --dirty | sed 's/-/./g') #v2.3.3.631.g00abdc9b.dirty
  42. endif
  43. # Check if the tree is dirty. default to dirty(maybe u should commit?)
  44. GIT_TREE_STATE:="dirty"
  45. ifeq (, $(shell git status --porcelain 2>/dev/null))
  46. GIT_TREE_STATE="clean"
  47. endif
  48. GIT_COMMIT:=$(shell git rev-parse HEAD)
  49. IMG ?= ghcr.io/OpenIMSDK/OpenKF:latest
  50. BUILDFILE = "./main.go"
  51. BUILDAPP = "$(OUTPUT_DIR)/"
  52. # Define the directory you want to copyright
  53. CODE_DIRS := $(ROOT_DIR)/ #$(ROOT_DIR)/pkg $(ROOT_DIR)/core $(ROOT_DIR)/integrationtest $(ROOT_DIR)/lib $(ROOT_DIR)/mock $(ROOT_DIR)/db $(ROOT_DIR)/openapi
  54. FINDS := find $(CODE_DIRS)
  55. ifndef V
  56. MAKEFLAGS += --no-print-directory
  57. endif
  58. # The OS must be linux when building docker images
  59. PLATFORMS ?= linux_amd64 linux_arm64
  60. # The OS can be linux/windows/darwin when building binaries
  61. # PLATFORMS ?= darwin_amd64 windows_amd64 linux_amd64 linux_arm64
  62. # Set a specific PLATFORM
  63. ifeq ($(origin PLATFORM), undefined)
  64. ifeq ($(origin GOOS), undefined)
  65. GOOS := $(shell go env GOOS)
  66. endif
  67. ifeq ($(origin GOARCH), undefined)
  68. GOARCH := $(shell go env GOARCH)
  69. endif
  70. PLATFORM := $(GOOS)_$(GOARCH)
  71. # Use linux as the default OS when building images
  72. IMAGE_PLAT := linux_$(GOARCH)
  73. else
  74. GOOS := $(word 1, $(subst _, ,$(PLATFORM)))
  75. GOARCH := $(word 2, $(subst _, ,$(PLATFORM)))
  76. IMAGE_PLAT := $(PLATFORM)
  77. endif
  78. # Copy githook scripts when execute makefile
  79. # TODO! GIT_FILE_SIZE_LIMIT=42000000 git commit -m "This commit is allowed file sizes up to 42MB"
  80. COPY_GITHOOK:=$(shell cp -f scripts/githooks/* .git/hooks/; chmod +x .git/hooks/*)
  81. # Linux command settings
  82. FIND := find . ! -path './image/*' ! -path './vendor/*' ! -path './bin/*'
  83. XARGS := xargs -r
  84. # ==============================================================================
  85. # TODO: License selection
  86. LICENSE_TEMPLATE ?= $(ROOT_DIR)/scripts/LICENSE/license_templates.txt # MIT License
  87. # LICENSE_TEMPLATE ?= $(ROOT_DIR)/scripts/LICENSE/LICENSE_TEMPLATES # Apache License
  88. # COMMA: Concatenate multiple strings to form a list of strings
  89. COMMA := ,
  90. # SPACE: Used to separate strings
  91. SPACE :=
  92. # SPACE: Replace multiple consecutive Spaces with a single space
  93. SPACE +=
  94. # ==============================================================================
  95. # Build definition
  96. GO_SUPPORTED_VERSIONS ?= 1.18|1.19|1.20
  97. GO_LDFLAGS += -X $(VERSION_PACKAGE).GitVersion=$(VERSION) \
  98. -X $(VERSION_PACKAGE).GitCommit=$(GIT_COMMIT) \
  99. -X $(VERSION_PACKAGE).GitTreeState=$(GIT_TREE_STATE) \
  100. -X $(VERSION_PACKAGE).BuildDate=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ')
  101. ifneq ($(DLV),)
  102. GO_BUILD_FLAGS += -gcflags "all=-N -l"
  103. LDFLAGS = ""
  104. endif
  105. GO_BUILD_FLAGS += -ldflags "$(GO_LDFLAGS)"
  106. ifeq ($(GOOS),windows)
  107. GO_OUT_EXT := .exe
  108. endif
  109. ifeq ($(ROOT_PACKAGE),)
  110. $(error the variable ROOT_PACKAGE must be set prior to including golang.mk)
  111. endif
  112. GOPATH := $(shell go env GOPATH)
  113. ifeq ($(origin GOBIN), undefined)
  114. GOBIN := $(GOPATH)/bin
  115. endif
  116. COMMANDS ?= $(filter-out %.md, $(wildcard ${ROOT_DIR}/cmd/*))
  117. BINS ?= $(foreach cmd,${COMMANDS},$(notdir ${cmd}))
  118. ifeq (${COMMANDS},)
  119. $(error Could not determine COMMANDS, set ROOT_DIR or run in source dir)
  120. endif
  121. ifeq (${BINS},)
  122. $(error Could not determine BINS, set ROOT_DIR or run in source dir)
  123. endif
  124. EXCLUDE_TESTS=github.com/OpenIMSDK/OpenKF/test
  125. # ==============================================================================
  126. # Build
  127. ## all: Build all the necessary targets.
  128. .PHONY: all
  129. all: copyright-verify build # tidy lint cover
  130. ## build: Build binaries by default.
  131. .PHONY: build
  132. build: go.build.verify $(addprefix go.build., $(addprefix $(PLATFORM)., $(BINS)))
  133. .PHONY: build.%
  134. build.%:
  135. @echo "$(shell go version)"
  136. @echo "===========> Building binary $(BUILDAPP) *[Git Info]: $(VERSION)-$(GIT_COMMIT)"
  137. @export CGO_ENABLED=0 && GOOS=linux go build -o $(BUILDAPP)/$*/ -ldflags '-s -w' $*/example/$(BUILDFILE)
  138. .PHONY: go.build.verify
  139. go.build.verify:
  140. ifneq ($(shell $(GO) version | grep -q -E '\bgo($(GO_SUPPORTED_VERSIONS))\b' && echo 0 || echo 1), 0)
  141. $(error unsupported go version. Please make install one of the following supported version: '$(GO_SUPPORTED_VERSIONS)')
  142. endif
  143. ## go.build: Build the binary file of the specified platform.
  144. .PHONY: go.build.%
  145. go.build.%:
  146. $(eval COMMAND := $(word 2,$(subst ., ,$*)))
  147. $(eval PLATFORM := $(word 1,$(subst ., ,$*)))
  148. $(eval OS := $(word 1,$(subst _, ,$(PLATFORM))))
  149. $(eval ARCH := $(word 2,$(subst _, ,$(PLATFORM))))
  150. @echo "=====> COMMAND=$(COMMAND)"
  151. @echo "=====> PLATFORM=$(PLATFORM)"
  152. @echo "=====> BIN_DIR=$(BIN_DIR)"
  153. @echo "===========> Building binary $(COMMAND) $(VERSION) for $(OS)_$(ARCH)"
  154. @mkdir -p $(BIN_DIR)/platforms/$(OS)/$(ARCH)
  155. @CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH) $(GO) build $(GO_BUILD_FLAGS) -o $(BIN_DIR)/platforms/$(OS)/$(ARCH)/$(COMMAND)$(GO_OUT_EXT) $(ROOT_PACKAGE)/cmd/$(COMMAND)
  156. ## build-multiarch: Build binaries for multiple platforms.
  157. .PHONY: build-multiarch
  158. build-multiarch: go.build.verify $(foreach p,$(PLATFORMS),$(addprefix go.build., $(addprefix $(p)., $(BINS))))
  159. # ==============================================================================
  160. # Targets
  161. ## tidy: tidy go.mod
  162. .PHONY: tidy
  163. tidy:
  164. @$(GO) mod tidy
  165. ## style: Code style -> fmt,vet,lint
  166. .PHONY: style
  167. style: fmt vet lint
  168. ## fmt: Run go fmt against code.
  169. .PHONY: fmt
  170. fmt:
  171. @$(GO) fmt ./...
  172. ## vet: Run go vet against code.
  173. .PHONY: vet
  174. vet:
  175. @$(GO) vet ./...
  176. ## generate: Run go generate against code.
  177. .PHONY: generate
  178. generate:
  179. @$(GO) generate ./...
  180. ## lint: Run go lint against code.
  181. .PHONY: lint
  182. lint:
  183. @echo "===========> Run golangci to lint source codes"
  184. @golangci-lint run -c $(ROOT_DIR)/.golangci.yml $(ROOT_DIR)/...
  185. ## test: Run unit test
  186. .PHONY: test
  187. test:
  188. @$(GO) test ./...
  189. ## cover: Run unit test with coverage.
  190. .PHONY: cover
  191. cover: test
  192. @$(GO) test -cover
  193. ## docker-build: Build docker image with the manager.
  194. .PHONY: docker-build
  195. docker-build: test
  196. docker build -t ${IMG} .
  197. ## docker-push: Push docker image with the manager.
  198. .PHONY: docker-push
  199. docker-push:
  200. docker push ${IMG}
  201. ## docker-buildx-push: Push docker image with the manager using buildx.
  202. .PHONY: docker-buildx-push
  203. docker-buildx-push:
  204. docker buildx build --platform linux/arm64,linux/amd64 -t ${IMG} . --push
  205. ## copyright-verify: Validate boilerplate headers for assign files.
  206. .PHONY: copyright-verify
  207. copyright-verify: tools.verify.addlicense copyright-add
  208. @echo "===========> Validate boilerplate headers for assign files starting in the $(ROOT_DIR) directory"
  209. @$(TOOLS_DIR)/addlicense -v -check -ignore **/test/** -f $(LICENSE_TEMPLATE) $(CODE_DIRS)
  210. @echo "===========> End of boilerplate headers check..."
  211. ## copyright-add: Add the boilerplate headers for all files.
  212. .PHONY: copyright-add
  213. copyright-add: tools.verify.addlicense
  214. @echo "===========> Adding $(LICENSE_TEMPLATE) the boilerplate headers for all files"
  215. @$(TOOLS_DIR)/addlicense -y $(shell date +"%Y") -v -c "OpenIMSDK open source community." -f $(LICENSE_TEMPLATE) $(CODE_DIRS)
  216. @echo "===========> End the copyright is added..."
  217. ## clean: Clean all builds.
  218. .PHONY: clean
  219. clean:
  220. @echo "===========> Cleaning all builds TMP_DIR($(TMP_DIR)) AND BIN_DIR($(BIN_DIR))"
  221. @-rm -vrf $(TMP_DIR) $(BIN_DIR)
  222. @echo "===========> End clean..."
  223. ## help: Show this help info.
  224. .PHONY: help
  225. help: Makefile
  226. @printf "\n\033[1mUsage: make <TARGETS> ...\033[0m\n\n\\033[1mTargets:\\033[0m\n\n"
  227. @sed -n 's/^##//p' $< | awk -F':' '{printf "\033[36m%-28s\033[0m %s\n", $$1, $$2}' | sed -e 's/^/ /'
  228. ######################################=> common tools<= ############################################
  229. # tools
  230. BUILD_TOOLS ?= go-gitlint golangci-lint goimports addlicense deepcopy-gen conversion-gen ginkgo go-junit-report
  231. # tools.verify.%: Check if a tool is installed and install it
  232. .PHONY: tools.verify.%
  233. tools.verify.%:
  234. @echo "===========> Verifying $* is installed"
  235. @if [ ! -f $(TOOLS_DIR)/$* ]; then GOBIN=$(TOOLS_DIR) $(MAKE) tools.install.$*; fi
  236. @echo "===========> $* is install in $(TOOLS_DIR)/$*"
  237. # tools: Install a must tools
  238. .PHONY: tools
  239. tools: $(addprefix tools.verify., $(BUILD_TOOLS))
  240. # tools.install.%: Install a single tool in $GOBIN/
  241. .PHONY: tools.install.%
  242. tools.install.%:
  243. @echo "===========> Installing $,The default installation path is $(GOBIN)/$*"
  244. @$(MAKE) install.$*
  245. .PHONY: install.golangci-lint
  246. install.golangci-lint:
  247. @$(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
  248. .PHONY: install.goimports
  249. install.goimports:
  250. @$(GO) install golang.org/x/tools/cmd/goimports@latest
  251. .PHONY: install.addlicense
  252. install.addlicense:
  253. @$(GO) install github.com/google/addlicense@latest
  254. .PHONY: install.deepcopy-gen
  255. install.deepcopy-gen:
  256. @$(GO) install k8s.io/code-generator/cmd/deepcopy-gen@latest
  257. .PHONY: install.conversion-gen
  258. install.conversion-gen:
  259. @$(GO) install k8s.io/code-generator/cmd/conversion-gen@latest
  260. .PHONY: install.ginkgo
  261. install.ginkgo:
  262. @$(GO) install github.com/onsi/ginkgo/ginkgo@v1.16.2
  263. .PHONY: install.go-gitlint
  264. install.go-gitlint:
  265. @$(GO) install github.com/marmotedu/go-gitlint/cmd/go-gitlint@latest
  266. .PHONY: install.go-junit-report
  267. install.go-junit-report:
  268. @$(GO) install github.com/jstemmer/go-junit-report@latest
  269. # ==============================================================================
  270. # Tools that might be used include go gvm, cos
  271. #
  272. ## install.kube-score: Install kube-score, used to check kubernetes yaml files
  273. .PHONY: install.kube-score
  274. install.kube-score:
  275. @$(GO) install github.com/zegl/kube-score/cmd/kube-score@latest
  276. ## install.kubeconform: Install kubeconform, used to check kubernetes yaml files
  277. .PHONY: install.kubeconform
  278. install.kubeconform:
  279. @$(GO) install github.com/yannh/kubeconform/cmd/kubeconform@latest
  280. ## install.gsemver: Install gsemver, used to generate semver
  281. .PHONY: install.gsemver
  282. install.gsemver:
  283. @$(GO) install github.com/arnaud-deprez/gsemver@latest
  284. ## install.git-chglog: Install git-chglog, used to generate changelog
  285. .PHONY: install.git-chglog
  286. install.git-chglog:
  287. @$(GO) install github.com/git-chglog/git-chglog/cmd/git-chglog@latest
  288. ## install.github-release: Install github-release, used to create github release
  289. .PHONY: install.github-release
  290. install.github-release:
  291. @$(GO) install github.com/github-release/github-release@latest
  292. ## install.coscli: Install coscli, used to upload files to cos
  293. # example: ./coscli cp/sync -r /root/workspaces/OpenIMSDK/OpenKF/ cos://OpenIMSDK-1306374445/code/ -e cos.ap-hongkong.myqcloud.com
  294. # https://cloud.tencent.com/document/product/436/71763
  295. # OpenIMSDK/*
  296. # - code/
  297. # - docs/
  298. # - images/
  299. # - scripts/
  300. .PHONY: install.coscli
  301. install.coscli:
  302. @wget -q https://github.com/tencentyun/coscli/releases/download/v0.13.0-beta/coscli-linux -O ${TOOLS_DIR}/coscli
  303. @chmod +x ${TOOLS_DIR}/coscli
  304. ## install.coscmd: Install coscmd, used to upload files to cos
  305. .PHONY: install.coscmd
  306. install.coscmd:
  307. @if which pip &>/dev/null; then pip install coscmd; else pip3 install coscmd; fi
  308. ## install.delve: Install delve, used to debug go program
  309. .PHONY: install.delve
  310. install.delve:
  311. @$(GO) install github.com/go-delve/delve/cmd/dlv@latest
  312. ## install.air: Install air, used to hot reload go program
  313. .PHONY: install.air
  314. install.air:
  315. @$(GO) install github.com/cosmtrek/air@latest
  316. ## install.gvm: Install gvm, gvm is a Go version manager, built on top of the official go tool.
  317. .PHONY: install.gvm
  318. install.gvm:
  319. @echo "===========> Installing gvm,The default installation path is ~/.gvm/script/gvm"
  320. @bash < <(curl -s -S -L https://raw.gitee.com/moovweb/gvm/master/binscripts/gvm-installer)
  321. @$(shell source /root/.gvm/script/gvm)
  322. ## install.golines: Install golines, used to format long lines
  323. .PHONY: install.golines
  324. install.golines:
  325. @$(GO) install github.com/segmentio/golines@latest
  326. ## install.go-mod-outdated: Install go-mod-outdated, used to check outdated dependencies
  327. .PHONY: install.go-mod-outdated
  328. install.go-mod-outdated:
  329. @$(GO) install github.com/psampaz/go-mod-outdated@latest
  330. ## install.mockgen: Install mockgen, used to generate mock functions
  331. .PHONY: install.mockgen
  332. install.mockgen:
  333. @$(GO) install github.com/golang/mock/mockgen@latest
  334. ## install.gotests: Install gotests, used to generate test functions
  335. .PHONY: install.gotests
  336. install.gotests:
  337. @$(GO) install github.com/cweill/gotests/gotests@latest
  338. ## install.protoc-gen-go: Install protoc-gen-go, used to generate go source files from protobuf files
  339. .PHONY: install.protoc-gen-go
  340. install.protoc-gen-go:
  341. @$(GO) install github.com/golang/protobuf/protoc-gen-go@latest
  342. ## install.cfssl: Install cfssl, used to generate certificates
  343. .PHONY: install.cfssl
  344. install.cfssl:
  345. @$(ROOT_DIR)/script/install/install.sh iam::install::install_cfssl
  346. ## install.depth: Install depth, used to check dependency tree
  347. .PHONY: install.depth
  348. install.depth:
  349. @$(GO) install github.com/KyleBanks/depth/cmd/depth@latest
  350. ## install.go-callvis: Install go-callvis, used to visualize call graph
  351. .PHONY: install.go-callvis
  352. install.go-callvis:
  353. @$(GO) install github.com/ofabry/go-callvis@latest
  354. ## install.gothanks: Install gothanks, used to thank go dependencies
  355. .PHONY: install.gothanks
  356. install.gothanks:
  357. @$(GO) install github.com/psampaz/gothanks@latest
  358. ## install.richgo: Install richgo
  359. .PHONY: install.richgo
  360. install.richgo:
  361. @$(GO) install github.com/kyoh86/richgo@latest
  362. ## install.rts: Install rts
  363. .PHONY: install.rts
  364. install.rts:
  365. @$(GO) install github.com/galeone/rts/cmd/rts@latest

openkf

Contributors (1)