2016-07-12 04:37:03 +02:00
|
|
|
# Copyright 2015 MongoDB Inc.
|
2014-12-24 18:12:22 +01:00
|
|
|
#
|
2016-07-12 04:37:03 +02:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
2014-12-24 18:12:22 +01:00
|
|
|
#
|
2016-07-12 04:37:03 +02:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2014-12-24 18:12:22 +01:00
|
|
|
#
|
2016-07-12 04:37:03 +02:00
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2014-12-24 18:12:22 +01:00
|
|
|
|
|
|
|
import json
|
|
|
|
import SCons
|
|
|
|
import itertools
|
|
|
|
|
|
|
|
# Implements the ability for SCons to emit a compilation database for the MongoDB project. See
|
|
|
|
# http://clang.llvm.org/docs/JSONCompilationDatabase.html for details on what a compilation
|
|
|
|
# database is, and why you might want one. The only user visible entry point here is
|
|
|
|
# 'env.CompilationDatabase'. This method takes an optional 'target' to name the file that
|
|
|
|
# should hold the compilation database, otherwise, the file defaults to compile_commands.json,
|
|
|
|
# which is the name that most clang tools search for by default.
|
|
|
|
|
|
|
|
# TODO: Is there a better way to do this than this global? Right now this exists so that the
|
|
|
|
# emitter we add can record all of the things it emits, so that the scanner for the top level
|
|
|
|
# compilation database can access the complete list, and also so that the writer has easy
|
|
|
|
# access to write all of the files. But it seems clunky. How can the emitter and the scanner
|
|
|
|
# communicate more gracefully?
|
2019-02-19 16:50:57 +01:00
|
|
|
__COMPILATION_DB_ENTRIES = []
|
2014-12-24 18:12:22 +01:00
|
|
|
|
2016-06-08 17:28:07 +02:00
|
|
|
# Cribbed from Tool/cc.py and Tool/c++.py. It would be better if
|
|
|
|
# we could obtain this from SCons.
|
|
|
|
_CSuffixes = ['.c']
|
|
|
|
if not SCons.Util.case_sensitive_suffixes('.c', '.C'):
|
|
|
|
_CSuffixes.append('.C')
|
|
|
|
|
|
|
|
_CXXSuffixes = ['.cpp', '.cc', '.cxx', '.c++', '.C++']
|
|
|
|
if SCons.Util.case_sensitive_suffixes('.c', '.C'):
|
|
|
|
_CXXSuffixes.append('.C')
|
|
|
|
|
2019-02-19 16:50:57 +01:00
|
|
|
|
2014-12-24 18:12:22 +01:00
|
|
|
# We make no effort to avoid rebuilding the entries. Someday, perhaps we could and even
|
|
|
|
# integrate with the cache, but there doesn't seem to be much call for it.
|
2017-06-15 19:50:28 +02:00
|
|
|
class __CompilationDbNode(SCons.Node.Python.Value):
|
|
|
|
def __init__(self, value):
|
|
|
|
SCons.Node.Python.Value.__init__(self, value)
|
2016-06-16 22:20:53 +02:00
|
|
|
self.Decider(changed_since_last_build_node)
|
2014-12-24 18:12:22 +01:00
|
|
|
|
2017-06-15 19:50:28 +02:00
|
|
|
|
2016-06-16 22:20:53 +02:00
|
|
|
def changed_since_last_build_node(node, target, prev_ni):
|
2017-06-15 19:50:28 +02:00
|
|
|
""" Dummy decider to force alwasy building"""
|
2016-06-16 22:20:53 +02:00
|
|
|
return True
|
2014-12-24 18:12:22 +01:00
|
|
|
|
2017-06-15 19:50:28 +02:00
|
|
|
|
2014-12-24 18:12:22 +01:00
|
|
|
def makeEmitCompilationDbEntry(comstr):
|
2017-06-15 19:50:28 +02:00
|
|
|
"""
|
|
|
|
Effectively this creates a lambda function to capture:
|
|
|
|
* command line
|
|
|
|
* source
|
|
|
|
* target
|
|
|
|
:param comstr: unevaluated command line
|
|
|
|
:return: an emitter which has captured the above
|
|
|
|
"""
|
2014-12-24 18:12:22 +01:00
|
|
|
user_action = SCons.Action.Action(comstr)
|
2017-06-15 19:50:28 +02:00
|
|
|
|
2014-12-24 18:12:22 +01:00
|
|
|
def EmitCompilationDbEntry(target, source, env):
|
2017-06-15 19:50:28 +02:00
|
|
|
"""
|
|
|
|
This emitter will be added to each c/c++ object build to capture the info needed
|
|
|
|
for clang tools
|
|
|
|
:param target: target node(s)
|
|
|
|
:param source: source node(s)
|
|
|
|
:param env: Environment for use building this node
|
|
|
|
:return: target(s), source(s)
|
|
|
|
"""
|
2014-12-24 18:12:22 +01:00
|
|
|
|
2017-06-15 19:50:28 +02:00
|
|
|
dbtarget = __CompilationDbNode(source)
|
2014-12-24 18:12:22 +01:00
|
|
|
|
|
|
|
entry = env.__COMPILATIONDB_Entry(
|
2019-02-19 16:50:57 +01:00
|
|
|
target=dbtarget, source=[], __COMPILATIONDB_UTARGET=target,
|
|
|
|
__COMPILATIONDB_USOURCE=source, __COMPILATIONDB_UACTION=user_action,
|
2017-01-10 22:37:58 +01:00
|
|
|
__COMPILATIONDB_ENV=env)
|
2014-12-24 18:12:22 +01:00
|
|
|
|
|
|
|
# TODO: Technically, these next two lines should not be required: it should be fine to
|
|
|
|
# cache the entries. However, they don't seem to update properly. Since they are quick
|
|
|
|
# to re-generate disable caching and sidestep this problem.
|
|
|
|
env.AlwaysBuild(entry)
|
|
|
|
env.NoCache(entry)
|
|
|
|
|
|
|
|
__COMPILATION_DB_ENTRIES.append(dbtarget)
|
|
|
|
|
|
|
|
return target, source
|
|
|
|
|
|
|
|
return EmitCompilationDbEntry
|
|
|
|
|
2017-06-15 19:50:28 +02:00
|
|
|
|
2014-12-24 18:12:22 +01:00
|
|
|
def CompilationDbEntryAction(target, source, env, **kw):
|
2017-06-15 19:50:28 +02:00
|
|
|
"""
|
|
|
|
Create a dictionary with evaluated command line, target, source
|
|
|
|
and store that info as an attribute on the target
|
|
|
|
(Which has been stored in __COMPILATION_DB_ENTRIES array
|
|
|
|
:param target: target node(s)
|
|
|
|
:param source: source node(s)
|
|
|
|
:param env: Environment for use building this node
|
|
|
|
:param kw:
|
|
|
|
:return: None
|
|
|
|
"""
|
2014-12-24 18:12:22 +01:00
|
|
|
|
|
|
|
command = env['__COMPILATIONDB_UACTION'].strfunction(
|
|
|
|
target=env['__COMPILATIONDB_UTARGET'],
|
|
|
|
source=env['__COMPILATIONDB_USOURCE'],
|
2019-02-19 16:50:57 +01:00
|
|
|
env=env['__COMPILATIONDB_ENV'],
|
|
|
|
)
|
2014-12-24 18:12:22 +01:00
|
|
|
|
|
|
|
entry = {
|
2019-02-19 16:50:57 +01:00
|
|
|
"directory": env.Dir('#').abspath, "command": command,
|
2014-12-24 18:12:22 +01:00
|
|
|
"file": str(env['__COMPILATIONDB_USOURCE'][0])
|
|
|
|
}
|
|
|
|
|
2017-06-15 19:50:28 +02:00
|
|
|
target[0].write(entry)
|
|
|
|
|
2014-12-24 18:12:22 +01:00
|
|
|
|
|
|
|
def WriteCompilationDb(target, source, env):
|
|
|
|
entries = []
|
|
|
|
|
|
|
|
for s in __COMPILATION_DB_ENTRIES:
|
2017-06-15 19:50:28 +02:00
|
|
|
entries.append(s.read())
|
2014-12-24 18:12:22 +01:00
|
|
|
|
|
|
|
with open(str(target[0]), 'w') as target_file:
|
2019-02-19 16:50:57 +01:00
|
|
|
json.dump(entries, target_file, sort_keys=True, indent=4, separators=(',', ': '))
|
2014-12-24 18:12:22 +01:00
|
|
|
|
2017-06-15 19:50:28 +02:00
|
|
|
|
2014-12-24 18:12:22 +01:00
|
|
|
def ScanCompilationDb(node, env, path):
|
|
|
|
return __COMPILATION_DB_ENTRIES
|
|
|
|
|
2019-02-19 16:50:57 +01:00
|
|
|
|
2014-12-24 18:12:22 +01:00
|
|
|
def generate(env, **kwargs):
|
|
|
|
|
|
|
|
static_obj, shared_obj = SCons.Tool.createObjBuilders(env)
|
|
|
|
|
2019-02-19 16:50:57 +01:00
|
|
|
env['COMPILATIONDB_COMSTR'] = kwargs.get('COMPILATIONDB_COMSTR',
|
|
|
|
'Building compilation database $TARGET')
|
2014-12-24 18:12:22 +01:00
|
|
|
|
|
|
|
components_by_suffix = itertools.chain(
|
2016-06-08 17:28:07 +02:00
|
|
|
itertools.product(_CSuffixes, [
|
2014-12-24 18:12:22 +01:00
|
|
|
(static_obj, SCons.Defaults.StaticObjectEmitter, '$CCCOM'),
|
|
|
|
(shared_obj, SCons.Defaults.SharedObjectEmitter, '$SHCCCOM'),
|
|
|
|
]),
|
2016-06-08 17:28:07 +02:00
|
|
|
itertools.product(_CXXSuffixes, [
|
2014-12-24 18:12:22 +01:00
|
|
|
(static_obj, SCons.Defaults.StaticObjectEmitter, '$CXXCOM'),
|
|
|
|
(shared_obj, SCons.Defaults.SharedObjectEmitter, '$SHCXXCOM'),
|
|
|
|
]),
|
|
|
|
)
|
|
|
|
|
|
|
|
for entry in components_by_suffix:
|
|
|
|
suffix = entry[0]
|
|
|
|
builder, base_emitter, command = entry[1]
|
|
|
|
|
2016-06-08 17:28:07 +02:00
|
|
|
# Assumes a dictionary emitter
|
|
|
|
emitter = builder.emitter[suffix]
|
2019-02-19 16:50:57 +01:00
|
|
|
builder.emitter[suffix] = SCons.Builder.ListEmitter([
|
|
|
|
emitter,
|
|
|
|
makeEmitCompilationDbEntry(command),
|
|
|
|
])
|
2014-12-24 18:12:22 +01:00
|
|
|
|
|
|
|
env['BUILDERS']['__COMPILATIONDB_Entry'] = SCons.Builder.Builder(
|
2019-02-19 16:50:57 +01:00
|
|
|
action=SCons.Action.Action(CompilationDbEntryAction, None), )
|
2014-12-24 18:12:22 +01:00
|
|
|
|
|
|
|
env['BUILDERS']['__COMPILATIONDB_Database'] = SCons.Builder.Builder(
|
|
|
|
action=SCons.Action.Action(WriteCompilationDb, "$COMPILATIONDB_COMSTR"),
|
2019-02-19 16:50:57 +01:00
|
|
|
target_scanner=SCons.Scanner.Scanner(function=ScanCompilationDb, node_class=None))
|
2014-12-24 18:12:22 +01:00
|
|
|
|
|
|
|
def CompilationDatabase(env, target):
|
2019-02-19 16:50:57 +01:00
|
|
|
result = env.__COMPILATIONDB_Database(target=target, source=[])
|
2014-12-24 18:12:22 +01:00
|
|
|
|
|
|
|
env.AlwaysBuild(result)
|
|
|
|
env.NoCache(result)
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
env.AddMethod(CompilationDatabase, 'CompilationDatabase')
|
|
|
|
|
2019-02-19 16:50:57 +01:00
|
|
|
|
2014-12-24 18:12:22 +01:00
|
|
|
def exists(env):
|
|
|
|
return True
|