Makefile condition judgment —————————— "Badboy"

Source: Internet
Author: User

By using conditional judgment, you can have make to select different execution branches depending on the runtime's circumstances. A conditional expression can be a value of a comparison variable, or a value that compares variables and constants.

One, example

The following example determines whether the $ (CC) variable is "gcc" and, if so, compiles the target using the GNU function.

LIBS_FOR_GCC =-lgnu

normal_libs=

Foo: $ (objects)

Ifeq ($ (CC), GCC)

$ (CC)-O-foo $ (objects) $ (LIBS_FOR_GCC)

Else

$ (CC)-O-foo $ (objects) $ (normal_libs)

endif

It can be seen that in this rule of the example above, the target "foo" could choose a different library of functions to compile the program based on the variable "$ (CC)" value.

We can see three keywords from the example above: ifeq, else, and endif.ifeq denote the beginning of a conditional statement, and specify a conditional expression that contains two arguments, separated by commas, with expressions enclosed in parentheses. else indicates that the conditional expression is false. ENDIF represents the end of a conditional statement, and any one of the conditional expressions should end with endif.

When our variable $ (CC) value is "gcc", the rule for the target Foo is:

Foo: $ (objects)

$ (CC)-O-foo $ (objects) $ (LIBS_FOR_GCC)

When our variable $ (cc) value is not "gcc" (e.g. "CC"), the rule for the target Foo is:

Foo: $ (objects)

$ (CC)-O-foo $ (objects) $ (normal_libs)

Of course, we can also write the above example more concise:

LIBS_FOR_GCC =-lgnu

normal_libs=

Ifeq ($ (CC), GCC)

libs=$ (LIBS_FOR_GCC)

Else

libs=$ (Normal_libs)

endif

Foo: $ (objects)

$ (CC)-O-foo $ (objects) $ (LIBS)

Second, the grammar

The syntax for a conditional expression is:

endif

And:

Else

endif

which represents a conditional keyword, such as "ifeq". This keyword has four.

The first one is the "ifeq" we've seen before.

Ifeq (,)

Ifeq ""

Ifeq "" ""

Ifeq "'"

Ifeq ' ""

Compare the values of the parameter "Arg1" and "arg2". Of course, we can also use the Make function in the parameters. Such as:

Ifeq ($ (Strip $ (foo)),)

endif

In this example, the "strip" function is used, and if the return value of the function is null (empty), then it takes effect.


The second conditional keyword is "ifneq". The syntax is:

Ifneq (,)

Ifneq ""

Ifneq "" ""

Ifneq "'"

Ifneq ' ""

Whether the values for the comparison parameters "Arg1" and "arg2" are the same, or true if they are different. Similar to "ifeq".

The third conditional keyword is "ifdef". The syntax is:

Ifdef

If the value of the variable is not empty, then the expression is true. Otherwise, the expression is false. Of course, the same can be the return value of a function. Note that ifdef just tests whether a variable has a value, and does not extend the variable to its current position. Let's take a look at two examples:

Example one:

Bar =

Foo =$ (BAR)

Ifdeffoo

Frobozz =yes

Else

Frobozz =no

endif

Example two:

Foo =

Ifdeffoo

Frobozz =yes

Else

Frobozz =no

endif

In the first example, the "$ (frobozz)" value is "yes" and the second is "no".

The fourth conditional keyword is "ifndef". The syntax is:

Ifndef

I will not say more, and "ifdef" is the opposite meaning.

In this line, the extra space is allowed, but cannot start with the [Tab] key (otherwise it is considered a command). The comment "#" is also safe. "Else" and "endif" are the same, as long as you do not start with the [Tab] key.

It is especially important to note that make is the value of the conditional expression when reading makefile, and select the statement based on the value of the conditional expression, so you'd better not put the automation variables (such as "[email protected]", etc.) into the conditional expression, Because automation variables are only at run time.

Also, to avoid confusion, make does not allow the entire conditional statement to be divided into two parts in separate files.


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.