String handling functions
1, subst
$ (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. "
2, Patsubst
$ (Patsubst <pattern>,<replacement>,<text>)
Name: pattern string substitution function--patsubst.
Features: Find words in <text> (word with "space", "tab" or "carriage return" delimited) whether the character
Match mode <pattern>, and if matched, replace with <replacement>. Here,<pattern> can include a pass
Wildcard "%",
Represents a string of any length.
If <replacement> also contains "%",
So
<replacement>
The "%" in the <pattern> will be the string represented by the "%" in the. Can be escaped with "\",
(
With "\%"
To represent 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.
3, Strip
$ (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".
4, FindString
$ (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)
5. Filter
$ (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>. OK
There are 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".
6, Filter-out
$ (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>. OK
There are 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".
7. Sort
$ (sort <list>)
Name: Sort function--sort.
Function: Sort the words in string <list> (ascending)
。
Return: Returns the sorted string.
Example: $ (Sort foo bar lose) returns "Bar Foo lose".
Note: The sort function removes the same word from <list>.
8. Word
$ (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 return to
The go home string.
Example: $ (Word 2, foo bar baz) The return value is "bar".
9, Wordlist
$ (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>, it
Returns an empty string. If <e> is larger than the number of <text> words, then return the word from <s> start to <text> end
String.
Example: $ (wordlist 2, 3, foo bar baz) The return value is "bar baz".
10, words
$ (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 <te
xt>),<text>).
11, FirstWord
$ (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>).
12. String function Instances
Above
Is all the string manipulation functions,
If paired with a mix,
Can accomplish more complex functions.
Over 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 of 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 the CC or GCC search
The parameters of the header file path.
File name manipulation function
The function we are going to introduce here is mainly to handle the file name. The argument string for each function will be treated as one or
A series of filenames to treat.
1. Dir
$ (dir <names...>)
Name: Take the directory function--dir.
Function: Remove the directory part from the file name sequence <names>. The directory part refers to the last backslash ("/") before
Part of the. 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/./".
2, Notdir
$ (Notdir <names...>)
Name: Take the file function--notdir.
Function: Remove the non-directory part from the file name sequence <names>. The non-directory part refers to the last backslash ("/")
After the part.
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".
3, suffix
$ (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".
4, basename
$ (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/b
AR hacks ".
5, Addsuffix
$ (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".
6, Addprefix
$ (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".
7. Join
$ (Join <list1>,<list2>)
Name: Connection function--join.
Function: Add the words in <list2> to the <list1> after the words. If the number of words <list1>
<list2> more, then the extra words in the,<list1> will remain the same. If the number of words <list2>
<list1> more, then,<list2> more 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".
foreach function
The foreach function is very different from other functions. Because this function is used for recycling, Makefile
The foreach function in is almost modeled after a for statement in the Unix standard Shell (/bin/sh), or C-shell
(/BIN/CSH) of the foreach statement. Its syntax is:
$ (foreach <var>,<list>,<text>)
This function means that the words in the parameter <list> are taken out of the parameters <var> the specified variables.
Then execute the expression that <text> contains. Each time <text> returns a string that is,<text> during the loop
Each string that is returned is separated by a space,
Finally, when the entire loop is over,
Each string returned by <text>
The entire string, separated by spaces, will be the return value of the Foreach function.
So
<var> better be a variable name,
<list> can be an expression,
In general, <text> will use <var>
This parameter enumerates the words in the <list> in turn. 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 one by one and coexist into the variable "n", "$ (N)". O "Each time
Calculates a value based on "$ (N)", separated by a space, 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 parameter
The <var> variable will not function, its scope is only in the Foreach function.
The IF function is much like the conditional statement--ifeq supported by GNU make (see the section above)
, if function
The syntax is:
$ (if <condition>,<then-part>)
$ (if <condition>,<then-part>,<else-part>)
Visible, the IF function can contain the "else" part, or not. That is, the IF function can have a parameter of two, or it can be
To be three. The <condition> parameter is an if expression, and if it returns a non-empty string, then the expression
is equivalent to returning to true, so,<then-part> will be calculated, otherwise <else-part> will be calculated.
If the return value of the IF function is, if <condition> is true (non-empty string)
, the <then-part> will be the whole
The return value of a function if <condition> is False (an empty string)
, then <else-part> will be the return of the entire function
return value, 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.
The Call function call function is the only one that can be used to create new parameterized functions. 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 a parameter to the expression
Number. Its syntax is:
$ (call <expression>,<parm1>,<parm2>,<parm3> ...)
When make executes this function the variables in the,<expression> parameter, such as $ (1), $ (2), $ (3), etc., are
Parameters <parm1>,<parm2>,<parm3> Replace in turn. The return value of <expression> is the return of the call function.
Value. 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".
The origin function is not like any other function, he does not manipulate the value of the variable, he just tells you what the variable is.
? The 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 it in <variable>.
The "$" character. The origin function will tell you the "birth condition" of the variable with its return value, and the following is the origin
The return value of the 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 in the back
Tell.
Environment "If <variable> is an environment variable,
And when Makefile is executed,
"-E"
The parameter was 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 its bag
A definition file make.def, a variable "bletch" is defined in make.def, and our environment
There is an environment variable "Bletch", at this point, we want to determine if the variable originates from the environment, then we will
It is redefined that we do not redefine it if it originates from a make.def or a non-environment such as a command line. In
Yes, 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?
Do you need to use such a step? Yes, we can use override to achieve this effect, but override
Too rough, it also covers the variables defined from the command line, and we just want to redefine the environment, and
Don't want to redefine the command line.
Shell function shell functions are also not like other functions. 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 takes the output after executing the operating system command as
function to return. 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 your
There are some more complex rules in Makefile, and this function is used extensively, so for your system performance there is
The damage. In particular, the obscure rules of Makefile may make your shell function perform more often than you think.
Many.
The function
make provides functions to control the run of make. In general, you need to detect some of the
run-time information when you run Makefile, and depending on the information, decide whether you want make to continue or stop.
1, Error
$ (Error <text ...>)
produces a fatal error, <text ...> is an error message. Note that the error function will not be used in a single
to generate a fault message, so if you define it in a variable and use that variable in subsequent scripts,
is also possible. Example:
Example one:
ifdef error_001
$ (Error error is $ (error_001))
EndIf
Example two:
ERR = $ (Error found an ER ror!)
. Phony:err
Err: $ (ERR)
Example one will produce an error call when execution occurs after the variable error_001 is defined, while example two occurs when the directory Err
is executed.
2, Warning
$ (warning <text ...>)
This function is much like an error function, except that it does not let make quit, but only outputs a warning message, while the MA
Ke continues to execute.