In makefile, functions can be used to process variables, making our commands or rules more flexible and intelligent. Make does not support many functions, but it is enough for us to operate. After a function is called, the return value of the function 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:
$ ()
Or
$ {}
Here, the function name is used. Make does not support many functions. Is a function parameter. parameters are separated by commas (,), and function names and parameters are separated by spaces. Function calls start with "$" and enclose function names and parameters in parentheses or curly brackets. It feels like a variable, isn't it? Variables can be used for parameters in a function. For style unification, the brackets of the function and the variable are the best, for example, in the form of $ (SUBST a, B, $ (x, instead of "$ (SUBST a, B, $ {x. Because unification will be clearer and reduce unnecessary troubles.
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) defines a space using $ (empty). The value of $ (FOO) is "a B C", and $ (bar) is used for definition, the "SUBST" function is called. This is a replacement function. This function has three parameters. The first parameter is the replaced string, and the second parameter is the replaced string, the third parameter is the string used by the replacement operation. This function replaces spaces in $ (FOO) with commas, so the value of $ (bar) is "a, B, c ".
Ii. string processing functions
$ (SUBST ,,)
Name: String replacement function-SUBST.
Function: Replace the string in the string.
Return: The function returns the replaced string.
Example:
$ (Subst ee, EE, feet on the street ),
Replace "ee" in "feet on the street" with "ee" and return "feet on the street ".
$ (Patsubst ,,)
Name: patsubst, a mode string replacement function.
Function: Find whether the matching mode is correct for words (separated by "space", "tab", "press ENTER", and "line feed. Here, the wildcard "%" can be included to indicate strings of any length. If "%" is also included, the "%" in will be the string represented by the "%" in. (Escape with "/" and use "/%" to indicate the "%" character of the true meaning)
Return: The function returns the replaced string.
Example:
$ (Patsubst %. C, %. O, X. c. c bar. c)
Returns the string "x. c. c bar. C "Conformity mode [%. c] to [%. o], the returned result is "x. c. O Bar. o"
Note:
This is a bit similar to the knowledge we have mentioned in the previous "variable chapter. For example:
"$ (VaR: = )"
Equivalent
"$ (Patsubst, $ (VAR ))",
And "$ (VaR: = )"
Is equivalent
"$ (Patsubst %, %, $ (VAR ))".
For example, objects = Foo. O Bar. O Baz. O,
Then, "$ (Objects:. O =. C)" and "$ (patsubst %. O, %. C, $ (objects)" are the same.
$ (Strip)
Name: Remove space function -- strip.
Function: removes the starting and ending null characters in the string.
Return: returns the string value with spaces removed.
Example:
$ (Strip a B C)
Place the string "a B c" to the space at the beginning and end, and the result is "a B C ".
$ (Findstring ,)
Name: search for the string function-findstring.
Function: searches for strings.
Return: if it is found, return; otherwise, return an empty string.
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 ,)
Name: filter function.
Function: filter the words in the string in the pattern and retain the words that match the pattern. There can be multiple modes.
Return: returns a pattern-compliant string.
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 ,)
Name: Filter-out.
Function: filter the words in the string in the correct mode to remove the words in the correct mode. There can be multiple modes.
Return: returns a non-conforming string.
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)
Name: Sorting function --Sort.
Function: returns a string.In ascending order ).
Return: returns the sorted string.
Example:$ (Sort Foo bar lose)Return"Bar Foo lose" .
Note:SortThe function is removed..
$ (Word ,)
Name: Word function --Word.
Function: returns a string.Center. (From the beginning)
Return: returns a string.Center. IfRatioIf the number of words in is large, an empty string is returned.
Example:$ (Word 2, foo bar BAZ)The returned value is"Bar".
$ (Wordlist ,,)
Name: wordlist, a single string function.
Function: obtains the start word string from a string. And is a number.
Return: returns the word string from the string. If it is larger than the number of words in it, an empty string is returned. If the value is greater than the number of words, a string of words from start to end is returned.
Example: $ (wordlist 2, 3, foo bar BAZ) returns "bar Baz ".
$ (Words)
Name: Word Count statistical function -- words.
Function: counts the number of words in a string.
Return: Number of words returned.
Example: $ (words, foo bar BAZ) returns "3 ".
Note: if we want to take the last word, we can: $ (word $ (words ),).
$ (Firstword)
Name: the first word function, firstword.
Function: gets the first word in a string.
Return: returns the first word of the string.
Example: $ (firstword Foo bar) returns "foo ".
Note: this function can be implemented using the word function: $ (word 1 ,).
The above are all string operation functions. If used in combination, you can complete more complex functions. Here is an example of a real application. We know that make uses the "vpath" variable to specify the search path for "dependent Files. Therefore, we can use this search path to specify the search path parameter cflags of the compiler's first-class file, for example:
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 strings of each function are treated as one or a series of file names.
$ (Wildcard)
Name: name extension function -- wildcard.
Function: This function is used to expand a column of all file names that comply with the descriptions of their parameters. Files are separated by spaces.
Return: the expanded file name list is returned.
Example:
$ (Wildcard *. c)
The returned result is a list of all objects ending with '. C.
$ (DIR)
Name: Directory function -- dir.
Function: extracts the directory part from the file name sequence. The directory part refers to the part before the last backslash. If there is no backslash, "./" is returned.
Return: return the directory part of the file name sequence.
Example: $ (DIR src/Foo. c hacks) returns "src /./".
$ (Notdir)
Name: file function -- notdir.
Function: Extracts non-directory parts from the file name sequence. The non-directory part is the part after the last backslash.
Return: return the non-directory part of the file name sequence.
Example: $ (notdir src/Foo. c hacks) returns "foo. c hacks ".
$ (Suffix)
Name: suffix, A suffix function.
Function: extracts the suffix of each file name from the file name sequence.
Return: returns the suffix sequence of the file name sequence. If the file does not have a suffix, an empty string is returned.
Example: $ (suffix src/Foo. c src-1.0/bar. c hacks) returns ". c. c ".
$ (Basename)
Name: prefix function -- basename.
Function: extracts the prefix of each file name from the file name sequence.
Return: the prefix sequence of the file name sequence. If the file does not have a prefix, an empty string is returned.
Example: $ (basename src/Foo. c src-1.0/bar. c hacks) returns "src/Foo src-1.0/bar hacks ".
$ (Addsuffix ,)
Name: add the suffix function addsuffix.
Function: adds the suffix to the end of each word.
Return: returns the sequence of file names with suffixes.
Example: $ (addsuffix. C, foo bar) returns "foo. c bar. c ".
$ (Addprefix ,)
Name: The addprefix function.
Function: adds the prefix to the end of each word.
Return: returns the sequence of file names that have been prefixed.
Example: $ (addprefix src/, foo bar) returns "src/Foo src/bar ".
$ (Join ,)
Name: Join function -- join.
Function: add the corresponding words to the back of the word. If the number of words is more than the number of words, the extra words in them will remain unchanged. If the number of words is greater than the number of words, the extra words will be copied.
Return: returns the connected string.
Example: $ (join aaa bbb, 111 222 333) returns "aaa111 bbb222 333 ".
4. foreach Functions
The foreach function is very different from other functions. Because this function is used for loop purposes, the foreach function in makefile is almost the same as the for statement in the UNIX standard shell (/bin/sh, or a foreach statement in C-shell (/bin/CSH. Its syntax is:
$ (Foreach ,,)
This function is used to extract the words in the parameter one by one and put them in the variable specified by the parameter, and then execute the contained expression. Each time a string is returned, each string returned during the loop is separated by a space. Finally, when the entire loop ends, the entire string (separated by spaces) composed of each returned string is the return value of the foreach function.
Therefore, it is better to use a variable name, which can be an expression, and this parameter is generally used to enumerate words in sequence. For example:
Names: = a B c d
Files: = $ (foreach N, $ (names), $ (N). O)
In the above example, the words in $ (name) are extracted one by one and coexist in the variable "N", "$ (n ). O "calculates a value based on" $ (n) "each time. These values are separated by spaces and finally returned as the foreach function. Therefore, the value of $ (Files) is". o B. o C. o d. O ".
Note that the parameter in foreach is a temporary local variable. After the foreach function is executed, the variable of the parameter will not function 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). the syntax of the IF function is:
$ (If ,)
Or
$ (If ,,)
It can be seen that the IF function can contain "else" or not. That is, if function parameters can be two or three. The parameter is an if expression. If it returns a non-null string, this expression is equivalent to true. Therefore, it is calculated. Otherwise, it is calculated.
The Return Value of the IF function is true (non-null string), which is the return value of the entire function. If it is false (Null String), it is the return value of the entire function, if not, the entire function returns an empty string.
Therefore, only one sum is 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. Its syntax is:
$ (Call ,,,...)
When make executes this function, the variables in parameters, such as $ (1), $ (2), and $ (3), are replaced by parameters. The return value is the return value of the call function. For example:
Reverse = $ (1) $ (2)
Foo = $ (call reverse, a, B)
Then, the value of foo is "a B ". 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 "B ".
VII. Origin Functions
Unlike other functions, the origin function does not operate on the value of a variable. It just tells you where the variable comes from? Its syntax is:
$ (Origin)
Note: It is the variable name and should not be a reference. So you 'd better not use the "$" character in. The origin function uses its return value to tell you the "Birth condition" of the variable. below, the return value of the origin function is:
"Undefined"
If it has never been defined, the origin function returns the value "undefined ".
"Default"
If it is a default definition, such as the "cc" variable, this variable will be described later.
"Environment"
If it is an environment variable and makefile is executed, the "-e" parameter is not opened.
"File"
If this variable is defined in makefile.
"Command Line"
If the variable is defined by the command line.
"Override"
If it is redefined by the override indicator.
"Automatic"
If it is an automation variable in the Command running. Automation variables will be described later.
This information is very useful for compiling makefile. For example, suppose we have a makefile which contains a definition file make. def, in make. def defines a variable "Bletch", and our environment also has an environment variable "Bletch". At this time, we want to determine if the variable is from the environment, then we will redefine it, if it comes from make. if DEF or command line is not in the environment, we will not redefine 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 we need to use this step? Yes, we can achieve this by using override, but override is too crude. It will also overwrite the variables defined from the command line, and we just want to redefine the environment, I don't want to redefine what comes from 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 is the same as the anti-quotation mark. This means that the shell function returns the output after the operating system command is executed. 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: This function will generate a new shell program to execute commands, so you should pay attention to its running performance. If your makefile contains some complicated rules and uses this function extensively, this is harmful to your system performance. In particular, the hidden rules of makefile may make your shell function run more times than you think.