Imported Upstream version 1.49.0
[platform/upstream/boost.git] / tools / build / v2 / doc / src / extending.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE appendix PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN"
3   "http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
4
5   <chapter id="bbv2.extender">
6     <title>Extender Manual</title>
7
8     <section id="bbv2.extender.intro">
9       <title>Introduction</title>
10
11       <para>
12         This section explains how to extend Boost.Build to accomodate your
13         local requirements&mdash;primarily to add support for non-standard
14         tools you have. Before we start, be sure you have read and understoon
15         the concept of metatarget, <xref linkend="bbv2.overview.concepts"/>,
16         which is critical to understanding the remaining material.
17       </para>
18
19       <para>
20         The current version of Boost.Build has three levels of targets, listed
21         below.
22       </para>
23
24       <variablelist>
25         
26         <varlistentry>
27           <term>metatarget</term>
28           <listitem>
29             <para>
30               Object that is created from declarations in Jamfiles. May
31               be called with a set of properties to produce concrete
32               targets.
33             </para>
34           </listitem>
35         </varlistentry>
36         
37         <varlistentry>
38           <term>concrete target</term>
39           <listitem>
40             <para>
41               Object that corresponds to a file or an action.
42             </para>
43           </listitem>
44         </varlistentry>
45         
46         <varlistentry>
47           <term>jam target</term>
48           <listitem>
49             <para>
50               Low-level concrete target that is specific to Boost.Jam build
51               engine. Essentially a string&mdash;most often a name of file.                
52             </para>
53           </listitem>
54         </varlistentry>
55         
56       </variablelist>
57
58       <para>
59         In most cases, you will only have to deal with concrete targets and
60         the process that creates concrete targets from
61         metatargets. Extending metatarget level is rarely required. The jam
62         targets are typically only used inside the command line patterns.
63       </para>
64
65       <warning>
66         <para>All of the Boost.Jam target-related builtin functions, like
67         <code>DEPENDS</code> or <code>ALWAYS</code> operate on jam
68         targets. Applying them to metatargets or concrete targets has no
69         effect.</para>
70       </warning>
71             
72       <section id="bbv2.extender.overview.metatargets">
73         <title>Metatargets</title>
74         
75         <para>Metatarget is an object that records information specified
76         in Jamfile, such as metatarget kind, name, sources and properties,
77         and can be called with specific properties to generate concrete
78         targets. At the code level it is represented by an instance of
79         class derived from <classname>abstract-target</classname>. 
80         <footnote><para>This name is historic, and will be eventuall changed to 
81         <code>metatarget</code></para></footnote>
82         </para>
83         
84         <para>The <methodname>generate</methodname> method takes the build properties
85         (as an instance of the <classname>property-set</classname> class) and returns
86         a list containing:</para>
87         <itemizedlist>
88           <listitem><para>As front element&mdash;Usage-requirements from this invocation
89           (an instance of <classname>property-set</classname>)</para></listitem>
90           <listitem><para>As subsequent elements&mdash;created concrete targets (
91           instances of the <classname>virtual-target</classname> class.)</para></listitem>
92         </itemizedlist>
93         
94         <para>It's possible to lookup a metataget by target-id using the 
95         <code>targets.resolve-reference</code> function, and the
96         <code>targets.generate-from-reference</code> function can both
97         lookup and generate a metatarget.</para>
98         
99         <para>The <classname>abstract-target</classname> class has three immediate
100         derived classes:</para>
101         <itemizedlist>
102           
103           <listitem><para><classname>project-target</classname> that
104           corresponds to a project and is not intended for further
105           subclassing. The <methodname>generate</methodname> method of this
106           class builds all targets in the project that are not marked as
107           explicit.</para></listitem>
108           
109           <listitem><para><classname>main-target</classname> corresponds to a target in a project
110           and contains one or more target alternatives. This class also should not be 
111           subclassed. The <methodname>generate</methodname> method of this class selects 
112           an alternative to build, and calls the <methodname>generate</methodname> method of that
113           alternative.</para></listitem>
114           
115           <listitem><para><classname>basic-target</classname> corresponds to a
116           specific target alternative. This is base class, with a number of
117           derived classes.  The <methodname>generate</methodname> method
118           processes the target requirements and requested build properties to
119           determine final properties for the target, builds all sources, and
120           finally calls the abstract <classname>construct</classname> method with the list
121           of source virtual targets, and the final properties.
122           </para></listitem>
123           
124         </itemizedlist>
125         
126         <para>The instances of the <classname>project-target</classname> and
127         <classname>main-target</classname> classes are created
128         implicitly&mdash;when loading a new Jamfiles, or when a new target
129         alternative with as-yet unknown name is created.  The instances of the
130         classes derived from <classname>basic-target</classname> are typically
131         created when Jamfile calls a <firstterm>metatarget rule</firstterm>,
132         such as such as <code>exe</code>.
133         </para>
134         
135         <para>It it permissible to create a custom class derived from
136         <classname>basic-target</classname> and create new metatarget rule
137         that creates instance of such target. However, in the majority
138         of cases, a specific subclass of <classname>basic-target</classname>&mdash;
139         <classname>typed-target</classname> is used. That class is associated
140         with a <firstterm>type</firstterm> and relays to <firstterm>generators</firstterm>
141         to construct concrete targets of that type. This process will be explained below.
142         When a new type is declared, a new metatarget rule is automatically defined.
143         That rule creates new instance of type-target, associated with that type.                
144         </para>
145         
146       </section>
147
148       <section id="bbv2.extender.overview.targets">
149         <title>Concrete targets</title>
150
151         <para>Concrete targets are represented by instance of classes derived
152         from <classname>virtual-target</classname>. The most commonly used
153         subclass is <classname>file-target</classname>. A file target is associated
154         with an action that creates it&mdash; an instance of the <classname>action</classname>
155         class. The action, in turn, hold a list of source targets. It also holds the 
156         <classname>property-set</classname> instance with the build properties that
157         should be used for the action.</para>
158
159         <para>Here's an example of creating a target from another target, <code>source</code></para>
160 <programlisting>
161 local a = [ new action $(source) : common.copy : $(property-set) ] ;
162 local t = [ new file-target $(name) : CPP : $(project) : $(a) ] ;
163 </programlisting>
164         <para>The first line creates an instance of the <classname>action></classname> class.
165         The first parameter is the list of sources. The second parameter is the name
166         a jam-level <link linkend="bbv2.overview.jam_language.actions">action</link>.
167         The third parameter is the property-set applying to this action. The second line
168         creates a target. We specifie a name, a type and a project. We also pass the
169         action object created earlier.  If the action creates several targets, we can repeat
170         the second line several times.</para>
171
172         <para>In some cases, code that creates concrete targets may be invoked more than
173         once with the same properties. Returning to different instance of <classname>file-target</classname>
174         that correspond to the same file clearly will result in problems. Therefore, whenever
175         returning targets you should pass them via the <code>virtual-target.register</code>
176         function, that will replace targets with previously created identical ones, as
177         necessary.<footnote><para>This create-then-register pattern is caused by limitations 
178         of the Boost.Jam language. Python port is likely to never create duplicate targets.</para></footnote>
179         Here are a couple of examples:
180 <programlisting>
181 return [ virtual-target.register $(t) ] ;
182 return [ sequence.transform virtual-target.register : $(targets) ] ;
183 </programlisting>
184         </para>
185
186       </section>
187       
188       <section id="bbv2.extender.overview.generators">
189         <title>Generators</title>
190
191         <para>In theory, every kind of metatarget in Boost.Build (like <code>exe</code>, 
192         <code>lib</code> or <code>obj</code>) could be implemented
193         by writing a new metatarget class that, independently of the other code, figures
194         what files to produce and what commands to use. However, that would be rather inflexible.
195         For example, adding support for a new compiler would require editing several metatargets.
196         </para>
197
198         <para>In practice, most files have specific types, and most tools
199         consume and produce files of specific type. To take advantage of this
200         fact, Boost.Build defines concept of target type and
201         <indexterm><primary>generators</primary></indexterm>
202         <firstterm>generators</firstterm>, and has special metatarget class
203         <classname>typed-target</classname>.  Target type is merely an
204         identifier. It is associated with a set of file extensions that
205         correspond to that type. Generator is an abstraction of a tool. It advertises
206         the types it produces and, if called with a set of input target, tries to construct
207         output targets of the advertised types. Finally, <classname>typed-target</classname>
208         is associated with specific target type, and relays the generator (or generators)
209         for that type.
210         </para>
211
212         <para>A generator is an instance of a class derived from <classname>generator</classname>.
213         The <classname>generator</classname> class itself is suitable for common cases.
214         You can define derived classes for custom scenarios.</para>
215
216         <!--
217         <para>Given a set of generators, the fundamental operation is to
218         construct a target of a given type, with given properties, from a
219         set of targets. That operation is performed by rule
220         <literal>generators.construct</literal> and the used algorithm is described
221         below.</para>
222
223         <section>
224           <title>Selecting and ranking viable generators</title>
225
226           <para>Each generator, in addition to target types that it can
227           produce, have attribute that affects its applicability in
228           particular sitiation. Those attributes are:</para>
229
230           <orderedlist>
231             <listitem>
232               <simpara>
233                 Required properties, which are properties absolutely
234                 necessary for the generator to work. For example, generator
235                 encapsulating the gcc compiler would have &lt;toolset&gt;gcc as
236                 required property.
237               </simpara>
238             </listitem>
239
240             <listitem>
241               <simpara>
242                 Optional properties, which increase the generators
243                 suitability for a particual build.
244               </simpara>
245             </listitem>
246           </orderedlist>
247
248           <para>
249             Generator's required and optional properties may not include
250             either free or incidental properties. (Allowing this would
251             greatly complicate caching targets).
252           </para>
253
254           <para>When trying to construct a target, the first step is to select
255           all possible generators for the requested target type, which
256           required properties are a subset of requested properties.
257           Generators that were already selected up the call stack are
258           excluded. In addition, if any composing generators were selected
259           up the call stack, all other composing generators are ignored
260           (TODO: define composing generators). The found generators
261           are assigned a rank, which is the number of optional properties
262           present in requested properties. Finally, generators with highest
263           rank are selected for futher processing.</para>
264
265         </section>
266         <section>
267           <title>Running generators</title>
268
269           <para>When generators are selected, each is run to produce a list of
270           created targets. This list might include targets that are not of
271           requested types, because generators create the same targets as
272           some tool, and tool's behaviour is fixed. (Note: should specify
273           that in some cases we actually want extra targets). If generator
274           fails, it returns an empty list. Generator is free to call
275           'construct' again, to convert sources to the types it can handle.
276           It also can pass modified properties to 'construct'. However, a
277           generator is not allowed to modify any propagated properties,
278           otherwise when actually consuming properties we might discover
279           that the set of propagated properties is different from what was
280           used for building sources.</para>
281
282           <para>For all targets that are not of requested types, we try to
283           convert them to requested type, using a second call to
284           <literal>construct</literal>. This is done in order to support
285           transformation sequences where single source file expands to
286           several later. See <ulink url=
287           "http://groups.yahoo.com/group/jamboost/message/1667">this
288           message</ulink> for details.</para>
289
290         </section>
291
292         -->
293
294         <!-- FIXME: review the below content. Maybe, some of it is
295              still useful.
296         <section>
297           <title>Property adjustment</title>
298
299           <para>Because target location is determined by the build system, it
300           is sometimes necessary to adjust properties, in order to not
301           break actions. For example, if there's an action that generates
302           a header, say "a_parser.h", and a source file "a.cpp" which
303           includes that file, we must make everything work as if a_parser.h
304           is generated in the same directory where it would be generated
305           without any subvariants.</para>
306
307           <para>Correct property adjustment can be done only after all targets
308           are created, so the approach taken is:</para>
309
310           <orderedlist>
311             <listitem>
312               <para>
313                 When dependency graph is constructed, each action can be
314                 assigned a rule for property adjustment.
315               </para>
316             </listitem>
317
318             <listitem>
319               <para>
320                 When virtual target is actualized, that rule is run and
321                 return the final set of properties. At this stage it can use
322                 information of all created virtual targets.
323               </para>
324             </listitem>
325           </orderedlist>
326
327           <para>In case of quoted includes, no adjustment can give 100% correct
328           results. If target dirs are not changed by build system, quoted
329           includes are searched in "." and then in include path, while angle
330           includes are searched only in include path. When target dirs are
331           changed, we'd want to make quoted includes to be search in "." then in
332           additional dirs and then in the include path and make angle includes
333           be searched in include path, probably with additional paths added at
334           some position. Unless, include path already has "." as the first
335           element, this is not possible. So, either generated headers should not
336           be included with quotes, or first element of include path should be
337           ".", which essentially erases the difference between quoted and angle
338           includes. <emphasis role="bold">Note:</emphasis> the only way to get
339           "." as include path into compiler command line is via verbatim
340           compiler option. In all other case, Boost.Build will convert "." into
341           directory where it occurs.</para> 
342
343         </section>
344
345         -->
346
347       </section>
348       
349     </section>
350
351     <section id="bbv2.extender.example">
352       <title>Example: 1-to-1 generator</title>
353
354       <para>Say you're writing an application that generates C++ code. If
355       you ever did this, you know that it's not nice. Embedding large
356       portions of C++ code in string literals is very awkward. A much
357       better solution is:</para>
358
359       <orderedlist>
360         <listitem>
361           <simpara>
362             Write the template of the code to be generated, leaving
363             placeholders at the points that will change
364           </simpara>
365         </listitem>
366         
367         <listitem>
368           <simpara>
369             Access the template in your application and replace
370             placeholders with appropriate text.
371           </simpara>
372         </listitem>
373         
374         <listitem>
375           <simpara>Write the result.</simpara>
376         </listitem>
377       </orderedlist>
378       
379       <para>It's quite easy to achieve. You write special verbatim files that are
380       just C++, except that the very first line of the file contains the name of a
381       variable that should be generated. A simple tool is created that takes a
382       verbatim file and creates a cpp file with a single <code>char*</code> variable
383       whose name is taken from the first line of the verbatim file and whose value
384       is the file's properly quoted content.</para>
385
386       <para>Let's see what Boost.Build can do.</para>
387       
388       <para>First off, Boost.Build has no idea about "verbatim files". So, you must
389       register a new target type. The following code does it:</para>
390       
391 <programlisting>
392 import type ;
393 type.register VERBATIM : verbatim ;
394 </programlisting>
395
396       <para>The first parameter to <functionname>type.register</functionname> gives
397       the name of the declared type. By convention, it's uppercase. The second
398       parameter is the suffix for files of this type. So, if Boost.Build sees
399       <filename>code.verbatim</filename> in a list of sources, it knows that it's of
400       type <code>VERBATIM</code>.</para>
401
402       <para>Next, you tell Boost.Build that the verbatim files can be
403       transformed into C++ files in one build step.  A
404       <firstterm>generator</firstterm> is a template for a build step that
405       transforms targets of one type (or set of types) into another.  Our
406       generator will be called <code>verbatim.inline-file</code>; it
407       transforms <code>VERBATIM</code> files into <code>CPP</code> files:
408
409 <programlisting>
410 import generators ;
411 generators.register-standard verbatim.inline-file : VERBATIM : CPP ;
412 </programlisting>
413   </para>
414
415        <para>Lastly, you have to inform Boost.Build about the shell
416        commands used to make that transformation.  That's done with an
417        <code>actions</code> declaration.
418
419 <programlisting>
420 actions inline-file
421 {
422     "./inline-file.py" $(&lt;) $(&gt;)
423 }
424 </programlisting>
425
426 <!-- You need to explain all the parameters to an "actions" and
427      describe the accompanying rule declaration: the user has no clue
428      what $(<) and $(>) are, and doesn't know about the third
429      parameter that gets passed to the rule. -->
430
431 <!-- We use verbatim.inline-file in one place and just inline-file in
432              another. Is this confusing for user?
433         -->
434 </para>
435
436   <para>
437     Now, we're ready to tie it all together. Put all the code above in file
438     <filename>verbatim.jam</filename>, add <code>import verbatim ;</code> to
439     <filename>Jamroot.jam</filename>, and it's possible to write the following
440     in your Jamfile:
441   </para>
442
443 <programlisting>
444 exe codegen : codegen.cpp class_template.verbatim usage.verbatim ;
445 </programlisting>
446
447   <para>
448     The listed verbatim files will be automatically converted into C++ source
449     files, compiled and then linked to the codegen executable.
450   </para>
451
452   <para>
453     In subsequent sections, we will extend this example, and review all the
454     mechanisms in detail. The complete code is available in the
455     <filename>example/customization</filename> directory.
456   </para>
457   </section>
458
459   <section id="bbv2.extending.targets">
460     <title>Target types</title>
461       <para>The first thing we did in the <link
462           linkend="bbv2.extender.intro">intruduction</link> was declaring a
463       new target type:
464 <programlisting>
465 import type ;
466 type.register VERBATIM : verbatim ;
467 </programlisting>
468         The type is the most important property of a target. Boost.Build can
469         automatically generate necessary build actions only because you
470         specify the desired type (using the different main target rules), and
471         because Boost.Build can guess the type of sources from their
472         extensions.
473       </para>
474
475       <para>The first two parameters for the <code>type.register</code> rule
476         are the name of new type and the list of extensions associated with
477         it. A file with an extension from the list will have the given target
478         type. In the case where a target of the declared type is generated
479         from other sources, the first specified extension will be used.
480       </para>
481
482     <para>Sometimes you want to change the suffix used for generated targets
483       depending on build properties, such as toolset. For example, some compiler
484       uses extension <literal>elf</literal> for executable files. You can use the
485       <code>type.set-generated-target-suffix</code> rule:
486 <programlisting>
487 type.set-generated-target-suffix EXE : &lt;toolset&gt;elf : elf ;
488 </programlisting>
489     </para>
490
491     <para>A new target type can be inherited from an existing one.
492 <programlisting>
493 type.register PLUGIN : : SHARED_LIB ;
494 </programlisting>
495       The above code defines a new type derived from
496       <code>SHARED_LIB</code>. Initially, the new type inherits all the
497       properties of the base type - in particular generators and suffix.
498       Typically, you'll change the new type in some way. For example, using
499       <code>type.set-generated-target-suffix</code> you can set the suffix for
500       the new type. Or you can write special a generator for the new type. For
501       example, it can generate additional metainformation for the plugin.
502       In either way, the <code>PLUGIN</code> type can be used whenever
503       <code>SHARED_LIB</code> can. For example, you can directly link plugins
504       to an application.
505     </para>
506
507     <para>A type can be defined as "main", in which case Boost.Build will
508       automatically declare a main target rule for building targets of that
509       type. More details can be found <link
510       linkend="bbv2.extending.rules.main-type">later</link>.
511     </para>
512
513       <section id="bbv2.extending.scanners">
514         <title>Scanners</title>
515         <para>
516           Sometimes, a file can refer to other files via some include system. To
517           make Boost.Build track dependencies between included files, you need
518           to provide a scanner. The primary limitation is that only one scanner
519           can be assigned to a target type.
520         </para>
521
522         <para>First, we need to declare a new class for the scanner:
523 <programlisting>
524 class verbatim-scanner : common-scanner
525 {
526     rule pattern ( )
527     {
528         return "//###include[ ]*\"([^\"]*)\"" ;
529     }
530 }
531 </programlisting>
532           All the complex logic is in the <code>common-scanner</code>
533           class, and you only need to override the method that returns
534           the regular expression to be used for scanning. The
535           parentheses in the regular expression indicate which part
536           of the string is the name of the included file.  Only the
537           first parenthesized group in the regular expression will be
538           recognized; if you can't express everything you want that
539           way, you can return multiple regular expressions, each of
540           which contains a parenthesized group to be matched.
541         </para>
542
543         <para>After that, we need to register our scanner class:
544 <programlisting>
545 scanner.register verbatim-scanner : include ;
546 </programlisting>
547             The value of the second parameter, in this case
548             <code>include</code>, specifies the properties that contain the list
549             of paths that should be searched for the included files.
550          </para>
551
552         <para>Finally, we assign the new scanner to the <code>VERBATIM</code>
553         target type:
554 <programlisting>
555 type.set-scanner VERBATIM : verbatim-scanner ;
556 </programlisting>
557           That's enough for scanning include dependencies.
558         </para>
559
560       </section>
561
562   </section>
563
564   <section id="bbv2.extending.tools">
565     <title>Tools and generators</title>
566       <para>
567         This section will describe how Boost.Build can be extended to support
568         new tools.
569       </para>
570
571       <para>For each additional tool, a Boost.Build object called generator
572         must be created. That object has specific types of targets that it
573         accepts and produces. Using that information, Boost.Build is able
574         to automatically invoke the generator. For example, if you declare a
575         generator that takes a target of the type <literal>D</literal> and
576         produces a target of the type <literal>OBJ</literal>, when placing a
577         file with extention <literal>.d</literal> in a list of sources will
578         cause Boost.Build to invoke your generator, and then to link the
579         resulting object file into an application. (Of course, this requires
580         that you specify that the <literal>.d</literal> extension corresponds
581         to the <literal>D</literal> type.)
582       </para>
583
584       <para>Each generator should be an instance of a class derived from the
585         <code>generator</code> class. In the simplest case, you don't need to
586         create a derived class, but simply create an instance of the
587         <code>generator</code> class. Let's review the example we've seen in the
588         <link linkend="bbv2.extender.intro">introduction</link>.
589         <!-- Is the following supposed to be verbatim.jam?  Tell the
590              user so.  You also need to describe the meanings of $(<)
591              and $(>); this is the first time they're encountered. -->
592 <programlisting>
593 import generators ;
594 generators.register-standard verbatim.inline-file : VERBATIM : CPP ;
595 actions inline-file
596 {
597     "./inline-file.py" $(&lt;) $(&gt;)
598 }
599 </programlisting>
600       </para>
601
602       <para>We declare a standard generator, specifying its id, the source type
603         and the target type. When invoked, the generator will create a target
604         of type <literal>CPP</literal> with a source target of
605         type <literal>VERBATIM</literal> as the only source. But what command
606         will be used to actually generate the file? In bjam, actions are
607         specified using named "actions" blocks and the name of the action
608         block should be specified when creating targets. By convention,
609         generators use the same name of the action block as their own id. So,
610         in above example, the "inline-file" actions block will be used to
611         convert the source into the target.
612       </para>
613
614       <para>
615         There are two primary kinds of generators: standard and composing,
616         which are registered with the
617         <code>generators.register-standard</code> and the
618         <code>generators.register-composing</code> rules, respectively. For
619         example:
620 <programlisting>
621 generators.register-standard verbatim.inline-file : VERBATIM : CPP ;
622 generators.register-composing mex.mex : CPP LIB : MEX ;
623 </programlisting>
624         The first (standard) generator takes a <emphasis>single</emphasis>
625         source of type <code>VERBATIM</code> and produces a result. The second
626         (composing) generator takes any number of sources, which can have either
627         the <code>CPP</code> or the <code>LIB</code> type. Composing generators
628         are typically used for generating top-level target type. For example,
629         the first generator invoked when building an <code>exe</code> target is
630         a composing generator corresponding to the proper linker.
631       </para>
632
633       <para>You should also know about two specific functions for registering
634         generators: <code>generators.register-c-compiler</code> and
635         <code>generators.register-linker</code>. The first sets up header
636         dependecy scanning for C files, and the seconds handles various
637         complexities like searched libraries. For that reason, you should always
638         use those functions when adding support for compilers and linkers.
639       </para>
640
641       <para>(Need a note about UNIX)</para>
642       <!-- What kind of note?  Either write the note or don't, but remove this dross. -->
643       <bridgehead>Custom generator classes</bridgehead>
644
645       <para>The standard generators allows you to specify source and target
646         types, an action, and a set of flags. If you need anything more complex,
647         <!-- What sort of flags?  Command-line flags?  What does the system do with them? -->
648         you need to create a new generator class with your own logic. Then,
649         you have to create an instance of that class and register it. Here's
650         an example how you can create your own generator class:
651 <programlisting>
652 class custom-generator : generator
653 {
654     rule __init__ ( * : * )
655     {
656         generator.__init__ $(1) : $(2) : $(3) : $(4) : $(5) : $(6) : $(7) : $(8) : $(9) ;
657     }
658 <!-- What is the point of this __init__ function?? -->
659 }
660
661 generators.register
662   [ new custom-generator verbatim.inline-file : VERBATIM : CPP ] ;
663 </programlisting>
664         This generator will work exactly like the
665         <code>verbatim.inline-file</code> generator we've defined above, but
666         it's possible to customize the behaviour by overriding methods of the
667         <code>generator</code> class.
668       </para>
669
670       <para>There are two methods of interest. The <code>run</code> method is
671         responsible for the overall process - it takes a number of source targets,
672         converts them to the right types, and creates the result. The
673         <code>generated-targets</code> method is called when all sources are
674         converted to the right types to actually create the result.
675       </para>
676
677       <para>The <code>generated-targets</code> method can be overridden when you
678         want to add additional properties to the generated targets or use
679         additional sources. For a real-life example, suppose you have a program
680         analysis tool that should be given a name of executable and the list of
681         all sources. Naturally, you don't want to list all source files
682         manually. Here's how the <code>generated-targets</code> method can find
683         the list of sources automatically:
684 <programlisting>
685 class itrace-generator : generator {
686 ....
687     rule generated-targets ( sources + : property-set : project name ? )
688     {
689         local leaves ;
690         local temp = [ virtual-target.traverse $(sources[1]) : : include-sources ] ;<!-- You must explain include-sources! -->
691         for local t in $(temp)
692         {
693             if ! [ $(t).action<!-- In what namespace is this evaluated? --> ]
694             {
695                 leaves += $(t) ;
696             }
697         }
698         return [ generator.generated-targets $(sources) $(leafs)
699           : $(property-set) : $(project) $(name) ] ;
700     }
701 }
702 generators.register [ new itrace-generator nm.itrace : EXE : ITRACE ] ;
703 </programlisting>
704         The <code>generated-targets</code> method will be called with a single
705         source target of type <literal>EXE</literal>. The call to
706         <code>virtual-target.traverse</code> will return all targets the
707         executable depends on, and we further find files that are not
708         produced from anything. <!-- What does "not produced from anything" mean? -->
709         The found targets are added to the sources.
710       </para>
711
712       <para>The <code>run</code> method can be overriden to completely
713         customize the way the generator works. In particular, the conversion of
714         sources to the desired types can be completely customized. Here's
715         another real example. Tests for the Boost Python library usually
716         consist of two parts: a Python program and a C++ file. The C++ file is
717         compiled to Python extension that is loaded by the Python
718         program. But in the likely case that both files have the same name,
719         the created Python extension must be renamed. Otherwise, the Python
720         program will import itself, not the extension. Here's how it can be
721         done:
722 <programlisting>
723 rule run ( project name ? : property-set : sources * )
724 {
725     local python ;
726     for local s in $(sources)
727     {
728         if [ $(s).type ] = PY
729         {
730             python = $(s) ;
731         }
732     }
733     <!-- This is horrible code.  Use a filter function, or at _least_ consolidate the two loops! -->
734     local libs ;
735     for local s in $(sources)
736     {
737         if [ type.is-derived [ $(s).type ] LIB ]
738         {
739             libs += $(s) ;
740         }
741     }
742
743     local new-sources ;
744     for local s in $(sources)
745     {
746         if [ type.is-derived [ $(s).type ] CPP ]
747         {
748             local name = [ $(s).name ] ;    # get the target's basename
749             if $(name) = [ $(python).name ]
750             {
751                 name = $(name)_ext ;        # rename the target
752             }
753             new-sources += [ generators.construct $(project) $(name) :
754               PYTHON_EXTENSION : $(property-set) : $(s) $(libs) ] ;
755         }
756     }
757
758     result = [ construct-result $(python) $(new-sources) : $(project) $(name)
759                  : $(property-set) ] ;
760 }
761 </programlisting>
762         <!-- Why are we doing this with a generator??? It seems
763              insane.  We could just use a nice front-end rule that
764              calls some normal target-creation rules. No? -->
765
766         First, we separate all source into python files, libraries and C++
767         sources. For each C++ source we create a separate Python extension by
768         calling <code>generators.construct</code> and passing the C++ source
769         and the libraries. At this point, we also change the extension's name,
770         if necessary.
771       </para>
772
773
774     </section>
775
776     <section id="bbv2.extending.features">
777       <title>Features</title>
778       <para>
779         Often, we need to control the options passed the invoked tools. This
780         is done with features. Consider an example:
781 <programlisting>
782 # Declare a new free feature
783 import feature : feature ;
784 feature verbatim-options : : free ;
785
786 # Cause the value of the 'verbatim-options' feature to be
787 # available as 'OPTIONS' variable inside verbatim.inline-file
788 import toolset : flags ;
789 flags verbatim.inline-file OPTIONS &lt;verbatim-options&gt; ;<!-- You must tell the reader what the syntax of the flags rule is -->
790
791 # Use the "OPTIONS" variable
792 actions inline-file
793 {
794     "./inline-file.py" $(OPTIONS) $(&lt;) $(&gt;)
795 }
796 </programlisting>
797         We first define a new feature. Then, the <code>flags</code> invocation
798         says that whenever verbatin.inline-file action is run, the value of
799         the <code>verbatim-options</code> feature will be added to the
800         <code>OPTIONS</code> variable, and can be used inside the action body.
801         You'd need to consult online help (--help) to find all the features of
802         the <code>toolset.flags</code> rule.
803         <!-- It's been a while since I wrote these notes, so I don't
804              remember what I meant.  But right here, I wrote "bad" and
805              circled it.  Maybe you can figure out what I meant. ;-)
806              -->
807       </para>
808
809     <para>
810       Although you can define any set of features and interpret their values
811       in any way, Boost.Build suggests the following coding standard for
812       designing features.
813     </para>
814
815     <para>Most features should have a fixed set of values that is portable
816       (tool neutral) across the class of tools they are designed to work
817       with. The user does not have to adjust the values for a exact tool.  For
818       example, <code>&lt;optimization&gt;speed</code> has the same meaning for
819       all C++ compilers and the user does not have to worry about the exact
820       options passed to the compiler's command line.
821     </para>
822
823     <para>
824       Besides such portable features there are special 'raw' features that
825       allow the user to pass any value to the command line parameters for a
826       particular tool, if so desired. For example, the
827       <code>&lt;cxxflags&gt;</code> feature allows you to pass any command line
828       options to a C++ compiler. The <code>&lt;include&gt;</code> feature
829       allows you to pass any string preceded by <code>-I</code> and the interpretation
830       is tool-specific. <!-- It's really tool-specific?  That surprises me --> (See <xref
831       linkend="bbv2.faq.external"/> for an example of very smart usage of that
832       feature).  Of course one should always strive to use portable
833       features, but these are still be provided as a backdoor just to make
834       sure Boost.Build does not take away any control from the user.
835     </para>
836
837     <para>
838       Using portable features is a good idea because:
839       <itemizedlist>
840         <listitem>
841           <para>When a portable feature is given a fixed set of
842           values, you can build your project with two different
843           settings of the feature and Boost.Build will automatically
844           use two different directories for generated files.
845           Boost.Build does not try to separate targets built with
846           different raw options.
847             <!-- It's a computer program.  It doesn't "care" about options -->
848           </para>
849         </listitem>
850
851         <listitem>
852           <para>Unlike with “raw” features, you don't need to use
853           specific command-line flags in your Jamfile, and it will be
854           more likely to work with other tools.
855           </para>
856         </listitem>
857       </itemizedlist>
858     </para>
859
860       <bridgehead>Steps for adding a feauture</bridgehead>
861       <!-- This section is redundant with the previous one -->
862       <para>Adding a feature requires three steps:
863
864         <orderedlist>
865           <listitem><para>Declaring a feature. For that, the "feature.feature"
866               rule is used. You have to decide on the set of <link
867               linkend="bbv2.reference.features.attributes">feature
868               attributes</link>:
869
870               <itemizedlist>
871                 <listitem><para>if you want a feature value set for one target
872                 to automaticaly propagate to its dependant targets then make it
873                 “propagated”. <!-- Examples needed. --></para></listitem>
874
875                 <listitem><para>if a feature does not have a fixed list of
876                 values, it must be “free.”  For example, the <code>include
877                 </code> feature is a free feature.</para></listitem>
878
879                 <listitem><para>if a feature is used to refer to a path relative
880                 to the Jamfile, it must be a “path” feature. Such features will
881                 also get their values automatically converted to Boost Build's
882                 internal path representation. For example, <code>include</code>
883                 is a path feature.</para></listitem>
884
885                 <listitem><para>if feature is used to refer to some target, it
886                 must be a “dependency” feature. <!-- for example? --></para>
887
888                 <!-- Any other feature attributes? -->
889                 </listitem>
890               </itemizedlist>
891               </para>
892           </listitem>
893
894           <listitem><para>Representing the feature value in a
895           target-specific variable. Build actions are command
896           templates modified by Boost.Jam variable expansions.  The
897           <code>toolset.flags</code> rule sets a target-specific
898           variable to the value of a feature.</para></listitem>
899
900                 <listitem><para>Using the variable. The variable set in step 2 can
901               be used in a build action to form command parameters or
902               files.</para></listitem>
903
904         </orderedlist>
905       </para>
906
907       <bridgehead>Another example</bridgehead>
908
909       <para>Here's another example.
910         Let's see how we can make a feature that refers to a target. For example,
911         when linking dynamic libraries on Windows, one sometimes needs to
912         specify a "DEF file", telling what functions should be exported. It
913         would be nice to use this file like this:
914 <programlisting>
915         lib a : a.cpp : &lt;def-file&gt;a.def ;
916 </programlisting>
917 <!-- Why would that be nice?  It seems to me that having a.def in the sources is the obvious and much nicer thing to do:
918
919         lib a : a.cpp a.def ;
920 -->
921         Actually, this feature is already supported, but anyway...
922         <!-- Something about saying that is very off-putting.  I'm
923              sorry that I can't put my finger on it -->
924       </para>
925
926       <orderedlist>
927         <listitem>
928           <para>Since the feature refers to a target, it must be "dependency".
929 <programlisting>
930 feature def-file : : free dependency ;
931 </programlisting>
932             </para></listitem>
933
934         <listitem><para>One of the toolsets that cares about
935         <!-- The toolset doesn't "care." What do your really mean? -->
936         DEF files is msvc. The following line should be added to it.
937         <!-- Are you saying the msvc toolset is broken (or that it
938              doesn't use DEF files) as-shipped and the reader needs to
939              fix it? -->
940
941 <programlisting>
942 flags msvc.link DEF_FILE &lt;def-file&gt; ;
943 </programlisting>
944             <!-- And that line does... what? -->
945             </para></listitem>
946
947         <listitem><para>Since the DEF_FILE variable is not used by the
948 msvc.link action,
949 <!-- It's not?  You just told us that MSVC "cares" about DEF files. I
950      presume that means that it uses them in some appropriate way? -->
951 we need to modify it to be:
952
953 <programlisting>
954 actions link bind DEF_FILE
955 {
956     $(.LD) .... /DEF:$(DEF_FILE) ....
957 }
958 </programlisting>
959             </para>
960
961
962           <para> Note the <code>bind DEF_FILE</code> part. It tells
963           bjam to translate the internal target name in
964           <varname>DEF_FILE</varname> to a corresponding filename in
965           the <code>link</code> action.  Without it the expansion of
966           <code>$(DEF_FILE)</code> would be a strange symbol that is
967           not likely to make sense for the linker.
968           </para>
969
970           <!-- I have a note here that says: "none of this works for
971                targets in general, only source files."  I'm not sure
972                what I meant by that; maybe you can figure it out. -->
973           <para>
974             We are almost done, but we should stop for a small workaround. Add the following
975             code to msvc.jam
976
977 <programlisting>
978 rule link
979 {
980     DEPENDS $(&lt;) : [ on $(&lt;) return $(DEF_FILE) ] ;
981 }
982 </programlisting>
983 <!-- You *must* explain the part in [...] above. It's completely opaque to the casual reader -->
984
985             This is needed to accomodate some bug in bjam, which hopefully
986             will be fixed one day.
987             <!-- This is *NOT* a bug!!  Anyway, BBv2 shouild handle this automatically. Why doesn't it? -->
988 </para></listitem>
989
990       </orderedlist>
991
992       <bridgehead>Variants and composite features.</bridgehead>
993
994       <para>Sometimes you want to create a shortcut for some set of
995         features. For example, <code>release</code> is a value of
996         <code>&lt;variant&gt;</code> and is a shortcut for a set of features.
997       </para>
998
999       <para>It is possible to define your own build variants. For example:
1000 <programlisting>
1001 variant crazy : &lt;optimization&gt;speed &lt;inlining&gt;off
1002                 &lt;debug-symbols&gt;on &lt;profiling&gt;on ;
1003 </programlisting>
1004         will define a new variant with the specified set of properties. You
1005         can also extend an existing variant:
1006 <programlisting>
1007 variant super_release : release : &lt;define&gt;USE_ASM ;
1008 </programlisting>
1009         In this case, <code>super_release</code> will expand to all properties
1010         specified by <code>release</code>, and the additional one you've specified.
1011       </para>
1012
1013       <para>You are not restricted to using the <code>variant</code> feature
1014       only.
1015       <!-- What do you mean by that?  How is defining a new feature related to what came before? -->
1016       Here's example that defines a brand new feature:
1017 <programlisting>
1018 feature parallelism : mpi fake none : composite link-incompatible ;
1019 feature.compose &lt;parallelism&gt;mpi : &lt;library&gt;/mpi//mpi/&lt;parallelism&gt;none ;
1020 feature.compose &lt;parallelism&gt;fake : &lt;library&gt;/mpi//fake/&lt;parallelism&gt;none ;
1021 </programlisting>
1022 <!-- The use of the <library>/mpi//mpi/<parallelism>none construct
1023      above is at best confusing and unexplained -->
1024         This will allow you to specify the value of feature
1025         <code>parallelism</code>, which will expand to link to the necessary
1026         library.
1027       </para>
1028
1029   </section>
1030
1031   <section id="bbv2.extending.rules">
1032     <title>Main target rules</title>
1033     <para>
1034       A main target rule (e.g “<functionname>exe</functionname>”
1035       Or “<functionname>lib</functionname>”) creates a top-level target. It's quite likely that you'll want to declare your own and
1036       there are two ways to do that.
1037       <!-- Why did "that" get changed to "this" above? -->
1038     </para>
1039
1040     <para id="bbv2.extending.rules.main-type">The first way applies when
1041 <!-- This is not a "way of defining a main target rule."  Rephrase this and the previous sentence. -->
1042       your target rule should just produce a target of specific type. In that case, a
1043       rule is already defined for you! When you define a new type, Boost.Build
1044       automatically defines a corresponding rule. The name of the rule is
1045       obtained from the name of the type, by downcasing all letters and
1046       replacing underscores with dashes.
1047       <!-- This strikes me as needless complexity, and confusing.  Why
1048            do we have the uppercase-underscore convention for target
1049            types?  If we just dropped that, the rule names could be
1050            the same as the type names. -->
1051       For example, if you create a module
1052       <filename>obfuscate.jam</filename> containing:
1053
1054 <programlisting>
1055 import type ;
1056 type.register OBFUSCATED_CPP  : ocpp ;
1057
1058 import generators ;
1059 generators.register-standard obfuscate.file : CPP : OBFUSCATED_CPP ;
1060 </programlisting>
1061       and import that module, you'll be able to use the rule "obfuscated-cpp"
1062       in Jamfiles, which will convert source to the OBFUSCATED_CPP type.
1063     </para>
1064
1065     <para>
1066       The second way is to write a wrapper rule that calls any of the existing
1067       rules. For example, suppose you have only one library per directory and
1068       want all cpp files in the directory to be compiled into that library. You
1069       can achieve this effect using:
1070 <programlisting>
1071 lib codegen : [ glob *.cpp ] ;
1072 </programlisting>
1073       If you want to make it even simpler, you could add the following
1074       definition to the <filename>Jamroot.jam</filename> file:
1075 <programlisting>
1076 rule glib ( name : extra-sources * : requirements * )
1077 {
1078     lib $(name) : [ glob *.cpp ] $(extra-sources) : $(requirements) ;
1079 }
1080 </programlisting>
1081       allowing you to reduce the Jamfile to just
1082 <programlisting>
1083 glib codegen ;
1084 </programlisting>
1085     </para>
1086
1087     <para>
1088       Note that because you can associate a custom generator with a target type,
1089       the logic of building can be rather complicated. For example, the
1090       <code>boostbook</code> module declares a target type
1091       <code>BOOSTBOOK_MAIN</code> and a custom generator for that type. You can
1092       use that as example if your main target rule is non-trivial.
1093     </para>
1094   </section>
1095
1096   <section id="bbv2.extending.toolset_modules">
1097
1098     <title>Toolset modules</title>
1099
1100     <para>
1101       If your extensions will be used only on one project, they can be placed in
1102       a separate <filename>.jam</filename> file and imported by your
1103       <filename>Jamroot.jam</filename>. If the extensions will be used on many
1104       projects, users will thank you for a finishing touch.
1105     </para>
1106
1107     <para>The <code>using</code> rule provides a standard mechanism
1108     for loading and configuring extensions.  To make it work, your module
1109     <!-- "module" hasn't been defined yet.  Furthermore you haven't
1110          said anything about where that module file must be
1111          placed. -->
1112     should provide an <code>init</code> rule. The rule will be called
1113     with the same parameters that were passed to the
1114     <code>using</code> rule. The set of allowed parameters is
1115     determined by you. For example, you can allow the user to specify
1116     paths, tool versions, and other options.
1117     <!-- But it's not entirely arbitrary.  We have a standard
1118          parameter order which you should describe here for
1119          context. -->
1120     </para>
1121
1122     <para>Here are some guidelines that help to make Boost.Build more
1123       consistent:
1124       <itemizedlist>
1125         <listitem><para>The <code>init</code> rule should never fail. Even if
1126           the user provided an incorrect path, you should emit a warning and go
1127           on. Configuration may be shared between different machines, and
1128           wrong values on one machine can be OK on another.
1129           <!-- So why shouldn't init fail on machines where it's wrong?? -->
1130           </para></listitem>
1131
1132         <listitem><para>Prefer specifying the command to be executed
1133         to specifying the tool's installation path. First of all, this
1134         gives more control: it's possible to specify
1135 <programlisting>
1136 /usr/bin/g++-snapshot
1137 time g++
1138 <!-- Is this meant to be a single command?  If not, insert "or" -->
1139 </programlisting>
1140             as the command. Second, while some tools have a logical
1141             "installation root", it's better if the user doesn't have to remember whether
1142             a specific tool requires a full command or a path.
1143             <!-- But many tools are really collections: e.g. a
1144                  compiler, a linker, and others.  The idea that the
1145                  "command to invoke" has any significance may be
1146                  completely bogus.  Plus if you want to allow "time
1147                  /usr/bin/g++" the toolset may need to somehow parse
1148                  the command and find the path when it needs to invoke
1149                  some related executable.  And in that case, will the
1150                  command be ignored?  This scheme doesn't scale and
1151                  should be fixed. -->
1152           </para></listitem>
1153
1154         <listitem><para>Check for multiple initialization. A user can try to
1155             initialize the module several times. You need to check for this
1156             and decide what to do. Typically, unless you support several
1157             versions of a tool, duplicate initialization is a user error.
1158             <!-- Why should that be typical? -->
1159             If the
1160             tool's version can be specified during initialization, make sure the
1161             version is either always specified, or never specified (in which
1162             case the tool is initialied only once). For example, if you allow:
1163 <programlisting>
1164 using yfc ;
1165 using yfc : 3.3 ;
1166 using yfc : 3.4 ;
1167 </programlisting>
1168             Then it's not clear if the first initialization corresponds to
1169             version 3.3 of the tool, version 3.4 of the tool, or some other
1170             version. This can lead to building twice with the same version.
1171             <!-- That would not be so terrible, and is much less harmful
1172                  than this restriction, IMO.  It makes site-config
1173                  harder to maintain than necessary. -->
1174             </para></listitem>
1175
1176         <listitem><para>If possible, <code>init</code> must be callable
1177           with no parameters. In which case, it should try to autodetect all
1178           the necessary information, for example, by looking for a tool in
1179           <envar>PATH</envar> or in common installation locations. Often this
1180           is possible and allows the user to simply write:
1181 <programlisting>
1182 using yfc ;
1183 </programlisting>
1184           </para></listitem>
1185
1186         <listitem><para>Consider using facilities in the
1187           <code>tools/common</code> module. You can take a look at how
1188           <code>tools/gcc.jam</code> uses that module in the <code>init</code> rule.
1189           </para></listitem>
1190
1191       </itemizedlist>
1192     </para>
1193
1194
1195
1196
1197   </section>
1198
1199   </chapter>
1200
1201 <!--
1202      Local Variables:
1203      sgml-indent-data: t
1204      sgml-parent-document: ("userman.xml" "chapter")
1205      sgml-set-face: t
1206      End:
1207 -->