summaryrefslogtreecommitdiff
path: root/doc/user/external.xml
blob: ceeece084d1fa794f3ce5153513a1553bb0fc9fc (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
<?xml version='1.0'?>
<!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-external"
         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>Using SCons with other build tools</title>

    <!--

     MIT License

     Copyright The SCons Foundation

     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.

    -->

    <para>

        Sometimes a project needs to interact with other projects
        in various ways. For example, many open source projects
        make use of components from other open source projects,
        and want to use those in their released form, not recode
        their builds into &SCons;. As another example, sometimes
        the flexibility and power of &SCons; is useful for managing the
        overall project, but developers might like faster incremental
        builds when making small changes by using a different tool.

    </para>

    <para>

        This chapter shows some techniques for interacting with other
        projects and tools effectively from within &SCons;.

    </para>

    <section>
        <title>Creating a Compilation Database</title>

        <para>

            Tooling to perform analysis and modification
            of source code often needs to know not only the source code
            itself, but also how it will be compiled, as the compilation line
            affects the behavior of macros, includes, etc. &SCons; has a
            record of this information once it has run, in the form of
            Actions associated with the sources, and can emit this information
            so tools can use it.

        </para>

        <para>

            The Clang project has defined a <firstterm>JSON Compilation Database</firstterm>.
            This database is in common use as input into Clang tools
            and many IDEs and editors as well.
            See
            <ulink url="https://clang.llvm.org/docs/JSONCompilationDatabase.html">
                <citetitle>JSON Compilation Database Format Specification</citetitle>
            </ulink>
            for complete information. &SCons; can emit a
            compilation database in this format
            by enabling the &t-link-compilation_db; tool
            and calling the &b-link-CompilationDatabase; builder
            (<emphasis>available since &scons; 4.0</emphasis>).

        </para>

        <para>

            The compilation database can be populated with
            source and output files either with paths relative
            to the top of the build, or using absolute paths.
            This is controlled by
            <envar>COMPILATIONDB_USE_ABSPATH=(True|False)</envar>
            which defaults to <constant>False</constant>.
            The entries in this file can be filtered by using

            <envar>COMPILATIONDB_PATH_FILTER='pattern'</envar>
            where the filter pattern is a string following the &Python;
            <ulink url="https://docs.python.org/3/library/fnmatch.html">
                <systemitem>fnmatch</systemitem>
            </ulink>
            syntax.
            This filtering can be used for outputting different
            build variants to different compilation database files.

        </para>

        <para>

            The following example illustrates generating a compilation
            database containing absolute paths:

        </para>

        <scons_example name="external_cdb_ex1">
            <file name="SConstruct" printme="1">
env = Environment(COMPILATIONDB_USE_ABSPATH=True)
env.Tool('compilation_db')
env.CompilationDatabase()
env.Program('hello.c')
            </file>
            <file name="hello.c">
                int main(int argc, char* argv[])
                {
                return 0;
                }
            </file>
        </scons_example>

        <scons_output example="external_cdb_ex1" suffix="1">
            <scons_output_command>scons -Q</scons_output_command>
        </scons_output>

        <para>
            <filename>compile_commands.json</filename>
            contains:
        </para>

        <programlisting language="json">
[
    {
        "command": "gcc -o hello.o -c hello.c",
        "directory": "/home/user/sandbox",
        "file": "/home/user/sandbox/hello.c",
        "output": "/home/user/sandbox/hello.o"
    }
]
        </programlisting>

        <para>

            Notice that the generated database contains only an entry for
            the <filename>hello.c/hello.o</filename> pairing,
            and nothing for the generation of the final executable
            <filename>hello</filename>
            - the transformation of
            <filename>hello.o</filename>
            to
            <filename>hello</filename>
            does not have any information that affects interpretation
            of the source code,
            so it is not interesting to the compilation database.

        </para>

        <para>

            Although it can be a little surprising at first glance,
            a compilation database target is, like any other target,
            subject to &scons; target selection rules.
            This means if you set a default target (that does not
            include the compilation database), or use command-line
            targets, it might not be selected for building.
            This can actually be an advantage, since you don't
            necessarily want to regenerate the compilation database
            every build.
            The following example
            shows selecting relative paths (the default)
            for output and source,
            and also giving a non-default name to the database.
            In order to be able to generate the database separately from building,
            an alias is set referring to the database,
            which can then be used as a target - here we are only
            building the compilation database target, not the code.

        </para>

        <scons_example name="external_cdb_ex2">
            <file name="SConstruct" printme="1">
env = Environment()
env.Tool('compilation_db')
cdb = env.CompilationDatabase('compile_database.json')
Alias('cdb', cdb)
env.Program('test_main.c')
            </file>
            <file name="test_main.c">
                #include "test_main.h"
                int main(int argc, char* argv[])
                {
                return 0;
                }
            </file>
            <file name="test_main.h">
                /* dummy include file */
            </file>
        </scons_example>

        <scons_output example="external_cdb_ex2" suffix="1">
            <scons_output_command>scons -Q cdb</scons_output_command>
        </scons_output>

        <para>
            <filename>compile_database.json</filename> contains:
        </para>

        <programlisting language="json">
[
    {
        "command": "gcc -o test_main.o -c test_main.c",
        "directory": "/home/user/sandbox",
        "file": "test_main.c",
        "output": "test_main.o"
    }
]
        </programlisting>

        <para>
            The following (incomplete) example shows using filtering
            to separate build variants.
            In the case of using variants,
            you want different compilation databases for each,
            since the build parameters differ, so the code analysis
            needs to see the correct build lines for the 32-bit build
            and 64-bit build hinted at here.
            For simplicity of presentation,
            the example omits the setup details of the variant directories:
        </para>

        <sconstruct>
env = Environment()
env.Tool("compilation_db")

env1 = env.Clone()
env1["COMPILATIONDB_PATH_FILTER"] = "build/linux32/*"
env1.CompilationDatabase("compile_commands-linux32.json")

env2 = env.Clone()
env2["COMPILATIONDB_PATH_FILTER"] = "build/linux64/*"
env2.CompilationDatabase('compile_commands-linux64.json')
        </sconstruct>

        <para>
            <filename>compile_commands-linux32.json</filename>
            contains:
        </para>

        <programlisting language="json">
[
    {
        "command": "gcc -o hello.o -c hello.c",
        "directory": "/home/mats/github/scons/exp/compdb",
        "file": "hello.c",
        "output": "hello.o"
    }
]
        </programlisting>

        <para>
            <filename>compile_commands-linux64.json</filename> contains:
        </para>

        <programlisting language="json">
[
    {
        "command": "gcc -m64 -o build/linux64/test_main.o -c test_main.c",
        "directory": "/home/user/sandbox",
        "file": "test_main.c",
        "output": "build/linux64/test_main.o"
    }
]
        </programlisting>

    </section>

    <section>
        <title>Ninja Build Generator</title>
<note>
    <para>
                This is an experimental new feature.
                It is subject to change and/or removal without a depreciation cycle.
    </para>

    <para>
            Loading the &t-link-ninja; tool into SCons will make significant changes
            in SCons' normal functioning.
    </para>
    <itemizedlist>
        <listitem>
            <para>
        SCons will no longer execute any commands directly and will only create the <filename>build.ninja</filename> and
        run ninja.
            </para>
        </listitem>
        <listitem>
            <para>
        Any targets specified on the command line will be passed along to &ninja;
            </para>
        </listitem>
    </itemizedlist>

    <para>
                To enable this feature you'll need to use one of the following:
    </para>
    <!--        NOTE DO NOT INDENT example_commands CONTENTS AS IT WILL ALTER THE FORMATTING-->
    <example_commands>
# On the command line --experimental=ninja

# Or in your SConstruct
SetOption('experimental', 'ninja')
    </example_commands>
</note>

        <para>
            Ninja is a small build system that tries to be fast
            by not making decisions. &SCons; can at times be slow
            because it makes lots of decisions to carry out its goal
            of "correctness".  The two tools can be paired to benefit
            some build scenarios: by using the &t-link-ninja; tool,
            &SCons; can generate the build file &ninja; uses (basically
            doing the decision-making ahead of time and recording that
            for &ninja;), and can invoke &ninja; to perform a build.
            For situations where relationships are not changing, such
            as edit/build/debug iterations, this works fine and should
            provide considerable speedups for more complex builds.
            The implication is if there are larger changes taking place,
            &ninja; is not as appropriate - but you can always use &SCons;
            to regenerate the build file.  You are NOT advised to use
            this for production builds.
        </para>

        <para>
            To use the &t-link-ninja; tool you'll need to first install the
            &Python; &ninja; package, as the tool depends on being able to do an
            <systemitem>import</systemitem> of the package.
            This can be done via:
        </para>

        <example_commands>
# In a virtualenv, or "python" is the native executable:
python -m pip install ninja

# Windows using Python launcher:
py -m pip install ninja

# Anaconda:
conda install -c conda-forge ninja
        </example_commands>

        <para>
            Reminder that like any non-default tool, you need to initialize it before use
            (e.g. <code>env.Tool('ninja')</code>).
        </para>

        <para>
            It is not expected that the &b-link-Ninja; builder will work for all builds at this point. It is still under active
            development. If you find that your build doesn't work with &ninja; please bring this to the <ulink url="https://pairlist4.pair.net/mailman/listinfo/scons-users">users mailing list</ulink>
            or <ulink url="https://discord.gg/bXVpWAy">
            <literal>#scons-help</literal>
            </ulink> channel on our Discord server.
        </para>

        <para>
            Specifically if your build has many (or even any) &Python; function actions you may find that the &ninja; build
            will be slower as it will run &ninja;, which will then run SCons for each target created by a &Python; action.
            To alleviate some of these, especially those &Python; based actions built into SCons there is special logic to
            implement those actions via shell commands in the &ninja; build file.
        </para>

        <para>
            When &ninja; runs the generated &ninja; build file, &ninja; will launch &scons; as a daemon and feed commands
            to that &scons; process which &ninja; is unable to build directly.  This daemon will stay alive until
            explicitly killed, or it times out. The timeout is set by &cv-link-NINJA_SCONS_DAEMON_KEEP_ALIVE; .
        </para>

        <para>
            The daemon will be restarted if any &SConscript; file(s) change or the build changes in a way that &ninja; determines
            it needs to regenerate the build.ninja file
        </para>

        <para>See:</para>

        <simplelist type="vert">
            <member>
            <ulink url="https://ninja-build.org/">
                <citetitle>Ninja Build System</citetitle>
            </ulink>
            </member>
            <member>
            <ulink url="https://ninja-build.org/manual.html#ref_ninja_file">
                <citetitle>Ninja File Format Specification</citetitle>
            </ulink>
            </member>
        </simplelist>
    </section>
</chapter>