Files
oldlinux-files/Ref-docs/manual make/make_7.html
2024-02-19 00:21:47 -05:00

361 lines
11 KiB
HTML

<HTML>
<HEAD>
<!-- This HTML file has been created by texi2html 1.54
from ../texi/make.texinfo on 19 July 2000 -->
<TITLE>GNU make - Conditional Parts of Makefiles</TITLE>
<link href="make_8.html" rel=Next>
<link href="make_6.html" rel=Previous>
<link href="make_toc.html" rel=ToC>
</HEAD>
<BODY>
<p>Go to the <A HREF="make_1.html">first</A>, <A HREF="make_6.html">previous</A>, <A HREF="make_8.html">next</A>, <A HREF="make_19.html">last</A> section, <A HREF="make_toc.html">table of contents</A>.
<P><HR><P>
<H1><A NAME="SEC71" HREF="make_toc.html#TOC71">Conditional Parts of Makefiles</A></H1>
<P>
<A NAME="IDX481"></A>
A <STRONG>conditional</STRONG> causes part of a makefile to be obeyed or ignored
depending on the values of variables. Conditionals can compare the
value of one variable to another, or the value of a variable to
a constant string. Conditionals control what <CODE>make</CODE> actually
"sees" in the makefile, so they <EM>cannot</EM> be used to control shell
commands at the time of execution.
</P>
<H2><A NAME="SEC72" HREF="make_toc.html#TOC72">Example of a Conditional</A></H2>
<P>
The following example of a conditional tells <CODE>make</CODE> to use one set
of libraries if the <CODE>CC</CODE> variable is <SAMP>`gcc'</SAMP>, and a different
set of libraries otherwise. It works by controlling which of two
command lines will be used as the command for a rule. The result is
that <SAMP>`CC=gcc'</SAMP> as an argument to <CODE>make</CODE> changes not only which
compiler is used but also which libraries are linked.
</P>
<PRE>
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
</PRE>
<P>
This conditional uses three directives: one <CODE>ifeq</CODE>, one <CODE>else</CODE>
and one <CODE>endif</CODE>.
</P>
<P>
The <CODE>ifeq</CODE> directive begins the conditional, and specifies the
condition. It contains two arguments, separated by a comma and surrounded
by parentheses. Variable substitution is performed on both arguments and
then they are compared. The lines of the makefile following the
<CODE>ifeq</CODE> are obeyed if the two arguments match; otherwise they are
ignored.
</P>
<P>
The <CODE>else</CODE> directive causes the following lines to be obeyed if the
previous conditional failed. In the example above, this means that the
second alternative linking command is used whenever the first alternative
is not used. It is optional to have an <CODE>else</CODE> in a conditional.
</P>
<P>
The <CODE>endif</CODE> directive ends the conditional. Every conditional must
end with an <CODE>endif</CODE>. Unconditional makefile text follows.
</P>
<P>
As this example illustrates, conditionals work at the textual level:
the lines of the conditional are treated as part of the makefile, or
ignored, according to the condition. This is why the larger syntactic
units of the makefile, such as rules, may cross the beginning or the
end of the conditional.
</P>
<P>
When the variable <CODE>CC</CODE> has the value <SAMP>`gcc'</SAMP>, the above example has
this effect:
</P>
<PRE>
foo: $(objects)
$(CC) -o foo $(objects) $(libs_for_gcc)
</PRE>
<P>
When the variable <CODE>CC</CODE> has any other value, the effect is this:
</P>
<PRE>
foo: $(objects)
$(CC) -o foo $(objects) $(normal_libs)
</PRE>
<P>
Equivalent results can be obtained in another way by conditionalizing a
variable assignment and then using the variable unconditionally:
</P>
<PRE>
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)
</PRE>
<H2><A NAME="SEC73" HREF="make_toc.html#TOC73">Syntax of Conditionals</A></H2>
<P>
<A NAME="IDX482"></A>
<A NAME="IDX483"></A>
<A NAME="IDX484"></A>
<A NAME="IDX485"></A>
<A NAME="IDX486"></A>
<A NAME="IDX487"></A>
</P>
<P>
The syntax of a simple conditional with no <CODE>else</CODE> is as follows:
</P>
<PRE>
<VAR>conditional-directive</VAR>
<VAR>text-if-true</VAR>
endif
</PRE>
<P>
The <VAR>text-if-true</VAR> may be any lines of text, to be considered as part
of the makefile if the condition is true. If the condition is false, no
text is used instead.
</P>
<P>
The syntax of a complex conditional is as follows:
</P>
<PRE>
<VAR>conditional-directive</VAR>
<VAR>text-if-true</VAR>
else
<VAR>text-if-false</VAR>
endif
</PRE>
<P>
If the condition is true, <VAR>text-if-true</VAR> is used; otherwise,
<VAR>text-if-false</VAR> is used instead. The <VAR>text-if-false</VAR> can be any
number of lines of text.
</P>
<P>
The syntax of the <VAR>conditional-directive</VAR> is the same whether the
conditional is simple or complex. There are four different directives that
test different conditions. Here is a table of them:
</P>
<DL COMPACT>
<DT><CODE>ifeq (<VAR>arg1</VAR>, <VAR>arg2</VAR>)</CODE>
<DD>
<DT><CODE>ifeq '<VAR>arg1</VAR>' '<VAR>arg2</VAR>'</CODE>
<DD>
<DT><CODE>ifeq "<VAR>arg1</VAR>" "<VAR>arg2</VAR>"</CODE>
<DD>
<DT><CODE>ifeq "<VAR>arg1</VAR>" '<VAR>arg2</VAR>'</CODE>
<DD>
<DT><CODE>ifeq '<VAR>arg1</VAR>' "<VAR>arg2</VAR>"</CODE>
<DD>
Expand all variable references in <VAR>arg1</VAR> and <VAR>arg2</VAR> and
compare them. If they are identical, the <VAR>text-if-true</VAR> is
effective; otherwise, the <VAR>text-if-false</VAR>, if any, is effective.
Often you want to test if a variable has a non-empty value. When the
value results from complex expansions of variables and functions,
expansions you would consider empty may actually contain whitespace
characters and thus are not seen as empty. However, you can use the
<CODE>strip</CODE> function (see section <A HREF="make_8.html#SEC77">Functions for String Substitution and Analysis</A>) to avoid interpreting
whitespace as a non-empty value. For example:
<PRE>
ifeq ($(strip $(foo)),)
<VAR>text-if-empty</VAR>
endif
</PRE>
will evaluate <VAR>text-if-empty</VAR> even if the expansion of
<CODE>$(foo)</CODE> contains whitespace characters.
<DT><CODE>ifneq (<VAR>arg1</VAR>, <VAR>arg2</VAR>)</CODE>
<DD>
<DT><CODE>ifneq '<VAR>arg1</VAR>' '<VAR>arg2</VAR>'</CODE>
<DD>
<DT><CODE>ifneq "<VAR>arg1</VAR>" "<VAR>arg2</VAR>"</CODE>
<DD>
<DT><CODE>ifneq "<VAR>arg1</VAR>" '<VAR>arg2</VAR>'</CODE>
<DD>
<DT><CODE>ifneq '<VAR>arg1</VAR>' "<VAR>arg2</VAR>"</CODE>
<DD>
Expand all variable references in <VAR>arg1</VAR> and <VAR>arg2</VAR> and
compare them. If they are different, the <VAR>text-if-true</VAR> is
effective; otherwise, the <VAR>text-if-false</VAR>, if any, is effective.
<DT><CODE>ifdef <VAR>variable-name</VAR></CODE>
<DD>
If the variable <VAR>variable-name</VAR> has a non-empty value, the
<VAR>text-if-true</VAR> is effective; otherwise, the <VAR>text-if-false</VAR>,
if any, is effective. Variables that have never been defined have an
empty value.
Note that <CODE>ifdef</CODE> only tests whether a variable has a value. It
does not expand the variable to see if that value is nonempty.
Consequently, tests using <CODE>ifdef</CODE> return true for all definitions
except those like <CODE>foo =</CODE>. To test for an empty value, use
<CODE>ifeq ($(foo),)</CODE>. For example,
<PRE>
bar =
foo = $(bar)
ifdef foo
frobozz = yes
else
frobozz = no
endif
</PRE>
sets <SAMP>`frobozz'</SAMP> to <SAMP>`yes'</SAMP>, while:
<PRE>
foo =
ifdef foo
frobozz = yes
else
frobozz = no
endif
</PRE>
sets <SAMP>`frobozz'</SAMP> to <SAMP>`no'</SAMP>.
<DT><CODE>ifndef <VAR>variable-name</VAR></CODE>
<DD>
If the variable <VAR>variable-name</VAR> has an empty value, the
<VAR>text-if-true</VAR> is effective; otherwise, the <VAR>text-if-false</VAR>,
if any, is effective.
</DL>
<P>
Extra spaces are allowed and ignored at the beginning of the conditional
directive line, but a tab is not allowed. (If the line begins with a tab,
it will be considered a command for a rule.) Aside from this, extra spaces
or tabs may be inserted with no effect anywhere except within the directive
name or within an argument. A comment starting with <SAMP>`#'</SAMP> may appear at
the end of the line.
</P>
<P>
The other two directives that play a part in a conditional are <CODE>else</CODE>
and <CODE>endif</CODE>. Each of these directives is written as one word, with no
arguments. Extra spaces are allowed and ignored at the beginning of the
line, and spaces or tabs at the end. A comment starting with <SAMP>`#'</SAMP> may
appear at the end of the line.
</P>
<P>
Conditionals affect which lines of the makefile <CODE>make</CODE> uses. If
the condition is true, <CODE>make</CODE> reads the lines of the
<VAR>text-if-true</VAR> as part of the makefile; if the condition is false,
<CODE>make</CODE> ignores those lines completely. It follows that syntactic
units of the makefile, such as rules, may safely be split across the
beginning or the end of the conditional.
</P>
<P>
<CODE>make</CODE> evaluates conditionals when it reads a makefile.
Consequently, you cannot use automatic variables in the tests of
conditionals because they are not defined until commands are run
(see section <A HREF="make_10.html#SEC101">Automatic Variables</A>).
</P>
<P>
To prevent intolerable confusion, it is not permitted to start a
conditional in one makefile and end it in another. However, you may
write an <CODE>include</CODE> directive within a conditional, provided you do
not attempt to terminate the conditional inside the included file.
</P>
<H2><A NAME="SEC74" HREF="make_toc.html#TOC74">Conditionals that Test Flags</A></H2>
<P>
You can write a conditional that tests <CODE>make</CODE> command flags such as
<SAMP>`-t'</SAMP> by using the variable <CODE>MAKEFLAGS</CODE> together with the
<CODE>findstring</CODE> function
(see section <A HREF="make_8.html#SEC77">Functions for String Substitution and Analysis</A>).
This is useful when <CODE>touch</CODE> is not enough to make a file appear up
to date.
</P>
<P>
The <CODE>findstring</CODE> function determines whether one string appears as a
substring of another. If you want to test for the <SAMP>`-t'</SAMP> flag,
use <SAMP>`t'</SAMP> as the first string and the value of <CODE>MAKEFLAGS</CODE> as
the other.
</P>
<P>
For example, here is how to arrange to use <SAMP>`ranlib -t'</SAMP> to finish
marking an archive file up to date:
</P>
<PRE>
archive.a: ...
ifneq (,$(findstring t,$(MAKEFLAGS)))
+touch archive.a
+ranlib -t archive.a
else
ranlib archive.a
endif
</PRE>
<P>
The <SAMP>`+'</SAMP> prefix marks those command lines as "recursive" so
that they will be executed despite use of the <SAMP>`-t'</SAMP> flag.
See section <A HREF="make_5.html#SEC50">Recursive Use of <CODE>make</CODE></A>.
</P>
<P><HR><P>
<p>Go to the <A HREF="make_1.html">first</A>, <A HREF="make_6.html">previous</A>, <A HREF="make_8.html">next</A>, <A HREF="make_19.html">last</A> section, <A HREF="make_toc.html">table of contents</A>.
</BODY>
</HTML>