In makefile, functions can be used to process variables, making our commands or rules more flexible and effective.
Smart. Make does not support many functions, but it is enough for us to operate. After the function is called
The Return Value of the number can be used as a variable.
I. function call syntax
Function calling is similar to the use of variables and identified by "$". Its syntax is as follows:
$ (<Function> <arguments>)
Or
$ {<Function> <arguments>}
Here, <function> is the function name. Make does not support many functions. <Arguments> is a function parameter.
Separated by commas (,), while function names and parameters are separated by spaces. Function calls start with "$" and start with a circle
Function names and parameters are enclosed in brackets or curly brackets. It feels like a variable, isn't it? The parameters in the function can make
Use variables. For the sake of style unification, the brackets of functions and variables are the best, for example, use the "$ (substa, B, $ (x )"
The format of the sample, instead of the form of "$ (substa, B, $ {x. Because unification will be clearer and will reduce unnecessary
The trouble.
Let's look at an example:
Comma: =,
Empty: =
Space: = $ (empty)
Foo: = A B C
Bar: =$ (SUBST $ (Space), $ (comma), $ (FOO ))
In this example, $ (comma) is a comma. $ (Space) uses $ (empty) to define a space,
The value of $ (FOO) is "a B C", $ (bar) is used for definition, and the function "SUBST" is called. This is a replacement function.
Each function has three parameters. The first parameter is the replaced string, the second parameter is the replaced string, and the third parameter.
Is the string used to replace the operation. This function replaces spaces in $ (FOO) with commas, so the value of $ (bar)
It is "a, B, c ".
Ii. string processing functions
$ (SUBST <from >,< to >,< text>)
Name: String replacement function-SUBST.
Function: Replace the <from> string in the <text> string with <to>.
Return: The function returns the replaced string.
Example:
$ (Subst ee, EE, feet on the street ),
Replace "ee" in "feetonthestreet" with "ee" and return "feetonthestreet ".
$ (Patsubst <pattern >,< replacement >,< text>)
Name: patsubst, a mode string replacement function.
Function: Find the words in <text> (the words are separated by "space", "tab", or "Press ENTER" and "line feed ").
Match mode <pattern>. If yes, replace it with <replacement>. Here, <pattern> can include
Wildcard character "%", indicating a string of any length. If <replacement> also contains "%,
The "%" in <replacement> is the string represented by the "%" in <pattern>. (You can use "\"
To escape, and use "\ %" to indicate the true meaning of the "%" character)
Return: The function returns the replaced string.
Example:
$ (Patsubst %. C, %. O, X. c. c bar. c)
Replace the word "x. C. cbar. c" that matches the pattern [%. C] with [%. O]. The returned result is "x. C. obar. O"
Note:
This is a bit similar to the knowledge we have mentioned in the previous "variable chapter. For example:
"$ (VaR: <pattern >=< replacement> )"
Equivalent
"$ (Patsubst <pattern>, <replacement>, $ (VAR ))",
And "$ (VaR: <suffix >=< replacement> )"
Is equivalent
"$ (Patsubst % <suffix>, % <replacement>, $ (VAR ))".
For example, objects = Foo. obar. obaz. O,
Then, "$ (Objects:. O =. C)" and "$ (patsubst %. O, %. C, $ (objects)" are the same.
$ (Strip <string>)
Name: Remove space function -- strip.
Function: removes the <string> Empty characters at the beginning and end of the string.
Return: returns the string value with spaces removed.
Example:
$ (Strip a B C)
Place the string "ABC" in the space at the beginning and end. The result is "ABC ".
$ (Findstring <find >,< in>)
Name: search for the string function-findstring.
Function: Find the <find> string in the <in> string.
Return: If yes, <find> is returned. Otherwise, an empty string is returned.
Example:
$ (Findstring A, a B C)
$ (Findstring A, B C)
The first function returns the "A" string, and the second returns the "" string (Null String)
$ (Filter <pattern...>, <text>)
Name: filter function.
Function: filter the words in the <text> string in <pattern> mode and retain the words that match the <pattern> mode. Yes
In multiple modes.
Return: returns the string that matches the pattern.
Example:
Sources: = Foo. c bar. c Baz. s ugh. h
Foo: $ (sources)
CC $ (filter %. C %. S, $ (sources)-O foo
$ (Filter %. C %. S, $ (sources) returns "foo. c bar. c Baz. s ".
$ (Filter-out <pattern...>, <text>)
Name: Filter-out.
Function: Filter words in the <text> string in <pattern> mode to remove words in the <pattern> mode. Yes
In multiple modes.
Return: returns a string that does not conform to the pattern.
Example:
Objects = main1.o Foo. O main2.o bar. o
Mains = main1.o main2.o
$ (Filter-out $ (mains), $ (objects) returns "foo. O Bar. O ".
$ (Sort <list>)
Name: Sorting function -- sort.
Function: sorts the words in the string <list> in ascending order ).
Return: returns the sorted string.
Example: $ (sortfoobarlose) returns "barfoolose ".
Note: The sort function removes the same words from <list>.
$ (Word <n>, <text>)
Name: Word function -- word.
Function: obtains the <n> word in the <text> string. (From the beginning)
Return: returns the <n> Number of words in the <text> string. If <n> is greater than the number of words in <text>
Empty string.
Example: $ (word2, foobarbaz) returns "bar ".
$ (Wordlist <s >,< e >,< text>)
Name: wordlist, a single string function.
Function: obtains the word string starting from <S> to <E> from <text>. <S> and <E> are numbers.
Return: returns the word string from <S> to <E> In the <text> string. If <S> is larger than the number of words in <text>,
Returns an empty string. If the number of words <E> greater than <text>, the return value starts from <S> and ends with <text>.
.
Example: $ (wordlist2, 3, foobarbaz) returns "barbaz ".
$ (Words <text>)
Name: Word Count statistical function -- words.
Function: counts the number of words in a string in <text>.
Return: returns the number of words in <text>.
Example: $ (words, foobarbaz) returns "3 ".
Note: if we want to take the last word in <text>, we can: $ (word $ (words
<Text>), <text> ).
$ (Firstword <text>)
Name: the first word function, firstword.
Function: gets the first word in the string <text>.
Return: returns the first word of the string <text>.
Example: $ (firstwordfoobar) returns "foo ".
Note: this function can be implemented using the word function: $ (word1, <text> ).
The above are all string operation functions. If used in combination, you can complete more complex functions. Here,
Here is an example of a real application. We know that make uses the "vpath" variable to specify the "dependency file"
Search Path. Therefore, we can use this search path to specify the search path parameter of the compiler's first-class file.
Cflags, such:
Override cflags + = $ (patsubst %,-I %, $ (SUBST:, $ (vpath )))
If our "$ (vpath)" value is "src: ../headers", then "$ (patsubst %,-I %, $ (SUBST :,,
$ (Vpath) "will return"-isrc-I ../headers ", which is the parameter of the header file path for CC or GCC search.
Iii. File Name operation functions
The functions we will introduce below mainly process file names. The parameter string of each function is treated as one or
Is a series of file names.
$ (DIR <names...>)
Name: Directory function -- dir.
Function: extracts the directory from the file name sequence <names>. The directory part refers to the last backslash ("/").
The previous section. If there is no backslash, "./" is returned.
Return: returns the directory part of the file name sequence <names>.
Example: $ (dirsrc/Foo. chacks) returns "src /./".
$ (Notdir <names...>)
Name: file function -- notdir.
Function: Extracts non-directory parts from the file name sequence <names>. The non-directory part refers to the last backslash ("/
.
Return: return the non-directory part of the file name sequence <names>.
Example: $ (notdirsrc/Foo. chacks) returns "foo. chacks ".
$ (Suffix <names...>)
Name: suffix, A suffix function.
Function: extracts the suffix of each file name from the file name sequence <names>.
Return: returns the suffix sequence of the file name sequence <names>. If the file does not have a suffix, an empty string is returned.
Example: $ (suffixsrc/Foo. csrc-1.0/bar. chacks) returns ". c. c ".
$ (Basename <names...>)
Name: prefix function -- basename.
Function: extracts the prefix of each file name from the file name sequence <names>.
Return: the prefix sequence of the file name sequence <names>. If the file does not have a prefix, an empty string is returned.
Example: $ (basenamesrc/Foo. csrc-1.0/bar. chacks) returns "src/foosrc-1.0/barhacks ".
$ (Addsuffix <suffix>, <names...>)
Name: add the suffix function addsuffix.
Function: adds the suffix <suffix> to the end of each word in <names>.
Return: returns the sequence of file names with suffixes.
Example: $ (addsuffix. C, foobar) returns "foo. cbar. c ".
$ (Addprefix <prefix>, <names...>)
Name: The addprefix function.
Function: adds the prefix <prefix> to the end of each word in <names>.
Return: returns the sequence of file names that have been prefixed.
Example: $ (addprefixsrc/, foobar) returns "src/foosrc/bar ".
$ (Join <list1>, <list2>)
Name: Join function -- join.
Function: append the word in <list2> to the word in <list1>. If the number of <list1> words is greater
If there are many <list2> words, the words in <list1> remain unchanged. If the number of <list2> words is greater
<List1> multiple words, then <list2> multiple words will be copied to <list2>.
Return: returns the connected string.
Example: $ (joinaaabbb, 111222333) returns "aaa111bbb222333 ".
4. foreach Functions
The foreach function is very different from other functions. This function is used for looping.
The foreach function is similar to the for statement in the UNIX standard shell (/bin/sh) or C-shell.
(/Bin/CSH. Its syntax is:
$ (Foreach <var >,< list >,< text>)
This function is used to extract the words in the <list> parameter one by one and put them in the variable specified by the <var> parameter,
Then execute the expression contained in <text>. Each <text> operation returns a string,
Each string returned by <text> is separated by spaces. When the entire loop ends, <text> Returns
The entire string consisting of each string (separated by spaces) will be the return value of the foreach function.
Therefore, <var> is a variable name, <list> can be an expression, and <text> generally uses
<Var> this parameter is used to enumerate words in <list> in sequence. For example:
Names: = a B c d
Files: = $ (foreach N, $ (names), $ (N). O)
In the above example, the words in $ (name) will be taken out one by one and coexist in the variable "N", "$ (N). O"
According to "$ (n)", a value is calculated. These values are separated by spaces and finally returned as the foreach function. Therefore,
The value of $ (Files) is "a. o B. o c. o d. o ".
Note that the <var> parameter in foreach is a temporary local variable. After the foreach function is executed
The <var> variable does not work, and its scope is only in the foreach function.
V. If Functions
The IF function is similar to the conditional statement supported by GNU make-ifeq (see the previous section). If Function
Syntax:
$ (If <condition>, <then-part>)
Or
$ (If <condition>, <then-part>, <else-part>)
It can be seen that the IF function can contain "else" or not. That is, if function parameters can be two or
Three. <Condition> the parameter is the if expression. If it returns a non-null string, the expression is
<Then-part> is calculated. Otherwise, <else-part> is calculated.
The Return Value of the IF function is that if <condition> is true (non-null string), the <then-part> is an integer.
Function return value. If <condition> is false (Null String), then <else-part> is
Return value. If <else-part> is not defined, the entire function returns an empty string.
Therefore, only one <then-part> and <else-part> are calculated.
Vi. Call Functions
The call function is the only parameterized function that can be created. You can write a very complex expression,
In this expression, you can define many parameters, and then you can use the call function to pass parameters to this expression.
Number. Its syntax is:
$ (Call <expression>, <parm1>, <parm2>, <parm3> ...)
When make executes this function, the variables in the <expression> parameter, such as $ (1), $ (2), and $ (3), will be
Replace <parm1>, <parm2>, and <parm3>. The return value of <expression> is the return value of the call function.
Return Value. For example:
Reverse = $ (1) $ (2)
Foo = $ (call reverse, a, B)
Then, the foo value is "AB ". Of course, the order of parameters can be customized, not necessarily in order, for example:
Reverse = $ (2) $ (1)
Foo = $ (call reverse, a, B)
The value of foo is "ba ".
VII. Origin Functions
Unlike other functions, the origin function does not operate on the value of a variable. It only tells you where the variable is.
Come? Its syntax is:
$ (Origin <variable>)
Note that <variable> is the variable name and should not be referenced. Therefore, you 'd better not use"
$ "Character. The origin function uses its return value to tell you the "birth status" of the variable. below is the origin
Function return value:
"Undefined"
If <variable> has never been defined, the origin function returns the value "undefined ".
"Default"
If <variable> is a default definition, such as the "cc" variable, this variable will be described later.
"Environment"
If <variable> is an environment variable and makefile is executed, the "-e" parameter is not enabled.
"File"
If <variable> is defined in makefile.
"Command Line"
If the variable <variable> is defined by the command line.
"Override"
If <variable> is redefined by the override indicator.
"Automatic"
If <variable> is an automation variable in command running. Automation variables will be described later.
This information is very useful for writing makefile. For example, suppose we have a makefile and its package
A definition file make. Def is defined, and a variable "Bletch" is defined in make. Def, while in our environment
There is also an environment variable "Bletch". At this time, we want to determine, if the variable comes from the environment, then I
We will redefine it. If it comes from non-environment such as make. Def or command line, we will not re-define it.
Define it. Therefore, in our makefile, we can write as follows:
Ifdef Bletch
Ifeq "$ (origin Bletch)" "Environment"
Bletch = barf, gag, etc.
Endif
Endif
Of course, you may say that you can't redefine the variables in the environment without using the override keyword? Why
Do you need to use this step? Yes, we can achieve this with override, but override
It overwrites the variables defined from the command line, and we just want to redefine the environment.
Instead of redefining the command line.
VIII. Shell functions
Shell functions are not like other functions. As the name suggests, its parameter should be the command of the operating system shell. It and
'Is the same function. This means that the shell function uses the output after the operating system command is executed as the function.
. Therefore, we can use the operating system commands and string processing commands awk, sed, and so on to generate
A variable, such:
Contents: = $ (shell cat Foo)
Files: = $ (shell echo *. c)
Note that this function will generate a new shell program to execute commands, so pay attention to its running performance. If
Your makefile contains some complicated rules and uses this function in large quantities. Therefore, for your system
Performance is harmful. In particular, the hidden rules of makefile may make your shell function run more times than you
Much more.