forked from Project-OSRM/osrm-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmake_options_script.py
43 lines (35 loc) · 1.47 KB
/
cmake_options_script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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
compilation_database[compilation_entry["file"]] = [ p.strip() for p in compilation_database_pattern.findall(compilation_entry["command"]) ]
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])
with open(cache_file, "wb") as f:
pickle.dump(db, f)
else:
with open(cache_file) as f:
db = pickle.load(f)
if db and sys.argv[2] in db:
for option in db[sys.argv[2]]:
print option