打赏

Makefile

Makefile语法

<target> : <prerequisites> 
[tab]  <commands> 

- target : 即自定义的想要执行的命令
- prerequisites: 前置条件,即执行 target 命令之前执行的命令
- commands : 具体的执行的命令
- .PHONY 伪指令,内置的关键字
- 不带参数,默认执行第一个 target
- @ 表示禁止回声,即终端不会打印真实的执行命令
- # 表示注释
- ${val} 表示变量,和 shell 脚本中的变量的声明和使用一致
- 允许使用 通配符

1.在命令的前面加上@,就可以关闭回声。

// 现在再执行make test,就不会有任何输出。

test:
    @# 这是测试

2.伪目标

// 声明clean是"伪目标"之后,make就不会去检查是否存在一个叫做clean的文件,而是每次运行都执行对应的命令。

.PHONY: clean
clean:
        rm *.o temp

3.内置变量

$(CC) 指向当前使用的编译器,$(MAKE) 指向当前使用的Make工具.
$@指代当前目标,就是Make命令当前构建的那个目标。 $< 指代第一个前置条件。
  1. go makefile 示例

make default : 编译
make fmt: 格式化
make vet: 静态检查
make test: 运行测试
make install: 下载依赖库
make clean: 移除编译的二进制文件

BINARY="example"
VERSION=1.0.0
BUILD=`date +%FT%T%z`

PACKAGES=`go list ./... | grep -v /vendor/`
VETPACKAGES=`go list ./... | grep -v /vendor/ | grep -v /examples/`
GOFILES=`find . -name "*.go" -type f -not -path "./vendor/*"`

default:
	@go build -o ${BINARY} -tags=jsoniter

list:
	@echo ${PACKAGES}
	@echo ${VETPACKAGES}
	@echo ${GOFILES}

fmt:
	@gofmt -s -w ${GOFILES}

fmt-check:
	@diff=$$(gofmt -s -d $(GOFILES)); \
	if [ -n "$$diff" ]; then \
		echo "Please run 'make fmt' and commit the result:"; \
		echo "$${diff}"; \
		exit 1; \
	fi;

install:
	@govendor sync -v

test:
	@go test -cpu=1,2,4 -v -tags integration ./...

vet:
	@go vet $(VETPACKAGES)

docker:
    @docker build -t wuxiaoxiaoshen/example:latest .

clean:
	@if [ -f ${BINARY} ] ; then rm ${BINARY} ; fi

.PHONY: default fmt fmt-check install test vet docker clean

相关链接

http://c.biancheng.net/view/7113.html

posted @ 2020-05-15 15:51  苍山落暮  阅读(190)  评论(0编辑  收藏  举报