互联网就业技术指南

golang构建工具之Makefile

2018-11-12  本文已影响82人  Chole121

可能是因为编译太简单了,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 工具

1.export PATH:=${PATH}:${GOPATH}/bin:$(shell pwd)/third/go/bin:$(shell pwd)/third/protobuf/bin:$(shell pwd)/third/cloc-1.76
2.
3..PHONY: all
4.all: third vendor api json build test stat
5.
6.build: cmd/rta_server/*.go internal/*/*.go scripts/version.sh Makefile vendor api json
7.    @echo "编译"
8.    @rm -rf build/ && mkdir -p build/bin/ && \
9.   go build -ldflags "-X 'main.AppVersion=`sh scripts/version.sh`'" cmd/rta_server/main.go && \
10.    mv main build/bin/rta_server && \
11.    cp -r configs build/configs/
12.
13.vendor: glide.lock glide.yaml
14.    @echo "下载 golang 依赖"
15.    glide install
16.
17.api: vendor
18.    @echo "生成协议文件"
19.    @rm -rf api && mkdir api && \
20.    cd vendor/gitlab.mobvista.com/vta/rta_proto.git/ && \
21.    protoc --go_out=plugins=grpc:. *.proto && \
22.    cd - && \
23.    cp vendor/gitlab.mobvista.com/vta/rta_proto.git/* api/
24.
25.json: internal/rcommon/rta_common_easyjson.go
26.
27.internal/rcommon/rta_common_easyjson.go: internal/rcommon/rta_common.go Makefile
28.    easyjson internal/rcommon/rta_common.go
29.
30..PHONY: test
31.test: vendor api json
32.    @echo "运行单元测试"
33.    go test -cover internal/rranker/*.go
34.    go test -cover internal/rserver/*.go
35.    go test -cover internal/rworker/*.go
36.    go test -cover internal/rloader/*.go
37.    go test -cover internal/rrecall/*.go
38.    go test -cover internal/rmaster/*.go
39.    go test -cover internal/rsender/*.go
40.
41.benchmark: benchmarkloader benchmarkall
42.
43..PHONY: benchmarkloader
44.benchmarkloader: vendor api json
45.   @echo "运行 loader 性能测试"
46.    go test -timeout 2h -bench BenchmarkS3Loader_Load -benchmem -cpuprofile cpu.out -memprofile mem.out -run=^$$ internal/rloader/*
47.    go tool pprof -svg ./rloader.test cpu.out > cpu.benchmarkloader.svg
48.    go tool pprof -svg ./rloader.test mem.out > mem.benchmarkloader.svg
49.
50..PHONY: benchmarkserver
51.benchmarkserver: vendor api json
52.    @echo "运行 server 性能测试"
53.    go test -timeout 2h -bench BenchmarkServer -benchmem -cpuprofile cpu.out -memprofile mem.out -run=^$$ internal/rserver/*
54.    go tool pprof -svg ./rserver.test cpu.out > cpu.benchmarkserver.svg
55.    go tool pprof -svg ./rserver.test mem.out > mem.benchmarkserver.svg
56.
57..PHONY: benchmarkall
58.benchmarkall: vendor api json
59.    @echo "运行 server 性能测试"
60.    go test -timeout 2h -bench BenchmarkAll -benchmem -cpuprofile cpu.out -memprofile mem.out -run=^$$ internal/rserver/*
61.    go tool pprof -svg ./rserver.test cpu.out > cpu.benchmarkall.svg    
62.    go tool pprof -svg ./rserver.test mem.out > mem.benchmarkall.svg
63.
64..PHONY: benchmarkcache
65.benchmarkcache: vendor api json
66.    @echo "测试 redis 集群性能"
67.    go test -timeout 5m -bench BenchmarkRtaCacheBatch -benchmem -cpuprofile cpu.out -memprofile mem.out -run=^$$ internal/rserver/*
68.
69..PHONY: stat
70.stat: cloc gocyclo
71.    @echo "代码行数统计"
72.    @ls internal/*/* scripts/* configs/* Makefile | xargs cloc --by-file
73.    @echo "圈复杂度统计"
74.    @ls internal/*/* | grep -v _test | xargs gocyclo
75.    @ls internal/*/* | grep -v _test | xargs gocyclo | awk '{sum+=$$1}END{printf("总圈复杂度: %s", sum)}'
76.
77..PHONY: clean
78.clean:
79.    rm -rf build
80.
81..PHONY: deep_clean
82.deep_clean:
83.    rm -rf vendor api build third
84.
85.third: protoc glide golang cloc gocyclo easyjson
86.
87..PHONY: protoc
88.protoc: golang
89.    @hash protoc 2>/dev/null || { \
90.        echo "安装 protobuf 代码生成工具 protoc" && \
91.        mkdir -p third && cd third && \
92.        wget https://github.com/google/protobuf/releases/download/v3.2.0/protobuf-cpp-3.2.0.tar.gz && \
93.        tar -xzvf protobuf-cpp-3.2.0.tar.gz && \
94.        cd protobuf-3.2.0 && \
95.        ./configure --prefix=`pwd`/../protobuf && \
96.        make -j8 && \
97.        make install && \
98.        cd ../.. && \
99.        protoc --version; \
100.    }
101.    @hash protoc-gen-go 2>/dev/null || { \
102.       echo "安装 protobuf golang 插件 protoc-gen-go" && \
103.        go get -u github.com/golang/protobuf/{proto,protoc-gen-go}; \
104.    }
105.
106..PHONY: glide
107.glide: golang
108.    @mkdir -p $$GOPATH/bin
109.    @hash glide 2>/dev/null || { \
110.        echo "安装依赖管理工具 glide" && \
111.        curl https://glide.sh/get | sh; \
112.    }
113.
114..PHONY: golang
115.golang:
116.    @hash go 2>/dev/null || { \
117.        echo "安装 golang 环境 go1.10" && \
118.        mkdir -p third && cd third && \
119.        wget https://dl.google.com/go/go1.10.linux-amd64.tar.gz && \
120.        tar -xzvf go1.10.linux-amd64.tar.gz && \
121.        cd .. && \
122.        go version; \
123.    }
124.
125..PHONY: cloc
126.cloc:
127.    @hash cloc 2>/dev/null || { \
128.        echo "安装代码统计工具 cloc" && \
129.        mkdir -p third && cd third && \
130.        wget https://github.com/AlDanial/cloc/archive/v1.76.zip && \
131.       unzip v1.76.zip; \
132.    }
133.
134..PHONY: gocyclo
135.gocyclo: golang
136.    @hash gocyclo 2>/dev/null || { \
137.        echo "安装代码圈复杂度统计工具 gocyclo" && \
138.        go get -u github.com/fzipp/gocyclo; \
139.    }
140.
141..PHONY: easyjson
142.easyjson: golang
143.    @hash easyjson 2>/dev/null || { \
144.        echo "安装 json 编译工具 easyjson" && \
145.        go get -u github.com/mailru/easyjson/...; \
146.    }

文章作者:hatlonely
文章链接:http://www.hatlonely.com/2018/04/11/golang-构建工具之-Makefile/

小编微信:grey0805,欢迎添加

上一篇下一篇

猜你喜欢

热点阅读