summaryrefslogtreecommitdiff
path: root/doc/user/misc.in
blob: 704a1f50b02a6d120a46e05f41c37cf8ea3e906c (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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
<!--

  Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 The SCons Foundation

  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.

-->

  <para>

  &SCons; supports a lot of additional functionality
  that doesn't readily fit into the other chapters.

  </para>

  <section>
  <title>Verifying the Python Version:  the &EnsurePythonVersion; Function</title>

    <para>

    Although the &SCons; code itself will run 
    on any 2.x Python version 2.4 or later,
    you are perfectly free to make use of
    Python syntax and modules from more modern versions
    (for example, Python 2.5 or 2.6)
    when writing your &SConscript; files
    or your own local modules.
    If you do this, it's usually helpful to
    configure &SCons; to exit gracefully with an error message
    if it's being run with a version of Python
    that simply won't work with your code.
    This is especially true if you're going to use &SCons;
    to build source code that you plan to distribute publicly,
    where you can't be sure of the Python version
    that an anonymous remote user might use
    to try to build your software.

    </para>

    <para>

    &SCons; provides an &EnsurePythonVersion; function for this.
    You simply pass it the major and minor versions
    numbers of the version of Python you require:

    </para>

    <!--

    TODO:  Figure out how to generate the error message
    regardless of executing Python version by faking out
    the infrastructure in some way.

    <scons_example name="EnsurePythonVersion">
      <file name="SConstruct" printme="1">
      EnsurePythonVersion(2, 5)
      </file>
    </scons_example>

    -->

    <sconstruct>
      EnsurePythonVersion(2, 5)
    </sconstruct>

    <para>

    And then &SCons; will exit with the following error
    message when a user runs it with an unsupported
    earlier version of Python:

    </para>

    <!--

    TODO:  Figure out how to generate the error message
    regardless of executing Python version by faking out
    the infrastructure in some way.

    <scons_output example="EnsurePythonVersion">
      <scons_output_command>scons -Q</scons_output_command>
    </scons_output>

    -->

    <screen>
      % <userinput>scons -Q</userinput>
      Python 2.5 or greater required, but you have Python 2.3.6
    </screen>

  </section>

  <section>
  <title>Verifying the SCons Version:  the &EnsureSConsVersion; Function</title>

    <para>

    You may, of course, write your &SConscript; files
    to use features that were only added in
    recent versions of &SCons;.
    When you publicly distribute software that is built using &SCons;,
    it's helpful to have &SCons;
    verify the version being used and
    exit gracefully with an error message
    if the user's version of &SCons; won't work
    with your &SConscript; files.
    &SCons; provides an &EnsureSConsVersion; function
    that verifies the version of &SCons;
    in the same
    the &EnsurePythonVersion; function
    verifies the version of Python,
    by passing in the major and minor versions
    numbers of the version of SCons you require:

    </para>

    <!--

    TODO:  Figure out how to generate the error message
    regardless of executing SCons version by faking out
    the infrastructure in some way.

    <scons_example name="EnsureSConsVersion">
      <file name="SConstruct" printme="1">
      EnsureSConsVersion(1, 0)
      </file>
    </scons_example>

    -->

    <sconstruct>
      EnsureSConsVersion(1, 0)
    </sconstruct>

    <para>

    And then &SCons; will exit with the following error
    message when a user runs it with an unsupported
    earlier version of &SCons;:

    </para>

    <!--

    TODO:  Figure out how to generate the error message
    regardless of executing SCons version by faking out
    the infrastructure in some way.

    <scons_output example="EnsureSConsVersion">
      <scons_output_command>scons -Q</scons_output_command>
    </scons_output>

    -->

    <screen>
      % <userinput>scons -Q</userinput>
      SCons 1.0 or greater required, but you have SCons 0.98.5
    </screen>

  </section>

  <section>
  <title>Explicitly Terminating &SCons; While Reading &SConscript; Files:  the &Exit; Function</title>

    <para>

    &SCons; supports an &Exit; function
    which can be used to terminate &SCons;
    while reading the &SConscript; files,
    usually because you've detected a condition
    under which it doesn't make sense to proceed:

    </para>

    <scons_example name="Exit">
      <file name="SConstruct" printme="1">
      if ARGUMENTS.get('FUTURE'):
          print "The FUTURE option is not supported yet!"
          Exit(2)
      env = Environment()
      env.Program('hello.c')
      </file>
      <file name="hello.c">
      hello.c
      </file>
    </scons_example>

    <scons_output example="Exit">
      <scons_output_command>scons -Q FUTURE=1</scons_output_command>
      <scons_output_command>scons -Q</scons_output_command>
    </scons_output>

    <para>

    The &Exit; function takes as an argument
    the (numeric) exit status that you want &SCons; to exit with.
    If you don't specify a value,
    the default is to exit with <literal>0</literal>,
    which indicates successful execution.

    </para>

    <para>

    Note that the &Exit; function
    is equivalent to calling the Python
    <function>sys.exit</function> function
    (which the it actually calls),
    but because &Exit; is a &SCons; function,
    you don't have to import the Python
    <literal>sys</literal> module to use it.

    </para>

  </section>

  <section>
  <title>Searching for Files:  the &FindFile; Function</title>

    <para>

     The &FindFile; function searches for a file in a list of directories.
     If there is only one directory, it can be given as a simple string.
     The function returns a File node if a matching file exists,
     or None if no file is found.
     (See the documentation for the &Glob; function for an alternative way
     of searching for entries in a directory.)

    </para>

    <scons_example name="FindFile1a">
      <file name="SConstruct" printme="1">
       # one directory
       print FindFile('missing', '.')
       t = FindFile('exists', '.')
       print t.__class__, t
      </file>
      <file name="exists">
        exists
      </file>
    </scons_example>

    <scons_output example="FindFile1a" os="posix">
       <scons_output_command>scons -Q</scons_output_command>
    </scons_output>

    <scons_example name="FindFile1b">
      <file name="SConstruct" printme="1">
       # several directories
       includes = [ '.', 'include', 'src/include']
       headers = [ 'nonesuch.h', 'config.h', 'private.h', 'dist.h']
       for hdr in headers:
           print '%-12s' % ('%s:' % hdr), FindFile(hdr, includes)
      </file>
      <file name="config.h">
        exists
      </file>
      <directory name="src"></directory>
      <directory name="src/include"></directory>
      <file name="src/include/private.h">
        exists
      </file>
      <directory name="include"></directory>
      <file name="include/dist.h">
        exists
      </file>
    </scons_example>

    <scons_output example="FindFile1b" os="posix">
       <scons_output_command>scons -Q</scons_output_command>
    </scons_output>

   <!-- The man page says this should work, but it fails.
    <para>

     If the 'file' parameter is a list of files,
     a list of File nodes is returned.

    </para>

    <scons_example name="FindFile1c">
      <file name="SConstruct" printme="1">
       # several directories
       includes = [ '.', 'include', 'src/include']
       headers = [ 'nonesuch.h', 'config.h', 'private.h', 'dist.h']
       print FindFile(headers, includes)
      </file>
      <file name="config.h">
        exists
      </file>
      <directory name="src"></directory>
      <directory name="src/include"></directory>
      </file>
      <file name="src/include/private.h">
        exists
      <directory name="include"></directory>
      </file>
      <file name="include/dist.h">
        exists
    </scons_example>

    <scons_output example="FindFile1c" os="posix">
       <scons_output_command>scons -Q</scons_output_command>
    </scons_output>
   -->

    <para>

     If the file exists in more than one directory,
     only the first occurrence is returned.

    </para>

    <scons_example name="FindFile1d">
      <file name="SConstruct" printme="1">
        print FindFile('multiple', ['sub1', 'sub2', 'sub3'])
        print FindFile('multiple', ['sub2', 'sub3', 'sub1'])
        print FindFile('multiple', ['sub3', 'sub1', 'sub2'])
      </file>
      <directory name="sub1"></directory>
      <file name="sub1/multiple">
        exists
      </file>
      <directory name="sub2"></directory>
      <file name="sub2/multiple">
        exists
      </file>
      <directory name="sub3"></directory>
      <file name="sub3/multiple">
        exists
      </file>
    </scons_example>

    <scons_output example="FindFile1d" os="posix">
       <scons_output_command>scons -Q</scons_output_command>
    </scons_output>

   <!-- file may be a list of file names or a single file name. -->

    <para>

    In addition to existing files, &FindFile; will also find derived files
    (that is, non-leaf files) that haven't been built yet.
    (Leaf files should already exist, or the build will fail!)

    </para>

    <scons_example name="FindFile2">
      <file name="SConstruct" printme="1">
       # Neither file exists, so build will fail
       Command('derived', 'leaf', 'cat >$TARGET $SOURCE')
       print FindFile('leaf', '.')
       print FindFile('derived', '.')
      </file>
    </scons_example>

    <scons_output example="FindFile2" os="posix">
       <scons_output_command>scons -Q</scons_output_command>
    </scons_output>

    <scons_example name="FindFile2">
      <file name="SConstruct" printme="1">
       # Only 'leaf' exists
       Command('derived', 'leaf', 'cat >$TARGET $SOURCE')
       print FindFile('leaf', '.')
       print FindFile('derived', '.')
      </file>
      <file name="leaf">
       leaf
      </file>
    </scons_example>

    <scons_output example="FindFile2" os="posix">
       <scons_output_command>scons -Q</scons_output_command>
    </scons_output>

    <para>

    If a source file exists, &FindFile; will correctly return the name
    in the build directory.

    </para>

    <scons_example name="FindFile3">
      <file name="SConstruct" printme="1">
       # Only 'src/leaf' exists
       VariantDir('build', 'src')
       print FindFile('leaf', 'build')
      </file>
      <directory name="src"></directory>
      <file name="src/leaf">
       leaf
      </file>
    </scons_example>

    <scons_output example="FindFile3" os="posix">
       <scons_output_command>scons -Q</scons_output_command>
    </scons_output>

  </section>

  <section>
  <title>Handling Nested Lists:  the &Flatten; Function</title>

    <para>

    &SCons; supports a &Flatten; function
    which takes an input Python sequence
    (list or tuple)
    and returns a flattened list
    containing just the individual elements of
    the sequence.
    This can be handy when trying to examine
    a list composed of the lists
    returned by calls to various Builders.
    For example, you might collect
    object files built in different ways
    into one call to the &Program; Builder
    by just enclosing them in a list, as follows:

    </para>

    <scons_example name="Flatten1">
      <file name="SConstruct" printme="1">
      objects = [
          Object('prog1.c'),
          Object('prog2.c', CCFLAGS='-DFOO'),
      ]
      Program(objects)
      </file>
      <file name="prog1.c">
      prog1.c
      </file>
      <file name="prog2.c">
      prog2.c
      </file>
    </scons_example>

    <para>

    Because the Builder calls in &SCons;
    flatten their input lists,
    this works just fine to build the program:

    </para>

    <scons_output example="Flatten1">
      <scons_output_command>scons -Q</scons_output_command>
    </scons_output>

    <para>

    But if you were debugging your build
    and wanted to print the absolute path
    of each object file in the
    <varname>objects</varname> list,
    you might try the following simple approach,
    trying to print each Node's
    <literal>abspath</literal>
    attribute:

    </para>

    <scons_example name="Flatten2">
      <file name="SConstruct" printme="1">
      objects = [
          Object('prog1.c'),
          Object('prog2.c', CCFLAGS='-DFOO'),
      ]
      Program(objects)

      for object_file in objects:
          print object_file.abspath
      </file>
      <file name="prog1.c">
      prog1.c
      </file>
      <file name="prog2.c">
      prog2.c
      </file>
    </scons_example>

    <para>

    This does not work as expected
    because each call to <function>str</function>
    is operating an embedded list returned by
    each &Object; call,
    not on the underlying Nodes within those lists:

    </para>

    <scons_output example="Flatten2">
      <scons_output_command>scons -Q</scons_output_command>
    </scons_output>

    <para>

    The solution is to use the &Flatten; function
    so that you can pass each Node to
    the <function>str</function> separately:

    </para>

    <scons_example name="Flatten3">
      <file name="SConstruct" printme="1">
      objects = [
          Object('prog1.c'),
          Object('prog2.c', CCFLAGS='-DFOO'),
      ]
      Program(objects)

      for object_file in Flatten(objects):
          print object_file.abspath
      </file>
      <file name="prog1.c">
      prog1.c
      </file>
      <file name="prog2.c">
      prog2.c
      </file>
    </scons_example>

    <!--

    TODO:  can't use this now because it displays the temporary path name

    <scons_output example="Flatten3">
      <scons_output_command>scons -Q</scons_output_command>
    </scons_output>

    -->

    <screen>
      % <userinput>scons -Q</userinput>
      /home/me/project/prog1.o
      /home/me/project/prog2.o
      cc -o prog1.o -c prog1.c
      cc -o prog2.o -c -DFOO prog2.c
      cc -o prog1 prog1.o prog2.o
    </screen>

  </section>

  <section>
  <title>Finding the Invocation Directory:  the &GetLaunchDir; Function</title>

    <para>

    If you need to find the directory from
    which the user invoked the &scons; command,
    you can use the &GetLaunchDir; function:

    </para>

    <sconstruct>
      env = Environment(
          LAUNCHDIR = GetLaunchDir(),
      )
      env.Command('directory_build_info',
                  '$LAUNCHDIR/build_info'
                  Copy('$TARGET', '$SOURCE'))
    </sconstruct>

    <para>

    Because &SCons; is usually invoked from the top-level
    directory in which the &SConstruct; file lives,
    the Python <function>os.getcwd()</function>
    is often equivalent.
    However, the &SCons;
    <literal>-u</literal>,
    <literal>-U</literal>
    and
    <literal>-D</literal>
    command-line options,
    when invoked from a subdirectory,
    will cause &SCons; to change to the directory
    in which the &SConstruct; file is found.
    When those options are used,
    &GetLaunchDir; will still return the path to the
    user's invoking subdirectory,
    allowing the &SConscript; configuration
    to still get at configuration (or other) files
    from the originating directory.

    </para>

  </section>