summaryrefslogtreecommitdiffstats
path: root/doc/user/simple.xml
blob: 34a3761ee336c940926ca38578cd1de2e90f98e6 (plain)
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
<?xml version='1.0'?>

<!--
SPDX-FileCopyrightText: Copyright The SCons Foundation (https://scons.org)
SPDX-License-Identifier: MIT
-->

<!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-simple"
         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>Simple Builds</title>

 <para>

 The single most important thing you do when writing a
 build system for your project is to describe the "what":
 what you want to build, and which files you want to build it from.
 And, in fact, simpler builds may need no more.
 In this chapter, you will see several examples of
 very simple build configurations using &SCons;,
 which will demonstrate how easy &SCons; makes it to
 build programs on different types of systems.

 </para>

 <section id="sect-build-simple">
 <title>Building Simple C / C++ Programs</title>

   <para>

   Here's the ubiquitous
   <ulink url="https://en.wikipedia.org/wiki/%22Hello,_World!%22_program">
   "Hello, World!"</ulink> program in C:

   </para>

   <programlisting>
#include &lt;stdio.h&gt;

int
main()
{
        printf("Hello, world!\n");
}
   </programlisting>

   <para>

   And here's how to build it using &SCons;.
   Save the code above into <filename>hello.c</filename>,
   and enter the following into a file named &SConstruct;:

   </para>

   <scons_example name="simple_ex1">
      <file name="SConstruct" printme="1">
Program('hello.c')
      </file>
      <file name="hello.c">
int main() { printf("Hello, world!\n"); }
      </file>
   </scons_example>

   <para>

   This minimal build file gives
   &SCons; three key pieces of information:
   what you want to build (a program);
   what you want to call that program (its
   base name will be <filename>hello</filename>),
   and the source file you want it built from
   (the <filename>hello.c</filename> file).
   &b-link-Program; is a <firstterm>&Builder;</firstterm>,
   an &SCons; function that you use to instruct
   &SCons; about the "what" of your build.

   </para>

   <para>

   That's it.  Now run the &scons; command to build the program.
   On a POSIX-compliant system like Linux or UNIX,
   you'll see something like:

   </para>

   <scons_output example="simple_ex1" os="posix" suffix="1">
      <scons_output_command>scons</scons_output_command>
   </scons_output>

   <para>

   On a Windows system with the &MSVC; compiler,
   you'll see something like:

   </para>

   <scons_output example="simple_ex1" os="win32" suffix="2">
      <scons_output_command>scons</scons_output_command>
   </scons_output>

   <para>

   Notice that &SCons; deduced quite a bit here: it figured
   out the name of the program to build, including operating
   system specific suffixes (&hello; or &hello_exe;), based off
   the basename of the source file; it knows an intermediate
   object file should be built (&hello_o; or &hello_obj;);
   and it knows how to build those things using the compiler
   that is appropriate on the system you're using.
   It was not necessary to instruct &SCons; about any of those
   details.
   This is an example of how &SCons;
   makes it easy to write portable software builds.

   </para>

   <para>

   For the programming languages &SCons; already knows about,
   it will mostly just figure it out.
   Here's the "Hello, World!" example in Fortran:

   </para>

   <programlisting>
program hello
  print *, 'Hello, World!'
end program hello
   </programlisting>

   <sconstruct>
Program('hello', 'hello.f90')
   </sconstruct>

   <!--  # SConsExample doesn't speak Fortran, will show as "cc"
   <scons_example name="simple_ex1f">
      <file name="SConstruct" printme="1">
Program('hello', 'hello.f90')
      </file>
      <file name="hello.f90">
program hello
  print *, 'Hello, World!'
end program hello
      </file>
   </scons_example>

   <scons_output example="simple_ex1f" os="posix" suffix="1">
      <scons_output_command>scons</scons_output_command>
   </scons_output-->

   <!-- so just hardcode it: -->
   <screen>
$ <userinput>scons</userinput>
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
gfortran -o hello.o -c hello.f90
gfortran -o hello hello.o
scons: done building targets.
   </screen>

 </section>

 <section id="sect-building-object">
 <title>Building Object Files</title>

   <para>

   The &b-link-Program; builder is only one of
   many builders (also called a <firstterm>&builder_method;</firstterm>)
   that &SCons; provides to build different types of files.
   Another is the &b-link-Object; builder method,
   which tells &SCons; to build an object file
   from the specified source file:

   </para>

   <scons_example name="simple_Object">
      <file name="SConstruct" printme="1">
Object('hello.c')
      </file>
      <file name="hello.c">
int main() { printf("Hello, world!\n"); }
      </file>
   </scons_example>

   <para>

   Now when you run the &scons; command to build the program,
   it will build just the &hello_o; object file on a POSIX system:

   </para>

   <scons_output example="simple_Object" os="posix" suffix="1">
      <scons_output_command>scons</scons_output_command>
   </scons_output>

   <para>

   And just the &hello_obj; object file
   on a Windows system (with the &MSVC; compiler):

   </para>

   <scons_output example="simple_Object" os="win32" suffix="2">
      <scons_output_command>scons</scons_output_command>
   </scons_output>

   <para>

   (Note that this guide will not continue to provide duplicate side-by-side
   POSIX and Windows output for all of the examples.
   Just keep in mind that, unless otherwise specified,
   any of the examples should work equally well on both types of systems.)

   </para>

 </section>

 <section id="sect-building-java">
 <title>Simple Java Builds</title>

   <para>

   &SCons; also makes building with Java extremely easy.
   Unlike the &b-link-Program; and &b-link-Object; builder methods,
   however, the &b-link-Java; builder method
   requires that you specify
   the name of a destination directory in which
   you want the class files placed,
   followed by the source directory
   in which the <filename>.java</filename> files live:

   </para>

   <scons_example name="simple_java">
     <file name="SConstruct" printme="1">
Java('classes', 'src')
     </file>
     <file name="src/hello.java">
public class Example1
{
  public static void main(String[] args)
  {
    System.out.println("Hello Java world!\n");
  }
}
     </file>
   </scons_example>

   <para>

   If the <filename>src</filename> directory
   contains a single <filename>hello.java</filename> file,
   then the output from running the &scons; command
   would look something like this
   (on a POSIX system):

   </para>

   <scons_output example="simple_java" os="posix" suffix="1">
      <scons_output_command>scons</scons_output_command>
   </scons_output>

   <para>

   Java builds will be covered in much more detail,
   including building a Java archive (<filename>.jar</filename>)
   and other types of files,
   in <xref linkend="chap-java"></xref>.

   </para>

 </section>

 <section id="sect-building-clean">
 <title>Cleaning Up After a Build</title>

   <para>

   For cleaning up your build tree, &SCons; provides a
   "clean" mode, selected by the
   <option>-c</option> or <option>--clean</option>
   option when you invoke &SCons;.
   &SCons; selects the same set of targets it would in build mode,
   but instead of building, removes them.
   That means you can control what is cleaned
   in exactly the same way as you control what gets built.
   If you build the C example above
   and then invoke <userinput>scons -c</userinput>
   afterwards, the output on POSIX looks like:

   </para>

   <scons_example name="simple_clean">
      <file name="SConstruct">
Program('hello.c')
      </file>
      <file name="hello.c">
int main() { printf("Hello, world!\n"); }
      </file>
   </scons_example>

   <scons_output example="simple_clean" os="posix" suffix="1">
      <scons_output_command>scons</scons_output_command>
      <scons_output_command>scons -c</scons_output_command>
   </scons_output>

   <para>

   And the output on Windows looks like:

   </para>

   <scons_output example="simple_clean" os="win32" suffix="2">
      <scons_output_command>scons</scons_output_command>
      <scons_output_command>scons -c</scons_output_command>
   </scons_output>

   <para>

   Notice that &SCons; changes its output to tell you that it
   is <computeroutput>Cleaning targets ...</computeroutput> and
   <computeroutput>done cleaning targets.</computeroutput>

   </para>

 </section>

 <section id="sect-sconstruct-file">
 <title>The &SConstruct; File</title>

   <para>

   If you're used to build systems like &Make;
   you've already figured out that the &SConstruct; file
   is the &SCons; equivalent of a &Makefile;.
   That is, the &SConstruct; file is the input file
   that &SCons; reads to control the build.

   </para>

   <section id="sect-sconstruct-python">
   <title>&SConstruct; Files Are Python Scripts</title>

     <para>

     There is, however, an important difference between
     an &SConstruct; file and a &Makefile;:
     the &SConstruct; file is actually a &Python; script.
     If you're not already familiar with &Python;, don't worry.
     This User's Guide will introduce you step-by-step
     to the relatively small amount of &Python; you'll
     need to know to be able to use &SCons; effectively.
     And &Python; is very easy to learn.

     </para>

     <para>

     One aspect of using &Python; as the
     scripting language is that you can put comments
     in your &SConstruct; file using &Python;'s commenting convention:
     everything between a <literal>#</literal> character
     and the end of the line will be ignored
     (unless the character appears inside a string constant).

     </para>

     <sconstruct>
# Arrange to build the "hello" program.
Program("hello.c")    # "hello.c" is the source file.
Program("#goodbye.c") # the # in "#goodbye" does not indicate a comment
     </sconstruct>

     <para>

     You'll see throughout the remainder of this Guide
     that being able to use the power of a
     real scripting language
     can greatly simplify the solutions
     to complex requirements of real-world builds.

     </para>

   </section>

   <section id="sect-order-independent">
   <title>&SCons; Builders Are Order-Independent</title>

     <para>

     One important way in which the &SConstruct;
     file is not exactly like a normal &Python; script,
     and is more like a &Makefile;,
     is that the order in which
     the &SCons; Builder functions are called in
     the &SConstruct; file
     does <emphasis>not</emphasis>
     affect the order in which &SCons;
     actually builds the programs and object files
     you want it to build.
     <footnote><para>In programming parlance,
     the &SConstruct; file is
     <emphasis>declarative</emphasis>,
     meaning you tell &SCons; what you want done
     and let it figure out the order in which to do it,
     rather than strictly <emphasis>imperative</emphasis>,
     where you specify explicitly the order in
     which to do things.
     </para>
     </footnote>.
     In other words, when you call the &b-link-Program; builder
     (or any other builder method),
     you're not telling &SCons; to build
     the program at that moment.
     Instead, you're telling &SCons; what you want accomplished,
     and it's up to &SCons; to figure out how to do that, and to
     take those steps if/when it's necessary.
     you'll learn more about how
     &SCons; decides when building or rebuilding a target
     is necessary in <xref linkend="chap-depends"></xref>, below.

     </para>

     <para>

     &SCons; reflects this distinction between
     <emphasis>calling a builder method like</emphasis> &b-Program;
     and <emphasis>actually building the program</emphasis>
     by printing the status messages that indicate
     when it's "just reading" the &SConstruct; file,
     and when it's actually building the target files.
     This is to make it clear when &SCons; is
     executing the &Python; statements that make up the &SConstruct; file,
     and when &SCons; is actually executing the
     commands or other actions to
     build the necessary files.

     </para>

     <para>

     Let's clarify this with an example.
     &Python; has a <function>print</function> function that
     prints a string of characters to the screen.
     If you put <function>print</function> calls around
     the calls to the &b-Program; builder method:

     </para>

     <scons_example name="simple_declarative">
       <file name="SConstruct" printme="1">
print("Calling Program('hello.c')")
Program('hello.c')
print("Calling Program('goodbye.c')")
Program('goodbye.c')
print("Finished calling Program()")
       </file>
       <file name="hello.c">
int main() { printf("Hello, world!\n"); }
       </file>
       <file name="goodbye.c">
int main() { printf("Goodbye, world!\n"); }
       </file>
     </scons_example>

     <para>

     Then when you execute &SCons;,
     you will see the output from calling the <function>print</function>
     function in between the messages about
     reading the &SConscript; files,
     indicating that is when the
     &Python; statements are being executed:

     </para>

     <scons_output example="simple_declarative" os="posix" suffix="1">
       <scons_output_command>scons</scons_output_command>
     </scons_output>

     <para>

     Notice that &SCons; built the &goodbye; program first,
     even though the "reading &SConscript;" output
     shows that <function>Program('hello.c')</function> was called
     first in the &SConstruct; file.

     </para>

   </section>

 </section>

 <section id="sect_building_simple">
 <title>Making the &SCons; Output Less Verbose</title>

   <para>

   You've already seen how &SCons; prints
   some messages about what it's doing,
   surrounding the actual commands used to build the software:

   </para>

   <scons_output example="simple_ex1" os="win32" suffix="3">
      <scons_output_command>scons</scons_output_command>
   </scons_output>

   <para>

   These messages emphasize the
   order in which &SCons; does its work:
   all of the configuration files
   (generically referred to as &SConscript; files)
   are read and executed first,
   and only then are the target files built.
   Among other benefits, these messages help to distinguish between
   errors that occur while the configuration files are read,
   and errors that occur while targets are being built.

   </para>

   <para>

   One drawback, of course, is that these messages clutter the output.
   Fortunately, they're easily disabled by using
   the &Q; option when invoking &SCons;:

   </para>

   <scons_output example="simple_ex1" os="win32" suffix="4">
      <scons_output_command>scons -Q</scons_output_command>
   </scons_output>

   <para>

   So this User's Guide can focus
   on what &SCons; is actually doing,
   the &Q; option will be used
   to remove these messages from the
   output of all the remaining examples in this Guide.

   </para>

 </section>

</chapter>