summaryrefslogtreecommitdiffstats
path: root/doc/user/hierarchy.sgml
blob: e39da8f2d7929c49b0749d552f9a7263b76b92c2 (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
<!--

  Copyright (c) 2001, 2002, 2003 Steven Knight

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be included
  in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

-->

<!--

=head2 A hierarchy of build scripts

A larger build, in Cons, is organized by creating a hierarchy of B<build
scripts>. At the top of the tree is a script called F<Construct>. The rest
of the scripts, by convention, are each called F<Conscript>. These scripts
are connected together, very simply, by the C<Build>, C<Export>, and
C<Import> commands.


=head2 The Build command

By default, Cons does not change its working directory to the directory
containing a subsidiary F<Conscript> file it is including.  This behavior
can be enabled for a build by specifying, in the top-level F<Construct>
file:

  Conscript_chdir 1;

When enabled, Cons will change to the subsidiary F<Conscript> file's
containing directory while reading in that file, and then change back
to the top-level directory once the file has been processed.

It is expected that this behavior will become the default in some future
version of Cons.  To prepare for this transition, builds that expect
Cons to remain at the top of the build while it reads in a subsidiary
F<Conscript> file should explicitly disable this feature as follows:

  Conscript_chdir 0;

=head2 Relative, top-relative, and absolute file names

You may have noticed that the file names specified to the Build command are
relative to the location of the script it is invoked from. This is generally
true for other filename arguments to other commands, too, although we might
as well mention here that if you begin a file name with a hash mark, ``#'',
then that file is interpreted relative to the top-level directory (where the
F<Construct> file resides). And, not surprisingly, if you begin it with ``/'',
then it is considered to be an absolute pathname. This is true even on
systems which use a back slash rather than a forward slash to name absolute
paths.

(There is another file prefix, ``!'', that is interpreted specially by
Cons.  See discussion of the C<Link> command, below, for details.)


=head2 Using modules in build scripts

You may pull modules into each F<Conscript> file using the normal Perl
C<use> or C<require> statements:

  use English;
  require My::Module;

Each C<use> or C<require> only affects the one F<Conscript> file in which
it appears.  To use a module in multiple F<Conscript> files, you must
put a C<use> or C<require> statement in each one that needs the module.


=head2 Scope of variables

The top-level F<Construct> file and all F<Conscript> files begin life in
a common, separate Perl package.  B<Cons> controls the symbol table for
the package so that, the symbol table for each script is empty, except
for the F<Construct> file, which gets some of the command line arguments.
All of the variables that are set or used, therefore, are set by the
script itself, not by some external script.

Variables can be explicitly B<imported> by a script from its parent
script. To import a variable, it must have been B<exported> by the parent
and initialized (otherwise an error will occur).


=head2 The Export command

The C<Export> command is used as in the following example:

  $env = new cons();
  $INCLUDE = "#export/include";
  $LIB = "#export/lib";
  Export qw( env INCLUDE LIB );
  Build qw( util/Conscript );

The values of the simple variables mentioned in the C<Export> list will be
squirreled away by any subsequent C<Build> commands. The C<Export> command
will only export Perl B<scalar> variables, that is, variables whose name
begins with C<$>. Other variables, objects, etc. can be exported by
reference, but all scripts will refer to the same object, and this object
should be considered to be read-only by the subsidiary scripts and by the
original exporting script. It's acceptable, however, to assign a new value
to the exported scalar variable, that won't change the underlying variable
referenced. This sequence, for example, is OK:

  $env = new cons();
  Export qw( env INCLUDE LIB );
  Build qw( util/Conscript );
  $env = new cons(CFLAGS => '-O');
  Build qw( other/Conscript );

It doesn't matter whether the variable is set before or after the C<Export>
command. The important thing is the value of the variable at the time the
C<Build> command is executed. This is what gets squirreled away. Any
subsequent C<Export> commands, by the way, invalidate the first: you must
mention all the variables you wish to export on each C<Export> command.


=head2 The Import command

Variables exported by the C<Export> command can be imported into subsidiary
scripts by the C<Import> command. The subsidiary script always imports
variables directly from the superior script. Consider this example:

  Import qw( env INCLUDE );

This is only legal if the parent script exported both C<$env> and
C<$INCLUDE>. It also must have given each of these variables values. It is
OK for the subsidiary script to only import a subset of the exported
variables (in this example, C<$LIB>, which was exported by the previous
example, is not imported).

All the imported variables are automatically re-exported, so the sequence:

  Import qw ( env INCLUDE );
  Build qw ( beneath-me/Conscript );

will supply both C<$env> and C<$INCLUDE> to the subsidiary file. If only
C<$env> is to be exported, then the following will suffice:

  Import qw ( env INCLUDE );
  Export qw ( env );
  Build qw ( beneath-me/Conscript );

Needless to say, the variables may be modified locally before invoking
C<Build> on the subsidiary script.


=head2 Build script evaluation order

The only constraint on the ordering of build scripts is that superior
scripts are evaluated before their inferior scripts. The top-level
F<Construct> file, for instance, is evaluated first, followed by any
inferior scripts. This is all you really need to know about the evaluation
order, since order is generally irrelevant. Consider the following C<Build>
command:

  Build qw(
	drivers/display/Conscript
	drivers/mouse/Conscript
	parser/Conscript
	utilities/Conscript
  );

We've chosen to put the script names in alphabetical order, simply because
that's the most convenient for maintenance purposes. Changing the order will
make no difference to the build.

-->

  <para>

  The source code for large software projects
  rarely stays in a single directory,
  but is nearly always divided into a
  hierarchy of directories.
  Organizing a large software build using &SCons;
  involves creating a hierarchy of build scripts
  using the &SConscript; function.

  </para>

  <section>
  <title>&SConscript; Files</title>

    <para>

    As we've already seen,
    the build script at the top of the tree is called &SConstruct;.
    The top-level &SConstruct; file can
    use the &SConscript; function to
    include other subsidiary scripts in the build.
    These subsidiary scripts can, in turn,
    use the &SConscript; function
    to include still other scripts in the build.
    By convention, these subsidiary scripts are usually
    named &SConscript;.
    For example, a top-level &SConstruct; file might
    arrange for four subsidiary scripts to be included
    in the build as follows:

    </para>

    <programlisting>
      SConscript(['drivers/display/SConscript',
                  'drivers/mouse/SConscript',
                  'parser/SConscript',
                  'utilities/SConscript'])
    </programlisting>

    <para>

    In this case, the &SConstruct; file
    lists all of the &SConscript; files in the build explicitly.
    (Note, however, that not every directory in the tree
    necessarily has an &SConscript; file.)
    Alternatively, the <literal>drivers</literal>
    subdirectory might contain an intermediate
    &SConscript; file,
    in which case the &SConscript; call in
    the top-level &SConstruct; file
    would look like:

    </para>

    <programlisting>
      SConscript(['drivers/SConscript',
                  'parser/SConscript',
                  'utilities/SConscript'])
    </programlisting>

    <para>

    And the subsidiary &SConscript; file in the
    <literal>drivers</literal> subdirectory
    would look like:

    </para>

    <programlisting>
      SConscript(['display/SConscript',
                  'mouse/SConscript'])
    </programlisting>

    <para>

    Whether you list all of the &SConscript; files in the
    top-level &SConstruct; file,
    or place a subsidiary &SConscript; file in
    intervening directories,
    or use some mix of the two schemes,
    is up to you and the needs of your software.

    </para>

  </section>

  <section>
  <title>Path Names Are Relative to the &SConscript; Directory</title>

    <para>

    XXX

    </para>

    <programlisting>
      SConscript(['display/SConscript',
                  'mouse/SConscript'])
    </programlisting>

  </section>

  <section>
  <title>Sharing Environments (and Other Variables) Between &SConscript; Files</title>

    <para>

    XXX

    </para>

    <programlisting>
      SConscript(['display/SConscript',
                  'mouse/SConscript'])
    </programlisting>

    <section>
    <title>Exporting Variables</title>

      <para>

      XXX

      </para>

      <programlisting>
        SConscript(['display/SConscript',
                    'mouse/SConscript'])
      </programlisting>

    </section>

    <section>
    <title>Importing Variables</title>

      <para>

      XXX

      </para>

      <programlisting>
        SConscript(['display/SConscript',
                   'mouse/SConscript'])
      </programlisting>

    </section>

    <section>
    <title>Returning Values</title>

      <para>

      XXX

      </para>

      <programlisting>
        SConscript(['display/SConscript',
                    'mouse/SConscript'])
      </programlisting>

    </section>

  </section>