|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright 2016-2019 Envoy Project Authors |
| 4 | +# Copyright 2020 Google LLC |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | + |
| 18 | +import argparse |
| 19 | +import json |
| 20 | +import os |
| 21 | +import shlex |
| 22 | +import subprocess |
| 23 | +from pathlib import Path |
| 24 | + |
| 25 | +# This is copied from https://github.com/envoyproxy/envoy and remove unnecessary code. |
| 26 | + |
| 27 | +# This method is equivalent to https://github.com/grailbio/bazel-compilation-database/blob/master/generate.py |
| 28 | +def generate_compilation_database(args): |
| 29 | + # We need to download all remote outputs for generated source code. This option lives here to override those |
| 30 | + # specified in bazelrc. |
| 31 | + bazel_startup_options = shlex.split(os.environ.get("BAZEL_STARTUP_OPTION_LIST", "")) |
| 32 | + bazel_options = shlex.split(os.environ.get("BAZEL_BUILD_OPTION_LIST", "")) + [ |
| 33 | + "--remote_download_outputs=all", |
| 34 | + ] |
| 35 | + |
| 36 | + source_dir_targets = args.bazel_targets |
| 37 | + |
| 38 | + subprocess.check_call(["bazel", *bazel_startup_options, "build"] + bazel_options + [ |
| 39 | + "--aspects=@bazel_compdb//:aspects.bzl%compilation_database_aspect", |
| 40 | + "--output_groups=compdb_files,header_files" |
| 41 | + ] + source_dir_targets) |
| 42 | + |
| 43 | + execroot = subprocess.check_output( |
| 44 | + ["bazel", *bazel_startup_options, "info", *bazel_options, |
| 45 | + "execution_root"]).decode().strip() |
| 46 | + |
| 47 | + db_entries = [] |
| 48 | + for db in Path(execroot).glob('**/*.compile_commands.json'): |
| 49 | + db_entries.extend(json.loads(db.read_text())) |
| 50 | + |
| 51 | + def replace_execroot_marker(db_entry): |
| 52 | + if 'directory' in db_entry and db_entry['directory'] == '__EXEC_ROOT__': |
| 53 | + db_entry['directory'] = execroot |
| 54 | + if 'command' in db_entry: |
| 55 | + db_entry['command'] = ( |
| 56 | + db_entry['command'].replace('-isysroot __BAZEL_XCODE_SDKROOT__', '')) |
| 57 | + return db_entry |
| 58 | + |
| 59 | + return list(map(replace_execroot_marker, db_entries)) |
| 60 | + |
| 61 | +if __name__ == "__main__": |
| 62 | + parser = argparse.ArgumentParser(description='Generate JSON compilation database') |
| 63 | + parser.add_argument('--include_external', action='store_true') |
| 64 | + parser.add_argument('--include_genfiles', action='store_true') |
| 65 | + parser.add_argument('--include_headers', action='store_true') |
| 66 | + parser.add_argument('--include_all', action='store_true') |
| 67 | + parser.add_argument( |
| 68 | + '--system-clang', |
| 69 | + action='store_true', |
| 70 | + help= |
| 71 | + 'Use `clang++` instead of the bazel wrapper for commands. This may help if `clangd` cannot find/run the tools.' |
| 72 | + ) |
| 73 | + parser.add_argument('bazel_targets', nargs='*', default=[]) |
| 74 | + |
| 75 | + args = parser.parse_args() |
| 76 | + db = generate_compilation_database(args) |
| 77 | + |
| 78 | + with open("compile_commands.json", "w") as db_file: |
| 79 | + json.dump(db, db_file, indent=2) |
0 commit comments