Makefile completely parses part7. use the Function

Source: Internet
Author: User

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:

$ (<Function >;< arguments> ;)

Or

$ {<Function >;< arguments> ;}

Here, <function>; is the function name. Make does not support many functions. <Arguments>; 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 <from >;,< to >;,< text> ;)

Name: String replacement function-SUBST.

Function: replace <from>; In the <text>; string with <to> ;.

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 <pattern >;,< replacement >;,< text> ;)

Name: patsubst, a mode string replacement function.

Function: query whether the matching mode is true for words in <text>; (the words are separated by "space", "tab", or "Press ENTER" and "line feed, replace with <replacement>. Here, <pattern>; can include the wildcard "%" to indicate strings of any length. If <replacement>; also contains "%", the "%" in <replacement>; will be the string represented by the "%" in <pattern>. (Escape with "\" and use "\ %" to indicate the "%" characters of 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: <pattern >;=< replacement> ;)"

Equivalent

"$ (Patsubst <pattern >;,< replacement >;,$ (VAR ))",

And "$ (VaR: <suffix >;=< replacement> ;)"

Is equivalent

"$ (Patsubst % <suffix >;,%< replacement >;,$ (VAR ))".

For example, objects = Foo. O Bar. O Baz. 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 "a B c" to the space at the beginning and end, and the result is "a B C ".

$ (Findstring <find >;,< in> ;)

Name: search for the string function-findstring.

Function: Find the <find>; string in the <in>; string.

Return Value: 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 <text> in <pattern>; the words in the string, retain the words that match the pattern <pattern>. There can be multiple modes.

Return: returns the string that matches the pattern <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 <text>; strings in the <pattern>; mode to remove words in the <pattern>; mode. There can be multiple modes.

Return: returns a string that does not conform to the pattern <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 string <list>; in ascending order ).

Return: returns the sorted string.

Example: $ (sort Foo bar lose) returns "bar Foo lose ".

Note: The sort function removes the same words from <list>.

$ (Word <n >;,< text> ;)

Name: Word function -- word.

Function: obtains the <text>; nth> word in the string. (From the beginning)

Return: the number of <n> In the <text>; string is returned. If <n>; is greater than the number of words in <text>;, an empty string is returned.

Example: $ (Word 2, foo bar BAZ) returns "bar ".

$ (Wordlist <s >;,< e >;,< text> ;)

Name: wordlist, a single string function.

Function: obtains the word string from <S>; to <E>. <S>; and <E>; are numbers.

Return: return the word string from <S>; to <E>; in <text>. If <S>; is greater than the number of words in <text>;, an empty string is returned. If the number of words <E>; greater than <text>;, the string starting from <S>; and ending from <text>; is returned.

Example: $ (wordlist 2, 3, foo bar BAZ) returns "bar Baz ".

$ (Words <text> ;)

Name: Word Count statistical function -- words.

Function: counts the number of words in a string.

Return: returns the number of words in <text>.

Example: $ (words, foo bar BAZ) 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 <text>; string.

Return: returns the first word of the string <text>.

Example: $ (firstword Foo bar) returns "foo ".

Note: this function can be implemented using the word function: $ (word 1, <text> ;).

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.

$ (DIR <names...> ;)

Name: Directory function -- dir.

Function: extracts the directory from the file name sequence <names>. The directory part refers to the part before the last backslash. If there is no backslash, "./" is returned.

Return: returns the directory part of the file name sequence <names>.

Example: $ (DIR src/Foo. c hacks) returns "src /./".

$ (Notdir <names...> ;)

Name: file function -- notdir.

Function: Extracts non-directory parts from the file name sequence <names>. The non-directory part is the part after the last backslash.

Return: the non-directory part of the file name sequence <names>.

Example: $ (notdir src/Foo. c hacks) returns "foo. c hacks ".

$ (Suffix <names...> ;)

Name: suffix, A suffix function.

Function: extracts the suffix of each file name from the file name sequence <names>.

Return: The suffix sequence of the file name sequence <names>;. 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 <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: $ (basename src/Foo. c src-1.0/bar. c hacks) returns "src/Foo src-1.0/bar hacks ".

$ (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, foo bar) returns "foo. c bar. 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: $ (addprefix src/, foo bar) returns "src/Foo src/bar ".

$ (Join <list1 >;,< list2> ;)

Name: Join function -- join.

Function: append the word in <list2>; to the word in <list1>. If <list1>; has more words than <list2>;, the multiple words in <list1>; remain unchanged. If <list2>; has more words than <list1>;, <list2>; multiple words are copied to <list2>.

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 <var >;,< list >;,< text> ;)

This function is used to extract the words in the <list>; parameter one by one and put them into the parameter <var>; the specified variable, and then execute the expressions contained in <text>. Each <text>; returns a string. During the loop, each string returned by <text>; is separated by spaces. When the loop ends, <text>; the entire string (separated by spaces) composed of each returned string is the return value of the foreach function.

Therefore, <var>; preferably a variable name, <list>; can be an expression, and <text>; is generally used in <var>; this parameter is used to enumerate the words in <list>. 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: The <var>; parameter in foreach is a temporary local variable. After the foreach function is executed, the variable of the <var>; parameter does not work, 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 <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 returns the true result. Therefore, <then-part>; is calculated, otherwise, <else-part>; is calculated.

The Return Value of the IF function is: <then-part> If <condition>; true (non-null string), which is the return value of the entire function, if <condition>; if it is a false (Null String), <else-part>; is the return value of the entire function. 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. Its syntax is:

$ (Call <expression >;,< parm1 >;,< parm2 >;,< parm3> ;...)

When make executes this function, variables in the <expression>; parameter, such as $ (1), $ (2), $ (3), will be <parm1> ;, <parm2>;, <parm3>; are replaced in sequence. The return value of <expression>; 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 <variable> ;)

Note: <variable>; is the variable name and should not be referenced. Therefore, you 'd better not use the "$" character in <variable>. 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 <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 the makefile is executed, the "-e" parameter is not enabled.

"File"

If <variable>; the variable is defined in makefile.

"Command Line"

If <variable>; the 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 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.ProgramTo execute the command, so pay attention to its running performance. If your makefile contains some complicated rules and uses this function in large quantities, it will be harmful to your system performance. In particular, the hidden rules of makefile may make your shell function run more times than you think.

9. Make control functions

Make provides some functions to control the running of make. Generally, you need to check the runtime information when running makefile, and decide whether to let make continue or stop based on the information.

$ (Error <text...> ;)

Generate a fatal error, <text...>; is the error message. Note: The error function will not generate an error message when it is used. Therefore, if you define it in a variable and use this variable in subsequent scripts, that's fine. For example:

Example 1:

Ifdef error_001

$ (Error error is $(error_001 ))

Endif

Example 2:

Err = $ (error found an error !)

. Phony: Err

Err:; $ (ERR)

The example will generate an error call when the variable error_001 is defined and then executed, while the Example 2 will generate an error call when the directory err is executed.

$ (Warning <text...> ;)

this function is similar to the error function, but it does not let make exit. It only outputs a warning message and make continues to execute.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.