3-Makefile高级语法

嵌套Makefile

在Makefile初级语法中已经提到过引用其它Makefile的方法,这里有另一种写法,并且可以向引用的其它Makefile传递参数。

【范例代码】不传递参数,只是调用子文件夹other中的Makefile

# Makefile 内容
all:
    @echo "主 Makefile begin"
    @cd ./other && make
    @echo "主 Makefile end"
# ./other/Makefile 内容
other-all:
    @echo "other makefile begin"
    @echo "other makefile end"

# bash中执行 make
$ ll
total 28K
-rw-r--r-- 1 wangyubin wangyubin  104 Sep 23 20:43 Makefile
-rw-r--r-- 1 wangyubin wangyubin  17K Sep 23 20:44 makefile.org   <-- 这个文件不用管
drwxr-xr-x 2 wangyubin wangyubin 4.0K Sep 23 20:42 other
$ ll other/
total 4.0K
-rw-r--r-- 1 wangyubin wangyubin 71 Sep 23 16:11 Makefile

$ make
主 Makefile begin
make[1]: Entering directory `/path/to/test/makefile/other'
other makefile begin
other makefile end
make[1]: Leaving directory `/path/to/test/makefile/other'
主 Makefile end

【范例代码】用export传递参数

# Makefile 内容
export VALUE1 := export.c    <-- 用了 export, 此变量能够传递到 ./other/Makefile 中
VALUE2 := no-export.c        <-- 此变量不能传递到 ./other/Makefile 中

all:
    @echo "主 Makefile begin"
    @cd ./other && make
    @echo "主 Makefile end"


# ./other/Makefile 内容
other-all:
    @echo "other makefile begin"
    @echo "VALUE1: " $(VALUE1)
    @echo "VALUE2: " $(VALUE2)
    @echo "other makefile end"

# bash中执行 make
$ make
主 Makefile begin
make[1]: Entering directory `/path/to/test/makefile/other'
other makefile begin
VALUE1:  export.c        <-- VALUE1 传递成功
VALUE2:                  <-- VALUE2 传递失败
other makefile end
make[1]: Leaving directory `/path/to/test/makefile/other'
主 Makefile end

【补充】export语法格式如下:

→ export variable = value

→ export variable := value

→ export variable += value

定义命令包

命令包有点像是个函数,将连续的相同的命令合成一条,减少akefile中的代码量,便于以后维护。

define <command-name>
command
...
endef

【范例代码】命令包的简单定义使用

# Makefile 内容
define run-hello-makefile
@echo -n "Hello"
@echo " Makefile!"
@echo "这里可以执行多条 Shell 命令!"
endef

all:
    $(run-hello-makefile)


# bash 中运行make
$ make
Hello Makefile!
这里可以执行多条 Shell 命令!

条件判断

条件判断的关键字主要有:ifeq、ifneq、ifdef、ifndef。

<conditional-directive>
<text-if-true>
endif

# 或者
<conditional-directive>
<text-if-true>
else
<text-if-false>
endif

【范例代码】ifeq的例子,ifneq和ifeq的使用方法类似,就是取反

# Makefile 内容
all:
ifeq ("aa", "bb")
    @echo "equal"
else
    @echo "not equal"
endif

# bash 中执行 make
$ make
not equal

 

【范例代码】 ifdef的例子,ifndef和ifdef的使用方法类似,就是取反

# Makefile 内容
SRCS := program.c

all:
ifdef SRCS
    @echo $(SRCS)
else
    @echo "no SRCS"
endif

# bash 中执行 make
$ make
program.c

 

Makefile中的函数

Makefile中自带了一些函数,利用这些函数可以简化Makefile的编写。

$(<function> <arguments>)
# 或者
${<function> <arguments>}
  • <function> 是函数名
  • <arguments> 是函数参数

1、字符串函数

字符串替换函数: $(subst <from>,<to>,<text>)

功能:把字符串<text>中的<from>替换为<to> 

返回:替换过的字符串

# Makefile 内容
all:
    @echo $(subst t,e,maktfilt)  <-- 将t替换为e

# bash 中执行 make
$ make
makefile

 

模式字符串替换函数: $(patsubst <pattern>,<replacement>,<text>)

功能:查找<text>中的单词(单词以"空格", "tab", "换行"来分割)是否符合<pattern>,符合的话,用 <replacement>替代。

 

posted @ 2018-06-13 17:59  老姚大大  阅读(887)  评论(0编辑  收藏  举报