golang构建工具Makefile使用详解

 更新时间:2022年07月25日 12:14:44   作者:hatlonely  
这篇文章主要为大家介绍了golang构建工具Makefile的使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

正文

可能是因为编译太简单了,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/...; \
    }

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

相关文章

  • 一文详解Go Http Server原理

    一文详解Go Http Server原理

    这篇文章主要为大家介绍了Go Http Server原理示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-01-01
  • GO语言类型查询类型断言示例解析

    GO语言类型查询类型断言示例解析

    这篇文章主要为大家介绍了GO语言类型判断及类型断言,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职加薪
    2022-04-04
  • 一文带你了解Golang中强大的重试机制

    一文带你了解Golang中强大的重试机制

    在 Go 语言中,处理瞬态错误是常见的挑战,这些错误可能会在一段时间后自动恢复,因此,重试机制在这些情况下非常重要,所以本文就来和大家聊聊Golang中强大的重试机制吧
    2025-01-01
  • Golang 官方依赖注入工具wire示例详解

    Golang 官方依赖注入工具wire示例详解

    这篇文章主要为大家介绍了Golang 官方依赖注入工具wire示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • Go语言对JSON进行编码和解码的方法

    Go语言对JSON进行编码和解码的方法

    这篇文章主要介绍了Go语言对JSON进行编码和解码的方法,涉及Go语言操作json的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-02-02
  • golang调用windows平台的dll库的方法实现

    golang调用windows平台的dll库的方法实现

    本文主要介绍了golang调用windows平台的dll库的方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-03-03
  • GoLand编译带有构建标签的程序思路详解

    GoLand编译带有构建标签的程序思路详解

    这篇文章主要介绍了GoLand编译带有构建标签的程序,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-11-11
  • 使用go xorm来操作mysql的方法实例

    使用go xorm来操作mysql的方法实例

    今天小编就为大家分享一篇关于使用go xorm来操作mysql的方法实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-04-04
  • Golang Mongodb模糊查询的使用示例

    Golang Mongodb模糊查询的使用示例

    这篇文章主要给大家介绍了关于Golang Mongodb模糊查询的使用示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-02-02
  • 详解golang中的闭包与defer

    详解golang中的闭包与defer

    闭包一个函数与其相关的引用环境组合的一个实体,其实可以理解为面向对象中类中的属性与方法,这篇文章主要介绍了golang的闭包与defer,需要的朋友可以参考下
    2022-09-09

最新评论