命令执行后 make 会检查其返回码,如果成功则执行下一条指令,否则终止,可以使用 - 忽略出错命令
1 2 3
clean: -@rm not_exist_file @echo "hello world"
1 2 3 4
$ make clean rm: not_exist_file: No such file or directory make: [Makefile:7: clean] Error 1 (ignored) hello world
变量
Makefile 支持变量赋值,多行变量和环境变量,还内置了特殊变量和自动化变量
变量引用
引用变量,可以通过 ${} 或者 $() – 推荐
1 2 3
GO=go build: @$(GO) build -v .
展开后为
1 2 3
GO=go build: @go build -v .
变量赋值
=
B 最后的值为 c b,取的是最终的变量值
1 2 3
A = a B = $(A) b A = c
:=
B 最后的值为 a b,赋予当前位置的值
1 2 3
A = a B := $(A) b A = c
?=
如果该变量没有被赋值,则赋予等号后的值
1 2 3 4 5 6 7
PLATFORMS ?= linux_amd64 linux_arm64 Arch := X86 Arch ?= ARM
info: @echo $(PLATFORMS) @echo $(Arch)
1 2 3
$ make info linux_amd64 linux_arm64 X86
+=
将等号后面的值添加到前面的变量
1 2 3 4 5
Archs := X86 Archs += ARM
info: @echo $(Archs)
1 2
$ make info X86 ARM
多行变量
通过 define 关键字设置多行变量,变量中允许换行,变量的内容可以包含函数、命令和变量
1 2 3 4 5 6 7
define USAGE_OPTIONS
Options: DEBUG Whether to generate debug symbols. Default is 0. BINS The binaries to build. Default is all of cmd. V Set to 1 enable verbose build. Default is 0. endef
$* 使用最为广泛,如果目标文件的后缀能被 make 识别,那么 $* 就是除了后缀的那部分,如 foo.c -> foo
条件
1 2 3 4 5 6
info: ifeq ($(ROOT_PACKAGE),) $(error the variable ROOT_PACKAGE must be set prior to including golang.mk) else $(info the value of ROOT_PACKAGE is $(ROOT_PACKAGE)) endif
Targets: build Build source code for host platform. build.multiarch Build source code for multiple platforms. See option PLATFORMS. image Build docker images for host arch. image.multiarch Build docker images for multiple platforms. See option PLATFORMS. push Build docker images for host arch and push images to registry. push.multiarch Build docker images for multiple platforms and push images to registry. deploy Deploy updated components to development env. clean Remove all files that are created by building. lint Check syntax and styling of go sources. test Run unit test. cover Run unit test and get test coverage. release Release iam format Gofmt (reformat) package sources (exclude vendor dir if existed). verify-copyright Verify the boilerplate headers for all files. add-copyright Ensures source code files have copyright license headers. gen Generate all necessary files, such as error code files. ca Generate CA files for all iam components. install Install iam system with all its components. swagger Generate swagger document. serve-swagger Serve swagger spec and docs. dependencies Install necessary dependencies. tools install dependent tools. check-updates Check outdated dependencies of the go projects. help Show this help info.
Options: DEBUG Whether to generate debug symbols. Default is 0. BINS The binaries to build. Default is all of cmd. This option is available when using: make build/build.multiarch Example: make build BINS="iam-apiserver iam-authz-server" IMAGES Backend images to make. Default is all of cmd starting with iam-. This option is available when using: make image/image.multiarch/push/push.multiarch Example: make image.multiarch IMAGES="iam-apiserver iam-authz-server" REGISTRY_PREFIX Docker registry prefix. Default is marmotedu. Example: make push REGISTRY_PREFIX=ccr.ccs.tencentyun.com/marmotedu VERSION=v1.6.2 PLATFORMS The multiple platforms to build. Default is linux_amd64 and linux_arm64. This option is available when using: make build.multiarch/image.multiarch/push.multiarch Example: make image.multiarch IMAGES="iam-apiserver iam-pump" PLATFORMS="linux_amd64 linux_arm64" VERSION The version information compiled into binaries. The default is obtained from gsemver or git. V Set to 1 enable verbose build. Default is 0.
define USAGE_OPTIONS Options: ... BINS The binaries to build. Default is all of cmd. ... ... V Set to 1 enable verbose build. Default is 0. endef export USAGE_OPTIONS