Linux Makefile Tutorial Function VII [GO]

Source: Internet
Author: User

Using functions

————

In makefile, you can use functions to manipulate variables to make our commands or rules more flexible and intelligent. The functions supported by make are not much, but they are sufficient for our operation. 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 by "$", with the following syntax:

$ (<function> <arguments>)

Or

${<function> <arguments>}

Here,<function> is the function name, and make does not support many functions. <arguments> is the parameter of the function, separated by a comma "," and a "space" between the functions name and the parameter. A function call begins with "$", enclosing the function name and arguments in parentheses or curly braces. It feels like a variable, doesn't it? Arguments in a function can use variables, in order to unify the style, the function and the parentheses of the variable are best, such as using the form "$ (subst a,b,$ (x))" Instead of "$ (subst a,b,${x})". Because 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) used $ (empty) to define a space, the value of $ (foo) is "a B C", the definition of $ (bar), called the function "subst", which is a replacement function, this function has three parameters, the first parameter is the replacement string, the second argument is the replacement string, The third parameter is a string that the substitution operation acts on. This function replaces the space in $ (foo) with a comma, so the value of $ (bar) is "A,b,c".

Second, string processing function

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

Name: string substitution function--subst.

function: Replace the <from> string in strings <text> with <to>.

Return: function returns the string that was replaced.

Example:

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

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

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

Name: pattern string substitution function--patsubst.

Function: Find words in <text> (the words are separated by "space", "tab" or "carriage return") conform to the pattern <pattern>, and if so, replace with <replacement>. Here,<pattern> can include the wildcard "%", which represents any length of string. If <replacement> also contains "%", then the "%" in the,<replacement> will be the string represented by the "%" in <pattern>. (Can be escaped with "/", "/%" to indicate the true meaning of the "%" character)

Return: function returns the string that was replaced.

Example:

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

Replace the word string "x.c.c bar.c" with [%.c] with [%.O] and return the result "X.C.O bar.o"

Note:

This is somewhat similar to what we said earlier in the "Variable chapters". Such as:

"$ (var:<pattern>=<replacement>)"

Equivalent

"$ (Patsubst <pattern>,<replacement>,$ (Var))",

and "$ (var: <suffix>=<replacement>)"

is equivalent to

"$ (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 empty characters from the beginning and end of the <string> string.

Return: Returns the string value of the stripped space.

Example:

$ (Strip a B c)

The string "A B C" goes 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: Returns <FIND> if found, otherwise returns 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 (an empty string)

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

Name: Filter function--filter.

Function: Filter the words in the string with the <pattern> mode, and keep the words that conform to the pattern <pattern>. There can be multiple modes.

Return: Returns a string that conforms to the pattern <pattern>.

Example:

Sources: = foo.c bar.c baz.s ugh.h

Foo: $ (sources)

CC $ (Filter%.c%.s,$ (sources))-O foo

The value returned by $ (filter%.c%.s,$ (sources)) is "foo.c bar.c baz.s".

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

Name: Anti-filter function--filter-out.

Function: Filter the words in the string by <pattern> mode, and remove the words that conform to the pattern <pattern>. There can be multiple modes.

Return: 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: Sorts the words in the string <list> (ascending order).

Return: Returns the sorted string.

Example: $ (Sort foo bar lose) returns "Bar Foo lose".

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

$ (Word <n>,<text>)

Name: Take the word function--word.

Function: Take the string <text> the first <n> words. (from the Beginning)

Return: Returns the string <text> the <n> Word. If <n> is larger than the number of words in <text>, then an empty string is returned.

Example: $ (Word 2, foo bar baz) The return value is "bar".

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

Name: Take the word string function--wordlist.

Function: From the string <text> from the <s> start to the word string <e>. <s> and <e> are a number.

Return: Returns the string <text> Word string from <s> to <e>. If <s> is larger than the number of words in <text>, then an empty string is returned. If <e> is larger than the number of <text> words, then return the word string from <s> start to <text> end.

Example: $ (wordlist 2, 3, foo bar baz) The return value is "bar baz".

$ (words <text>)

Name: number of words statistic function--words.

Function: Counts the number of words in a string in <text>.

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 a string <text>.

Example: $ (FirstWord foo bar) The return value is "foo".

Note: This function can be implemented with the word function: $ (word 1,<text>).

Above, is all the string operation function, if collocation mix use, can complete more complex function. Here, give an example of a practical application. We know that make uses the "VPATH" variable to specify a "dependent file" search path. 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 ", this is exactly the parameter of the CC or GCC search header file path.

Third, file name operation function

The function we are going to introduce here is mainly to handle the file name. The argument strings for each function are treated as one or a series of filenames.

$ (dir <names...>)

Name: Take the directory function--dir.

Function: Remove the directory part from the file name sequence <names>. The contents section refers to the previous part of the last backslash ("/"). If there is no backslash, return "./".

Return: Returns the directory portion of the file name sequence <names>.

Example: $ (dir src/foo.c hacks) The return value is "src/./".

$ (Notdir <names...>)

Name: Take the file function--notdir.

Function: Remove the non-directory part from the file name sequence <names>. The non-catalog part refers to the part after the last backslash ("/").

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

Example: $ (notdir src/foo.c hacks) The return value is "foo.c hacks".

$ (suffix <names...>)

Name: Take the suffix function--suffix.

function: Remove the suffix of each file name from the file name sequence <names>.

Return: Returns the suffix sequence of the file name sequence <names> 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: Take the prefix function--basename.

Function: Remove the prefix portion of each file name from the file name sequence <names>.

Return: Returns the prefix sequence of the file name sequence <names> 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 the back of each word in <names>.

Return: Returns a sequence of file names with the suffix added.

Example: $ (addsuffix. C,foo bar) The return value is "foo.c bar.c".

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

Name: Add prefix function--addprefix.

Function: prefix <prefix> add to <names> after each word.

Return: Returns a sequence of filenames prefixed by the prefix.

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 <list1> after the words. If the number of <list1> words is more than <list2>, then the more words in the,<list1> will remain intact. If the number of <list2> words is more than <list1>, then the,<list2> Word 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".

Four, the Foreach function

The foreach function is very different from other functions. Because this function is used for looping, the Foreach function in makefile is almost modeled after a 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 means that the words in the parameter <list> are taken out of the argument <var> the variable specified, and then the <text> contained expression is executed. Each time the <text> returns a string, each string returned by the,<text> during the loop is separated by a space, and finally when the entire loop ends,<text> The entire string (separated by spaces) of each string that is returned will be the return value of the Foreach function.

So,<var> better be a variable name,<list> can be an expression, and <text> typically uses the <var> parameter to enumerate the words in the <list> sequence. As an example:

Names: = a b c d

Files: = $ (foreach n,$ (names), $ (n). O)

In the example above, the words in the $ (name) are taken out to the variable "n", "$ (N)". O "Each time a value is computed based on" $ (N) ", the values are separated by spaces and finally returned as a foreach function, so 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, and when the foreach function finishes executing, the arguments <var> variables will not function, and their scope is only within the Foreach function.

Five, if function

The IF function is much like the conditional statement--ifeq supported by GNU make (see the previous section), the syntax of the IF function is:

$ (if <condition>,<then-part>)

Or

$ (if <condition>,<then-part>,<else-part>)

Visible, the IF function can contain the "else" part, or not. That is, the IF function can have two or three arguments. The <condition> parameter is an if expression, and if it returns a non-empty string, the expression is equivalent to returning true, so,<then-part> is evaluated, otherwise <else-part> is computed.

If the return value of the IF function is, if <condition> is true (not an empty string), that <then-part> will be the return value of the entire function, if <condition> is False (an empty string), then < Else-part> will be the return value of the entire function, and if <else-part> is not defined, then the entire function returns an empty string.

So,<then-part> and <else-part> will only have one to be counted.

Six, call function

The call function is the only one that can be used to create a new parameterized function. You can write a very complex expression in which you can define many parameters, and then you can use the call function to pass arguments to the expression. Its syntax is:

$ (call <expression>,<parm1>,<parm2>,<parm3> ...)

When make executes this function the variables in the,<expression> parameter, such as $ (1), $ (2), $ (3), and so on, are replaced by parameters <parm1>,<parm2>,<parm3> sequentially. The return value of <expression> is the return value of the call function. For example:

Reverse = $ (1) $ (2)

Foo = $ (call reverse,a,b)

Well, the value of Foo is "a B". Of course, the order of the parameters can be customized, not necessarily sequential, such as:

Reverse = $ (2) $ (1)

Foo = $ (call reverse,a,b)

The value of Foo at this point is "b a".

Vii. Origin function

The origin function is not like any other function, he does not manipulate the value of the variable, he just tells you where this variable comes from? Its syntax is:

$ (Origin <variable>)

Note that,<variable> is the name of the variable and should not be a reference. So you'd better not use the "$" character in <variable>. The origin function will tell you the "birth condition" of the variable with its return value, and the following is the return value of the origin function:

"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 when makefile is executed, the "-e" parameter is not opened.

"File"

If <variable> this variable is defined in makefile.

"Command Line"

If <variable> this variable is defined by the command line.

"Override"

If <variable> is redefined by the override indicator.

"Automatic"

If <variable> is an automation variable in a command run. About the automation variables are described later.

This information is very useful for us to write makefile, for example, suppose we have a makefile that has a definition file make.def, defines a variable "Bletch" in Make.def, and we have an environment variable "bletch" in our environment. , at this point, we want to judge if the variable originates from the environment, then we redefine it, and if it comes from a non-environment such as MAKE.DEF or command line, then we don't redefine it. So, in our makefile, we can write:

Ifdef Bletch

Ifeq "$ (Origin Bletch)" "Environment"

Bletch = barf, gag, etc.

endif

endif

Of course, you might say that using the override keyword doesn't make it possible to redefine the variables in the environment? Why do you need to use such a step? Yes, we can do this with override, but the override is too rough, it also overwrites the variables defined from the command line, and we just want to redefine the environment and not want to redefine the command line.

Eight, Shell functions

The Shell function is not like any other function. As the name implies, its parameters should be the command of the operating system shell. It is the same function as the anti-quote "'". This means that the Shell function returns the output after executing the operating system command as a function. Therefore, we can use the operating system command and string Processing command awk,sed and so on command to generate a variable, such as:

Contents: = $ (Shell cat foo)

Files: = $ (shell echo *.c)

Note that this function will be reborn into a shell program to execute the command, so you should pay attention to its performance, if you have some more complex rules in your makefile, and use this function extensively, it is harmful to your system. In particular, the obscure rules of makefile may make your shell functions perform much more often than you think.

Nine, the function of control make

Make provides a number of functions to control the run of make. In general, you need to detect some run-time information when running makefile, and depending on the information, whether you want make to continue or stop.

$ (Error <text ...>)

Generates a fatal error, <text ...> is an error message. Note that the error function will not be used in the event of a mistake, so if you define it in a variable and use it in subsequent scripts, it is also possible. For example:

Example one:

Ifdef error_001

$ (Error error is $ (error_001))

endif

Example two:

ERR = $ (Error found an error!)

. Phony:err

ERR:; $ (ERR)

The example one generates an error call when execution occurs after the variable error_001 is defined, while example two causes an error call when the directory err is executed.

$ (Warning <text ...>)

This function is much like an error function, except that it does not let make quit, just outputs a warning message, and makes continues execution.

Related Article

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.