Write Makefile with me. (ix) _makefile

Source: Internet
Author: User

Working with functions
————

Functions can be used in makefile to handle variables, making our commands or rules more flexible and intelligent. Make does not support a lot of functions, but it is enough for us to operate. After a function call, the return value of the function can be used as a variable.


The invocation syntax of a function

function calls, much like the use of variables, are also identified with "$", whose syntax is as follows:

$ (<function> <arguments>)

Or

${<function> <arguments>}

Here,<function> is the function name, and make does not support a lot of functions. <arguments> is the parameter of the function, separated by commas "," and the functions are separated by "spaces" between the parameters. The function call begins with "$" and encloses the function name and parameter in parentheses or curly braces. It feels like a variable, isn't it. Arguments in a function can use variables, for style unification, functions and variable parentheses are best, as in the form of "$ (subst a,b,$ (x))" Instead of "$ (subst a,b,${x})". Because the unification will be clearer, it will also reduce some unnecessary trouble.

Let's look at an example:

Comma:=,
empty:=
Space:= $ (empty) $ (empty)
Foo:= a b C
Bar:= $ (subst $ (space), $ (comma), $ (foo))

In this example, the value of $ (comma) is a comma. $ (space) uses the $ (empty) definition of a blank, the value of $ (foo) is defined by "a B C", $ (bar), and the function "subst" is called, which is a substitution function that has three arguments, the first argument is a substituted string, and the second argument is a replacement string. The third parameter is the string that replaces the action. The function is to replace the spaces in $ (foo) with commas, so the value of $ (bar) is "A,b,c".


Second, string processing functions

$ (subst <from>,<to>,<text>)

Name: string substitution function--subst.
function: Replace the <from> strings in string <text> with <to>.
Return: The function returns the string after being replaced.

Example:

$ (subst Ee,ee,feet on the street),

Replace "ee" in the "feet on the street" with "EE", and return the result is "feet on the street".


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

Name: pattern string substitution function--patsubst.
Features: Finds the words in <text> (words with "space", "tab" or "Carriage return", "newline" delimited) conform to mode <pattern>, and if they match, replace with <replacement>. Here,<pattern> can include the wildcard "%", which represents any length of string. If <replacement> also contains "%", then this "%" in,<replacement> will be the string represented by the "%" in <pattern>. (You can use "/" to escape, "/%" to represent the true meaning of the "%" character)
Return: The function returns the string after being replaced.

Example:

$ (patsubst%.c,%.o,x.c.c bar.c)

Replace the word "x.c.c bar.c" with the [%.O] of the words "%.C", returning the result is "X.C.O bar.o"

Note:

This is a bit like what we said in the "Variable chapters" earlier. Such as:

"$ (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: Go to the space function--strip.
function: Remove the null characters from the beginning and end of the <string> string.
Returns: Returns the string value of the removed space.
Example:

$ (Strip a B c)

The string "a B C" to the beginning and end of the space, the result is "a B C".

$ (findstring <find>,<in>)

Name: Find String function--findstring.
Function: Find <find> string in string <in>.
Returns: If found, then returns <FIND>, 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 (an empty string)

$ (Filter <pattern...>,<text>)

Name: Filter function--filter.
Function: Filter the words in <text> string with <pattern> mode, and keep the words that conform to the mode <pattern>. Can have more than one pattern.
Returns: Returns a string that conforms to 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 the value "Foo.c bar.c baz.s".

$ (filter-out <pattern...>,<text>)

Name: Anti-filter function--filter-out.
Function: Filter the words in <text> string with <pattern> mode, and remove the words conforming to the mode <pattern>. Can have more than one pattern.
Returns: Returns a String that does not conform to pattern <pattern>.
Example:

OBJECTS=MAIN1.O foo.o main2.o BAR.O
MAINS=MAIN1.O MAIN2.O

$ (filter-out $ (mains), $ (objects)) The return value is "foo.o bar.o".

$ (sort <list>)

Name: Sort function--sort.
Function: Sort the words in string <list> (ascending).
Returns: Returns the sorted string.
Example: $ (Sort foo bar lose) returns "Bar Foo lose".
Note: The sort function removes the same words in <list>.

$ (Word <n>,<text>)

Name: Take the word function--word.
Function: Take the string <text> <n> Word. (From the outset)
Returns: Returns the string <text> the word <n>. If <n> is larger than the number of words in <text>, an empty string is returned.
Example: $ (Word 2, foo bar baz) returns a value of "bar".

$ (wordlist <s>,<e>,<text>)

Name:--wordlist the word string function.
Function: From the string <text> from the start <s> to <e> the word string. <s> and <e> are a number.
Returns: Returns the string <text> the word strings from <s> to <e>. If <s> is larger than the number of words in <text>, an empty string is returned. If the <e> is greater than the number of words in <text>, then returns the string of words starting with <s> and ending with <text>.
Example: $ (wordlist 2, 3, foo bar baz) The return value is "bar baz".

$ (words <text>)

Name: Word Count function--words.
Features: Statistics <text> The number of words in a string.
Returns: Returns the number of words in <text>.
Example: $ (words, foo bar baz) The return value is "3".
Note: If we want to take the last word in <text>, we can do this: $ (Word $ (words <text>),<text>).

$ (FirstWord <text>)

Name: First word function--firstword.
Function: Takes the first word in the string <text>.
Returns: Returns the first word of the string <text>.
Example: $ (FirstWord foo bar) The return value is "foo".
Note: This function can be implemented using the Word function: $ (word 1,<text>).

Above, is all the string operation function, if mix uses, can complete the more complex function. Here, give an example of a practical application. We know that make uses the "VPATH" variable to specify the search path for dependent files. We can then use this search path to specify the search Path parameter cflags for the compiler's header file, such as:

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 exactly the parameter of the CC or GCC search header file path.


Three, file name operation function

The functions we're going to introduce are primarily for handling filenames. The parameter strings for each function are treated as one or a series of file names.

$ (dir <names...>)

Name: Take the directory function--dir.
Function: Remove the directory section from the file name sequence <names>. The directory section refers to the part before the last backslash ("/"). Returns "./" If there is no backslash.
Returns: Returns the directory portion of the file name sequence <names>.
Example: $ (dir src/foo.c hacks) The return value is "src/./".

$ (Notdir <names...>)

Name: Fetch file function--notdir.
Function: Remove the non-directory section from the file name sequence <names>. The non-directory part refers to the part after the last backslash ("/").
Returns: Returns the list of file names <names> the non-directory portion.
Example: $ (notdir src/foo.c hacks) The return value is "foo.c hacks".

$ (suffix <names...>)

Name:--suffix suffix function.
function: Remove the suffix of each filename from the file name sequence <names>.
Returns: Returns the sequence of file names <names> suffix sequences, and returns an empty string if the file does not have a suffix.
Example: $ (suffix src/foo.c src-1.0/bar.c hacks) The return value is ". C. C".

$ (basename <names...>)

Name: Takes the prefix function--basename.
Function: Remove the prefix portion of each file name from the file name sequence <names>.
Returns: Returns a sequence of prefixes <names> sequences of names, and returns an empty string if the file does not have a prefix.
Example: $ (basename src/foo.c src-1.0/bar.c hacks) The return value is "Src/foo src-1.0/bar hacks".

$ (Addsuffix <suffix>,<names...>)

Name: Add suffix function--addsuffix.
function: Add suffix <suffix> to each word in <names>.
Returns: Returns a sequence of file names added with a suffix.
Example: $ (addsuffix. C,foo bar) The return value is "foo.c bar.c".

$ (Addprefix <prefix>,<names...>)

Name: Plus prefix function--addprefix.
Function: prefix <prefix> add to each word in <names>.
Returns: Returns the prefix sequence of filenames.
Example: $ (addprefix src/,foo bar) The return value is "Src/foo src/bar".

$ (Join <list1>,<list2>)

Name: Connection function--join.
Function: Add the words in <list2> to the following words in <list1>. If the number of <list1> words is more than <list2>, then the extra words in the,<list1> will remain intact. If the number of <list2> words is more than <list1>, then,<list2> words will be copied to <list2>.
Return: Returns the string after the connection.
Example: $ (Join AAA BBB, 111 222 333) The return value is "aaa111 bbb222 333".

Source: http://blog.csdn.net/haoel/article/details/2894

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.