Skip to content

Commit 561438c

Browse files
authored
Merge pull request #32848 from RevoluPowered/feature/compilation-database-support
Compilation database support - clang, gcc, mingw
2 parents 092b2bd + 5a6f275 commit 561438c

File tree

3 files changed

+189
-1
lines changed

3 files changed

+189
-1
lines changed

.appveyor.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ cache:
2929

3030
install:
3131
- SET "PATH=%PYTHON%;%PYTHON%\\Scripts;%PATH%"
32-
- pip install scons==3.1.2
32+
- pip install -U wheel # needed for pip install scons to work, otherwise a flag is missing
33+
- pip install scons # use stable scons
34+
- if defined VS call "%VS%" %ARCH% # if defined - so we can also use mingw
3335

3436
before_build:
3537
- echo %GD_PLATFORM%

SConstruct

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,15 @@ if selected_platform in platform_list:
272272
else:
273273
env = env_base.Clone()
274274

275+
# Custom tools are loaded automatically by SCons from site_scons/site_tools,
276+
# but we want to use a different folder, so we register it manually.
277+
from SCons.Script.Main import _load_site_scons_dir
278+
279+
_load_site_scons_dir(".", "misc/scons")
280+
281+
env.Tool("compilation_db")
282+
env.Alias("compiledb", env.CompilationDatabase("compile_commands.json"))
283+
275284
if env["dev"]:
276285
env["verbose"] = True
277286
env["warnings"] = "extra"
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
# Copyright 2015 MongoDB Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import json
16+
import SCons
17+
import itertools
18+
19+
# Implements the ability for SCons to emit a compilation database for the MongoDB project. See
20+
# http://clang.llvm.org/docs/JSONCompilationDatabase.html for details on what a compilation
21+
# database is, and why you might want one. The only user visible entry point here is
22+
# 'env.CompilationDatabase'. This method takes an optional 'target' to name the file that
23+
# should hold the compilation database, otherwise, the file defaults to compile_commands.json,
24+
# which is the name that most clang tools search for by default.
25+
26+
# TODO: Is there a better way to do this than this global? Right now this exists so that the
27+
# emitter we add can record all of the things it emits, so that the scanner for the top level
28+
# compilation database can access the complete list, and also so that the writer has easy
29+
# access to write all of the files. But it seems clunky. How can the emitter and the scanner
30+
# communicate more gracefully?
31+
__COMPILATION_DB_ENTRIES = []
32+
33+
# We make no effort to avoid rebuilding the entries. Someday, perhaps we could and even
34+
# integrate with the cache, but there doesn't seem to be much call for it.
35+
class __CompilationDbNode(SCons.Node.Python.Value):
36+
def __init__(self, value):
37+
SCons.Node.Python.Value.__init__(self, value)
38+
self.Decider(changed_since_last_build_node)
39+
40+
41+
def changed_since_last_build_node(child, target, prev_ni, node):
42+
""" Dummy decider to force always building"""
43+
return True
44+
45+
46+
def makeEmitCompilationDbEntry(comstr):
47+
"""
48+
Effectively this creates a lambda function to capture:
49+
* command line
50+
* source
51+
* target
52+
:param comstr: unevaluated command line
53+
:return: an emitter which has captured the above
54+
"""
55+
user_action = SCons.Action.Action(comstr)
56+
57+
def EmitCompilationDbEntry(target, source, env):
58+
"""
59+
This emitter will be added to each c/c++ object build to capture the info needed
60+
for clang tools
61+
:param target: target node(s)
62+
:param source: source node(s)
63+
:param env: Environment for use building this node
64+
:return: target(s), source(s)
65+
"""
66+
67+
dbtarget = __CompilationDbNode(source)
68+
69+
entry = env.__COMPILATIONDB_Entry(
70+
target=dbtarget,
71+
source=[],
72+
__COMPILATIONDB_UTARGET=target,
73+
__COMPILATIONDB_USOURCE=source,
74+
__COMPILATIONDB_UACTION=user_action,
75+
__COMPILATIONDB_ENV=env,
76+
)
77+
78+
# TODO: Technically, these next two lines should not be required: it should be fine to
79+
# cache the entries. However, they don't seem to update properly. Since they are quick
80+
# to re-generate disable caching and sidestep this problem.
81+
env.AlwaysBuild(entry)
82+
env.NoCache(entry)
83+
84+
__COMPILATION_DB_ENTRIES.append(dbtarget)
85+
86+
return target, source
87+
88+
return EmitCompilationDbEntry
89+
90+
91+
def CompilationDbEntryAction(target, source, env, **kw):
92+
"""
93+
Create a dictionary with evaluated command line, target, source
94+
and store that info as an attribute on the target
95+
(Which has been stored in __COMPILATION_DB_ENTRIES array
96+
:param target: target node(s)
97+
:param source: source node(s)
98+
:param env: Environment for use building this node
99+
:param kw:
100+
:return: None
101+
"""
102+
103+
command = env["__COMPILATIONDB_UACTION"].strfunction(
104+
target=env["__COMPILATIONDB_UTARGET"], source=env["__COMPILATIONDB_USOURCE"], env=env["__COMPILATIONDB_ENV"],
105+
)
106+
107+
entry = {
108+
"directory": env.Dir("#").abspath,
109+
"command": command,
110+
"file": str(env["__COMPILATIONDB_USOURCE"][0]),
111+
}
112+
113+
target[0].write(entry)
114+
115+
116+
def WriteCompilationDb(target, source, env):
117+
entries = []
118+
119+
for s in __COMPILATION_DB_ENTRIES:
120+
entries.append(s.read())
121+
122+
with open(str(target[0]), "w") as target_file:
123+
json.dump(entries, target_file, sort_keys=True, indent=4, separators=(",", ": "))
124+
125+
126+
def ScanCompilationDb(node, env, path):
127+
return __COMPILATION_DB_ENTRIES
128+
129+
130+
def generate(env, **kwargs):
131+
132+
static_obj, shared_obj = SCons.Tool.createObjBuilders(env)
133+
134+
env["COMPILATIONDB_COMSTR"] = kwargs.get("COMPILATIONDB_COMSTR", "Building compilation database $TARGET")
135+
136+
components_by_suffix = itertools.chain(
137+
itertools.product(
138+
env["CPPSUFFIXES"],
139+
[
140+
(static_obj, SCons.Defaults.StaticObjectEmitter, "$CXXCOM"),
141+
(shared_obj, SCons.Defaults.SharedObjectEmitter, "$SHCXXCOM"),
142+
],
143+
),
144+
)
145+
146+
for entry in components_by_suffix:
147+
suffix = entry[0]
148+
builder, base_emitter, command = entry[1]
149+
150+
# Ensure we have a valid entry
151+
# used to auto ignore header files
152+
if suffix in builder.emitter:
153+
emitter = builder.emitter[suffix]
154+
builder.emitter[suffix] = SCons.Builder.ListEmitter([emitter, makeEmitCompilationDbEntry(command),])
155+
156+
env["BUILDERS"]["__COMPILATIONDB_Entry"] = SCons.Builder.Builder(
157+
action=SCons.Action.Action(CompilationDbEntryAction, None),
158+
)
159+
160+
env["BUILDERS"]["__COMPILATIONDB_Database"] = SCons.Builder.Builder(
161+
action=SCons.Action.Action(WriteCompilationDb, "$COMPILATIONDB_COMSTR"),
162+
target_scanner=SCons.Scanner.Scanner(function=ScanCompilationDb, node_class=None),
163+
)
164+
165+
def CompilationDatabase(env, target):
166+
result = env.__COMPILATIONDB_Database(target=target, source=[])
167+
168+
env.AlwaysBuild(result)
169+
env.NoCache(result)
170+
171+
return result
172+
173+
env.AddMethod(CompilationDatabase, "CompilationDatabase")
174+
175+
176+
def exists(env):
177+
return True

0 commit comments

Comments
 (0)