Eliminate / replace remaining cPickle references in test scripts.
[scons.git] / doc / user / builders-commands.xml
1 <!--
2
3   __COPYRIGHT__
4
5   Permission is hereby granted, free of charge, to any person obtaining
6   a copy of this software and associated documentation files (the
7   "Software"), to deal in the Software without restriction, including
8   without limitation the rights to use, copy, modify, merge, publish,
9   distribute, sublicense, and/or sell copies of the Software, and to
10   permit persons to whom the Software is furnished to do so, subject to
11   the following conditions:
12
13   The above copyright notice and this permission notice shall be included
14   in all copies or substantial portions of the Software.
15
16   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17   KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18   WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20   LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21   OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22   WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24 -->
25
26   <!--
27
28   =head2 The C<Command> method
29
30
31   The C<Command> method is called as follows:
32
33     Command $env <target>, <inputs>, <build action>;
34
35   The target is made dependent upon the list of input files specified, and the
36   inputs must be built successfully or Cons will not attempt to build the
37   target.
38
39   To specify a command with multiple targets, you can specify a reference to a
40   list of targets. In Perl, a list reference can be created by enclosing a
41   list in square brackets. Hence the following command:
42
43     Command $env ['foo.h', 'foo.c'], 'foo.template', q(
44         gen %1
45     );
46
47   could be used in a case where the command C<gen> creates two files, both
48   F<foo.h> and F<foo.c>.
49
50   -->
51
52   <para>
53
54   Creating a &Builder; and attaching it to a &consenv;
55   allows for a lot of flexibility when you
56   want to re-use actions
57   to build multiple files of the same type.
58   This can, however, be cumbersome
59   if you only need to execute one specific command
60   to build a single file (or group of files).
61   For these situations, &SCons; supports a
62   &Command; &Builder; that arranges
63   for a specific action to be executed
64   to build a specific file or files.
65   This looks a lot like the other builders
66   (like &b-link-Program;, &b-link-Object;, etc.),
67   but takes as an additional argument
68   the command to be executed to build the file:
69
70   </para>
71
72   <programlisting>
73      env = Environment()
74      env.Command('foo.out', 'foo.in', "sed 's/x/y/' &lt; $SOURCE &gt; $TARGET")
75   </programlisting>
76
77   <para>
78
79   When executed,
80   &SCons; runs the specified command,
81   substituting &cv-link-SOURCE; and &cv-link-TARGET;
82   as expected:
83
84   </para>
85
86   <screen>
87     % <userinput>scons -Q</userinput>
88     sed 's/x/y/' &lt; foo.in &gt; foo.out
89   </screen>
90
91   <para>
92
93   This is often more convenient than
94   creating a &Builder; object
95   and adding it to the &cv-link-BUILDERS; variable
96   of a &consenv;
97
98   </para>
99
100   <para>
101
102   Note that the action you specify to the
103   &Command; &Builder; can be any legal &SCons; &Action;,
104   such as a Python function:
105
106   </para>
107
108   <programlisting>
109      env = Environment()
110      def build(target, source, env):
111          # Whatever it takes to build
112          return None
113      env.Command('foo.out', 'foo.in', build)
114   </programlisting>
115
116   <para>
117
118   Which executes as follows:
119
120   </para>
121
122   <screen>
123     % <userinput>scons -Q</userinput>
124     build(["foo.out"], ["foo.in"])
125   </screen>
126
127   <para>
128
129   Note that &cv-link-SOURCE; and &cv-link-TARGET; are expanded 
130   in the source and target as well as of SCons 1.1,
131   so you can write:
132
133   </para>
134
135   <programlisting>
136      env.Command('${SOURCE.basename}.out', 'foo.in', build)
137   </programlisting>
138
139
140   <para>
141
142   which does the same thing as the previous example, but allows you
143   to avoid repeating yourself.
144
145   </para>
146