diff options
Diffstat (limited to 'devdocs/gnu_make/conditional-syntax.html')
| -rw-r--r-- | devdocs/gnu_make/conditional-syntax.html | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/devdocs/gnu_make/conditional-syntax.html b/devdocs/gnu_make/conditional-syntax.html new file mode 100644 index 00000000..a801cb3b --- /dev/null +++ b/devdocs/gnu_make/conditional-syntax.html @@ -0,0 +1,52 @@ + <h1 class="section">Syntax of Conditionals</h1> <p>The syntax of a simple conditional with no <code>else</code> is as follows: </p> <div class="example"> <pre class="example"><var>conditional-directive</var> +<var>text-if-true</var> +endif +</pre> +</div> <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> <div class="example"> <pre class="example"><var>conditional-directive</var> +<var>text-if-true</var> +else +<var>text-if-false</var> +endif +</pre> +</div> <p>or: </p> <div class="example"> <pre class="example"><var>conditional-directive-one</var> +<var>text-if-one-is-true</var> +else <var>conditional-directive-two</var> +<var>text-if-two-is-true</var> +else +<var>text-if-one-and-two-are-false</var> +endif +</pre> +</div> <p>There can be as many “<code>else</code> <var>conditional-directive</var>” clauses as necessary. Once a given condition is true, <var>text-if-true</var> is used and no other clause is used; if no condition is true then <var>text-if-false</var> is used. The <var>text-if-true</var> and <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; after an <code>else</code> or not. There are four different directives that test different conditions. Here is a table of them: </p> <dl compact> <dt id="ifeq (arg1, arg2)"><code>ifeq (<var>arg1</var>, <var>arg2</var>)</code></dt> <dt id="ifeq 'arg1' 'arg2'"><code>ifeq '<var>arg1</var>' '<var>arg2</var>'</code></dt> <dt id='ifeq "arg1" "arg2"'><code>ifeq "<var>arg1</var>" "<var>arg2</var>"</code></dt> <dt id="ifeq "arg1" 'arg2'"><code>ifeq "<var>arg1</var>" '<var>arg2</var>'</code></dt> <dt id="ifeq 'arg1' "arg2""><code>ifeq '<var>arg1</var>' "<var>arg2</var>"</code></dt> <dd> +<p>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. </p> <p>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 <a href="text-functions">Text Functions</a>) to avoid interpreting whitespace as a non-empty value. For example: </p> <div class="example"> <pre class="example">ifeq ($(strip $(foo)),) +<var>text-if-empty</var> +endif +</pre> +</div> <p>will evaluate <var>text-if-empty</var> even if the expansion of <code>$(foo)</code> contains whitespace characters. </p> </dd> <dt id="ifneq (arg1, arg2)"><code>ifneq (<var>arg1</var>, <var>arg2</var>)</code></dt> <dt id="ifneq 'arg1' 'arg2'"><code>ifneq '<var>arg1</var>' '<var>arg2</var>'</code></dt> <dt id='ifneq "arg1" "arg2"'><code>ifneq "<var>arg1</var>" "<var>arg2</var>"</code></dt> <dt id="ifneq "arg1" 'arg2'"><code>ifneq "<var>arg1</var>" '<var>arg2</var>'</code></dt> <dt id="ifneq 'arg1' "arg2""><code>ifneq '<var>arg1</var>' "<var>arg2</var>"</code></dt> <dd> +<p>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. </p> </dd> <dt id="ifdef variable-name"><code>ifdef <var>variable-name</var></code></dt> <dd> +<p>The <code>ifdef</code> form takes the <em>name</em> of a variable as its argument, not a reference to a variable. If the value of that variable 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. The text <var>variable-name</var> is expanded, so it could be a variable or function that expands to the name of a variable. For example: </p> <div class="example"> <pre class="example">bar = true +foo = bar +ifdef $(foo) +frobozz = yes +endif +</pre> +</div> <p>The variable reference <code>$(foo)</code> is expanded, yielding <code>bar</code>, which is considered to be the name of a variable. The variable <code>bar</code> is not expanded, but its value is examined to determine if it is non-empty. </p> <p>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, </p> <div class="example"> <pre class="example">bar = +foo = $(bar) +ifdef foo +frobozz = yes +else +frobozz = no +endif +</pre> +</div> <p>sets ‘<samp>frobozz</samp>’ to ‘<samp>yes</samp>’, while: </p> <div class="example"> <pre class="example">foo = +ifdef foo +frobozz = yes +else +frobozz = no +endif +</pre> +</div> <p>sets ‘<samp>frobozz</samp>’ to ‘<samp>no</samp>’. </p> </dd> <dt id="ifndef variable-name"><code>ifndef <var>variable-name</var></code></dt> <dd><p>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. The rules for expansion and testing of <var>variable-name</var> are identical to the <code>ifdef</code> directive. </p></dd> </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 part of a recipe 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 recipes are run (see <a href="automatic-variables">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><div class="_attribution"> + <p class="_attribution-p"> + Copyright © 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022 Free Software Foundation, Inc. <br>Licensed under the GNU Free Documentation License.<br> + <a href="https://www.gnu.org/software/make/manual/html_node/Conditional-Syntax.html" class="_attribution-link">https://www.gnu.org/software/make/manual/html_node/Conditional-Syntax.html</a> + </p> +</div> |
