|
| 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