/
cmake_options_script.py
144 lines (106 loc) · 4.67 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# 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 ",
# 2013.01.26: Pavlo Ilin
# - Removed the usage of cache file - if cmake-scripts are changed often,
# cache file quickly becomes old and wrong.
# - Will recursively go up in directory tree and try to find compile_commands.json
# using predefined search patterns (dbfile_search_pattern)
# - Will try to guess options for files which are not listed in compile_commands.json
# Those files are, mostly, header files. See guess_option()
# 2012.01.31: Pavlo Ilin
# - Support for arduino-cmake
import re
import os
import os.path
import sys
import json
import platform
from pprint import pprint
compilation_database_options_pattern = re.compile('(?<=\s)-[DIOUWfgs][^=\s]+(?:=\\"[^"]+\\"|=[^"]\S+)?')
compilation_database_additional_options_pattern = re.compile('(?<=\s)-[m][^=\s]+(?:=\\"[^"]+\\"|=[^"]\S+)?')
compilation_database_command_pattern = re.compile(r'(.+?)\s+-[DIOUWfgs]')
dbfile_search_patterns = [r'/build/compile_commands.json',
r'/debug/compile_commands.json',
r'/release/compile_commands.json']
def find_dffile(path):
for dbfile_pat in dbfile_search_patterns:
dbfile = path + dbfile_pat
if os.path.isfile(dbfile):
return dbfile
# not found
newpath = os.path.dirname(path)
if path == newpath:
raise Exception(r'Unable to find ' + dbfile_pat)
return find_dffile(newpath)
def load_db(filename):
compilation_database = {}
with open(filename) as compilation_database_file:
compilation_database_entries = json.load(compilation_database_file)
entry = 0
for compilation_entry in compilation_database_entries:
entry = entry + 1
o = {}
o['flags'] = [ p.strip() for p in compilation_database_options_pattern.findall(compilation_entry["command"]) ]
o['additional'] = [ p.strip() for p in compilation_database_additional_options_pattern.findall(compilation_entry["command"]) ]
o['command'] = compilation_database_command_pattern.match(compilation_entry["command"]).group(1)
compilation_database[compilation_entry["file"]] = o
return compilation_database
def guess_file(filename, db):
# possible source file names
name, ext = os.path.splitext(filename)
guesses = [name + '.cpp', name + '.c']
# possible main source file
for key in db.iterkeys():
if key.endswith('main.cpp') or key.endswith('main.c'):
guesses.append(key)
break
# copy options from existing possible file
for g in guesses:
if os.path.isfile(g):
db[filename] = db[g]
break
return db
def guess_additional_options(filename, options):
#pprint(options)
# c++11 on mac os x
if platform.mac_ver()[0] != '':
if any([r'-std=c++11' in s for s in options['flags']]) and any([r'-stdlib=libc++' in s for s in options['flags']]):
options['flags'].append(r'-I/usr/lib/c++/v1')
# arduino
if r'tools/avr/bin' in options['command']:
# recover avr-gcc system include path
avr = options['command'] # .../avr/bin/avr-gcc
avr = os.path.dirname(avr) # .../avr/bin
avr = os.path.dirname(avr) # .../avr
options['flags'].append("-I" + avr + r'/avr-4/include/')
options['flags'].append("-I" + avr + r'/lib/gcc/avr/4.3.2/include')
# recover avr type from linker flags
if 'additional' in options:
recpu = re.compile(r"-mmcu=atmega(.+)")
for o in options['additional']:
m = recpu.match(o)
if m:
mm = m.group(1)
options['flags'].append("-D__AVR_ATmega{0}__".format(mm.upper()))
# add few options to make clang happy
options['flags'].append("-Wno-attributes")
options['flags'].append("-Wno-builtin-requires-header")
options['flags'].append("-Wno-unused-command-line-argument")
return options
srcfile = sys.argv[1]
dbfile = find_dffile(os.path.dirname(srcfile))
dbpath = os.path.dirname(dbfile)
db = load_db(dbfile)
#pprint(db)
if db:
file_options = {'flags':"", 'command':""}
if not srcfile in db:
db = guess_file(srcfile, db)
if srcfile in db:
file_options = db[srcfile]
file_options = guess_additional_options(srcfile, file_options)
for option in file_options['flags']:
print option