gopkg.in/hedzr/errors.v3@v3.3.1/Makefile (about) 1 PROJECTNAME1=$(shell basename "$(PWD)") 2 PROJECTNAME=$(PROJECTNAME1:go-%=%) 3 APPNAME=$(patsubst "%",%,$(shell grep -E "AppName[ \t]+=[ \t]+" doc.go|grep -Eo "\\\".+\\\"")) 4 VERSION=$(shell grep -E "Version[ \t]+=[ \t]+" doc.go|grep -Eo "[0-9.]+") 5 include .env 6 -include .env.local 7 # ref: https://kodfabrik.com/journal/a-good-makefile-for-go/ 8 9 # https://www.gnu.org/savannah-checkouts/gnu/make/manual/html_node/Text-Functions.html 10 # https://stackoverflow.com/questions/19571391/remove-prefix-with-make 11 12 13 # Go related variables. 14 GOBASE = $(shell pwd) 15 #,#GOPATH="$(GOBASE)/vendor:$(GOBASE)" 16 #,#GOPATH=$(GOBASE)/vendor:$(GOBASE):$(shell dirname $(GOBASE)) 17 #GOPATH2 = $(shell dirname $(GOBASE)) 18 #GOPATH1 = $(shell dirname $(GOPATH2)) 19 #GOPATH0 = $(shell dirname $(GOPATH1)) 20 #GOPATH = $(shell dirname $(GOPATH0)) 21 GOBIN = $(GOBASE)/bin 22 GOFILES = $(wildcard *.go) 23 SRCS = $(shell git ls-files '*.go') 24 PKGS = $(shell go list ./...) 25 GIT_VERSION := $(shell git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0") 26 GIT_REVISION := $(shell git rev-parse --short HEAD) 27 #GITHASH = $(shell git rev-parse HEAD) 28 #BUILDTIME := $(shell date "+%Y%m%d_%H%M%S") 29 #BUILDTIME = $(shell date -u '+%Y-%m-%d_%I:%M:%S%p') 30 BUILDTIME = $(shell date -u '+%Y-%m-%d_%H-%M-%S') 31 GOVERSION = $(shell go version) 32 BIN = $(GOPATH)/bin 33 GOLINT = $(BIN)/golint 34 GOCYCLO = $(BIN)/gocyclo 35 GOYOLO = $(BIN)/yolo 36 37 38 # GO111MODULE = on 39 GOPROXY = $(or $(GOPROXY_CUSTOM),direct) 40 41 # Redirect error output to a file, so we can show it in development mode. 42 STDERR = $(or $(STDERR_CUSTOM),/tmp/.$(PROJECTNAME)-stderr.txt) 43 44 # PID file will keep the process id of the server 45 PID = $(or $(PID_CUSTOM),/tmp/.$(PROJECTNAME).pid) 46 47 # Make is verbose in Linux. Make it silent. 48 MAKEFLAGS += --silent 49 50 51 goarch=amd64 52 W_PKG=github.com/hedzr/cmdr/conf 53 LDFLAGS := -s -w \ 54 -X '$(W_PKG).Buildstamp=$(BUILDTIME)' \ 55 -X '$(W_PKG).Githash=$(GIT_REVISION)' \ 56 -X '$(W_PKG).GoVersion=$(GOVERSION)' \ 57 -X '$(W_PKG).Version=$(VERSION)' 58 # -X '$(W_PKG).AppName=$(APPNAME)' 59 GO := GOARCH="$(goarch)" GOOS="$(os)" \ 60 GOPATH="$(GOPATH)" GOBIN="$(GOBIN)" \ 61 GO111MODULE=on GOPROXY=$(GOPROXY) go 62 GO_OFF := GOARCH="$(goarch)" GOOS="$(os)" \ 63 GOPATH="$(GOPATH)" GOBIN="$(GOBIN)" \ 64 GO111MODULE=off go 65 66 67 68 69 # 70 #LDFLAGS= 71 M = $(shell printf "\033[34;1m▶\033[0m") 72 ADDR = ":5q5q" 73 CN = hedzr/$(N) 74 75 76 77 78 ifeq ($(OS),Windows_NT) 79 LS_OPT= 80 CCFLAGS += -D WIN32 81 ifeq ($(PROCESSOR_ARCHITEW6432),AMD64) 82 CCFLAGS += -D AMD64 83 else 84 ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) 85 CCFLAGS += -D AMD64 86 endif 87 ifeq ($(PROCESSOR_ARCHITECTURE),x86) 88 CCFLAGS += -D IA32 89 endif 90 endif 91 else 92 LS_OPT= 93 UNAME_S := $(shell uname -s) 94 ifeq ($(UNAME_S),Linux) 95 CCFLAGS += -D LINUX 96 LS_OPT=--color 97 endif 98 ifeq ($(UNAME_S),Darwin) 99 CCFLAGS += -D OSX 100 LS_OPT=-G 101 endif 102 UNAME_P := $(shell uname -p) 103 ifeq ($(UNAME_P),x86_64) 104 CCFLAGS += -D AMD64 105 endif 106 ifneq ($(filter %86,$(UNAME_P)),) 107 CCFLAGS += -D IA32 108 endif 109 ifneq ($(filter arm%,$(UNAME_P)),) 110 CCFLAGS += -D ARM 111 endif 112 endif 113 114 115 116 117 118 119 .PHONY: build compile exec clean 120 .PHONY: run build-linux build-ci 121 .PHONY: go-build go-generate go-mod-download go-get go-install go-clean 122 .PHONY: godoc format fmt lint cov gocov coverage codecov cyclo bench 123 124 125 126 # For the full list of GOARCH/GOOS, take a look at: 127 # https://github.com/golang/go/blob/master/src/go/build/syslist.go 128 # 129 # A snapshot is: 130 # const goosList = "aix android darwin dragonfly freebsd hurd illumos js linux nacl netbsd openbsd plan9 solaris windows zos " 131 # const goarchList = "386 amd64 amd64p32 arm armbe arm64 arm64be ppc64 ppc64le mips mipsle mips64 mips64le mips64p32 mips64p32le ppc riscv riscv64 s390 s390x sparc sparc64 wasm " 132 #© 133 134 135 ## build: Compile the binary. Synonym of `compile` 136 build: directories compile 137 138 139 ## build-win: build to windows executable, for LAN deploy manually. 140 build-win: 141 @-$(MAKE) -s go-build-task os=windows goarchset=amd64 142 143 ## build-linux: build to linux executable, for LAN deploy manually. 144 build-linux: 145 @-$(MAKE) -s go-build-task os=linux goarchset=amd64 146 147 ## build-nacl: build to nacl executable, for LAN deploy manually. 148 build-nacl: 149 # NOTE: can't build to nacl with golang 1.14 and darwin 150 # chmod +x $(GOBIN)/$(an)_$(os)_$(goarch)*; 151 # ls -la $(LS_OPT) $(GOBIN)/$(an)_$(os)_$(goarch)*; 152 # gzip -f $(GOBIN)/$(an)_$(os)_$(goarch); 153 # ls -la $(LS_OPT) $(GOBIN)/$(an)_$(os)_$(goarch)*; 154 @-$(MAKE) -s go-build-task os=nacl goarchset="386 arm amd64p32" 155 @echo " < All Done." 156 @ls -la $(LS_OPT) $(GOBIN)/* 157 158 159 ## build-plan9: build to plan9 executable, for LAN deploy manually. 160 build-plan9: goarchset = "386 amd64" 161 build-plan9: 162 @-$(MAKE) -s go-build-task os=plan9 goarchset=$(goarchset) 163 164 ## build-freebsd: build to freebsd executable, for LAN deploy manually. 165 build-freebsd: 166 @-$(MAKE) -s go-build-task os=freebsd goarchset=amd64 167 168 ## build-riscv: build to riscv64 executable, for LAN deploy manually. 169 build-riscv: 170 @-$(MAKE) -s go-build-task os=linux goarchset=riscv64 171 172 ## build-ci: run build-ci task. just for CI tools 173 build-ci: 174 @echo " > Building binaries in CI flow..." 175 $(foreach os, linux darwin windows, \ 176 @-$(MAKE) -s go-build-task os=$(os) goarchset="386 amd64" \ 177 ) 178 @-$(MAKE) -s go-build-task os="darwin" goarchset="arm64" 179 @echo " < All Done." 180 @ls -la $(LS_OPT) $(GOBIN)/* 181 182 ## build-m1: run build-ci task. just for CI tools 183 build-m1: 184 @-$(MAKE) -s go-build-task os="darwin" goarchset="arm64" 185 186 go-build-task: directories go-generate 187 @echo " > Building $(os)/$(goarchset) binary..." 188 @#echo " > LDFLAGS = $(LDFLAGS)" 189 # unsupported GOOS/GOARCH pair nacl/386 ?? 190 $(foreach an, $(MAIN_APPS), \ 191 echo " > APP NAMEs = appname:$(APPNAME)|projname:$(PROJECTNAME)|an:$(an)"; \ 192 $(eval ANAME := $(shell for an1 in $(MAIN_APPS); do \ 193 if [[ $(an) == $$an1 ]]; then \ 194 if [[ $$an1 == cli ]]; then echo $(APPNAME); else echo $$an1; fi; \ 195 fi; \ 196 done)) \ 197 $(foreach goarch, $(goarchset), \ 198 echo " >> Building (-trimpath) $(GOBIN)/$(ANAME)_$(os)_$(goarch)...$(os)" >/dev/null; \ 199 $(GO) build -ldflags "$(LDFLAGS)" -o $(GOBIN)/$(ANAME)_$(os)_$(goarch) $(GOBASE)/$(MAIN_BUILD_PKG)/$(an)/$(MAIN_ENTRY_FILE); \ 200 chmod +x $(GOBIN)/$(ANAME)_$(os)_$(goarch)*; \ 201 ls -la $(LS_OPT) $(GOBIN)/$(ANAME)_$(os)_$(goarch)*; \ 202 ) \ 203 ) 204 # $(foreach an, $(MAIN_APPS), \ 205 # $(eval ANAME := $(shell if [ "$(an)" == "cli" ]; then echo $(APPNAME); else echo $(an); fi; )) \ 206 # echo " > APP NAMEs = appname:$(APPNAME)|projname:$(PROJECTNAME)|an:$(an)|ANAME:$(ANAME)"; \ 207 # $(foreach goarch, $(goarchset), \ 208 # echo " >> Building (-trimpath) $(GOBIN)/$(ANAME)_$(os)_$(goarch)...$(os)" >/dev/null; \ 209 # $(GO) build -ldflags "$(LDFLAGS)" -o $(GOBIN)/$(ANAME)_$(os)_$(goarch) $(GOBASE)/$(MAIN_BUILD_PKG)/$(an); \ 210 # chmod +x $(GOBIN)/$(ANAME)_$(os)_$(goarch)*; \ 211 # ls -la $(LS_OPT) $(GOBIN)/$(ANAME)_$(os)_$(goarch)*; \ 212 # ) \ 213 # ) 214 #@ls -la $(LS_OPT) $(GOBIN)/*linux* 215 216 217 218 219 ## compile: Compile the binary. 220 compile: directories go-clean go-generate 221 @-touch $(STDERR) 222 @-rm $(STDERR) 223 @-$(MAKE) -s go-build 2> $(STDERR) 224 @cat $(STDERR) | sed -e '1s/.*/\nError:\n/' 1>&2 225 226 # @cat $(STDERR) | sed -e '1s/.*/\nError:\n/' | sed 's/make\[.*/ /' | sed "/^/s/^/ /" 1>&2 227 #@if [[ -z "$(STDERR)" ]]; then echo; else echo -e "\n\nError:\n\n"; cat $(STDERR) 1>&2; fi 228 229 ## exec: Run given cmd, wrapped with custom GOPATH. eg; make exec run="go test ./..." 230 exec: 231 @GOPATH=$(GOPATH) GOBIN=$(BIN) GO111MODULE=$(GO111MODULE) GOPROXY=$(GOPROXY) \ 232 $(run) 233 234 ## clean: Clean build files. Runs `go clean` internally. 235 clean: 236 @(MAKEFILE) go-clean 237 238 # go-compile: go-clean go-generate go-build 239 240 ooo: 241 $(eval ANAME := $(shell for an in $(MAIN_APPS); do \ 242 if [[ $$an == cli ]]; then A=$(APPNAME); echo $(APPNAME); \ 243 else A=$$an; echo $$an; \ 244 fi; \ 245 done)) 246 @echo "ANAME = $(ANAME), $$ANAME, $$A" 247 248 ox: go-clean go-generate 249 $(MAKE) -s go-build 250 251 ## run: go run xxx 252 run: 253 @$(GO) run -ldflags "$(LDFLAGS)" $(GOBASE)/cli/main.go 254 255 go-build: 256 @echo " > Building apps: $(MAIN_APPS)..." 257 $(foreach an, $(MAIN_APPS), \ 258 $(eval ANAME := $(shell for an1 in $(MAIN_APPS); do \ 259 if [[ "$(an)" == $$an1 ]]; then \ 260 if [[ $$an1 == cli ]]; then echo $(APPNAME); else echo $$an1; fi; \ 261 fi; \ 262 done)) \ 263 echo " > > Building $(MAIN_BUILD_PKG)/$(an) -> $(ANAME) ..."; \ 264 echo " +race. -trimpath. APPNAME = $(APPNAME), LDFLAGS = $(LDFLAGS)"; \ 265 $(GO) build -v -race -ldflags "$(LDFLAGS)" -o $(GOBIN)/$(ANAME) $(GOBASE)/$(MAIN_BUILD_PKG)/$(an)/$(MAIN_ENTRY_FILE); \ 266 ls -la $(LS_OPT) $(GOBIN)/$(ANAME); \ 267 ) 268 ls -la $(LS_OPT) $(GOBIN)/ 269 if [[ -d ./plugin/demo ]]; then \ 270 $(GO) build -v -race -buildmode=plugin -o ./ci/local/share/fluent/addons/demo.so ./plugin/demo && \ 271 chmod +x ./ci/local/share/fluent/addons/demo.so && \ 272 ls -la $(LS_OPT) ./ci/local/share/fluent/addons/demo.so; fi 273 # go build -o $(GOBIN)/$(APPNAME) $(GOFILES) 274 # chmod +x $(GOBIN)/* 275 276 go-generate: 277 @echo " > Generating dependency files ('$(generate)') ..." 278 @$(GO) generate $(generate) ./... 279 280 go-mod-download: 281 @$(GO) mod download 282 283 go-get: 284 # Runs `go get` internally. e.g; make install get=github.com/foo/bar 285 @echo " > Checking if there is any missing dependencies...$(get)" 286 @$(GO) get $(get) 287 288 go-install: 289 @$(GO) install $(GOFILES) 290 291 go-clean: 292 @echo " > Cleaning build cache" 293 @$(GO) clean 294 295 296 297 $(BIN)/golint: | $(GOBASE) # # # ❶ 298 @echo " > installing golint ..." 299 #@-mkdir -p $(GOPATH)/src/golang.org/x/lint/golint 300 #@cd $(GOPATH)/src/golang.org/x/lint/golint 301 #@pwd 302 #@GOPATH=$(GOPATH) GO111MODULE=$(GO111MODULE) GOPROXY=$(GOPROXY) \ 303 #go get -v golang.org/x/lint/golint 304 @echo " > installing golint ..." 305 @$(GO) install golang.org/x/lint/golint 306 @cd $(GOBASE) 307 308 $(BIN)/gocyclo: | $(GOBASE) # # # ❶ 309 @echo " > installing gocyclo ..." 310 @$(GO) install github.com/fzipp/gocyclo 311 312 $(BIN)/yolo: | $(GOBASE) # # # ❶ 313 @echo " > installing yolo ..." 314 @$(GO) install github.com/azer/yolo 315 316 $(BIN)/godoc: | $(GOBASE) # # # ❶ 317 @echo " > installing godoc ..." 318 @$(GO) install golang.org/x/tools/cmd/godoc 319 320 $(BASE): 321 # @mkdir -p $(dir $@) 322 # @ln -sf $(CURDIR) $@ 323 324 325 ## godoc: run godoc server at "localhost;6060" 326 godoc: | $(GOBASE) $(BIN)/godoc 327 @echo " > PWD = $(shell pwd)" 328 @echo " > started godoc server at :6060: http://localhost:6060/pkg/github.com/hedzr/$(PROJECTNAME1) ..." 329 @echo " $ cd $(GOPATH_) godoc -http=:6060 -index -notes '(BUG|TODO|DONE|Deprecated)' -play -timestamps" 330 ( cd $(GOPATH_) && pwd && godoc -v -index -http=:6060 -notes '(BUG|TODO|DONE|Deprecated)' -play -timestamps -goroot .; ) 331 # https://medium.com/@elliotchance/godoc-tips-tricks-cda6571549b 332 333 334 ## godoc1: run godoc server at "localhost;6060" 335 godoc1: # | $(GOBASE) $(BIN)/godoc 336 @echo " > PWD = $(shell pwd)" 337 @echo " > started godoc server at :6060: http://localhost:6060/pkg/github.com/hedzr/$(PROJECTNAME1) ..." 338 #@echo " $ GOPATH=$(GOPATH) godoc -http=:6060 -index -notes '(BUG|TODO|DONE|Deprecated)' -play -timestamps" 339 godoc -v -index -http=:6060 -notes '(BUG|TODO|DONE|Deprecated)' -play -timestamps # -goroot $(GOPATH) 340 # gopkg.in/hedzr/errors.v2.New 341 # -goroot $(GOPATH) -index 342 # https://medium.com/@elliotchance/godoc-tips-tricks-cda6571549b 343 344 ## fmt: =`format`, run gofmt tool 345 fmt: format 346 347 ## format: run gofmt tool 348 format: | $(GOBASE) 349 @echo " > gofmt ..." 350 @GOPATH=$(GOPATH) GOBIN=$(BIN) GO111MODULE=$(GO111MODULE) GOPROXY=$(GOPROXY) \ 351 gofmt -l -w -s . 352 353 ## lint: run golint tool 354 lint: | $(GOBASE) $(GOLINT) 355 @echo " > golint ..." 356 @GOPATH=$(GOPATH) GOBIN=$(BIN) GO111MODULE=$(GO111MODULE) GOPROXY=$(GOPROXY) \ 357 $(GOLINT) ./... 358 359 ## cov: =`coverage`, run go coverage test 360 cov: coverage 361 362 ## gocov: =`coverage`, run go coverage test 363 gocov: coverage 364 365 ## coverage: run go coverage test 366 coverage: | $(GOBASE) 367 @echo " > gocov ..." 368 @$(GO) test $(COVER_TEST_TARGETS) -v -race -coverprofile=coverage.txt -covermode=atomic -timeout=20m -test.short | tee coverage.log 369 @$(GO) tool cover -html=coverage.txt -o cover.html 370 @open cover.html 371 372 ## coverage-full: run go coverage test (with the long tests) 373 coverage-full: | $(GOBASE) 374 @echo " > gocov ..." 375 @$(GO) test $(COVER_TEST_TARGETS) -v -race -coverprofile=coverage.txt -covermode=atomic -timeout=20m | tee coverage.log 376 @$(GO) tool cover -html=coverage.txt -o cover.html 377 @open cover.html 378 379 ## codecov: run go test for codecov; (codecov.io) 380 codecov: | $(GOBASE) 381 @echo " > codecov ..." 382 @$(GO) test $(COVER_TEST_TARGETS) -v -race -coverprofile=coverage.txt -covermode=atomic 383 @bash <(curl -s https://codecov.io/bash) -t $(CODECOV_TOKEN) 384 385 ## cyclo: run gocyclo tool 386 cyclo: | $(GOBASE) $(GOCYCLO) 387 @echo " > gocyclo ..." 388 @GOPATH=$(GOPATH) GO111MODULE=$(GO111MODULE) GOPROXY=$(GOPROXY) \ 389 $(GOCYCLO) -top 20 . 390 391 ## bench-std: benchmark test 392 bench-std: 393 @echo " > benchmark testing ..." 394 @$(GO) test -bench="." -run=^$ -benchtime=10s $(COVER_TEST_TARGETS) 395 # go test -bench "." -run=none -test.benchtime 10s 396 # todo: go install golang.org/x/perf/cmd/benchstat 397 398 399 ## bench: benchmark test 400 bench: 401 @echo " > benchmark testing (manually) ..." 402 @$(GO) test ./fast -v -race -run 'TestQueuePutGetLong' -timeout=20m 403 404 405 ## linux-test: call ci/linux_test/Makefile 406 linux-test: 407 @echo " > linux-test ..." 408 @-touch $(STDERR) 409 @-rm $(STDERR) 410 @echo $(MAKE) -f ./ci/linux_test/Makefile test 2> $(STDERR) 411 @$(MAKE) -f ./ci/linux_test/Makefile test 2> $(STDERR) 412 @echo " > linux-test ..." 413 $(MAKE) -f ./ci/linux_test/Makefile all 2> $(STDERR) 414 @cat $(STDERR) | sed -e '1s/.*/\nError:\n/' 1>&2 415 416 417 ## docker: docker build 418 docker: 419 @if [ -n "$(DOCKER_APP_NAME)" ]; then \ 420 echo " > docker build $(DOCKER_APP_NAME):$(VERSION)..."; \ 421 docker build --build-arg CN=1 --build-arg GOPROXY="https://gocenter.io,direct" -t $(DOCKER_APP_NAME):latest -t $(DOCKER_APP_NAME):$(VERSION) . ; \ 422 else \ 423 echo " > docker build not available since DOCKER_APP_NAME is empty"; \ 424 fi 425 426 427 ## rshz: rsync to my TP470P 428 rshz: 429 @echo " > sync to hz-pc ..." 430 rsync -arztopg --delete $(GOBASE) hz-pc:$(HZ_PC_GOBASE)/src/github.com/hedzr/ 431 432 433 434 435 436 .PHONY: directories 437 438 MKDIR_P = mkdir -p 439 440 directories: $(GOBIN) 441 442 $(GOBIN): 443 $(MKDIR_P) $(GOBIN) 444 445 446 447 448 .PHONY: printvars info help all 449 printvars: 450 $(foreach V, $(sort $(filter-out .VARIABLES,$(.VARIABLES))), $(info $(v) = $($(v))) ) 451 # Simple: 452 # (foreach v, $(filter-out .VARIABLES,$(.VARIABLES)), $(info $(v) = $($(v))) ) 453 454 print-%: 455 @echo $* = $($*) 456 457 info: 458 @echo " GO_VERSION: $(GOVERSION)" 459 @echo " GOPROXY: $(GOPROXY)" 460 @echo " GOROOT: $(GOROOT) | GOPATH: $(GOPATH)" 461 #@echo " GO111MODULE: $(GO111MODULE)" 462 @echo 463 @echo " GOBASE: $(GOBASE)" 464 @echo " GOBIN: $(GOBIN)" 465 @echo " PROJECTNAME: $(PROJECTNAME)" 466 @echo " APPNAME: $(APPNAME)" 467 @echo " VERSION: $(VERSION)" 468 @echo " BUILDTIME: $(BUILDTIME)" 469 @echo " GIT_VERSION: $(GIT_VERSION)" 470 @echo " GIT_REVISION: $(GIT_REVISION)" 471 @echo 472 @echo " MAIN_BUILD_PKG: $(MAIN_BUILD_PKG)" 473 @echo " MAIN_APPS: $(MAIN_APPS)" 474 @echo 475 #@echo "export GO111MODULE=on" 476 @echo "export GOPROXY=$(GOPROXY)" 477 #@echo "export GOPATH=$(GOPATH)" 478 479 all: help 480 help: Makefile 481 @echo 482 @echo " Choose a command run in "$(PROJECTNAME)":" 483 @echo 484 @sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /' 485 @echo 486