1. 程式人生 > >golang 構建工具之 Makefile

golang 構建工具之 Makefile

worker 還需 工具 vendor redis 集群 fix cover IT itl

可能是因為編譯太簡單了,golang 並沒有一個官方的構建工具(類似於 java 的 maven 和 gradle之類的),但是除了編譯,我們可能還需要下載依賴,運行測試,甚至像 easyjson,protobuf,thrift 這樣的工具下載和代碼生成,如果沒有構建工具,這些工作就會非常麻煩

為了解決這個問題,之前寫過一個 everything.sh 的腳本,把所有的操作都封裝在這個腳本裏面,只需要執行類似於 sh everything.sh dependency 的命令就可以完成對應的工作,大大簡化了構建過程,但是也有一個問題,shell 腳本本身的可讀性並不是很好,而且對於各個操作之間的依賴不好描述

一次偶然的機會,在 github 上看到有人用 Makefile,就嘗試了一下,發現真的非常合適,Makefile 本身就是用來描述依賴的,可讀性非常好,而且與強大的 shell 結合在一起,基本可以實現任何想要的功能

下面是我在實際項目中使用的一個 Makefile,支持的功能包括

  • make build: 編譯
  • make vendor: 下載依賴
  • make api: 生成協議代碼
  • make json: easyjson 代碼生成
  • make test: 運行單元測試
  • make benchmark: 運行性能測試
  • make stat: 代碼復雜度統計,代碼行數統計
  • make clean: 清理 build 目錄
  • make deep_clean: 清理所有代碼以外的其他文件
  • make third: 下載所有依賴的第三方工具
  • make protoc: 下載 protobuf 工具
  • make glide: 下載 glide 依賴管理工具
  • make golang: 下載 golang 環境
  • make cloc: 下載 cloc 統計工具
  • make gocyclo: 下載 gocyclo 圈復雜度計算工具
  • make easyjson: 下載 easyjson 工具
export PATH:=${PATH}:${GOPATH}/bin:$(shell pwd)/third/go/bin:$(shell pwd)/third/protobuf/bin:
$(shell pwd)/third/cloc-1.76 .PHONY: all all: third vendor api json build test stat build: cmd/rta_server/*.go internal/*/*.go scripts/version.sh Makefile vendor api json @echo "編譯" @rm -rf build/ && mkdir -p build/bin/ && \ go build -ldflags "-X ‘main.AppVersion=`sh scripts/version.sh`‘" cmd/rta_server/main.go && \ mv main build/bin/rta_server && \ cp -r configs build/configs/ vendor: glide.lock glide.yaml @echo "下載 golang 依賴" glide install api: vendor @echo "生成協議文件" @rm -rf api && mkdir api && \ cd vendor/gitlab.mobvista.com/vta/rta_proto.git/ && \ protoc --go_out=plugins=grpc:. *.proto && \ cd - && \ cp vendor/gitlab.mobvista.com/vta/rta_proto.git/* api/ json: internal/rcommon/rta_common_easyjson.go internal/rcommon/rta_common_easyjson.go: internal/rcommon/rta_common.go Makefile easyjson internal/rcommon/rta_common.go .PHONY: test test: vendor api json @echo "運行單元測試" go test -cover internal/rranker/*.go go test -cover internal/rserver/*.go go test -cover internal/rworker/*.go go test -cover internal/rloader/*.go go test -cover internal/rrecall/*.go go test -cover internal/rmaster/*.go go test -cover internal/rsender/*.go benchmark: benchmarkloader benchmarkall .PHONY: benchmarkloader benchmarkloader: vendor api json @echo "運行 loader 性能測試" go test -timeout 2h -bench BenchmarkS3Loader_Load -benchmem -cpuprofile cpu.out -memprofile mem.out -run=^$$ internal/rloader/* go tool pprof -svg ./rloader.test cpu.out > cpu.benchmarkloader.svg go tool pprof -svg ./rloader.test mem.out > mem.benchmarkloader.svg .PHONY: benchmarkserver benchmarkserver: vendor api json @echo "運行 server 性能測試" go test -timeout 2h -bench BenchmarkServer -benchmem -cpuprofile cpu.out -memprofile mem.out -run=^$$ internal/rserver/* go tool pprof -svg ./rserver.test cpu.out > cpu.benchmarkserver.svg go tool pprof -svg ./rserver.test mem.out > mem.benchmarkserver.svg .PHONY: benchmarkall benchmarkall: vendor api json @echo "運行 server 性能測試" go test -timeout 2h -bench BenchmarkAll -benchmem -cpuprofile cpu.out -memprofile mem.out -run=^$$ internal/rserver/* go tool pprof -svg ./rserver.test cpu.out > cpu.benchmarkall.svg go tool pprof -svg ./rserver.test mem.out > mem.benchmarkall.svg .PHONY: benchmarkcache benchmarkcache: vendor api json @echo "測試 redis 集群性能" go test -timeout 5m -bench BenchmarkRtaCacheBatch -benchmem -cpuprofile cpu.out -memprofile mem.out -run=^$$ internal/rserver/* .PHONY: stat stat: cloc gocyclo @echo "代碼行數統計" @ls internal/*/* scripts/* configs/* Makefile | xargs cloc --by-file @echo "圈復雜度統計" @ls internal/*/* | grep -v _test | xargs gocyclo @ls internal/*/* | grep -v _test | xargs gocyclo | awk ‘{sum+=$$1}END{printf("總圈復雜度: %s", sum)}‘ .PHONY: clean clean: rm -rf build .PHONY: deep_clean deep_clean: rm -rf vendor api build third third: protoc glide golang cloc gocyclo easyjson .PHONY: protoc protoc: golang @hash protoc 2>/dev/null || { \ echo "安裝 protobuf 代碼生成工具 protoc" && \ mkdir -p third && cd third && \ wget https://github.com/google/protobuf/releases/download/v3.2.0/protobuf-cpp-3.2.0.tar.gz && \ tar -xzvf protobuf-cpp-3.2.0.tar.gz && \ cd protobuf-3.2.0 && \ ./configure --prefix=`pwd`/../protobuf && \ make -j8 && \ make install && \ cd ../.. && \ protoc --version; \ } @hash protoc-gen-go 2>/dev/null || { \ echo "安裝 protobuf golang 插件 protoc-gen-go" && \ go get -u github.com/golang/protobuf/{proto,protoc-gen-go}; \ } .PHONY: glide glide: golang @mkdir -p $$GOPATH/bin @hash glide 2>/dev/null || { \ echo "安裝依賴管理工具 glide" && \ curl https://glide.sh/get | sh; \ } .PHONY: golang golang: @hash go 2>/dev/null || { \ echo "安裝 golang 環境 go1.10" && \ mkdir -p third && cd third && \ wget https://dl.google.com/go/go1.10.linux-amd64.tar.gz && \ tar -xzvf go1.10.linux-amd64.tar.gz && \ cd .. && \ go version; \ } .PHONY: cloc cloc: @hash cloc 2>/dev/null || { \ echo "安裝代碼統計工具 cloc" && \ mkdir -p third && cd third && \ wget https://github.com/AlDanial/cloc/archive/v1.76.zip && \ unzip v1.76.zip; \ } .PHONY: gocyclo gocyclo: golang @hash gocyclo 2>/dev/null || { \ echo "安裝代碼圈復雜度統計工具 gocyclo" && \ go get -u github.com/fzipp/gocyclo; \ } .PHONY: easyjson easyjson: golang @hash easyjson 2>/dev/null || { \ echo "安裝 json 編譯工具 easyjson" && \ go get -u github.com/mailru/easyjson/...; \ }

轉載請註明出處
本文鏈接:http://www.hatlonely.com/2018/04/11/golang-構建工具之-Makefile/

golang 構建工具之 Makefile