Makefile condition judgment ---------- [Badboy], makefile condition badboy

Source: Internet
Author: User

Makefile condition judgment ---------- [Badboy], makefile condition badboy

Conditional judgment allows make to select different execution branches based on different running conditions. A conditional expression can be used to compare the value of a variable or a constant.

I. Example

In the following example, determine whether the $ (CC) variable is "gcc". If yes, use the GNU function to compile the target.

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 the rule in the above example, the target "foo" can select different function libraries based on the variable "$ (CC)" value to compile the program.

We can see three keywords from the preceding example: ifeq, else, And endif. ifeq indicates the start of a Condition Statement and specifies a condition expression. The expression contains two parameters separated by commas (,). The expression is enclosed in parentheses. Else indicates that the conditional expression is false. Endif indicates the end of a condition statement. Any condition expression should end with endif.

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

Foo: $ (objects)

$ (CC)-o foo $ (objects) $ (libs_for_gcc)

When the $ (CC) value of our variable is not "gcc" (such as "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 concisely:

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)

Ii. Syntax

The syntax of the conditional expression is:

Endif

And:

Else

Endif

It indicates the condition keyword, for example, "ifeq". There are four keywords.

The first one is the "ifeq" we have seen before"

Ifeq (,)

Ifeq ''''

Ifeq """"

Ifeq ""''

Ifeq ''""

Compare whether the values of "arg1" and "arg2" are the same. Of course, we can also use the make function in parameters. For example:

Ifeq ($ (strip $ (foo )),)

Endif

In this example, the "strip" function is used. If the return value of this function is Empty, it takes effect.


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

Ifneq (,)

Ifneq ''''

Ifneq """"

Ifneq ""''

Ifneq ''""

The comparison parameter "arg1" and "arg2" have the same value. If they are different, they are true. Similar to "ifeq.

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

Ifdef

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

Example 1:

Bar =

Foo = $ (bar)

Ifdeffoo

Frobozz = yes

Else

Frobozz = no

Endif

Example 2:

Foo =

Ifdeffoo

Frobozz = yes

Else

Frobozz = no

Endif

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

The fourth condition keyword is "ifndef". Its syntax is:

Ifndef

I will not say much about this. It is the opposite of "ifdef.

On this line, extra spaces are allowed, but cannot start with the [Tab] Key (otherwise it is considered a command ). The annotator "#" is also safe. The same is true for "else" and "endif", as long as it does not start with the [Tab] key.

Note that make calculates the value of the conditional expression when reading Makefile and selects the statement based on the value of the conditional expression, you 'd better not put automation variables (such as "$ @") into conditional expressions, because automation variables are available at runtime.

In addition, to avoid confusion, make does not allow the entire Condition Statement to be divided into two parts and placed in different files.



Wildcard in makefile determines whether the file exists

Because the if condition is that $ (wildcard test_file) is successful
If the operation succeeds, the value is 0, so the condition is false.
Show file not exist
Can be changed
$ (If $ (wildcard test_file) = 0 ,\

Makefile: Determine whether a file exists

Use shell to determine.

If [-f xxxfile]

Baidu, makefile calls shell.
 

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.