基本格式
$(<function> <arguments>)
function: 函数名
arguments: 参数
origin
输出变量的来源
输出值 | 含义 |
---|---|
undefined | 变量未定义 |
file | 文件中定义 |
environment | 环境变量 |
command line | 命令行传入的变量 |
override | 该变量使用 override 覆盖过 |
automatic | 自动变量 |
Makefile:
status = ok
override name = 1
name = 2
variable=
undef:
# 未定义 undefined
@echo $(origin isProduct)
file:
# file 文件中定义
@echo $(origin status)
env:
# environment 环境变量
@echo $(origin WORKSPACE)
commandLine:
# make
@echo $(origin kk)
over:
# override
@echo $(origin name)
ne.%:
# automatic
@echo $(origin *)
result:
其中 WORKSPACE 是在 Linux 环境中定义了
添加后缀
$(addsuffix suffix, p.....)
给 p 添加 suffix 后缀
Makefile:
list = file1 file2 file3
all:
@echo $(addsuffix ".txt",$(list))
@echo $(addsuffix ".exe",main)
@echo $(addsuffix ".exe",cc kk)
result:
添加前缀
Makefile:
list = file1 file2 file3
all:
@echo $(addprefix "a_",$(list))
@echo $(addprefix "b_",main)
@echo $(addprefix "c_",cc kk)
result:
获取后缀
$(suffix names...)
获取 names 中的后缀
Makefile:
ex := a.txt b.md b.me b.mf
## `$(suffix names...)` 获取 names 中的后缀
suf:
@echo $(suffix $(ex))
result:
字符串处理
函数 | 作用 |
---|---|
subst | $(subst find,replace,text) **在text中将find替换为replace |
first_word | $(firstword text) 获取text第一个单词 |
lastword | $(lastword text) 获取text最后一个单词 |
word | $(word position,text) 获取text中,位置为position的单词 |
strip | $(strip text) 剥离text头部和尾部空字符串 |
Makefile:
ex = good good study
ex2 = " good boby"
## 将 study 替换为 LOVE
## `$(subst find,replace,text)`在text中将find替换为replace
sub:
@echo $(subst study,good,$(ex))
## `$(firstword text)`获取text第一个单词
## 输出 good
first_word:
@echo $(firstword $(ex))
## `$(lastword text)` 获取text最后一个单词
## 输出 study
last_word:
@echo $(lastword $(ex))
## 输出 study
## `$(word position,text)` 获取text中,位置为position的单词
word:
@echo $(word 3,$(ex))
## `$(strip text)` 剥离text头部和尾部空字符串
strip:
@echo $(ex2)
@echo $(strip $(ex2))
result:
路径处理
函数 | 作用 |
---|---|
abspath | $(abspath path) 将 path 转为绝对路径 |
dir | $(dir path) 获取path的上级目录 |
notdir | $(notdir path) 获取path 最后一个 “/” 之后的部分 |
Makefile:
path = .
## `$(abspath path)` 将 path 转为绝对路径
abs:
@echo $(abspath $(path))
## `$(dir path)` 获取path的上级目录
dir:
@echo $(dir $(abspath $(path)))
## `$(notdir path)` 获取path 最后一个 "/" 之后的部分
not_dir:
@echo $(notdir $(abspath $(path)))
result:
信息处理
Makefile:
函数 | 作用 |
---|---|
info | $(info text) 输出 text |
warning | $(warning text) 输出一段警告信息 后续操作继续运行 |
error | $(error text) 输出一段致命错误信息 终断后续操作运行 |
## `$(info text)` 输出 text
in:
$(info "info message")
## `$(warning text)` 输出一段警告信息 后续操作继续运行
wa:
$(warning "warning message")
@echo "接着奏乐接着舞"
## `$(error text)` 输出一段致命错误信息 终断后续操作运行
er:
$(error "error message")
@echo "凉凉夜色"
result:
过滤处理
函数 | 作用 |
---|---|
filter | $(filter pattern,text) 从text中获取符合 pattern匹配模式的单词 |
filter-out | $(filter-out pattern,text) 从text中获取不符合 pattern匹配模式的单词 |
Makefile:
ex := a.txt b.md b.me b.mf
## `$(filter pattern,text)` 从text中获取符合 pattern匹配模式的单词
fi:
@echo $(filter b.m%,$(ex))
## `$(filter-out pattern,text)` 从text中获取不符合 pattern匹配模式的单词
nfi:
@echo $(filter-out b.m%,$(ex))
result:
eval
Makefile:
$(eval express)
将 express 作为 makefile的一部分执行
ex := miss you
# `$(eval express)` 将 express 作为 makefile的一部分执行
ev:
$(eval ex := Miss You)
@echo $(ex)
result:
shell
$(shell command)
使用shell执行command并返回结果
Makefile:
pwd := $(shell pwd)
# `$(shell command)` 使用shell执行command并返回结果
all:
@echo $(pwd)
result:
wildcard
$(wildcard)
扩展通配符可以使用*
匹配
Makefile:
wil := $(wildcard *.go)
# `$(wildcard)` 扩展通配符可以使用*匹配
all:
@echo $(wil)
result: