|
1 |
| -## 2 介绍 |
| 1 | +# 2 介绍 |
2 | 2 |
|
3 | 3 |
|
4 | 4 |
|
5 |
| -## 3 书写规则 |
| 5 | +# 3 书写规则 |
6 | 6 |
|
7 | 7 |
|
8 | 8 |
|
9 | 9 | ### 自动生成依赖性
|
10 | 10 |
|
11 | 11 |
|
12 | 12 |
|
13 |
| -## 4 书写命令 |
| 13 | +# 4 书写命令 |
14 | 14 |
|
15 | 15 | 命令以`tab`键开头
|
16 | 16 |
|
@@ -98,7 +98,7 @@ foo.c: foo.y
|
98 | 98 | $(run-yacc)
|
99 | 99 | ```
|
100 | 100 |
|
101 |
| -## 5 使用变量 |
| 101 | +# 5 使用变量 |
102 | 102 |
|
103 | 103 | 变量代表一个字符串 执行时会自动展开
|
104 | 104 |
|
@@ -289,7 +289,7 @@ bar.o: bar.c
|
289 | 289 |
|
290 | 290 |
|
291 | 291 |
|
292 |
| -## 6 使用条件判断 |
| 292 | +# 6 使用条件判断 |
293 | 293 |
|
294 | 294 | ### 示例
|
295 | 295 |
|
@@ -338,7 +338,7 @@ endif
|
338 | 338 |
|
339 | 339 |
|
340 | 340 |
|
341 |
| -## 7 使用函数 |
| 341 | +# 7 使用函数 |
342 | 342 |
|
343 | 343 | ### 函数的调用语法
|
344 | 344 |
|
@@ -504,15 +504,168 @@ $(join <list1>,<list2>)
|
504 | 504 | $(join aaa bbb, 111 222 333) #返回aaa111 bbb222 333
|
505 | 505 | ```
|
506 | 506 |
|
| 507 | +### foreach函数 |
507 | 508 |
|
| 509 | +```makefile |
| 510 | +$(foreach <var>,<list>,<text>) |
508 | 511 |
|
509 |
| -### foreach函数 |
| 512 | +names := a b c d |
| 513 | +files := $(foreach n,$(names),$(n).o) |
| 514 | +#返回` a.o b.o c.o d.o` |
| 515 | +``` |
| 516 | + |
| 517 | +参数 <var> 的作用域只在 foreach 函数当中 |
| 518 | + |
| 519 | +### if 函数 |
| 520 | + |
| 521 | +```makefile |
| 522 | +$(if <condition>,<then-part>) |
| 523 | +$(if <condition>,<then-part>,<else-part>) |
| 524 | +``` |
| 525 | + |
| 526 | +如果 <condition> 为真(非空字符串),那个 <then-part> 会是整个函数 |
| 527 | +的返回值,如果 <condition> 为假(空字符串),那么 <else-part> 会是整个函数的返回值,此时如果<else-part> 没有被定义,那么,整个函数返回空字串 |
| 528 | + |
| 529 | +### call函数 |
| 530 | + |
| 531 | +用来创建新的参数化的函数 |
| 532 | + |
| 533 | +```makefile |
| 534 | +$(call <expression>,<param1>,<param2>,...,<paramn>) |
| 535 | + |
| 536 | +reverse = $(1) $(2) |
| 537 | +foo = $(call reverse,a,b) #a b |
| 538 | + |
| 539 | +reverse = $(2) $(1) |
| 540 | +foo = $(call reverse,a,b) #b a |
| 541 | +``` |
| 542 | +
|
| 543 | +### origin 函数 |
| 544 | +
|
| 545 | +```makefile |
| 546 | +$(origin <variable>) |
| 547 | + |
| 548 | +ifdef bletch |
| 549 | + ifeq "$(origin bletch)" "environment" |
| 550 | + bletch = barf, gag, etc. |
| 551 | + endif |
| 552 | +endif |
| 553 | +``` |
510 | 554 |
|
| 555 | + origin 函数的返回值 |
511 | 556 |
|
| 557 | +| 返回值 | 描述 | |
| 558 | +| :----------: | :----------------------------------: | |
| 559 | +| undefined | 从来没有定义过 | |
| 560 | +| default | 比如“CC”这个变量 | |
| 561 | +| environment | 是一个环境变量 并且-e 参数没有被打开 | |
| 562 | +| file | 被定义在 Makefile 中 | |
| 563 | +| command line | 被命令行定义 | |
| 564 | +| override | 被 override 指示符重新定义 | |
| 565 | +| automatic | 是一个命令运行中的自动化变量 | |
512 | 566 |
|
| 567 | +### shell 函数 |
| 568 | + |
| 569 | +```makefile |
| 570 | +contents := $(shell cat foo) |
| 571 | +files := $(shell echo *.c) |
| 572 | +``` |
| 573 | + |
| 574 | +### 控制 make 的函数 |
| 575 | + |
| 576 | +```makefile |
| 577 | +$(error <text ...>) # 产生一个致命的错误 |
| 578 | +$(warning <text ...>) # 输出一段警告信息 |
| 579 | + |
| 580 | +ifdef ERROR_001 |
| 581 | + $(error error is $(ERROR_001)) |
| 582 | +endif |
| 583 | + |
| 584 | +ERR = $(error found an error!) |
| 585 | + |
| 586 | +.PHONY: err |
| 587 | + |
| 588 | +err: $(ERR) |
| 589 | +``` |
513 | 590 |
|
514 | 591 |
|
515 | 592 |
|
| 593 | +# 8 make的运行 |
516 | 594 |
|
| 595 | +### make 的退出码 |
517 | 596 |
|
| 597 | +| 退出码 | 描述 | |
| 598 | +| :----: | :------------------------------------------------------: | |
| 599 | +| 0 | 成功执行 | |
| 600 | +| 1 | 运行时出现任何错误 | |
| 601 | +| 2 | 使用了 make 的“-q”选项,并且 make 使得一些目标不需要更新 | |
| 602 | + |
| 603 | +### 指定 Makefile |
| 604 | + |
| 605 | +```shell |
| 606 | +make –f hchen.mk |
| 607 | +make --file hchen.mk |
| 608 | +``` |
| 609 | + |
| 610 | +### 指定目标 |
| 611 | + |
| 612 | +`MAKECMDGOALS` 存放你所指定的终极目标的列表 |
| 613 | + |
| 614 | +```makefile |
| 615 | +sources = foo.c bar.c |
| 616 | +ifneq ( $(MAKECMDGOALS),clean) |
| 617 | + include $(sources:.c=.d) |
| 618 | +endif |
| 619 | +``` |
| 620 | + |
| 621 | +```makefile |
| 622 | +.PHONY: all |
| 623 | +all: prog1 prog2 prog3 prog4 |
| 624 | +``` |
| 625 | + |
| 626 | +### 检查规则 |
| 627 | + |
| 628 | +| 参数 | 描述 | |
| 629 | +| :----------------------------------------------------------: | :------------------------------------: | |
| 630 | +| -n, --just-print, --dry-run, --recon | 不执行参数,只是打印命令 | |
| 631 | +| -t, --touch | 把目标文件的时间更新,但不更改目标文件 | |
| 632 | +| -q, --question | 如果目标不存在,会打印出一条出错信息 | |
| 633 | +| -W <file>, --what-if=<file>, --assume-new=<file>, --new-file=<file> | | |
| 634 | +| -p -v | | |
| 635 | + |
| 636 | +### make 的参数 |
| 637 | + |
| 638 | +| 参数 | 描述 | |
| 639 | +| :----------------------------------------------------------: | :----------------------------------------------------------: | |
| 640 | +| -b, -m | 忽略和其它版本 make 的兼容性 | |
| 641 | +| -B, --always-make | 认为所有的目标都需要更新(重编译) | |
| 642 | +| -C <dir>, --directory=<dir> | 指定读取 makefile 的目录 | |
| 643 | +| -debug[=<options>] | 输出 make 的调试信息<br />a: 也就是 all,输出所有的调试信息<br />b: 也就是 basic,只输出简单的调试信息。即输出不需要重编译的目标<br /> | |
| 644 | +| -d | 相当于“–debug=a” | |
| 645 | +| -e, --environment-overrides | 指明环境变量的值覆盖 makefile 中定义的变量的值 | |
| 646 | +| -f <file>, --file=<file>, --makefile=<file> | 指定需要执行的 makefile | |
| 647 | +| -h, --help | 显示帮助信息 | |
| 648 | +| -i , --ignore-errors | 在执行时忽略所有的错误 | |
| 649 | +| -I <dir>, --include-dir=<dir> | 指定一个被包含 makefile 的搜索目标 | |
| 650 | +| -j [N], --jobs[=N] | 同时运行命令的个数 | |
| 651 | +| -k, --keep-going | 出错也不停止运行 | |
| 652 | +| -l <load>, --load-average[=<load>], -max-load[=<load>] | 指定 make 运行命令的负载 | |
| 653 | +| -n, --just-print, --dry-run, --recon | 仅输出执行过程中的命令序列,但并不执行 | |
| 654 | +| -o <file>, --old-file=<file>, --assume-old=<file> | 不重新生成的指定的 <file>,即使这个目标的依赖文件新于它 | |
| 655 | +| -p, --print-data-base | 输出 makefile 中的所有数据,包括所有的规则和变量 | |
| 656 | +| -q, --question | 不运行命令,也不输出。仅仅是检查所指定的目标是否需要更新 | |
| 657 | +| -r, --no-builtin-rules | 禁止 make 使用任何隐含规则 | |
| 658 | +| -R, --no-builtin-variabes | 禁止 make 使用任何作用于变量上的隐含规则 | |
| 659 | +| -s, --silent, --quiet | 在命令运行时不输出命令的输出 | |
| 660 | +| -S, --no-keep-going, --stop | 取消“-k”选项的作用 | |
| 661 | +| -t, --touch | 相当于 UNIX 的 touch 命令,只是把目标的修改日期变成最新的,也就是阻止生成目标的命令运行 | |
| 662 | +| -v, --version | 输出 make 程序的版本、版权等关于 make 的信息 | |
| 663 | +| -w, --print-directory | 输出运行 makefile 之前和之后的当前目录 | |
| 664 | +| --no-print-directory | 禁止“-w”选项 | |
| 665 | +| -W <file>, --what-if=<file>, --new-file=<file>, --assume-new=<file> | | |
| 666 | +| --warn-undefined-variables | 只要 make 发现有未定义的变量,那么就输出警告信息 | |
| 667 | + |
| 668 | + |
| 669 | + |
| 670 | +# 9 隐含规则 |
518 | 671 |
|
0 commit comments