Write with Me Makefile (10) _makefile

Source: Internet
Author: User
Four, the Foreach function

The foreach function is very different from other functions. Because this function is used for looping purposes, the Foreach function in makefile is almost modeled on 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 each one and placed in the variable specified by the parameter <var>, and then the expression that is included in <text> is executed. Each time the <text> returns a string, each string returned by the,<text> in the loop is separated by a space, and finally when the entire loop ends,<text> The entire string (separated by a space) of each string returned will be the return value of the Foreach function.

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

Names: = a b c d

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

In the example above, the words in $ (name) are taken out, coexisting to the variable "n", "$ (n). O" computes a value each time according to "$ (n)", which is 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 is finished, the arguments <var> variables do not function, and the scope is only within the Foreach function.

Five, if function

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

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

Or

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

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

And if the return value of the IF function is, if <condition> is true (Non-empty string), that <then-part> will be the return value of the entire function, if <condition> is False (empty string), then < Else-part> is 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 the parameter <parm1>,<parm2>,<parm3> in turn. 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 in order, such as:

Reverse = $ (2) $ (1)

Foo = $ (call reverse,a,b)

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

Seven, Origin function

Unlike other functions, the Origin function does not manipulate the value of a variable, he just tells you where your variable is coming 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 tells you the "birth" of the variable by 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, we'll talk about this variable 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 the <variable> 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 automation variables are described later.

This information is very useful for us to write makefile, for example, suppose we have a makefile that contains a definition file make.def, a variable "bletch" is defined in make.def, and there is an environment variable "bletch" in our environment. , at this point, we want to judge if the variable comes from the environment, then we redefine it, and if it comes from make.def or the command line and so on, then we don't redefine it. So, in our makefile, we can write this:

Ifdef Bletch

Ifeq "$ (Origin Bletch)" "Environment"

Bletch = barf, gag, etc.

endif

endif

Of course, you might say that you can redefine the variables in your environment using the override keyword. Why you need to use such a step. Yes, we can do that with override, but the override is too rough, it also covers the variables that are defined from the command line, and we just want to redefine the environment and we don't want to redefine the command line.

Eight, Shell functions

The Shell function is not like any other function. As the name suggests, its parameters should be the operating system Shell's command. It is the same function as the inverted quote "'". This means that the Shell function returns the output that executes the operating system command as a function. We can then use the operating system commands and the string Processing command awk,sed and so on to generate a variable, such as:

Contents: = $ (Shell cat foo)

Files: = $ (shell echo *.c)

Note that this function will be reborn as a shell program to execute commands, so you should pay attention to its performance, if you have some more complex rules in your makefile, and use this function massively, then it is harmful to your system performance. In particular, makefile's cryptic rules may make your shell function execute more often than you think.

Nine, control make's function

Make provides a number of functions to control the run of made. Typically, you need to detect run-time information when running makefile, and depending on that 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 does not produce an incorrect message when it is used, so it is OK if you define it in a variable and use the variable in subsequent scripts. For example:

Example one:

Ifdef error_001

$ (Error error is $ (error_001))

endif

Example two:

ERR = $ (Error found an error!)

. Phony:err

ERR:; $ (ERR)

Example one generates an error call when the variable error_001 is defined, and example two causes an error call when the directory err is executed.

$ (Warning <text ...>)

This function is much like the error function, except that it doesn't let make quit, it just prints out a warning message and makes continues.


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

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.