Makefile function Summary

Source: Internet
Author: User

From: http://blog.csdn.net/haoel/archive/2004/02/25/2894.aspx

 

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, and the functions supported by make are not
Yes. <Arguments> is a function parameter. parameters are separated by commas (,), and function names and parameters are separated by spaces. Function calls start with "$" and start with parentheses or flowers
Function names and parameters are enclosed in 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
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) uses the $ (empty) Definition
With a space, the value of $ (FOO) is "a B
C ", $ (bar) is used for definition. The function" SUBST "is called. This is a replacement function. This function has three parameters. The first parameter is the replacement string, the second parameter is the replacement string, and the third parameter
The 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 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 "feet on the street" with "ee" and return "feet on the street ".

$ (Patsubst <pattern >,< replacement >,< text>)

Name: patsubst, a mode string replacement function.

Function: find out whether the words in <text> match the pattern (the words are separated by "space", "tab", or "Press ENTER" and "line feed"). If the match
<Replacement>. Here, <pattern> can include the wildcard "%" to indicate strings of any length. For example
If <replacement> also contains "%", the "%" in <replacement> will
Is the string represented by the "%" in <pattern>. (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: <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: 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. There can be 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. There can be 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: $ (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 <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>, 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 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> compared to <text>
If the number of words is large, an empty string is returned. If the number of words <E> greater than <text>, the return value starts from <S>,
The word string ending with <text>.
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 in <text>.
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 string <text>.
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: 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: returns 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 the number of <list1> words is
More than <list2>, the words in <list1> remain unchanged. If the number of <list2> words is
More than <list1>, then the words that are <list2> are copied to <list2>.
Return: returns the connected string.
Example: $ (join aaa bbb, 111 222 333) returns "aaa111 bbb222 333 ".

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.