Skip to content

Instantly share code, notes, and snippets.

@xchrdw
Last active March 28, 2018 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xchrdw/bfd2b3a5f765f4195a55d6351daf1b48 to your computer and use it in GitHub Desktop.
Save xchrdw/bfd2b3a5f765f4195a55d6351daf1b48 to your computer and use it in GitHub Desktop.
"""
Generates a compilation database with entries for cc and h files from
a db with just entries for cc files.
Expects a compile_commands_CC.json file and writes a compile_commands.json file.
usage:
ninja -C out/Release -t compdb cxx cc > compile_commands_CC.json
python3 make_compdb.py
Example input:
[{
"directory": "/path/to/chromium/src/out/Release",
"command": "../../third_party/llvm-build/Release+Asserts/bin/clang++ -MMD -MF obj/apps/apps/app_lifetime_monitor.o.d -DV8_DEPRECATION_WARNINGS -DDCHECK_ALWAYS_ON=1 -DUSE_UDEV -DUSE_AURA=1 -DUSE_GLIB=1 -DUSE_NSS_CERTS=1 -DUSE_X11=1 -DFULL_SAFE_BROWSING -DSAFE_BROWSING_CSD -DSAFE_BROWSING_DB_LOCAL -DCHROMIUM_BUILD -DFIELDTRIAL_TESTING_ENABLED -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -DCR_CLANG_REVISION=\\\"328575-1\\\" -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -DCOMPONENT_BUILD -DNDEBUG -DNVALGRIND -DDYNAMIC_ANNOTATIONS_ENABLED=0 -DGLIB_VERSION_MAX_ALLOWED=GLIB_VERSION_2_32 -DGLIB_VERSION_MIN_REQUIRED=GLIB_VERSION_2_26 -DWEBP_EXTERN=extern -DGL_GLEXT_PROTOTYPES -DUSE_GLX -DUSE_EGL -DANGLE_ENABLE_RELEASE_ASSERTS -DTOOLKIT_VIEWS=1 -DU_USING_ICU_NAMESPACE=0 -DU_ENABLE_DYLOAD=0 -DICU_UTIL_DATA_IMPL=ICU_UTIL_DATA_FILE -DUCHAR_TYPE=uint16_t -DGOOGLE_PROTOBUF_NO_RTTI -DGOOGLE_PROTOBUF_NO_STATIC_INITIALIZER -DHAVE_PTHREAD -DPROTOBUF_USE_DLLS -DV8_USE_EXTERNAL_STARTUP_DATA -DBORINGSSL_SHARED_LIBRARY -DLEVELDB_PLATFORM_CHROMIUM=1 -DSK_IGNORE_LINEONLY_AA_CONVEX_PATH_OPTS -DSK_HAS_PNG_LIBRARY -DSK_HAS_WEBP_LIBRARY -DSK_HAS_JPEG_LIBRARY -DSKIA_DLL -DGR_GL_IGNORE_ES3_MSAA=0 -DSK_SUPPORT_GPU=1 -DUSING_V8_SHARED -DWEBRTC_NON_STATIC_TRACE_EVENT_HANDLERS=0 -DGTEST_RELATIVE_PATH -DWEBRTC_CHROMIUM_BUILD -DWEBRTC_POSIX -DWEBRTC_LINUX -I../.. -Igen -I../../third_party/libwebp/src -I../../third_party/khronos -I../../gpu -I../../third_party/libyuv/include -I../../third_party/ced/src -I../../third_party/icu/source/common -I../../third_party/icu/source/i18n -I../../third_party/protobuf/src -I../../third_party/protobuf/src -Igen/protoc_out -I../../third_party/boringssl/src/include -I../../third_party/leveldatabase -I../../third_party/leveldatabase/src -I../../third_party/leveldatabase/src/include -I../../skia/config -I../../skia/ext -I../../third_party/skia/include/c -I../../third_party/skia/include/config -I../../third_party/skia/include/core -I../../third_party/skia/include/effects -I../../third_party/skia/include/encode -I../../third_party/skia/include/gpu -I../../third_party/skia/include/images -I../../third_party/skia/include/lazy -I../../third_party/skia/include/pathops -I../../third_party/skia/include/pdf -I../../third_party/skia/include/pipe -I../../third_party/skia/include/ports -I../../third_party/skia/include/utils -I../../third_party/skia/src/gpu -I../../third_party/skia/src/sksl -I../../third_party/libwebm/source -I../../third_party/WebKit -Igen/third_party/WebKit -I../../v8/include -Igen/v8/include -I../../third_party/webrtc_overrides -I../../third_party/webrtc -Igen -I../../third_party/flatbuffers/src/include -Igen -fno-strict-aliasing --param=ssp-buffer-size=4 -fstack-protector -Wno-builtin-macro-redefined -D__DATE__= -D__TIME__= -D__TIMESTAMP__= -funwind-tables -fPIC -pipe -B../../third_party/binutils/Linux_x64/Release/bin -pthread -fcolor-diagnostics -Xclang -mllvm -Xclang -instcombine-lower-dbg-declare=0 -no-canonical-prefixes -m64 -march=x86-64 -Wall -Werror -Wextra -Wimplicit-fallthrough -Wthread-safety -Wno-missing-field-initializers -Wno-unused-parameter -Wno-c++11-narrowing -Wno-covered-switch-default -Wno-unneeded-internal-declaration -Wno-inconsistent-missing-override -Wno-undefined-var-template -Wno-nonportable-include-path -Wno-address-of-packed-member -Wno-unused-lambda-capture -Wno-user-defined-warnings -Wno-enum-compare-switch -Wno-null-pointer-arithmetic -Wno-ignored-pragma-optimize -O2 -fno-ident -fdata-sections -ffunction-sections -fno-omit-frame-pointer -g0 -fvisibility=hidden -Xclang -load -Xclang ../../third_party/llvm-build/Release+Asserts/lib/libFindBadConstructs.so -Xclang -add-plugin -Xclang find-bad-constructs -Xclang -plugin-arg-find-bad-constructs -Xclang check-ipc -Wheader-hygiene -Wstring-conversion -Wtautological-overlap-compare -Wexit-time-destructors -isystem/path/to/chromium/src/build/linux/debian_sid_amd64-sysroot/usr/include/glib-2.0 -isystem/path/to/chromium/src/build/linux/debian_sid_amd64-sysroot/usr/lib/x86_64-linux-gnu/glib-2.0/include -Wno-header-guard -isystem/path/to/chromium/src/build/linux/debian_sid_amd64-sysroot/usr/include/nss -isystem/path/to/chromium/src/build/linux/debian_sid_amd64-sysroot/usr/include/nspr -Wno-exit-time-destructors -std=gnu++14 -fno-exceptions -fno-rtti -nostdinc++ -isystem../../buildtools/third_party/libc++/trunk/include -isystem../../buildtools/third_party/libc++abi/trunk/include --sysroot=../../build/linux/debian_sid_amd64-sysroot -fvisibility-inlines-hidden -c ../../apps/app_lifetime_monitor.cc -o obj/apps/apps/app_lifetime_monitor.o",
"file": "../../apps/app_lifetime_monitor.cc"
}, ... ]
"""
import bisect
import os
import json
BUILD_DIR = "out/Release/"
def find_match(a, x):
'Find most similar file to x in a'
i = bisect.bisect_right(a, x)
i = max(1, min(len(a)-1, i))
r1 = a[i-1]
r2 = a[i]
if len(os.path.commonprefix([x, r1])) >= len(os.path.commonprefix([x, r2])):
return r1
return r2
def read_headers():
h_files = []
for root, _, files in os.walk('.'):
if ("/out/" in root and not BUILD_DIR in root):
continue
root = root[2:] # strip "./"
if BUILD_DIR in root:
root = root.replace(BUILD_DIR, "")
else:
root = "../../" + root
for f in files:
if f.endswith(".h"):
h_files.append(os.path.join(root, f))
return h_files
def create_mapping(cc_files, h_files):
h_map = {}
for f in h_files:
h_map[f] = find_match(cc_files, f)
print(h_map["../../base/callback_forward.h"])
print(h_map["../../storage/browser/quota/special_storage_policy.h"])
print(h_map["gen/components/sync/protocol/user_event_specifics.pb.h"])
return h_map
def get_cc_files(db):
return sorted(e["file"] for e in db)
def read_compdb():
data = json.loads(open("compile_commands_CC.json").read())
return data
def write_compdb(data):
s = json.dumps(data, indent=2)
open("compile_commands.json", "w").write(s)
def add_entries(db, h_map):
db_map = {e["file"]: e for e in db}
new_db = []
for h, cc in h_map.items():
entry = db_map[cc].copy()
entry["file"] = h
entry["command"] = entry["command"].replace(cc, h)
entry["command"] += " -x c++-header"
new_db.append(entry)
return db + new_db
if __name__ == '__main__':
print("getting headers")
h_files = read_headers()
print("reading db")
db = read_compdb()
cc_files = get_cc_files(db)
print("{} cc rules, {} h files".format(len(cc_files), len(h_files)))
print("creating mapping")
h_map = create_mapping(cc_files, h_files)
print("adding header entries")
new_db = add_entries(db, h_map)
print("writing db")
write_compdb(new_db)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment