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">
25 <chapter id="chap-simple"
26 xmlns="http://www.scons.org/dbxsd/v1.0"
27 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
28 xsi:schemaLocation="http://www.scons.org/dbxsd/v1.0 http://www.scons.org/dbxsd/v1.0/scons.xsd">
30 <title>Simple Builds</title>
34 The single most important thing you do when writing a
35 build system for your project is to describe the "what":
36 what you want to build, and which files you want to build it from.
37 And, in fact, simpler builds may need no more.
38 In this chapter, you will see several examples of
39 very simple build configurations using &SCons;,
40 which will demonstrate how easy &SCons; makes it to
41 build programs on different types of systems.
45 <section id="sect-build-simple">
46 <title>Building Simple C / C++ Programs</title>
51 <ulink url="https://en.wikipedia.org/wiki/%22Hello,_World!%22_program">
52 "Hello, World!"</ulink> program in C:
57 #include <stdio.h>
62 printf("Hello, world!\n");
68 And here's how to build it using &SCons;.
69 Save the code above into <filename>hello.c</filename>,
70 and enter the following into a file named &SConstruct;:
74 <scons_example name="simple_ex1">
75 <file name="SConstruct" printme="1">
79 int main() { printf("Hello, world!\n"); }
85 This minimal build file gives
86 &SCons; three key pieces of information:
87 what you want to build (a program);
88 what you want to call that program (its
89 base name will be <filename>hello</filename>),
90 and the source file you want it built from
91 (the <filename>hello.c</filename> file).
92 &b-link-Program; is a <firstterm>&Builder;</firstterm>,
93 an &SCons; function that you use to instruct
94 &SCons; about the "what" of your build.
100 That's it. Now run the &scons; command to build the program.
101 On a POSIX-compliant system like Linux or UNIX,
102 you'll see something like:
106 <scons_output example="simple_ex1" os="posix" suffix="1">
107 <scons_output_command>scons</scons_output_command>
112 On a Windows system with the &MSVC; compiler,
113 you'll see something like:
117 <scons_output example="simple_ex1" os="win32" suffix="2">
118 <scons_output_command>scons</scons_output_command>
123 Notice that &SCons; deduced quite a bit here: it figured
124 out the name of the program to build, including operating
125 system specific suffixes (&hello; or &hello_exe;), based off
126 the basename of the source file; it knows an intermediate
127 object file should be built (&hello_o; or &hello_obj;);
128 and it knows how to build those things using the compiler
129 that is appropriate on the system you're using.
130 It was not necessary to instruct &SCons; about any of those
132 This is an example of how &SCons;
133 makes it easy to write portable software builds.
139 For the programming languages &SCons; already knows about,
140 it will mostly just figure it out.
141 Here's the "Hello, World!" example in Fortran:
147 print *, 'Hello, World!'
152 Program('hello', 'hello.f90')
155 <!-- # SConsExample doesn't speak Fortran, will show as "cc"
156 <scons_example name="simple_ex1f">
157 <file name="SConstruct" printme="1">
158 Program('hello', 'hello.f90')
160 <file name="hello.f90">
162 print *, 'Hello, World!'
167 <scons_output example="simple_ex1f" os="posix" suffix="1">
168 <scons_output_command>scons</scons_output_command>
171 <!-- so just hardcode it: -->
173 $ <userinput>scons</userinput>
174 scons: Reading SConscript files ...
175 scons: done reading SConscript files.
176 scons: Building targets ...
177 gfortran -o hello.o -c hello.f90
178 gfortran -o hello hello.o
179 scons: done building targets.
184 <section id="sect-building-object">
185 <title>Building Object Files</title>
189 The &b-link-Program; builder is only one of
190 many builders (also called a <firstterm>&builder_method;</firstterm>)
191 that &SCons; provides to build different types of files.
192 Another is the &b-link-Object; builder method,
193 which tells &SCons; to build an object file
194 from the specified source file:
198 <scons_example name="simple_Object">
199 <file name="SConstruct" printme="1">
202 <file name="hello.c">
203 int main() { printf("Hello, world!\n"); }
209 Now when you run the &scons; command to build the program,
210 it will build just the &hello_o; object file on a POSIX system:
214 <scons_output example="simple_Object" os="posix" suffix="1">
215 <scons_output_command>scons</scons_output_command>
220 And just the &hello_obj; object file
221 on a Windows system (with the &MSVC; compiler):
225 <scons_output example="simple_Object" os="win32" suffix="2">
226 <scons_output_command>scons</scons_output_command>
231 (Note that this guide will not continue to provide duplicate side-by-side
232 POSIX and Windows output for all of the examples.
233 Just keep in mind that, unless otherwise specified,
234 any of the examples should work equally well on both types of systems.)
240 <section id="sect-building-java">
241 <title>Simple Java Builds</title>
245 &SCons; also makes building with Java extremely easy.
246 Unlike the &b-link-Program; and &b-link-Object; builder methods,
247 however, the &b-link-Java; builder method
248 requires that you specify
249 the name of a destination directory in which
250 you want the class files placed,
251 followed by the source directory
252 in which the <filename>.java</filename> files live:
256 <scons_example name="simple_java">
257 <file name="SConstruct" printme="1">
258 Java('classes', 'src')
260 <file name="src/hello.java">
261 public class Example1
263 public static void main(String[] args)
265 System.out.println("Hello Java world!\n");
273 If the <filename>src</filename> directory
274 contains a single <filename>hello.java</filename> file,
275 then the output from running the &scons; command
276 would look something like this
281 <scons_output example="simple_java" os="posix" suffix="1">
282 <scons_output_command>scons</scons_output_command>
287 Java builds will be covered in much more detail,
288 including building a Java archive (<filename>.jar</filename>)
289 and other types of files,
290 in <xref linkend="chap-java"></xref>.
296 <section id="sect-building-clean">
297 <title>Cleaning Up After a Build</title>
301 For cleaning up your build tree, &SCons; provides a
302 "clean" mode, selected by the
303 <option>-c</option> or <option>--clean</option>
304 option when you invoke &SCons;.
305 &SCons; selects the same set of targets it would in build mode,
306 but instead of building, removes them.
307 That means you can control what is cleaned
308 in exactly the same way as you control what gets built.
309 If you build the C example above
310 and then invoke <userinput>scons -c</userinput>
311 afterwards, the output on POSIX looks like:
315 <scons_example name="simple_clean">
316 <file name="SConstruct">
319 <file name="hello.c">
320 int main() { printf("Hello, world!\n"); }
324 <scons_output example="simple_clean" os="posix" suffix="1">
325 <scons_output_command>scons</scons_output_command>
326 <scons_output_command>scons -c</scons_output_command>
331 And the output on Windows looks like:
335 <scons_output example="simple_clean" os="win32" suffix="2">
336 <scons_output_command>scons</scons_output_command>
337 <scons_output_command>scons -c</scons_output_command>
342 Notice that &SCons; changes its output to tell you that it
343 is <computeroutput>Cleaning targets ...</computeroutput> and
344 <computeroutput>done cleaning targets.</computeroutput>
350 <section id="sect-sconstruct-file">
351 <title>The &SConstruct; File</title>
355 If you're used to build systems like &Make;
356 you've already figured out that the &SConstruct; file
357 is the &SCons; equivalent of a &Makefile;.
358 That is, the &SConstruct; file is the input file
359 that &SCons; reads to control the build.
363 <section id="sect-sconstruct-python">
364 <title>&SConstruct; Files Are Python Scripts</title>
368 There is, however, an important difference between
369 an &SConstruct; file and a &Makefile;:
370 the &SConstruct; file is actually a &Python; script.
371 If you're not already familiar with &Python;, don't worry.
372 This User's Guide will introduce you step-by-step
373 to the relatively small amount of &Python; you'll
374 need to know to be able to use &SCons; effectively.
375 And &Python; is very easy to learn.
381 One aspect of using &Python; as the
382 scripting language is that you can put comments
383 in your &SConstruct; file using &Python;'s commenting convention:
384 everything between a <literal>#</literal> character
385 and the end of the line will be ignored
386 (unless the character appears inside a string constant).
391 # Arrange to build the "hello" program.
392 Program("hello.c") # "hello.c" is the source file.
393 Program("#goodbye.c") # the # in "#goodbye" does not indicate a comment
398 You'll see throughout the remainder of this Guide
399 that being able to use the power of a
400 real scripting language
401 can greatly simplify the solutions
402 to complex requirements of real-world builds.
408 <section id="sect-order-independent">
409 <title>&SCons; Builders Are Order-Independent</title>
413 One important way in which the &SConstruct;
414 file is not exactly like a normal &Python; script,
415 and is more like a &Makefile;,
416 is that the order in which
417 the &SCons; Builder functions are called in
418 the &SConstruct; file
419 does <emphasis>not</emphasis>
420 affect the order in which &SCons;
421 actually builds the programs and object files
422 you want it to build.
423 <footnote><para>In programming parlance,
424 the &SConstruct; file is
425 <emphasis>declarative</emphasis>,
426 meaning you tell &SCons; what you want done
427 and let it figure out the order in which to do it,
428 rather than strictly <emphasis>imperative</emphasis>,
429 where you specify explicitly the order in
433 In other words, when you call the &b-link-Program; builder
434 (or any other builder method),
435 you're not telling &SCons; to build
436 the program at that moment.
437 Instead, you're telling &SCons; what you want accomplished,
438 and it's up to &SCons; to figure out how to do that, and to
439 take those steps if/when it's necessary.
440 you'll learn more about how
441 &SCons; decides when building or rebuilding a target
442 is necessary in <xref linkend="chap-depends"></xref>, below.
448 &SCons; reflects this distinction between
449 <emphasis>calling a builder method like</emphasis> &b-Program;
450 and <emphasis>actually building the program</emphasis>
451 by printing the status messages that indicate
452 when it's "just reading" the &SConstruct; file,
453 and when it's actually building the target files.
454 This is to make it clear when &SCons; is
455 executing the &Python; statements that make up the &SConstruct; file,
456 and when &SCons; is actually executing the
457 commands or other actions to
458 build the necessary files.
464 Let's clarify this with an example.
465 &Python; has a <function>print</function> function that
466 prints a string of characters to the screen.
467 If you put <function>print</function> calls around
468 the calls to the &b-Program; builder method:
472 <scons_example name="simple_declarative">
473 <file name="SConstruct" printme="1">
474 print("Calling Program('hello.c')")
476 print("Calling Program('goodbye.c')")
478 print("Finished calling Program()")
480 <file name="hello.c">
481 int main() { printf("Hello, world!\n"); }
483 <file name="goodbye.c">
484 int main() { printf("Goodbye, world!\n"); }
490 Then when you execute &SCons;,
491 you will see the output from calling the <function>print</function>
492 function in between the messages about
493 reading the &SConscript; files,
494 indicating that is when the
495 &Python; statements are being executed:
499 <scons_output example="simple_declarative" os="posix" suffix="1">
500 <scons_output_command>scons</scons_output_command>
505 Notice that &SCons; built the &goodbye; program first,
506 even though the "reading &SConscript;" output
507 shows that <function>Program('hello.c')</function> was called
508 first in the &SConstruct; file.
516 <section id="sect_building_simple">
517 <title>Making the &SCons; Output Less Verbose</title>
521 You've already seen how &SCons; prints
522 some messages about what it's doing,
523 surrounding the actual commands used to build the software:
527 <scons_output example="simple_ex1" os="win32" suffix="3">
528 <scons_output_command>scons</scons_output_command>
533 These messages emphasize the
534 order in which &SCons; does its work:
535 all of the configuration files
536 (generically referred to as &SConscript; files)
537 are read and executed first,
538 and only then are the target files built.
539 Among other benefits, these messages help to distinguish between
540 errors that occur while the configuration files are read,
541 and errors that occur while targets are being built.
547 One drawback, of course, is that these messages clutter the output.
548 Fortunately, they're easily disabled by using
549 the &Q; option when invoking &SCons;:
553 <scons_output example="simple_ex1" os="win32" suffix="4">
554 <scons_output_command>scons -Q</scons_output_command>
559 So this User's Guide can focus
560 on what &SCons; is actually doing,
561 the &Q; option will be used
562 to remove these messages from the
563 output of all the remaining examples in this Guide.