Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

makefile tricks #209

Open
nnop opened this issue Aug 13, 2018 · 3 comments
Open

makefile tricks #209

nnop opened this issue Aug 13, 2018 · 3 comments

Comments

@nnop
Copy link
Owner

nnop commented Aug 13, 2018

6.3.1 Substitution References

A substitution reference substitutes the value of a variable with alterations that you specify. It has the form $(var:a=b) (or ${var:a=b}) and its meaning is to take the value of the variable var, replace every a at the end of a word with b in that value, and substitute the resulting string.

A substitution reference is actually an abbreviation for use of the patsubst expansion function

@nnop
Copy link
Owner Author

nnop commented Aug 13, 2018

rpath

$ORIGIN is a special variable that means ‘this executable’, and it means the actual executable filename, as readlink would see it, so symlinks are followed. In other words, $ORIGIN is special and resolves to wherever the binary is at runtime.

You need to pass the literal string $ORIGIN/../lib as an argument to your linker. The $ORIGIN token is kept inside your program after it's created and when the runtime linker starts to run your program it will replace $ORIGIN with the current path that your program was invoked from.

In order to pass a literal $ to the command invoked by a make recipe, you have to escape the $ by doubling it: $$. Otherwise, make will see the $ as introducing a make variable or function. Remember, it's perfectly legal for a make variable to avoid the parentheses etc., if it's a single character (note, $@, $<, etc.)

@nnop
Copy link
Owner Author

nnop commented Aug 15, 2018

@nnop
Copy link
Owner Author

nnop commented Sep 17, 2018

Makefile

rules

ordinary rules

  • 多个目标的时候,所有目标只能有同样的prerequisites

static pattern rules

  • 其实是相当于写了很多explicit rules

  • 每个target必须跟target pattern相匹配

implicit rules

其实是一些惯用的(customary)处理方法。

  • 用pattern rules来定义

  • 需要所有前置条件exists and can be made
    it is mentioned explicitly in the makefile as a target or a prerequisite, or if an implicit rule can be recursively found for how to make it.

  • make会为每个没有recipe的target找implicit rule

  • 只在prerequisites里提到的文件

  • explicit prerequisites不影响implicit rule search

built-in rules

  • make的惯例,对于.x源文件

    • 预处理PREPROCESS.x

    • 编译COMPILE.x

    • 链接LINK.x

pattern rules

  • %的扩展在所有变量扩展之后

  • 只有在所有prerequisites exists or can be made时才会应用

  • pattern match

    • target-pattern不包含‘/’时,匹配前会先将文件路径中的目录部分去掉,只匹配文件名。对prerequisite-pattern替换完后再在前面加上目录部分。

multiple targets

对于ordinary rules

  • 相当于很多具有相同prerequisites和recipe的rules

对于implicit rules

  • 一个recipe生成所有targets

自动依赖生成

编译器参数

  • -MMD根据-o值放.d文件

  • -MP对每个prerequisite加一个phony target,这样,文件被删的时候就不会报错了

directory organization

rules的选择顺序

1. ordinary rules

2. static pattern rules

3. implicit rules即pattern rules

  • 3.1 stem短的优先

  • 3.2 先定义的优先

4. chain rules

expansion

target和prerequisite立刻展开

运行机理

1st phase: 建依赖图

2nd phase: 决定哪些需要rebuild

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant