Standardize license header on in-use doc files [skip appveyor]
[scons.git] / doc / user / simple.xml
blob7a065136c1c57fe3444bc4a4d51fca6b832b570c
1 <?xml version='1.0'?>
3 <!--
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.
9 -->
11 <!DOCTYPE sconsdoc [
12     <!ENTITY % scons SYSTEM "../scons.mod">
13     %scons;
15     <!ENTITY % builders-mod SYSTEM "../generated/builders.mod">
16     %builders-mod;
17     <!ENTITY % functions-mod SYSTEM "../generated/functions.mod">
18     %functions-mod;
19     <!ENTITY % tools-mod SYSTEM "../generated/tools.mod">
20     %tools-mod;
21     <!ENTITY % variables-mod SYSTEM "../generated/variables.mod">
22     %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>
32  <para>
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.
43  </para>
45  <section id="sect-build-simple">
46  <title>Building Simple C / C++ Programs</title>
48    <para>
50    Here's the ubiquitous
51    <ulink url="https://en.wikipedia.org/wiki/%22Hello,_World!%22_program">
52    "Hello, World!"</ulink> program in C:
54    </para>
56    <programlisting>
57 #include &lt;stdio.h&gt;
59 int
60 main()
62         printf("Hello, world!\n");
64    </programlisting>
66    <para>
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;:
72    </para>
74    <scons_example name="simple_ex1">
75       <file name="SConstruct" printme="1">
76 Program('hello.c')
77       </file>
78       <file name="hello.c">
79 int main() { printf("Hello, world!\n"); }
80       </file>
81    </scons_example>
83    <para>
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.
96    </para>
98    <para>
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:
104    </para>
106    <scons_output example="simple_ex1" os="posix" suffix="1">
107       <scons_output_command>scons</scons_output_command>
108    </scons_output>
110    <para>
112    On a Windows system with the &MSVC; compiler,
113    you'll see something like:
115    </para>
117    <scons_output example="simple_ex1" os="win32" suffix="2">
118       <scons_output_command>scons</scons_output_command>
119    </scons_output>
121    <para>
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
131    details.
132    This is an example of how &SCons;
133    makes it easy to write portable software builds.
135    </para>
137    <para>
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:
143    </para>
145    <programlisting>
146 program hello
147   print *, 'Hello, World!'
148 end program hello
149    </programlisting>
151    <sconstruct>
152 Program('hello', 'hello.f90')
153    </sconstruct>
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')
159       </file>
160       <file name="hello.f90">
161 program hello
162   print *, 'Hello, World!'
163 end program hello
164       </file>
165    </scons_example>
167    <scons_output example="simple_ex1f" os="posix" suffix="1">
168       <scons_output_command>scons</scons_output_command>
169    </scons_output-->
171    <!-- so just hardcode it: -->
172    <screen>
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.
180    </screen>
182  </section>
184  <section id="sect-building-object">
185  <title>Building Object Files</title>
187    <para>
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:
196    </para>
198    <scons_example name="simple_Object">
199       <file name="SConstruct" printme="1">
200 Object('hello.c')
201       </file>
202       <file name="hello.c">
203 int main() { printf("Hello, world!\n"); }
204       </file>
205    </scons_example>
207    <para>
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:
212    </para>
214    <scons_output example="simple_Object" os="posix" suffix="1">
215       <scons_output_command>scons</scons_output_command>
216    </scons_output>
218    <para>
220    And just the &hello_obj; object file
221    on a Windows system (with the &MSVC; compiler):
223    </para>
225    <scons_output example="simple_Object" os="win32" suffix="2">
226       <scons_output_command>scons</scons_output_command>
227    </scons_output>
229    <para>
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.)
236    </para>
238  </section>
240  <section id="sect-building-java">
241  <title>Simple Java Builds</title>
243    <para>
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:
254    </para>
256    <scons_example name="simple_java">
257      <file name="SConstruct" printme="1">
258 Java('classes', 'src')
259      </file>
260      <file name="src/hello.java">
261 public class Example1
263   public static void main(String[] args)
264   {
265     System.out.println("Hello Java world!\n");
266   }
268      </file>
269    </scons_example>
271    <para>
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
277    (on a POSIX system):
279    </para>
281    <scons_output example="simple_java" os="posix" suffix="1">
282       <scons_output_command>scons</scons_output_command>
283    </scons_output>
285    <para>
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>.
292    </para>
294  </section>
296  <section id="sect-building-clean">
297  <title>Cleaning Up After a Build</title>
299    <para>
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:
313    </para>
315    <scons_example name="simple_clean">
316       <file name="SConstruct">
317 Program('hello.c')
318       </file>
319       <file name="hello.c">
320 int main() { printf("Hello, world!\n"); }
321       </file>
322    </scons_example>
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>
327    </scons_output>
329    <para>
331    And the output on Windows looks like:
333    </para>
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>
338    </scons_output>
340    <para>
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>
346    </para>
348  </section>
350  <section id="sect-sconstruct-file">
351  <title>The &SConstruct; File</title>
353    <para>
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.
361    </para>
363    <section id="sect-sconstruct-python">
364    <title>&SConstruct; Files Are Python Scripts</title>
366      <para>
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.
377      </para>
379      <para>
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).
388      </para>
390      <sconstruct>
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
394      </sconstruct>
396      <para>
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.
404      </para>
406    </section>
408    <section id="sect-order-independent">
409    <title>&SCons; Builders Are Order-Independent</title>
411      <para>
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
430      which to do things.
431      </para>
432      </footnote>.
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.
444      </para>
446      <para>
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.
460      </para>
462      <para>
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:
470      </para>
472      <scons_example name="simple_declarative">
473        <file name="SConstruct" printme="1">
474 print("Calling Program('hello.c')")
475 Program('hello.c')
476 print("Calling Program('goodbye.c')")
477 Program('goodbye.c')
478 print("Finished calling Program()")
479        </file>
480        <file name="hello.c">
481 int main() { printf("Hello, world!\n"); }
482        </file>
483        <file name="goodbye.c">
484 int main() { printf("Goodbye, world!\n"); }
485        </file>
486      </scons_example>
488      <para>
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:
497      </para>
499      <scons_output example="simple_declarative" os="posix" suffix="1">
500        <scons_output_command>scons</scons_output_command>
501      </scons_output>
503      <para>
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.
510      </para>
512    </section>
514  </section>
516  <section id="sect_building_simple">
517  <title>Making the &SCons; Output Less Verbose</title>
519    <para>
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:
525    </para>
527    <scons_output example="simple_ex1" os="win32" suffix="3">
528       <scons_output_command>scons</scons_output_command>
529    </scons_output>
531    <para>
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.
543    </para>
545    <para>
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;:
551    </para>
553    <scons_output example="simple_ex1" os="win32" suffix="4">
554       <scons_output_command>scons -Q</scons_output_command>
555    </scons_output>
557    <para>
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.
565    </para>
567  </section>
569 </chapter>