<!--  vim: set sw=2 sts=2 et ft=docbk:

  Part of the A-A-P recipe executive: Variants

  Copyright (C) 2002-2003 Stichting NLnet Labs
  Permission to copy and use this file is specified in the file COPYING.
  If this file is missing you can find it here: http://www.a-a-p.org/COPYING

-->

<para>
You might first want to read the <link linkend='tutor-variant'>tutorial</link>
for a few examples of using variants.
</para>

<para>
Here is an example how build variants can be specified.  This will be used to
explain how it works.
</para>

<programlisting>
     :variant Opt
         some
             OPTIMIZE = 2
         much
             OPTIMIZE = 6        
         *
             OPTIMIZE = 1
</programlisting>

<para>
"Opt" is the name of a variable.  It is used to select one of the variants.
Each possible value is listed in the following line and further lines with the
same indent.  In the example these are "some" and "much".  "*" is used to
accept any value, it must be the last one.  The first value mentioned is the
default when the variable isn't set.  
</para>

<para>
You can now start &Aap; with various arguments to specify the kind of
optimizing you want to use:
</para>

<literallayout>        <userinput>aap Opt=some</userinput>      will set <literal>OPTIMIZE</literal> to 2
        <userinput>aap Opt=much</userinput>      will set <literal>OPTIMIZE</literal> to 6
        <userinput>aap Opt=other</userinput>    will set <literal>OPTIMIZE</literal> to 1
        <userinput>aap</userinput>                      will set <literal>OPTIMIZE</literal> to 2
</literallayout>

<para>
Note that when "Opt" is not given a value the first entry is used, resulting
in <literal>OPTIMIZE</literal> being set to 2.  But when it is set to a value that
isn't mentioned the last entry "*" is used.
</para>


<bridgehead>The <literal>BDIR</literal> Variable</bridgehead>

<para>
The <literal>$BDIR</literal> variable will be adjusted for the variant used. CAREFUL:
this means that using <literal>$BDIR</literal> before
<link linkend="cmd-variant">:variant</link> commands will use a
different value, that might not always be what you want.
</para>

<para>
  Inside the
  <link linkend="cmd-variant">:variant</link> command the value of <literal>$BDIR</literal> has already been
adjusted.
</para>

<para>
When a target that is being build starts with <literal>$BDIR</literal> and
<literal>$BDIR</literal> doesn't exist, it is created. (Actually, this happens when
an item in the path is "build" or starts with "build-".
</para>

<para>
<literal>$BDIR</literal> is relative to the recipe.  When using ":child dir/main.aap"
the child recipe will use a different build directory <literal>dir/$BDIR</literal>.
Note that when building the same source file twice from recipes that are in
different directories, you will get two results.  Best is to always build a
target from the same recipe (that makes it easier to understand the
recipe anyway).
</para>


<bridgehead>Compile only when needed</bridgehead>

<para>
This continues the last example of the
<link linkend='tutor-variant'>tutorial</link>.
</para>

<para>
We happen to know that the main.c file does not depend on the GUI used.  With
the recipe above it will nevertheless be compiled again for every GUI version.
Although this is a small thing in this example, in a bigger project it becomes
more important to skip compilation when it is not needed.  Here is the
modified recipe:
</para>

<programlisting>
1    Source = main.c version.c gui.c
2
3    :variant Build
4        release
5            OPTIMIZE = 4
6            Target = myprog
7        debug
8            DEBUG = yes
9            Target = myprogd
10
11   :attr {var_DEFINE = $DEFINE} {var_BDIR = $BDIR} main.c
12    
13   Gui ?= motif
14   :variant Gui
15       console
16       motif
17            Source += motif.c
18       gtk
19            Source += gtk.c
20
21   DEFINE += -DGUI=$Gui
22
23   :program $Target : $Source
</programlisting>

<para>
The only new line is line 11.  The "main.c" file is given two extra
attributes: <literal>var_DEFINE</literal> and <literal>var_BDIR</literal>.  What happens is
that when "main.c" is being build, &Aap; will check for attributes of this
source file that start with "var_".  The values will be used to set variables
with the following name to the value of the attribute.  Thus
<literal>DEFINE</literal> gets the value of <literal>var_DEFINE</literal>.  This means that
the variable is overruled by the attribute while building "main.c".
</para>

<para>
The <literal>var_BDIR</literal> attribute is set to "$BDIR" before the second
<computeroutput>:variant</computeroutput> command.  It does not yet have the selected GUI
appended there.  The list of directories used is now:

<informaltable frame="none">
  <tgroup cols="2">
    <colspec colwidth="250"/>
    <thead>
      <row><entry>directory</entry> <entry>contains files</entry></row>
    </thead>
    <tbody>
      <row><entry>build-SYS-release</entry>         <entry>main</entry></row>
      <row><entry>build-SYS-debug</entry>           <entry>main</entry></row>
      <row><entry>build-SYS-release-console</entry> <entry>version, gui</entry></row>
      <row><entry>build-SYS-debug-console</entry>   <entry>version, gui</entry></row>
      <row><entry>build-SYS-release-motif</entry>   <entry>version, gui, motif</entry></row>
      <row><entry>build-SYS-debug-motif</entry>	    <entry>version, gui, motif</entry></row>
      <row><entry>build-SYS-release-gtk</entry>	    <entry>version, gui, gtk</entry></row>
      <row><entry>build-SYS-debug-gtk</entry>	    <entry>version, gui, gtk</entry></row>
    </tbody>
  </tgroup>
</informaltable>
</para>


<bridgehead>Building multiple variants at once</bridgehead>

<para>
If you want to build all the variants that are possible, use a few lines of
Python code.  Here is an example:
</para>
<programlisting>
 1   :variant license
 2       trial
 3           DEFINE += -DTRIAL
 4       demo
 5           DEFINE += -DDEMO
 6       full
 7           DEFINE += -DFULL
 8   
 9   :variant language
10       chinese
11           DEFINE += -DCHINESE
12       bulgarian
13           DEFINE += -DBULGARIAN
14       *
15           DEFINE += -DENGLISH
16    
17   build:
18       :print Building with $license license for language $language.
19       :print DEFINE=$DEFINE
10    
21   all:
22       @for a in ['trial', 'demo', 'full']:                    #license
23       @   for c in ['chinese', 'bulgarian', 'english']:       #language
24               :execute main.aap build license=$a language=$c
</programlisting>

<para>
Invoking &Aap; without arguments builds the "all" target, which loops over
all possible variants and invokes the
<link linkend="cmd-execute">:execute</link> command with the "build"
target.  The reason to use the "build" target is that without it the "all"
target would be built again and result in an endless loop.
</para>

<para>
This is the resulting output:
<literallayout>     Building with trial license for language chinese.
     DEFINE=-DTRIAL -DCHINESE
     Building with trial license for language bulgarian.
     DEFINE=-DTRIAL -DBULGARIAN
     Building with trial license for language english.
     DEFINE=-DTRIAL -DENGLISH
     Building with demo license for language chinese.
     DEFINE=-DDEMO -DCHINESE
     Building with demo license for language bulgarian.
     DEFINE=-DDEMO -DBULGARIAN
     Building with demo license for language english.
     DEFINE=-DDEMO -DENGLISH
     Building with full license for language chinese.
     DEFINE=-DFULL -DCHINESE
     Building with full license for language bulgarian.
     DEFINE=-DFULL -DBULGARIAN
     Building with full license for language english.
     DEFINE=-DFULL -DENGLISH
</literallayout>
</para>
