Makefile (Condition Statement)

Source: Internet
Author: User
7. conditional statements for makefile
 
A conditional statement can execute or ignore some scripts in the MAKEFILE file based on the value of the variable. A condition statement can compare a variable with the value of another variable or a variable with a String constant. The condition statement is used to control the makefile part actually seen by make. It cannot be used to control shell commands during execution.
7.1 example of a conditional statement
 
The example of the following condition statement tells make to use a database if the variable CC value is 'gcc '. If not, use other databases. It works by selecting one of the two command lines as the command for this rule. The result of 'CC = GCC 'as a parameter changed by make is not only used to determine which compiler to use, but also to decide which database to connect.
 
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
The Condition Statement uses three commands: ifeq, else, And endif.
 
The ifeq command is the start of the Condition Statement and specifies the condition. It contains two parameters separated by commas and expanded in parentheses. During the runtime, replace the two parameter variables and then compare them. The lines following ifeq in makefile are the commands executed when the conditions are met; otherwise, they will be ignored.
 
If the preceding condition fails, the else command will execute the command following it. In the above example, it means that when the first option is not executed, the command connected to the second option will be executed. The else command is optional in the Condition Statement.
 
Endif command end Condition Statement. Any condition statement must end with the endif command, followed by the normal content in the MAKEFILE file.
The preceding example shows that the condition statement works at the original level: the row of the Condition Statement is either processed as part of the MAKEFILE file or ignored according to the condition. This is the reason why a major syntax unit (such as a rule) in the makefile can span the start or end of a conditional statement.
 
When the value of the variable CC is GCC, the effect of the above example is:
 
Foo: $ (objects)
 
$ (CC)-O Foo $ (objects) $ (libs_for_gcc)
 
When the value of the variable CC is not GCC but other values, the effect of the above example is:
Foo: $ (objects)
 
$ (CC)-O Foo $ (objects) $ (normal_libs)
 
The same result can also be obtained using another method: assign a value to a variable first, and then use the variable:
 
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)
7.2 Condition Statement syntax
 
The syntax for conditional statements without else commands is:
 
Conditional-Directive
 
Text-if-true
 
Endif
'Text-if-true' can be any text line. If the condition is 'true', it is considered as part of the MAKEFILE file. If the condition is 'true', it is ignored. The complete Condition Statement syntax is:
 
Conditional-Directive
 
Text-if-true
 
Else
 
Text-if-false
 
Endif
If the condition is 'true', use 'text-if-true'. If the condition is 'false', use 'text-if-false '. 'Text-if-false' can be any number of lines of text.
 
The syntax for 'Conditional-ctive Ve' is exactly the same for simple and complex condition statements. There are four different commands used to test different conditions. The following is a script table:

Ifeq (Arg1,Arg2)

Ifeq'Arg1''Arg2'

Ifeq"Arg1""Arg2"

Ifeq"Arg1"'Arg2'

Ifeq'Arg1'"Arg2"

Extend all variable references in the arg1 and arg2 parameters and compare them. If they are identical, use 'text-if-true'; otherwise, use 'text-if-false' (if any ). You often need to test whether a variable has a non-null value. When complicated variables and function extensions get a value, it is considered null for you, in fact, it may be considered as a null value because it contains spaces, which may cause confusion. You can use the Strip function to avoid interference of spaces as non-null values. For example:
 
Ifeq ($ (Strip $ (FOO )),)
 
Text-if-empty
 
Endif
 
Use 'text-if-empty' even if $ (FOO) contains spaces '.

Ifneq (Arg1,Arg2)

Ifneq'Arg1''Arg2'

Ifneq"Arg1""Arg2"

Ifneq"Arg1"'Arg2'

Ifneq'Arg1'"Arg2"

 
Extend all variable references in the arg1 and arg2 parameters and compare them. If they are different, use 'text-if-true'; otherwise, use 'text-if-false' (if any ).

IfdefVariable-name

If the variable 'Variable-name' is not a null value, 'text-if-true' is valid. Otherwise, 'text-if-false' is valid (if any ). If a variable has never been defined, the variable is null.Note that ifdef only tests whether the variable has a value. It cannot be extended to check whether the variable has a non-null value. Therefore, if ifdef is used to test all Defined variables, 'true' is returned, except for those with 'foo =. Use ifeq ($ (FOO),) to test the null value ),). For example:
 
Bar =
 
Foo = $ (bar)
 
Ifdef foo
 
Frobozz = Yes
Else
 
Frobozz = No
 
Endif

Set 'frobozz 'to 'yes', and ::

 
Foo =
Ifdef foo
 
Frobozz = Yes
 
Else
 
Frobozz = No
 
Endif

Set 'froboz' to 'no '.

IfndefVariable-name

 
If the variable 'Variable-name' is null, 'text-if-true' is valid. Otherwise, 'text-if-false' is valid (if any ).
Extra spaces are allowed before the command line. They are ignored during processing, but tabs are not allowed. (If a line starts with a tab, then this row is considered as the command line of the rule ). In addition, spaces and tabs can be inserted anywhere in the row, except for command names and parameters. Comments starting with '#' can end with a row.
 
The other two influential commands in the condition statement are else and endif. These two commands appear in the form of one word without any parameters. Extra spaces are allowed before the command line. Spaces and tabs can be inserted in the middle of the line. Comments starting with '#' can end with a row.
 
Conditional statements affect makefile files used by make. If the condition is 'true', make reads the rows contained in 'text-if-true'. If the condition is 'false ', make reads the rows contained in 'text-if-false' (if any). The syntax unit of the MAKEFILE file, such as a rule, can span the start or end of a condition statement.
 
The value of the make condition when the MAKEFILE file is read. Therefore, you cannot use automatic variables when testing conditions, because they are defined only when the command is executed (seeAutomatic Variable).
To avoid intolerable confusion, it is not allowed to start a Condition Statement in a MAKEFILE file and end the statement in another MAKEFILE file. However, if you try to introduce the include MAKEFILE file without interrupting the conditional statement, you can write the include command in the conditional statement.
7.3 Condition Statement of the test flag
 
You can use the makeflags and findstring variables to compile a conditional statement to test the make command flag, such as '-t'. (SeeString replacement and analysis functions). This applies only when the touch mark cannot completely change the timestamp of the file.
 
The findstring function checks whether a string is a substring of another string. If you want to test the '-t' flag, use'-t' as the first string and use the value of the variable makeflags as another string. For example, the following example shows how to use 'ranlib-t' to update an archive file:
 
Archive. :...
Ifneq (, $ (findstring T, $ (makeflags )))
 
+ Touch archive.
 
+ Ranlib-T archive.
 
Else
 
Ranlib archive.
Endif
 
The prefix '+' indicates that these command lines are recursively called, even if '-t' is used to mark them to be executed. SeeRecursively call make

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.