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
|
<?xml version='1.0'?>
<!DOCTYPE sconsdoc [
<!ENTITY % scons SYSTEM "../scons.mod">
%scons;
<!ENTITY % builders-mod SYSTEM "../generated/builders.mod">
%builders-mod;
<!ENTITY % functions-mod SYSTEM "../generated/functions.mod">
%functions-mod;
<!ENTITY % tools-mod SYSTEM "../generated/tools.mod">
%tools-mod;
<!ENTITY % variables-mod SYSTEM "../generated/variables.mod">
%variables-mod;
]>
<chapter id="chap-sconf"
xmlns="http://www.scons.org/dbxsd/v1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.scons.org/dbxsd/v1.0 http://www.scons.org/dbxsd/v1.0/scons.xsd">
<title>Multi-Platform Configuration (&Autoconf; Functionality)</title>
<!--
Copyright (c) 2001 - 2015 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; has integrated support for multi-platform build configuration
similar to that offered by GNU &Autoconf;,
such as
figuring out what libraries or header files
are available on the local system.
This section describes how to use
this &SCons; feature.
</para>
<note>
<para>
This chapter is still under development,
so not everything is explained as well as it should be.
See the &SCons; man page for additional information.
</para>
</note>
<section>
<title>&Configure_Contexts;</title>
<para>
The basic framework for multi-platform build configuration
in &SCons; is to attach a &configure_context; to a
construction environment by calling the &Configure; function,
perform a number of checks for
libraries, functions, header files, etc.,
and to then call the configure context's &Finish; method
to finish off the configuration:
</para>
<sconstruct>
env = Environment()
conf = Configure(env)
# Checks for libraries, header files, etc. go here!
env = conf.Finish()
</sconstruct>
<para>
&SCons; provides a number of basic checks,
as well as a mechanism for adding your own custom checks.
</para>
<para>
Note that &SCons; uses its own dependency
mechanism to determine when a check
needs to be run--that is,
&SCons; does not run the checks
every time it is invoked,
but caches the values returned by previous checks
and uses the cached values unless something has changed.
This saves a tremendous amount
of developer time while working on
cross-platform build issues.
</para>
<para>
The next sections describe
the basic checks that &SCons; supports,
as well as how to add your own custom checks.
</para>
</section>
<section>
<title>Checking for the Existence of Header Files</title>
<para>
Testing the existence of a header file
requires knowing what language the header file is.
A configure context has a &CheckCHeader; method
that checks for the existence of a C header file:
</para>
<sconstruct>
env = Environment()
conf = Configure(env)
if not conf.CheckCHeader('math.h'):
print 'Math.h must be installed!'
Exit(1)
if conf.CheckCHeader('foo.h'):
conf.env.Append('-DHAS_FOO_H')
env = conf.Finish()
</sconstruct>
<para>
Note that you can choose to terminate
the build if a given header file doesn't exist,
or you can modify the construction environment
based on the existence of a header file.
</para>
<para>
If you need to check for the existence
a C++ header file,
use the &CheckCXXHeader; method:
</para>
<sconstruct>
env = Environment()
conf = Configure(env)
if not conf.CheckCXXHeader('vector.h'):
print 'vector.h must be installed!'
Exit(1)
env = conf.Finish()
</sconstruct>
</section>
<section>
<title>Checking for the Availability of a Function</title>
<para>
Check for the availability of a specific function
using the &CheckFunc; method:
</para>
<sconstruct>
env = Environment()
conf = Configure(env)
if not conf.CheckFunc('strcpy'):
print 'Did not find strcpy(), using local version'
conf.env.Append(CPPDEFINES = '-Dstrcpy=my_local_strcpy')
env = conf.Finish()
</sconstruct>
</section>
<section>
<title>Checking for the Availability of a Library</title>
<para>
Check for the availability of a library
using the &CheckLib; method.
You only specify the basename of the library,
you don't need to add a <literal>lib</literal>
prefix or a <literal>.a</literal> or <literal>.lib</literal> suffix:
</para>
<sconstruct>
env = Environment()
conf = Configure(env)
if not conf.CheckLib('m'):
print 'Did not find libm.a or m.lib, exiting!'
Exit(1)
env = conf.Finish()
</sconstruct>
<para>
Because the ability to use a library successfully
often depends on having access to a header file
that describes the library's interface,
you can check for a library
<emphasis>and</emphasis> a header file
at the same time by using the
&CheckLibWithHeader; method:
</para>
<sconstruct>
env = Environment()
conf = Configure(env)
if not conf.CheckLibWithHeader('m', 'math.h', 'c'):
print 'Did not find libm.a or m.lib, exiting!'
Exit(1)
env = conf.Finish()
</sconstruct>
<para>
This is essentially shorthand for
separate calls to the &CheckHeader; and &CheckLib;
functions.
</para>
</section>
<section>
<title>Checking for the Availability of a &typedef;</title>
<para>
Check for the availability of a &typedef;
by using the &CheckType; method:
</para>
<sconstruct>
env = Environment()
conf = Configure(env)
if not conf.CheckType('off_t'):
print 'Did not find off_t typedef, assuming int'
conf.env.Append(CCFLAGS = '-Doff_t=int')
env = conf.Finish()
</sconstruct>
<para>
You can also add a string that will be
placed at the beginning of the test file
that will be used to check for the &typedef;.
This provide a way to specify
files that must be included to find the &typedef;:
</para>
<sconstruct>
env = Environment()
conf = Configure(env)
if not conf.CheckType('off_t', '#include <sys/types.h>\n'):
print 'Did not find off_t typedef, assuming int'
conf.env.Append(CCFLAGS = '-Doff_t=int')
env = conf.Finish()
</sconstruct>
</section>
<section>
<title>Checking the size of a datatype</title>
<para>
Check the size of a datatype by using the &CheckTypeSize; method:
</para>
<sconstruct>
env = Environment()
conf = Configure(env)
int_size = conf.CheckTypeSize('unsigned int')
print 'sizeof unsigned int is', int_size
env = conf.Finish()
</sconstruct>
<screen>
% <userinput>scons -Q</userinput>
sizeof unsigned int is 4
scons: `.' is up to date.
</screen>
</section>
<section>
<title>Checking for the Presence of a program</title>
<para>
Check for the presence of a program
by using the &CheckProg; method:
</para>
<sconstruct>
env = Environment()
conf = Configure(env)
if not conf.CheckProg('foobar'):
print 'Unable to find the program foobar on the system'
Exit(1)
env = conf.Finish()
</sconstruct>
</section>
<section>
<title>Adding Your Own Custom Checks</title>
<para>
A custom check is a Python function
that checks for a certain condition to exist
on the running system,
usually using methods that &SCons;
supplies to take care of the details
of checking whether a compilation succeeds,
a link succeeds,
a program is runnable,
etc.
A simple custom check for the existence of
a specific library might look as follows:
</para>
<sconstruct>
mylib_test_source_file = """
#include <mylib.h>
int main(int argc, char **argv)
{
MyLibrary mylib(argc, argv);
return 0;
}
"""
def CheckMyLibrary(context):
context.Message('Checking for MyLibrary...')
result = context.TryLink(mylib_test_source_file, '.c')
context.Result(result)
return result
</sconstruct>
<para>
The &Message; and &Result; methods
should typically begin and end a custom check to
let the user know what's going on:
the &Message; call prints the
specified message (with no trailing newline)
and the &Result; call prints
<literal>yes</literal> if the check succeeds and
<literal>no</literal> if it doesn't.
The &TryLink; method
actually tests for whether the
specified program text
will successfully link.
</para>
<para>
(Note that a custom check can modify
its check based on any arguments you
choose to pass it,
or by using or modifying the configure context environment
in the <literal>context.env</literal> attribute.)
</para>
<para>
This custom check function is
then attached to the &configure_context;
by passing a dictionary
to the &Configure; call
that maps a name of the check
to the underlying function:
</para>
<sconstruct>
env = Environment()
conf = Configure(env, custom_tests = {'CheckMyLibrary' : CheckMyLibrary})
</sconstruct>
<para>
You'll typically want to make
the check and the function name the same,
as we've done here,
to avoid potential confusion.
</para>
<para>
We can then put these pieces together
and actually call the <literal>CheckMyLibrary</literal> check
as follows:
</para>
<sconstruct>
mylib_test_source_file = """
#include <mylib.h>
int main(int argc, char **argv)
{
MyLibrary mylib(argc, argv);
return 0;
}
"""
def CheckMyLibrary(context):
context.Message('Checking for MyLibrary... ')
result = context.TryLink(mylib_test_source_file, '.c')
context.Result(result)
return result
env = Environment()
conf = Configure(env, custom_tests = {'CheckMyLibrary' : CheckMyLibrary})
if not conf.CheckMyLibrary():
print 'MyLibrary is not installed!'
Exit(1)
env = conf.Finish()
# We would then add actual calls like Program() to build
# something using the "env" construction environment.
</sconstruct>
<para>
If MyLibrary is not installed on the system,
the output will look like:
</para>
<screen>
% <userinput>scons</userinput>
scons: Reading SConscript file ...
Checking for MyLibrary... failed
MyLibrary is not installed!
</screen>
<para>
If MyLibrary is installed,
the output will look like:
</para>
<screen>
% <userinput>scons</userinput>
scons: Reading SConscript file ...
Checking for MyLibrary... failed
scons: done reading SConscript
scons: Building targets ...
.
.
.
</screen>
</section>
<section>
<title>Not Configuring When Cleaning Targets</title>
<para>
Using multi-platform configuration
as described in the previous sections
will run the configuration commands
even when invoking
<userinput>scons -c</userinput>
to clean targets:
</para>
<screen>
% <userinput>scons -Q -c</userinput>
Checking for MyLibrary... yes
Removed foo.o
Removed foo
</screen>
<para>
Although running the platform checks
when removing targets doesn't hurt anything,
it's usually unnecessary.
You can avoid this by using the
&GetOption; method to
check whether the <option>-c</option> (clean)
option has been invoked on the command line:
</para>
<sconstruct>
env = Environment()
if not env.GetOption('clean'):
conf = Configure(env, custom_tests = {'CheckMyLibrary' : CheckMyLibrary})
if not conf.CheckMyLibrary():
print 'MyLibrary is not installed!'
Exit(1)
env = conf.Finish()
</sconstruct>
<screen>
% <userinput>scons -Q -c</userinput>
Removed foo.o
Removed foo
</screen>
</section>
<!--
<section>
<title>Controlling Configuration: the &config; Option</title>
<para>
XXX -D, -u and -U
</para>
</section>
-->
</chapter>
|