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:
13 The above copyright notice and this permission notice shall be included
14 in all copies or substantial portions of the Software.
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.
28 &SCons; has integrated support for multi-platform build configuration
29 similar to that offered by GNU &Autoconf;,
31 figuring out what libraries or header files
32 are available on the local system.
33 This section describes how to use
40 This chapter is still under development,
41 so not everything is explained as well as it should be.
42 See the &SCons; man page for additional information.
47 <title>&Configure_Contexts;</title>
51 The basic framework for multi-platform build configuration
52 in &SCons; is to attach a &configure_context; to a
53 construction environment by calling the &Configure; function,
54 perform a number of checks for
55 libraries, functions, header files, etc.,
56 and to then call the configure context's &Finish; method
57 to finish off the configuration:
64 # Checks for libraries, header files, etc. go here!
70 &SCons; provides a number of basic checks,
71 as well as a mechanism for adding your own custom checks.
77 Note that &SCons; uses its own dependency
78 mechanism to determine when a check
79 needs to be run--that is,
80 &SCons; does not run the checks
81 every time it is invoked,
82 but caches the values returned by previous checks
83 and uses the cached values unless something has changed.
84 This saves a tremendous amount
85 of developer time while working on
86 cross-platform build issues.
92 The next sections describe
93 the basic checks that &SCons; supports,
94 as well as how to add your own custom checks.
101 <title>Checking for the Existence of Header Files</title>
105 Testing the existence of a header file
106 requires knowing what language the header file is.
107 A configure context has a &CheckCHeader; method
108 that checks for the existence of a C header file:
114 conf = Configure(env)
115 if not conf.CheckCHeader('math.h'):
116 print 'Math.h must be installed!'
118 if conf.CheckCHeader('foo.h'):
119 conf.env.Append('-DHAS_FOO_H')
125 Note that you can choose to terminate
126 the build if a given header file doesn't exist,
127 or you can modify the contstruction environment
128 based on the existence of a header file.
134 If you need to check for the existence
136 use the &CheckCXXHeader; method:
142 conf = Configure(env)
143 if not conf.CheckCXXHeader('vector.h'):
144 print 'vector.h must be installed!'
152 <title>Checking for the Availability of a Function</title>
156 Check for the availability of a specific function
157 using the &CheckFunc; method:
163 conf = Configure(env)
164 if not conf.CheckFunc('strcpy'):
165 print 'Did not find strcpy(), using local version'
166 conf.env.Append('-Dstrcpy=my_local_strcpy')
173 <title>Checking for the Availability of a Library</title>
177 Check for the availability of a library
178 using the &CheckLib; method.
179 You only specify the basename of the library,
180 you don't need to add a <literal>lib</literal>
181 prefix or a <literal>.a</literal> or <literal>.lib</literal> suffix:
187 conf = Configure(env)
188 if not conf.CheckLib('m'):
189 print 'Did not find libm.a or m.lib, exiting!'
196 Because the ability to use a library successfully
197 often depends on having access to a header file
198 that describes the library's interface,
199 you can check for a library
200 <emphasis>and</emphasis> a header file
201 at the same time by using the
202 &CheckLibWithHeader; method:
208 conf = Configure(env)
209 if not conf.CheckLibWithHeader('m', 'math.h'):
210 print 'Did not find libm.a or m.lib, exiting!'
217 This is essentially shorthand for
218 separate calls to the &CheckHeader; and &CheckLib;
226 <title>Checking for the Availability of a &typedef;</title>
230 Check for the availability of a &typedef;
231 by using the &CheckType; method:
237 conf = Configure(env)
238 if not conf.CheckType('off_t'):
239 print 'Did not find off_t typedef, assuming int'
240 conf.env.Append(CCFLAGS = '-Doff_t=int')
246 You can also add a string that will be
247 placed at the beginning of the test file
248 that will be used to check for the &typedef;.
249 This provide a way to specify
250 files that must be included to find the &typedef;:
256 conf = Configure(env)
257 if not conf.CheckType('off_t', '#include &lt;sys/types.h&gt;\n'):
258 print 'Did not find off_t typedef, assuming int'
259 conf.env.Append(CCFLAGS = '-Doff_t=int')
266 <title>Adding Your Own Custom Checks</title>
270 A custom check is a Python function
271 that checks for a certain condition to exist
272 on the running system,
273 usually using methods that &SCons;
274 supplies to take care of the details
275 of checking whether a compilation succeeds,
277 a program is runnable,
279 A simple custom check for the existence of
280 a specific library might look as follows:
285 mylib_test_source_file = """
286 #include &lt;mylib.h&gt;
287 int main(int argc, char **argv)
289 MyLibrary mylib(argc, argv);
294 def CheckMyLibrary(context):
295 context.Message('Checking for MyLibrary...')
296 result = context.TryLink(mylib_test_source_file, '.c')
297 context.Result(result)
303 The &Message; and &Result; methods
304 should typically begin and end a custom check to
305 let the user know what's going on:
306 the &Message; call prints the
307 specified message (with no trailing newline)
308 and the &Result; call prints
309 <literal>ok</literal> if the check succeeds and
310 <literal>failed</literal> if it doesn't.
312 actually tests for whether the
313 specified program text
314 will successfully link.
320 (Note that a custom check can modify
321 its check based on any arguments you
323 or by using or modifying the configure context environment
324 in the <literal>context.env</literal> attribute.)
330 This custom check function is
331 then attached to the &configure_context;
332 by passing a dictionary
333 to the &Configure; call
334 that maps a name of the check
335 to the underlying function:
341 conf = Configure(env, custom_tests = {'CheckMyLibrary' : CheckMyLibrary})
346 You'll typically want to make
347 the check and the function name the same,
349 to avoid potential confusion.
355 We can then put these pieces together
356 and actually call the <literal>CheckMyLibrary</literal> check
362 mylib_test_source_file = """
363 #include &lt;mylib.h&gt;
364 int main(int argc, char **argv)
366 MyLibrary mylib(argc, argv);
371 def CheckMyLibrary(context):
372 context.Message('Checking for MyLibrary... ')
373 result = context.TryLink(mylib_test_source_file, '.c')
374 context.Result(result)
378 conf = Configure(env, custom_tests = {'CheckMyLibrary' : CheckMyLibrary})
379 if not conf.CheckMyLibrary():
380 print 'MyLibrary is not installed!'
384 # We would then add actual calls like Program() to build
385 # something using the "env" construction environment.
390 If MyLibrary is not installed on the system,
391 the output will look like:
396 % <userinput>scons</userinput>
397 scons: Reading SConscript file ...
398 Checking for MyLibrary... failed
399 MyLibrary is not installed!
404 If MyLibrary is installed,
405 the output will look like:
410 % <userinput>scons</userinput>
411 scons: Reading SConscript file ...
412 Checking for MyLibrary... failed
413 scons: done reading SConscript
414 scons: Building targets ...
423 <title>Not Configuring When Cleaning Targets</title>
427 Using multi-platform configuration
428 as described in the previous sections
429 will run the configuration commands
431 <userinput>scons -c</userinput>
437 % <userinput>scons -Q -c</userinput>
438 Checking for MyLibrary... ok
445 Although running the platform checks
446 when removing targets doesn't hurt anything,
447 it's usually unnecessary.
448 You can avoid this by using the
449 &GetOption(); method to
450 check whether the <option>-c</option> (clean)
451 option has been invoked on the command line:
457 if not env.GetOption('clean'):
458 conf = Configure(env, custom_tests = {'CheckMyLibrary' : CheckMyLibrary})
459 if not conf.CheckMyLibrary():
460 print 'MyLibrary is not installed!'
466 % <userinput>scons -Q -c</userinput>