4 SPDX-FileCopyrightText: Copyright The SCons Foundation (https://scons.org)
5 SPDX-License-Identifier: MIT
6 SPDX-FileType: DOCUMENTATION
8 This file is processed by the bin/SConsDoc.py module.
12 <!ENTITY % scons SYSTEM "../scons.mod">
15 <!ENTITY % builders-mod SYSTEM "../generated/builders.mod">
17 <!ENTITY % functions-mod SYSTEM "../generated/functions.mod">
19 <!ENTITY % tools-mod SYSTEM "../generated/tools.mod">
21 <!ENTITY % variables-mod SYSTEM "../generated/variables.mod">
26 <chapter id="chap-builders-built-in"
27 xmlns="http://www.scons.org/dbxsd/v1.0"
28 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
29 xsi:schemaLocation="http://www.scons.org/dbxsd/v1.0 http://www.scons.org/dbxsd/v1.0/scons.xsd">
31 <!-- NOTE: this file is incomplete, and main.xml does not currently include it -->
32 <title>Built-In Builders</title>
36 &SCons; provides the ability to build a lot of different
37 types of files right "out of the box."
38 So far, we've been using &SCons;' ability to build
39 programs, objects and libraries to
40 illustrate much of the underlying functionality of &SCons;
41 This section will describe all of the different
42 types of files that you can build with &SCons;,
43 and the built-in &Builder; objects used to build them.
44 By default, all of the &Builder; objects in this section
45 can be built either with or without an explicit
46 construction environment.
51 <title>Programs: the &Program; Builder</title>
55 As we've seen, the &b-link-Program; Builder
56 is used to build an executable program.
57 The &source; argument is one or more
58 source-code files or object files,
59 and the ⌖ argument is the
60 name of the executable program name to be created.
66 Program('prog', 'file1.o')
71 Will create the &prog;
72 executable on a POSIX system,
73 the &prog_exe; executable on a Windows system.
79 The target file's prefix and suffix may be omitted,
80 and the values from the
84 construction variables
85 will be appended appropriately.
91 env = Environment(PROGPREFIX='my', PROGSUFFIX='.xxx')
92 env.Program('prog', ['file1.o', 'file2.o'])
97 Will create a program named
98 <filename>myprog.xxx</filename>
99 regardless of the system on which it is run.
105 If you omit the ⌖,
106 the base of the first input
108 becomes the base of the target
115 Program(['hello.c', 'goodbye.c'])
120 Will create the &hello;
121 executable on a POSIX system,
122 the &hello_exe; executable on a Windows system.
128 Two construction variables control what libraries
129 will be linked with the resulting program.
130 The &cv-link-LIBS; variable is a list of the names of
131 libraries that will be linked into any programs,
132 and the &cv-link-LIBPATH; variables is a list of
133 directories that will be searched for
134 the specified libraries.
135 &SCons; will construct the right command-line
136 options for the running system.
141 <scons_example name="buildersbuiltin_libs">
142 <file name="SConstruct" printme="1">
143 env = Environment(LIBS=['foo1', 'foo2'], LIBPATH=['/usr/dir1', 'dir2'])
144 env.Program(['hello.c', 'goodbye.c'])
146 <file name="hello.c">
147 int hello() { printf("Hello, world!\n"); }
149 <file name="goodbye.c">
150 int goodbye() { printf("Goodbye, world!\n"); }
156 Will execute as follows on a POSIX system:
160 <scons_output example="buildersbuiltin_libs" os="posix" suffix="1">
161 <scons_output_command>scons -Q</scons_output_command>
166 And execute as follows on a Windows system:
170 <scons_output example="buildersbuiltin_libs" os="win32" suffix="2">
171 <scons_output_command>scons -Q</scons_output_command>
176 The &cv-LIBS; construction variable
177 is turned into command line options
178 by appending the &cv-link-LIBLINKPREFIX; and &cv-link-LIBLINKSUFFIX;
179 construction variables to the beginning and end,
180 respectively, of each specified library.
186 The &cv-LIBPATH; construction variable
187 is turned into command line options
188 by appending the &cv-link-LIBDIRPREFIX; and &cv-link-LIBDIRSUFFIX;
189 construction variables to the beginning and end,
190 respectively, of each specified library.
196 Other relevant construction variables
197 include those used by the &b-link-Object;
198 builders to affect how the
199 source files specified as input to the <literal>Program</literal>
200 builders are turned into object files;
201 see the next section.
207 The command line used to control how a program is linked
208 is specified by the &cv-link-LINKCOM; construction variable.
209 By default, it uses the
210 &cv-link-LINK; construction variable
211 and the &cv-link-LINKFLAGS; construction variable.
218 <title>Object-File Builders</title>
222 &SCons; provides separate Builder objects
223 to create static and shared object files.
224 The distinction becomes especially important when
225 archiving object files into different types of libraries.
230 <title>The &StaticObject; Builder</title>
234 The &b-link-StaticObject; Builder
235 is used to build an object file
236 suitable for static linking into a program,
237 or for inclusion in a static library.
238 The &source; argument is a single source-code file,
239 and the ⌖ argument is the
240 name of the static object file to be created.
246 StaticObject('file', 'file.c')
251 Will create the &file_o;
252 object file on a POSIX system,
253 the &file_obj; executable on a Windows system.
259 The target file's prefix and suffix may be omitted,
260 and the values from the
264 construction variables
265 will be appended appropriately.
271 env = Environment(OBJPREFIX='my', OBJSUFFIX='.xxx')
272 env.StaticObject('file', 'file.c')
277 Will create an object file named
278 <filename>myfile.xxx</filename>
279 regardless of the system on which it is run.
285 If you omit the ⌖,
286 the base of the first input
288 beomces the base of the name
289 of the static object file to be created.
295 StaticObject('file.c')
300 Will create the &file_o;
301 executable on a POSIX system,
302 the &file_obj; executable on a Windows system.
309 <title>The &SharedObject; Builder</title>
313 The &b-link-SharedObject; Builder
314 is used to build an object file
315 suitable for shared linking into a program,
316 or for inclusion in a shared library.
317 The &source; argument is a single source-code file,
318 and the ⌖ argument is the
319 name of the shared object file to be created.
325 SharedObject('file', 'file.c')
330 Will create the &file_o;
331 object file on a POSIX system,
332 the &file_obj; executable on a Windows system.
338 The target file's prefix and suffix may be omitted,
339 and the values from the
340 &cv-link-SHOBJPREFIX;
342 &cv-link-SHOBJSUFFIX;
343 construction variables
344 will be appended appropriately.
350 env = Environment(SHOBJPREFIX='my', SHOBJSUFFIX='.xxx')
351 env.SharedObject('file', 'file.c')
356 Will create an object file named
357 <filename>myfile.xxx</filename>
358 regardless of the system on which it is run.
364 If you omit the ⌖,
365 the base of the first input
367 becomes the base of the name
368 of the shared object file to be created.
374 SharedObject('file.c')
379 Will create the &file_o;
380 executable on a POSIX system,
381 the &file_obj; executable on a Windows system.
388 <title>The &Object; Builder</title>
392 The &b-link-Object; Builder is a synonym for &b-link-StaticObject;
393 and is completely equivalent.
402 <title>Library Builders</title>
406 &SCons; provides separate Builder objects
407 to create static and shared libraries.
412 <title>The &StaticLibrary; Builder</title>
416 The &b-link-StaticLibrary; Builder
417 is used to create a library
418 suitable for static linking into a program.
419 The &source; argument is one or more
420 source-code files or object files,
421 and the ⌖ argument is the
422 name of the static library to be created.
428 StaticLibrary('foo', ['file1.c', 'file2.c'])
433 The target file's prefix and suffix may be omitted,
434 and the values from the
438 construction variables
439 will be appended appropriately.
445 env = Environment(LIBPREFIX='my', LIBSUFFIX='.xxx')
446 env.StaticLibrary('lib', ['file1.o', 'file2.o'])
451 Will create an object file named
452 <filename>mylib.xxx</filename>
453 regardless of the system on which it is run.
458 StaticLibrary('foo', ['file1.c', 'file2.c'])
463 If you omit the ⌖,
464 the base of the first input
466 becomes the base of the name of the static object file to be created.
472 StaticLibrary(['file.c', 'another.c'])
477 Will create the &libfile_a;
478 library on a POSIX system,
479 the &file_lib; library on a Windows system.
486 <title>The &SharedLibrary; Builder</title>
490 The &b-link-SharedLibrary; Builder
491 is used to create a shared library
492 suitable for linking with a program.
493 The &source; argument is one or more
494 source-code files or object files,
495 and the ⌖ argument is the
496 name of the shared library to be created.
502 SharedLibrary('foo', ['file1.c', 'file2.c'])
507 The target file's prefix and suffix may be omitted,
508 and the values from the
509 &cv-link-SHLIBPREFIX;
511 &cv-link-SHLIBSUFFIX;
512 construction variables
513 will be appended appropriately.
519 env = Environment(SHLIBPREFIX='my', SHLIBSUFFIX='.xxx')
520 env.SharedLibrary('shared', ['file1.o', 'file2.o'])
525 Will create an object file named
526 <filename>myshared.xxx</filename>
527 regardless of the system on which it is run.
532 SharedLibrary('foo', ['file1.c', 'file2.c'])
537 If you omit the ⌖,
538 the base of the first input
540 becomes the base of the name of the shared library to be created.
546 SharedLibrary(['file.c', 'another.c'])
551 Will create the &libfile_so;
552 library on a POSIX system,
553 the &file_dll; library on a Windows system.
560 <title>The &Library; Builder</title>
564 The &b-link-Library; Builder is a synonym for &b-link-StaticLibrary;
565 and is completely equivalent.
574 <title>Pre-Compiled Headers: the &PCH; Builder</title>
585 <title>&MSVC; Resource Files: the &RES; Builder</title>
596 <title>Source Files</title>
601 &SCons; supports two Builder objects
602 that know how to build source files
603 from other input files.
604 These are typically invoked "internally"
605 to turn files that need preprocessing into other source files.
610 <title>The &CFile; Builder</title>
619 XXX CFile() programlisting
629 <title>The &CXXFile; Builder</title>
638 XXX CXXFILE() programlisting
650 <title>Documents</title>
654 &SCons; provides a number of Builder objects
655 for creating different types of documents.
660 <title>The &DVI; Builder</title>
669 XXX DVI() programlisting
679 <title>The &PDF; Builder</title>
690 <title>The &PostScript; Builder</title>
694 XXX PostScript() para
699 XXX PostScript() programlisting
703 XXX PostScript() screen
711 <title>Archives</title>
715 &SCons; provides Builder objects
716 for creating two different types of archive files.
721 <title>The &Tar; Builder</title>
725 The &b-link-Tar; Builder object uses the &tar;
726 utility to create archives of files
727 and/or directory trees:
731 <scons_example name="buildersbuiltin_ex1">
732 <file name="SConstruct" printme="1">
734 env.Tar('out1.tar', ['file1', 'file2'])
735 env.Tar('out2', 'directory')
743 <file name="directory/file3">
748 <scons_output example="buildersbuiltin_ex1" os="posix" suffix="1">
749 <scons_output_command>scons -Q .</scons_output_command>
754 One common requirement when creating a &tar; archive
755 is to create a compressed archive using the
756 <option>-z</option> option.
757 This is easily handled by specifying
758 the value of the &cv-link-TARFLAGS; variable
759 when you create the construction environment.
760 Note, however, that the <option>-c</option> used to
761 to instruct &tar; to create the archive
762 is part of the default value of &cv-TARFLAGS;,
763 so you need to set it both options:
767 <scons_example name="buildersbuiltin_ex2">
768 <file name="SConstruct" printme="1">
769 env = Environment(TARFLAGS = '-c -z')
770 env.Tar('out.tar.gz', 'directory')
772 <file name="directory/file">
777 <scons_output example="buildersbuiltin_ex2" os="posix" suffix="1">
778 <scons_output_command>scons -Q .</scons_output_command>
783 you may also wish to set the value of the
784 &cv-link-TARSUFFIX; construction variable
785 to your desired suffix for compress &tar; archives,
786 so that &SCons; can append it to the target file name
787 without your having to specify it explicitly:
791 <scons_example name="buildersbuiltin_ex3">
792 <file name="SConstruct" printme="1">
793 env = Environment(TARFLAGS='-c -z', TARSUFFIX='.tgz')
794 env.Tar('out', 'directory')
796 <file name="directory/file">
801 <scons_output example="buildersbuiltin_ex3" os="posix" suffix="1">
802 <scons_output_command>scons -Q .</scons_output_command>
808 <title>The &Zip; Builder</title>
812 The &b-link-Zip; Builder object creates archives of files
813 and/or directory trees in the ZIP file format.
814 Python versions 1.6 or later
815 contain an internal &zipfile; module
816 that &SCons; will use.
817 In this case, given the following
822 <scons_example name="buildersbuiltin_ex4">
823 <file name="SConstruct" printme="1">
825 env.Zip('out', ['file1', 'file2'])
837 Your output will reflect the fact
838 that an internal Python function
839 is being used to create the output ZIP archive:
843 <scons_output example="buildersbuiltin_ex4" os="posix" suffix="1">
844 <scons_output_command>scons -Q .</scons_output_command>
856 &SCons; provides Builder objects
857 for creating various types of Java output files.
862 <title>Building Class Files: the &Java; Builder</title>
866 The &b-link-Java; builder takes one or more input
867 <filename>.java</filename> files
868 and turns them into one or more
869 <filename>.class</filename> files
870 Unlike most builders, however,
871 the &Java; builder takes
872 target and source <emphasis>directories</emphasis>,
879 env.Java(target='classes', source='src')
884 The &Java; builder will then
885 search the specified source directory
886 tree for all <filename>.java</filename> files,
887 and pass any out-of-date
898 <title>The &Jar; Builder</title>
902 XXX The &Jar; builder object
908 env.Java(target='classes', source='src')
909 env.Jar(target='', source='classes')
919 <title>Building C header and stub files: the &JavaH; Builder</title>
928 XXX JavaH() programlisting
938 <title>Building RMI stub and skeleton class files: the &RMIC; Builder</title>
947 XXX RMIC() programlisting