Https://www.jianshu.com/p/7c20b299ee63
Traditionally, we've been calling this thing a variable in makefile, which is essentially a macro, just a string substitution. How can we call it a macro?
Name of the macro
Makefile macros can contain characters, numbers, and underscores. One thing to note is that a macro is a macro, anyway it's a string substitution, so it's okay to start with a number. The makefile itself is not used for numerical processing, but mainly for strings.
You need to use the $ symbol when using a macro, so if you want to use a shell variable in a string, use $$.
As you can see, the function we learned earlier is essentially a macro, but a macro with parameters.
Assignment of macros = and: =
Macros can define themselves with the values of other macros. The most powerful is with the = operator. Makefile is a defined language, not executed sequentially, so you can use a macro that is not defined in this line to assign a value to the current macro, which is a macro expansion anyway.
However, this can cause unexpected errors if a circular reference is made. In this case, use the: = operator, which is only allowed to reference the variables defined before it.
?=
If you do not know the definition is not defined, you can use the? = To assign a value, if undefined, the definition. If it is already defined, do nothing.
+=
If not previously defined, it is equivalent to =. If previously there is a: = definition, the new value is added to the value followed by the: = Rule.
To compare a macro
There are 4 keywords for comparing macros:
- Ifeq: judging Equality
- IFNEQ: Judging Unequal
- IFDEF: Judging non-empty, equivalent to IFNEQ (< parameter;,)
- Ifndef: If empty, equivalent to ifeq (< parameter;,)
Cases:
.PHONY : all7ifeq ($(DEX2OAT_HOST_INSTRUCTION_SET_FEATURES),)DEX2OAT_HOST_INSTRUCTION_SET_FEATURES := defaultendifall7 : @echo $(DEX2OAT_HOST_INSTRUCTION_SET_FEATURES)
Output:
$ make all7default
Note that ifeq, else, endif cannot have tab before these statements.
The example above is from ANDROID.OAT.MK. In fact, with the following is equivalent:
ifndef DEX2OAT_HOST_INSTRUCTION_SET_FEATURESDEX2OAT_HOST_INSTRUCTION_SET_FEATURES := defaultendif
Define multiple lines of macros
You can define multiple lines of Dai Wang by using define/endef.
Let's look at an example:
# $ (1): Compiler-default, optimizing, JIT or interpreter. # $ (2): wrapper. # $ (3): dex2oat suffix. define create-core-oat-host-rule-combination $ (call create-core-oat-host-rules,$ (1), no-pic,,$ (2), $ (3)) $ (call create-core-oat-host-rules,$ (1), pic,,$ (2), $ (3)) ifneq ( Span class= "hljs-variable" >$ (Host_prefer_32_bit), true) $ ( call create-core-oat-host-rules,$ (1), no-pic,2nd_,$ (2), $ (3)) $ (call create-core-oat-host-rules,$ (1), pic,2ND_,$ (2), $ ( 3) endifendef
This macro is actually a function.
eval function
The Eval function is used when you use other Makefile statements in a macro definition or in a branch or loop structure.
Let's look at an example of using Eval in foreach:
$(foreach M,$ (All_modules), $ (Eval r: = $ (all_modules.$ (m). REQUIRED))$(If$ (R), $ (Eval r: = $ (Call Module-installed-files,$ (R)))$(Eval t_m: = $ (Filter$ (Target_out_root)/%, $ (all_modules.$ (m). Installed)))$(Eval h_m: = $ (Filter$ (Host_out_root)/%, $ (all_modules.$ (m). Installed)))$(Eval T_r: = $ (Filter$ (target_out_root)/%,$ (R)))$(Eval H_r: = $ (Filter$ (host_out_root)/%,$ (R)))$(Eval t_m: = $ (filter-out $ ( t_r), $ (t_m)) $ (eval h_m: = $ (filter-out $ (h_r), $ (h_m))) $ (if c10>$ (T_m), $ (eval $ (callAdd-required-deps, $ (t_m),$ (t_r)))) $ (if $ (h_m), $ ( Eval $ (CallAdd-required-deps, $ (h_m),$ (h_r) ))))
VPATH macro
VPATH macro is used to specify the directory of Makefile search source files, this is a bit variable meaning.
referencing other makefile
Makefile is basically a string substitution, and we can of course use the include command to reference a series of other makefile into the current makefile.
Cases:
include art/build/Android.common_build.mk
Auto macros
An automatic macro is a variable that has been defined by the Make tool.
[Email protected] Target file set
[email protected] refers to the entire target file set
Cases:
$ (Built_odex):$(dir $ (local_built_module))%: $ (built_dex) $ (hide) mkdir-p $ (dir [email protected]) && rm-f [email protected] $ (add-dex-to-package) $ (hide) mv [email protected] [email protected] $ (call dexpreopt-one-file,< Span class= "hljs-variable" >[email protected],[email protected]) $ (hide) rm [email protected]endif
$<
The first file name that you rely on can be shared with [email protected].
Cases:
$(built_odex) : $(my_prebuilt_src_file) $(call dexpreopt-one-file,$<,[email protected])
Debugging and error handling output messages
The makefile command will be output by default. You can use @ To hide the display.
With the echo command, you can output messages. Usually use @echo, lest see two times.
Several commands to execute together
If this command uses the result of the previous command, you need to add a semicolon after the previous command
Error handling
If makefile encounters a command error, it aborts the current dependency execution. You can ignore the error by adding "-" before the command.
We can also ignore the wrong target to. Ignore to declare.
Such as:
.IGNORE : all7
Agent Jtag.
Links: Https://www.jianshu.com/p/7c20b299ee63
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.
ANDROID.MK (3) macro