-
-
Save jrk/4445206 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Based on @berenm's pull request https://github.com/quarnster/SublimeClang/pull/135 | |
# Create the database with cmake with for example: cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .. | |
# or you could have set(CMAKE_EXPORT_COMPILE_COMMANDS ON) in your CMakeLists.txt | |
# Usage within SublimeClang: | |
# "sublimeclang_options_script": "python ${home}/code/cmake_options_script.py ${project_path:build}/compile_commands.json", | |
import re | |
import os | |
import os.path | |
import pickle | |
import sys | |
import json | |
compilation_database_pattern = re.compile('(?<=\s)-[DIOUWfgs][^=\s]+(?:=\\"[^"]+\\"|=[^"]\S+)?') | |
def load_db(filename): | |
compilation_database = {} | |
with open(filename) as compilation_database_file: | |
compilation_database_entries = json.load(compilation_database_file) | |
total = len(compilation_database_entries) | |
entry = 0 | |
for compilation_entry in compilation_database_entries: | |
entry = entry + 1 | |
filepath = compilation_entry["file"] | |
try: | |
# resolve the true path to the file, unpacking symlinks and relative paths. | |
# this is important because SublimeClang will query based on realpath. | |
filepath = os.path.realpath(filepath) | |
except: | |
sys.stderr.write("SublimeClang cmake_options_script: Failed to resolve realpath for %s\n" % filepath) | |
opts = [ p.strip() for p in compilation_database_pattern.findall(compilation_entry["command"]) ] | |
compilation_database[filepath] = opts | |
# Also store the options list for the parent directory | |
# This is a kludge to handle header files | |
basepath = os.path.dirname(filepath) | |
if basepath not in compilation_database: | |
compilation_database[basepath] = opts | |
return compilation_database | |
scriptpath = os.path.dirname(os.path.abspath(sys.argv[1])) | |
cache_file = "%s/cached_options.txt" % (scriptpath) | |
db = None | |
if os.access(cache_file, os.R_OK) == 0: | |
db = load_db(sys.argv[1]) | |
f = open(cache_file, "wb") | |
pickle.dump(db, f) | |
f.close() | |
else: | |
f = open(cache_file) | |
db = pickle.load(f) | |
f.close() | |
if db: | |
query = sys.argv[2] | |
if query not in db: | |
query = os.path.dirname(os.path.realpath(sys.argv[2])) | |
options = [] | |
try: | |
options = db[query] | |
except: | |
pass | |
for option in options: | |
print option |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment