经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Go语言 » 查看文章
golang构建工具Makefile使用详解
来源:jb51  时间:2022/7/25 15:26:34  对本文有异议

正文

可能是因为编译太简单了,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. .PHONY: all
  3. all: third vendor api json build test stat
  4. build: cmd/rta_server/*.go internal/*/*.go scripts/version.sh Makefile vendor api json
  5. @echo "编译"
  6. @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/
  7. vendor: glide.lock glide.yaml
  8. @echo "下载 golang 依赖"
  9. glide install
  10. api: vendor
  11. @echo "生成协议文件"
  12. @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/
  13. json: internal/rcommon/rta_common_easyjson.go
  14. internal/rcommon/rta_common_easyjson.go: internal/rcommon/rta_common.go Makefile
  15. easyjson internal/rcommon/rta_common.go
  16. .PHONY: test
  17. test: vendor api json
  18. @echo "运行单元测试"
  19. go test -cover internal/rranker/*.go
  20. go test -cover internal/rserver/*.go
  21. go test -cover internal/rworker/*.go
  22. go test -cover internal/rloader/*.go
  23. go test -cover internal/rrecall/*.go
  24. go test -cover internal/rmaster/*.go
  25. go test -cover internal/rsender/*.go
  26. benchmark: benchmarkloader benchmarkall
  27. .PHONY: benchmarkloader
  28. benchmarkloader: vendor api json
  29. @echo "运行 loader 性能测试"
  30. go test -timeout 2h -bench BenchmarkS3Loader_Load -benchmem -cpuprofile cpu.out -memprofile mem.out -run=^? internal/rloader/*
  31. go tool pprof -svg ./rloader.test cpu.out > cpu.benchmarkloader.svg
  32. go tool pprof -svg ./rloader.test mem.out > mem.benchmarkloader.svg
  33. .PHONY: benchmarkserver
  34. benchmarkserver: vendor api json
  35. @echo "运行 server 性能测试"
  36. go test -timeout 2h -bench BenchmarkServer -benchmem -cpuprofile cpu.out -memprofile mem.out -run=^? internal/rserver/*
  37. go tool pprof -svg ./rserver.test cpu.out > cpu.benchmarkserver.svg
  38. go tool pprof -svg ./rserver.test mem.out > mem.benchmarkserver.svg
  39. .PHONY: benchmarkall
  40. benchmarkall: vendor api json
  41. @echo "运行 server 性能测试"
  42. go test -timeout 2h -bench BenchmarkAll -benchmem -cpuprofile cpu.out -memprofile mem.out -run=^? internal/rserver/*
  43. go tool pprof -svg ./rserver.test cpu.out > cpu.benchmarkall.svg
  44. go tool pprof -svg ./rserver.test mem.out > mem.benchmarkall.svg
  45. .PHONY: benchmarkcache
  46. benchmarkcache: vendor api json
  47. @echo "测试 redis 集群性能"
  48. go test -timeout 5m -bench BenchmarkRtaCacheBatch -benchmem -cpuprofile cpu.out -memprofile mem.out -run=^? internal/rserver/*
  49. .PHONY: stat
  50. stat: cloc gocyclo
  51. @echo "代码行数统计"
  52. @ls internal/*/* scripts/* configs/* Makefile | xargs cloc --by-file
  53. @echo "圈复杂度统计"
  54. @ls internal/*/* | grep -v _test | xargs gocyclo
  55. @ls internal/*/* | grep -v _test | xargs gocyclo | awk '{sum+=?1}END{printf("总圈复杂度: %s", sum)}'
  56. .PHONY: clean
  57. clean:
  58. rm -rf build
  59. .PHONY: deep_clean
  60. deep_clean:
  61. rm -rf vendor api build third
  62. third: protoc glide golang cloc gocyclo easyjson
  63. .PHONY: protoc
  64. protoc: golang
  65. @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; }
  66. @hash protoc-gen-go 2>/dev/null || { echo "安装 protobuf golang 插件 protoc-gen-go" && go get -u github.com/golang/protobuf/{proto,protoc-gen-go}; }
  67. .PHONY: glide
  68. glide: golang
  69. @mkdir -p ?GOPATH/bin
  70. @hash glide 2>/dev/null || { echo "安装依赖管理工具 glide" && curl https://glide.sh/get | sh; }
  71. .PHONY: golang
  72. golang:
  73. @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; }
  74. .PHONY: cloc
  75. cloc:
  76. @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; }
  77. .PHONY: gocyclo
  78. gocyclo: golang
  79. @hash gocyclo 2>/dev/null || { echo "安装代码圈复杂度统计工具 gocyclo" && go get -u github.com/fzipp/gocyclo; }
  80. .PHONY: easyjson
  81. easyjson: golang
  82. @hash easyjson 2>/dev/null || { echo "安装 json 编译工具 easyjson" && go get -u github.com/mailru/easyjson/...; }

以上就是golang构建工具Makefile使用详解的详细内容,更多关于golang构建工具Makefile的资料请关注w3xue其它相关文章!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号