golang構(gòu)建工具M(jìn)akefile使用詳解
正文
可能是因?yàn)榫幾g太簡單了,golang 并沒有一個(gè)官方的構(gòu)建工具(類似于 java 的 maven 和 gradle之類的),但是除了編譯,我們可能還需要下載依賴,運(yùn)行測試,甚至像 easyjson,protobuf,thrift 這樣的工具下載和代碼生成,如果沒有構(gòu)建工具,這些工作就會(huì)非常麻煩
為了解決這個(gè)問題,之前寫過一個(gè) everything.sh
的腳本,把所有的操作都封裝在這個(gè)腳本里面,只需要執(zhí)行類似于 sh everything.sh dependency
的命令就可以完成對應(yīng)的工作,大大簡化了構(gòu)建過程,但是也有一個(gè)問題,shell 腳本本身的可讀性并不是很好,而且對于各個(gè)操作之間的依賴不好描述
一次偶然的機(jī)會(huì),在 github 上看到有人用 Makefile,就嘗試了一下,發(fā)現(xiàn)真的非常合適,Makefile 本身就是用來描述依賴的,可讀性非常好,而且與強(qiáng)大的 shell 結(jié)合在一起,基本可以實(shí)現(xiàn)任何想要的功能
下面是我在實(shí)際項(xiàng)目中使用的一個(gè) Makefile,支持的功能包括
make build
: 編譯
make vendor
: 下載依賴
make api
: 生成協(xié)議代碼
make json
: easyjson 代碼生成
make test
: 運(yùn)行單元測試
make benchmark
: 運(yùn)行性能測試
make stat
: 代碼復(fù)雜度統(tǒng)計(jì),代碼行數(shù)統(tǒng)計(jì)
make clean
: 清理 build 目錄
make deep_clean
: 清理所有代碼以外的其他文件
make third
: 下載所有依賴的第三方工具
make protoc
: 下載 protobuf 工具
make glide
: 下載 glide 依賴管理工具
make golang
: 下載 golang 環(huán)境
make cloc
: 下載 cloc 統(tǒng)計(jì)工具
make gocyclo
: 下載 gocyclo 圈復(fù)雜度計(jì)算工具
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 "生成協(xié)議文件" @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 "運(yùn)行單元測試" 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 "運(yùn)行 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 "運(yùn)行 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 "運(yùn)行 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 "代碼行數(shù)統(tǒng)計(jì)" @ls internal/*/* scripts/* configs/* Makefile | xargs cloc --by-file @echo "圈復(fù)雜度統(tǒng)計(jì)" @ls internal/*/* | grep -v _test | xargs gocyclo @ls internal/*/* | grep -v _test | xargs gocyclo | awk '{sum+=?1}END{printf("總?cè)?fù)雜度: %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 環(huán)境 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 "安裝代碼統(tǒng)計(jì)工具 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 "安裝代碼圈復(fù)雜度統(tǒng)計(jì)工具 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/...; \ }
以上就是golang構(gòu)建工具M(jìn)akefile使用詳解的詳細(xì)內(nèi)容,更多關(guān)于golang構(gòu)建工具M(jìn)akefile的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Go-ethereum?解析ethersjs中產(chǎn)生的簽名信息思路詳解
這篇文章主要介紹了Go-ethereum?解析ethersjs中產(chǎn)生的簽名信息,我們解析簽名的需要知道,簽名的消息,簽名,和公鑰,按照這個(gè)思路,我們可以通過ethers實(shí)現(xiàn)消息的簽名,也可以通過go-ethereum實(shí)現(xiàn),需要的朋友可以參考下2022-08-08golang的匿名函數(shù)和普通函數(shù)的區(qū)別解析
匿名函數(shù)是不具名的函數(shù),可以在不定義函數(shù)名的情況下直接使用,通常用于函數(shù)內(nèi)部的局部作用域中,這篇文章主要介紹了golang的匿名函數(shù)和普通函數(shù)的區(qū)別,需要的朋友可以參考下2023-03-03go mayfly開源項(xiàng)目代碼結(jié)構(gòu)設(shè)計(jì)
這篇文章主要為大家介紹了go mayfly開源項(xiàng)目代碼結(jié)構(gòu)設(shè)計(jì)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11深入解析Go語言中HTTP請求處理的底層實(shí)現(xiàn)
本文將詳細(xì)介紹?Go?語言中?HTTP?請求處理的底層機(jī)制,包括工作流程、創(chuàng)建?Listen?Socket?監(jiān)聽端口、接收客戶端請求并建立連接以及處理客戶端請求并返回響應(yīng)等,需要的朋友可以參考下2023-05-05go語言LeetCode題解999可以被一步捕獲的棋子數(shù)
這篇文章主要為大家介紹了go語言LeetCode題解999可以被一步捕獲的棋子數(shù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12MacOS下本地golang環(huán)境搭建詳細(xì)教程
這篇文章主要介紹了MacOS下本地golang環(huán)境搭建詳細(xì)教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09