Makefile 函数

基本格式

$(<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 环境中定义了
image.png
image.png

添加后缀

$(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:
image.png

添加前缀

Makefile:

list = file1 file2 file3
all:
	@echo $(addprefix "a_",$(list))
	@echo $(addprefix "b_",main)
	@echo $(addprefix "c_",cc kk)

result:
image.png

获取后缀

$(suffix names...) 获取 names 中的后缀
Makefile:

ex := a.txt b.md b.me b.mf
## `$(suffix names...)` 获取 names 中的后缀
suf:
	@echo $(suffix $(ex))

result:
image.png

字符串处理
函数作用
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:
image.png

路径处理
函数作用
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:
image.png

信息处理

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:
image.png

过滤处理
函数作用
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:
image.png

eval

Makefile:
$(eval express)将 express 作为 makefile的一部分执行

ex := miss you
# `$(eval express)` 将 express 作为 makefile的一部分执行
ev:
	$(eval ex := Miss You)
	@echo $(ex)

result:
image.png

shell

$(shell command)使用shell执行command并返回结果
Makefile:

pwd := $(shell pwd)
# `$(shell command)` 使用shell执行command并返回结果
all:
	@echo $(pwd)

result:
image.png

wildcard

$(wildcard) 扩展通配符可以使用*匹配
Makefile:

wil := $(wildcard *.go)
# `$(wildcard)` 扩展通配符可以使用*匹配
all:
	@echo $(wil)

result:
image.png

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值