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
|
<?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 -m32 -o build/linux32/test_main.o -c test_main.c",
"directory": "/home/user/sandbox",
"file": "test_main.c",
"output": "build/linux32/test_main.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 depreciation cycle
</para>
<para>
To use this tool you must install pypi's <ulink url="https://pypi.org/project/ninja/">ninja
package</ulink>.
This can be done via
<command>pip install ninja</command>
</para>
<para>
To enable this feature you'll need to use one of the following
</para>
<example_commands>
# On the command line
--experimental=ninja
# Or in your SConstruct
SetOption('experimental', 'ninja')
</example_commands>
</note>
<para>
This tool will enabled creating a ninja build file from your SCons based build system. It can then invoke
ninja to run your build. For most builds ninja will be significantly faster, but you may have to give up
some accuracy. You are NOT advised to use this for production builds. It can however significantly speed up
your build/debug/compile iterations.
</para>
<para>
It's not expected that the ninja builder will work for all builds at this point. It's still under active
development. If you find that your build doesn't work with ninja please bring this to the users mailing list
or devel 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>
<ulink url="https://ninja-build.org/">
<citetitle>Ninja Build System</citetitle>
</ulink>
</para>
<para>
<ulink url="https://ninja-build.org/manual.html#ref_ninja_file">
<citetitle>Ninja File Format Specification</citetitle>
</ulink>
</para>
</section>
</chapter>
|