-
Notifications
You must be signed in to change notification settings - Fork 3
/
cmakefileapijson.py
420 lines (352 loc) · 15.8 KB
/
cmakefileapijson.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# SPDX-License-Identifier: Apache-2.0
import json
import os
import cmakefileapi
def parseReply(replyIndexPath):
replyDir, replyIndexFilename = os.path.split(replyIndexPath)
# first we need to find the codemodel reply file
try:
with open(replyIndexPath, 'r') as indexFile:
js = json.load(indexFile)
# get reply object
reply_dict = js.get("reply", {})
if reply_dict == {}:
print(f"no \"reply\" field found in index file")
return None
# get codemodel object
cm_dict = reply_dict.get("codemodel-v2", {})
if cm_dict == {}:
print(f"no \"codemodel-v2\" field found in \"reply\" object in index file")
return None
# and get codemodel filename
jsonFile = cm_dict.get("jsonFile", "")
if jsonFile == "":
print(f"no \"jsonFile\" field found in \"codemodel-v2\" object in index file")
return None
return parseCodemodel(replyDir, jsonFile)
except OSError as e:
print(f"Error loading {replyIndexPath}: {str(e)}")
return None
except json.decoder.JSONDecodeError as e:
print(f"Error parsing JSON in {replyIndexPath}: {str(e)}")
return None
def parseCodemodel(replyDir, codemodelFile):
codemodelPath = os.path.join(replyDir, codemodelFile)
try:
with open(codemodelPath, 'r') as cmFile:
js = json.load(cmFile)
cm = cmakefileapi.Codemodel()
# FIXME for correctness, should probably check kind and version
# get paths
paths_dict = js.get("paths", {})
cm.paths_source = paths_dict.get("source", "")
cm.paths_build = paths_dict.get("build", "")
# get configurations
configs_arr = js.get("configurations", [])
for cfg_dict in configs_arr:
cfg = parseConfig(cfg_dict, replyDir)
if cfg:
cm.configurations.append(cfg)
# and after parsing is done, link all the indices
linkCodemodel(cm)
return cm
except OSError as e:
print(f"Error loading {replyIndexPath}: {str(e)}")
return None
except json.decoder.JSONDecodeError as e:
print(f"Error parsing JSON in {replyIndexPath}: {str(e)}")
return None
def parseConfig(cfg_dict, replyDir):
cfg = cmakefileapi.Config()
cfg.name = cfg_dict.get("name", "")
# parse and add each directory
dirs_arr = cfg_dict.get("directories", [])
for dir_dict in dirs_arr:
if dir_dict != {}:
cfgdir = cmakefileapi.ConfigDir()
cfgdir.source = dir_dict.get("source", "")
cfgdir.build = dir_dict.get("build", "")
cfgdir.parentIndex = dir_dict.get("parentIndex", -1)
cfgdir.childIndexes = dir_dict.get("childIndexes", [])
cfgdir.projectIndex = dir_dict.get("projecttIndex", -1)
cfgdir.targetIndexes = dir_dict.get("targetIndexes", [])
minCMakeVer_dict = dir_dict.get("minimumCMakeVersion", {})
cfgdir.minimumCMakeVersion = minCMakeVer_dict.get("string", "")
cfgdir.hasInstallRule = dir_dict.get("hasInstallRule", False)
cfg.directories.append(cfgdir)
# parse and add each project
projects_arr = cfg_dict.get("projects", [])
for prj_dict in projects_arr:
if prj_dict != {}:
prj = cmakefileapi.ConfigProject()
prj.name = prj_dict.get("name", "")
prj.parentIndex = prj_dict.get("parentIndex", -1)
prj.childIndexes = prj_dict.get("childIndexes", [])
prj.directoryIndexes = prj_dict.get("directoryIndexes", [])
prj.targetIndexes = prj_dict.get("targetIndexes", [])
cfg.projects.append(prj)
# parse and add each target
cfgTargets_arr = cfg_dict.get("targets", [])
for cfgTarget_dict in cfgTargets_arr:
if cfgTarget_dict != {}:
cfgTarget = cmakefileapi.ConfigTarget()
cfgTarget.name = cfgTarget_dict.get("name", "")
cfgTarget.id = cfgTarget_dict.get("id", "")
cfgTarget.directoryIndex = cfgTarget_dict.get("directoryIndex", -1)
cfgTarget.projectIndex = cfgTarget_dict.get("projectIndex", -1)
cfgTarget.jsonFile = cfgTarget_dict.get("jsonFile", "")
if cfgTarget.jsonFile != "":
cfgTarget.target = parseTarget(os.path.join(replyDir, cfgTarget.jsonFile))
else:
cfgTarget.target = None
cfg.configTargets.append(cfgTarget)
return cfg
def parseTarget(targetPath):
try:
with open(targetPath, 'r') as targetFile:
js = json.load(targetFile)
target = cmakefileapi.Target()
target.name = js.get("name", "")
target.id = js.get("id", "")
target.type = parseTargetType(js.get("type", "UNKNOWN"))
target.backtrace = js.get("backtrace", -1)
target.folder = js.get("folder", "")
# get paths
paths_dict = js.get("paths", {})
target.paths_source = paths_dict.get("source", "")
target.paths_build = paths_dict.get("build", "")
target.nameOnDisk = js.get("nameOnDisk", "")
# parse artifacts if present
artifacts_arr = js.get("artifacts", [])
target.artifacts = []
for artifact_dict in artifacts_arr:
artifact_path = artifact_dict.get("path", "")
if artifact_path != "":
target.artifacts.append(artifact_path)
target.isGeneratorProvided = js.get("isGeneratorProvided", False)
# call separate functions to parse subsections
parseTargetInstall(target, js)
parseTargetLink(target, js)
parseTargetArchive(target, js)
parseTargetDependencies(target, js)
parseTargetSources(target, js)
parseTargetSourceGroups(target, js)
parseTargetCompileGroups(target, js)
parseTargetBacktraceGraph(target, js)
return target
except OSError as e:
print(f"Error loading {replyIndexPath}: {str(e)}")
return None
except json.decoder.JSONDecodeError as e:
print(f"Error parsing JSON in {replyIndexPath}: {str(e)}")
return None
def parseTargetType(targetType):
if targetType == "EXECUTABLE":
return cmakefileapi.TargetType.EXECUTABLE
elif targetType == "STATIC_LIBRARY":
return cmakefileapi.TargetType.STATIC_LIBRARY
elif targetType == "SHARED_LIBRARY":
return cmakefileapi.TargetType.SHARED_LIBRARY
elif targetType == "MODULE_LIBRARY":
return cmakefileapi.TargetType.MODULE_LIBRARY
elif targetType == "OBJECT_LIBRARY":
return cmakefileapi.TargetType.OBJECT_LIBRARY
elif targetType == "UTILITY":
return cmakefileapi.TargetType.UTILITY
else:
return cmakefileapi.TargetType.UNKNOWN
def parseTargetInstall(target, js):
install_dict = js.get("install", {})
if install_dict == {}:
return
prefix_dict = install_dict.get("prefix", {})
target.install_prefix = prefix_dict.get("path", "")
destinations_arr = install_dict.get("destinations", [])
for destination_dict in destinations_arr:
dest = cmakefileapi.TargetInstallDestination()
dest.path = destination_dict.get("path", "")
dest.backtrace = destination_dict.get("backtrace", -1)
target.install_destinations.append(dest)
def parseTargetLink(target, js):
link_dict = js.get("link", {})
if link_dict == {}:
return
target.link_language = link_dict.get("language", {})
target.link_lto = link_dict.get("lto", False)
sysroot_dict = link_dict.get("sysroot", {})
target.link_sysroot = sysroot_dict.get("path", "")
fragments_arr = link_dict.get("commandFragments", [])
for fragment_dict in fragments_arr:
fragment = cmakefileapi.TargetCommandFragment()
fragment.fragment = fragment_dict.get("fragment", "")
fragment.role = fragment_dict.get("role", "")
target.link_commandFragments.append(fragment)
def parseTargetArchive(target, js):
archive_dict = js.get("archive", {})
if archive_dict == {}:
return
target.archive_lto = archive_dict.get("lto", False)
fragments_arr = archive_dict.get("commandFragments", [])
for fragment_dict in fragments_arr:
fragment = cmakefileapi.TargetCommandFragment()
fragment.fragment = fragment_dict.get("fragment", "")
fragment.role = fragment_dict.get("role", "")
target.archive_commandFragments.append(fragment)
def parseTargetDependencies(target, js):
dependencies_arr = js.get("dependencies", [])
for dependency_dict in dependencies_arr:
dep = cmakefileapi.TargetDependency()
dep.id = dependency_dict.get("id", "")
dep.backtrace = dependency_dict.get("backtrace", -1)
target.dependencies.append(dep)
def parseTargetSources(target, js):
sources_arr = js.get("sources", [])
for source_dict in sources_arr:
src = cmakefileapi.TargetSource()
src.path = source_dict.get("path", "")
src.compileGroupIndex = source_dict.get("compileGroupIndex", -1)
src.sourceGroupIndex = source_dict.get("sourceGroupIndex", -1)
src.isGenerated = source_dict.get("isGenerated", False)
src.backtrace = source_dict.get("backtrace", -1)
target.sources.append(src)
def parseTargetSourceGroups(target, js):
sourceGroups_arr = js.get("sourceGroups", [])
for sourceGroup_dict in sourceGroups_arr:
srcgrp = cmakefileapi.TargetSourceGroup()
srcgrp.name = sourceGroup_dict.get("name", "")
srcgrp.sourceIndexes = sourceGroup_dict.get("sourceIndexes", [])
target.sourceGroups.append(srcgrp)
def parseTargetCompileGroups(target, js):
compileGroups_arr = js.get("compileGroups", [])
for compileGroup_dict in compileGroups_arr:
cmpgrp = cmakefileapi.TargetCompileGroup()
cmpgrp.sourceIndexes = compileGroup_dict.get("sourceIndexes", [])
cmpgrp.language = compileGroup_dict.get("language", "")
cmpgrp.sysroot = compileGroup_dict.get("sysroot", "")
commandFragments_arr = compileGroup_dict.get("compileCommandFragments", [])
for commandFragment_dict in commandFragments_arr:
fragment = commandFragment_dict.get("fragment", "")
if fragment != "":
cmpgrp.compileCommandFragments.append(fragment)
includes_arr = compileGroup_dict.get("includes", [])
for include_dict in includes_arr:
grpInclude = cmakefileapi.TargetCompileGroupInclude()
grpInclude.path = include_dict.get("path", "")
grpInclude.isSystem = include_dict.get("isSystem", False)
grpInclude.backtrace = include_dict.get("backtrace", -1)
cmpgrp.includes.append(grpInclude)
precompileHeaders_arr = compileGroup_dict.get("precompileHeaders", [])
for precompileHeader_dict in precompileHeaders_arr:
grpHeader = cmakefileapi.TargetCompileGroupPrecompileHeader()
grpHeader.header = precompileHeader_dict.get("header", "")
grpHeader.backtrace = precompileHeader_dict.get("backtrace", -1)
cmpgrp.precompileHeaders.append(grpHeader)
defines_arr = compileGroup_dict.get("defines", [])
for define_dict in defines_arr:
grpDefine = cmakefileapi.TargetCompileGroupDefine()
grpDefine.define = define_dict.get("define", "")
grpDefine.backtrace = define_dict.get("backtrace", -1)
cmpgrp.defines.append(grpDefine)
target.compileGroups.append(cmpgrp)
def parseTargetBacktraceGraph(target, js):
backtraceGraph_dict = js.get("backtraceGraph", {})
if backtraceGraph_dict == {}:
return
target.backtraceGraph_commands = backtraceGraph_dict.get("commands", [])
target.backtraceGraph_files = backtraceGraph_dict.get("files", [])
nodes_arr = backtraceGraph_dict.get("nodes", [])
for node_dict in nodes_arr:
node = cmakefileapi.TargetBacktraceGraphNode()
node.file = node_dict.get("file", -1)
node.line = node_dict.get("line", -1)
node.command = node_dict.get("command", -1)
node.parent = node_dict.get("parent", -1)
target.backtraceGraph_nodes.append(node)
# Create direct pointers for all Configs in Codemodel
# takes: Codemodel
def linkCodemodel(cm):
for cfg in cm.configurations:
linkConfig(cfg)
# Create direct pointers for all contents of Config
# takes: Config
def linkConfig(cfg):
for cfgDir in cfg.directories:
linkConfigDir(cfg, cfgDir)
for cfgPrj in cfg.projects:
linkConfigProject(cfg, cfgPrj)
for cfgTarget in cfg.configTargets:
linkConfigTarget(cfg, cfgTarget)
# Create direct pointers for ConfigDir indices
# takes: Config and ConfigDir
def linkConfigDir(cfg, cfgDir):
if cfgDir.parentIndex == -1:
cfgDir.parent = None
else:
cfgDir.parent = cfg.directories[cfgDir.parentIndex]
if cfgDir.projectIndex == -1:
cfgDir.project = None
else:
cfgDir.project = cfg.projects[cfgDir.projectIndex]
cfgDir.children = []
for childIndex in cfgDir.childIndexes:
cfgDir.children.append(cfg.directories[childIndex])
cfgDir.targets = []
for targetIndex in cfgDir.targetIndexes:
cfgDir.targets.append(cfg.configTargets[targetIndex])
# Create direct pointers for ConfigProject indices
# takes: Config and ConfigProject
def linkConfigProject(cfg, cfgPrj):
if cfgPrj.parentIndex == -1:
cfgPrj.parent = None
else:
cfgPrj.parent = cfg.projects[cfgPrj.parentIndex]
cfgPrj.children = []
for childIndex in cfgPrj.childIndexes:
cfgPrj.children.append(cfg.projects[childIndex])
cfgPrj.directories = []
for dirIndex in cfgPrj.directoryIndexes:
cfgPrj.directories.append(cfg.directories[dirIndex])
cfgPrj.targets = []
for targetIndex in cfgPrj.targetIndexes:
cfgPrj.targets.append(cfg.configTargets[targetIndex])
# Create direct pointers for ConfigTarget indices
# takes: Config and ConfigTarget
def linkConfigTarget(cfg, cfgTarget):
if cfgTarget.directoryIndex == -1:
cfgTarget.directory = None
else:
cfgTarget.directory = cfg.directories[cfgTarget.directoryIndex]
if cfgTarget.projectIndex == -1:
cfgTarget.project = None
else:
cfgTarget.project = cfg.projects[cfgTarget.projectIndex]
# and link target's sources and source groups
for ts in cfgTarget.target.sources:
linkTargetSource(cfgTarget.target, ts)
for tsg in cfgTarget.target.sourceGroups:
linkTargetSourceGroup(cfgTarget.target, tsg)
for tcg in cfgTarget.target.compileGroups:
linkTargetCompileGroup(cfgTarget.target, tcg)
# Create direct pointers for TargetSource indices
# takes: Target and TargetSource
def linkTargetSource(target, targetSrc):
if targetSrc.compileGroupIndex == -1:
targetSrc.compileGroup = None
else:
targetSrc.compileGroup = target.compileGroups[targetSrc.compileGroupIndex]
if targetSrc.sourceGroupIndex == -1:
targetSrc.sourceGroup = None
else:
targetSrc.sourceGroup = target.sourceGroups[targetSrc.sourceGroupIndex]
# Create direct pointers for TargetSourceGroup indices
# takes: Target and TargetSourceGroup
def linkTargetSourceGroup(target, targetSrcGrp):
targetSrcGrp.sources = []
for srcIndex in targetSrcGrp.sourceIndexes:
targetSrcGrp.sources.append(target.sources[srcIndex])
# Create direct pointers for TargetCompileGroup indices
# takes: Target and TargetCompileGroup
def linkTargetCompileGroup(target, targetCmpGrp):
targetCmpGrp.sources = []
for srcIndex in targetCmpGrp.sourceIndexes:
targetCmpGrp.sources.append(target.sources[srcIndex])