summaryrefslogtreecommitdiffstats
path: root/doc/user/builders.sgml
blob: c1b426943da5c797fad966e5b341eed39adea650 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
<!--

  Copyright (c) 2001, 2002, 2003 Steven Knight

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be included
  in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

-->

<!--

=head2 Adding new methods

For slightly more demanding changes, you may wish to add new methods to the
C<cons> package. Here's an example of a very simple extension,
C<InstallScript>, which installs a tcl script in a requested location, but
edits the script first to reflect a platform-dependent path that needs to be
installed in the script:

  # cons::InstallScript - Create a platform dependent version of a shell
  # script by replacing string ``#!your-path-here'' with platform specific
  # path $BIN_DIR.

  sub cons::InstallScript {
	my ($env, $dst, $src) = @_;
	Command $env $dst, $src, qq(
		sed s+your-path-here+$BIN_DIR+ %< > %>
		chmod oug+x %>
	);
  }

Notice that this method is defined directly in the C<cons> package (by
prefixing the name with C<cons::>). A change made in this manner will be
globally visible to all environments, and could be called as in the
following example:

  InstallScript $env "$BIN/foo", "foo.tcl";

For a small improvement in generality, the C<BINDIR> variable could be
passed in as an argument or taken from the construction environment-,-as
C<%BINDIR>.


=head2 Overriding methods

Instead of adding the method to the C<cons> name space, you could define a
new package which inherits existing methods from the C<cons> package and
overrides or adds others. This can be done using Perl's inheritance
mechanisms.

The following example defines a new package C<cons::switch> which
overrides the standard C<Library> method. The overridden method builds
linked library modules, rather than library archives. A new
constructor is provided. Environments created with this constructor
will have the new library method; others won't.

  package cons::switch;
  BEGIN {@ISA = 'cons'}

  sub new {
	shift;
	bless new cons(@_);
  }

  sub Library {
	my($env) = shift;
	my($lib) = shift;
	my(@objs) = Objects $env @_;
	Command $env $lib, @objs, q(
		%LD -r %LDFLAGS %< -o %>
	);
  }

This functionality could be invoked as in the following example:

  $env = new cons::switch(@overrides);
  ...
  Library $env 'lib.o', 'foo.c', 'bar.c';

-->

  <para>

    &SCons; provides many useful methods
    for building common software products:
    programs, libraries, documents.
    Frequently, however, you want to be
    able to build some other type of file
    not supported directly by &SCons;
    Fortunately, &SCons; makes it very easy
    to define your own &Builder; objects
    for any custom file types you want to build.

  </para>

  <section>
  <title>Builders That Execute External Commands</title>

    <para>

    The simplest &Builder; to create is
    one that executes an external command.
    For example, if we want to build
    an output file by running the contents
    of the input file through a command named
    <literal>foobuild</literal>,
    creating that &Builder; might look like:

    </para>

    <programlisting>
       bld = Builder(action = 'foobuild < $TARGET > $SOURCE')
    </programlisting>

  </section>

  <section>
  <title>Attaching a Builder to a &ConsEnv;</title>

    <para>

    A &Builder; object by itself, though,
    isn't useful until it's attached to a &consenv;
    so that we can call it to arrange
    for files to be built.
    This is done through the &BUILDERS;
    &consvar; in an environment.
    The &BUILDERS; variable is a Python dictionary
    that maps the names by which you want to call various &Builder;
    to the &Builder; objects themselves.
    For example, if we want to call the
    &Builder; we defined by the name
    <function>Foo</function>,
    our &SConstruct; file might look like:

    </para>

    <programlisting>
       bld = Builder(action = 'foobuild < $TARGET > $SOURCE')
       env = Environment(BUILDERS = {'Foo' : bld})
       env.Foo('file.foo', 'file.input')
    </programlisting>

    <para>

    Then when we run &SCons; it looks like:

    </para>

    <literallayout>
      % <userinput>scons</userinput>
      foobuild < file.input > file.foo
    </literallayout>

    <para>

    Note, however, that the default &BUILDERS;
    variable in a &consenv;
    comes with a default set of &Builder; objects
    already defined:
    &Program;, &Library;, etc.
    And when we explicitly set the &BUILDERS; variable
    when we create the &consenv;,
    the default &Builder;s are no longer part of
    the environment:

    </para>

    <programlisting>
       bld = Builder(action = 'foobuild < $TARGET > $SOURCE')
       env = Environment(BUILDERS = {'Foo' : bld})
       env.Foo('file.foo', 'file.input')
       env.Program('hello.c')
    </programlisting>

    <literallayout>
      % <userinput>scons</userinput>
      scons: Reading SConscript files ...
      other errors
      Traceback (most recent call last):
        File "/usr/lib/scons/SCons/Script/__init__.py", line 901, in main
          _main()
        File "/usr/lib/scons/SCons/Script/__init__.py", line 762, in _main
          SCons.Script.SConscript.SConscript(script)
        File "/usr/lib/scons/SCons/Script/SConscript.py", line 207, in SConscript
          exec _file_ in stack[-1].globals
        File "SConstruct", line 4, in ?
          env.Program('hello.c')
      scons: Environment instance has no attribute 'Program'
    </literallayout>

    <para>

    To be able use both our own defined &Builder; objects
    and the default &Builder; objects in the same &consenv;,
    you can either add to the &BUILDERS; variable
    using the &Append; function:

    </para>

    <programlisting>
       env = Environment()
       bld = Builder(action = 'foobuild < $TARGET > $SOURCE')
       env.Append(BUILDERS = {'Foo' : bld})
       env.Foo('file.foo', 'file.input')
       env.Program('hello.c')
    </programlisting>

    <para>

    Or you can explicitly set the appropriately-named
    key in the &BUILDERS; dictionary:

    </para>

    <programlisting>
       env = Environment()
       bld = Builder(action = 'foobuild < $TARGET > $SOURCE')
       env['BUILDERS']['Foo'] = bld
       env.Foo('file.foo', 'file.input')
       env.Program('hello.c')
    </programlisting>

    <para>

    Either way, the same &consenv;
    can then use both the newly-defined
    <function>Foo</function> &Builder;
    and the default &Program; &Builder;:

    </para>

    <literallayout>
      % <userinput>scons</userinput>
      foobuild < file.input > file.foo
      cc -c hello.c -o hello.o
      cc -o hello hello.o
    </literallayout>

  </section>

  <section>
  <title>Letting &SCons; Handle The File Suffixes</title>

    <para>

    By supplying additional information
    when you create a &Builder;,
    you can let &SCons; add appropriate file
    suffixes to the target and/or the source file.
    For example, rather than having to specify
    explicitly that you want the <literal>Foo</literal>
    &Builder; to build the <literal>file.foo</literal>
    target file from the <literal>file.input</literal> source file,
    you can give the <literal>.foo</literal>
    and <literal>.input</literal> suffixes to the &Builder;,
    making for more compact and readable calls to
    the <literal>Foo</literal> &Builder;:

    </para>

    <programlisting>
       bld = Builder(action = 'foobuild < $TARGET > $SOURCE',
                     suffix = '.foo',
                     src_suffix = '.input')
       env = Environment(BUILDERS = {'Foo' : bld})
       env.Foo('file1')
       env.Foo('file2')
    </programlisting>

    <literallayout>
      % <userinput>scons</userinput>
      foobuild < file1.input > file1.foo
      foobuild < file2.input > file2.foo
    </literallayout>

    <para>

    You can also supply a <literal>prefix</literal> keyword argument
    if it's appropriate to have &SCons; append a prefix
    to the beginning of target file names.

    </para>

  </section>

  <section>
  <title>Builders That Execute Python Functions</title>

    <para>

    In &SCons;, you don't have to call an external command
    to build a file.
    You can, instead, define a Python function
    that a &Builder; object can invoke
    to build your target file (or files).

    </para>

    <programlisting>
       def build_function(target, source, env):
           # XXX
           return None
    </programlisting>

    <para>

    XXX explain the function
    XXX define the arguments

    </para>

    <para>

    Once you've defined the Python function
    that will build your target file,
    defining a &Builder; object for it is as
    simple as specifying the name of the function,
    instead of an external command,
    as the &Builder;'s
    <literal>action</literal>
    argument:

    </para>

    <programlisting>
       def build_function(target, source, env):
           # XXX
           return None
       bld = Builder(action = build_function,
                     suffix = '.foo',
                     src_suffix = '.input')
       env = Environment(BUILDERS = {'Foo' : bld})
       env.Foo('file')
    </programlisting>

    <para>

    And notice that the output changes slightly,
    reflecting the fact that a Python function,
    not an external command,
    is now called to build the target file:

    </para>

    <literallayout>
      % <userinput>scons</userinput>
      build_function("file.foo", "file.input")
    </literallayout>

  </section>

  <section>
  <title>Builders That Generate Actions</title>

    <para>

    X

    </para>

    <programlisting>
       def generate_actions(XXX):
           return XXX
       bld = Builder(generator = generate_actions,
                     suffix = '.foo',
                     src_suffix = '.input')
       env = Environment(BUILDERS = {'Foo' : bld})
       env.Foo('file')
    </programlisting>

    <literallayout>
      % <userinput>scons</userinput>
      XXX
    </literallayout>

    <para>

    Note that it's illegal to specify both an
    <literal>action</literal>
    and a
    <literal>generator</literal>
    for a &Builder;.

    </para>

  </section>

  <section>
  <title>Builders That Modify the Target List</title>

    <para>

    X

    </para>

    <programlisting>
       def modify_targets(XXX):
           return XXX
       bld = Builder(action = 'XXX',
                     suffix = '.foo',
                     src_suffix = '.input',
                     emitter = modify_targets)
       env = Environment(BUILDERS = {'Foo' : bld})
       env.Foo('file')
    </programlisting>

    <literallayout>
      % <userinput>scons</userinput>
      XXX
    </literallayout>

    <programlisting>
       bld = Builder(action = 'XXX',
                     suffix = '.foo',
                     src_suffix = '.input',
                     emitter = 'MY_EMITTER')
       def modify1(XXX):
           return XXX
       def modify2(XXX):
           return XXX
       env1 = Environment(BUILDERS = {'Foo' : bld},
                          MY_EMITTER = modify1)
       env2 = Environment(BUILDERS = {'Foo' : bld},
                          MY_EMITTER = modify2)
       env1.Foo('file1')
       env2.Foo('file2')
    </programlisting>

  </section>

  <section>
  <title>Builders That Use Other Builders</title>

    <para>

    X

    </para>

    <programlisting>
       env = Environment()
       env.SourceCode('.', env.BitKeeper('XXX')
       env.Program('hello.c')
    </programlisting>

    <literallayout>
      % <userinput>scons</userinput>
      XXX
    </literallayout>

  </section>