codemodel-v2-check.py 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. from check_index import *
  2. import json
  3. import sys
  4. import os
  5. def read_codemodel_json_data(filename):
  6. abs_filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "codemodel-v2-data", filename)
  7. with open(abs_filename, "r") as f:
  8. return json.load(f)
  9. def check_objects(o, g):
  10. assert is_list(o)
  11. assert len(o) == 1
  12. check_index_object(o[0], "codemodel", 2, 3, check_object_codemodel(g))
  13. def check_backtrace(t, b, backtrace):
  14. btg = t["backtraceGraph"]
  15. for expected in backtrace:
  16. assert is_int(b)
  17. node = btg["nodes"][b]
  18. expected_keys = ["file"]
  19. assert matches(btg["files"][node["file"]], expected["file"])
  20. if expected["line"] is not None:
  21. expected_keys.append("line")
  22. assert is_int(node["line"], expected["line"])
  23. if expected["command"] is not None:
  24. expected_keys.append("command")
  25. assert is_int(node["command"])
  26. assert is_string(btg["commands"][node["command"]], expected["command"])
  27. if expected["hasParent"]:
  28. expected_keys.append("parent")
  29. assert is_int(node["parent"])
  30. b = node["parent"]
  31. else:
  32. b = None
  33. assert sorted(node.keys()) == sorted(expected_keys)
  34. assert b is None
  35. def check_backtraces(t, actual, expected):
  36. assert is_list(actual)
  37. assert is_list(expected)
  38. assert len(actual) == len(expected)
  39. i = 0
  40. while i < len(actual):
  41. check_backtrace(t, actual[i], expected[i])
  42. i += 1
  43. def check_directory(c):
  44. def _check(actual, expected):
  45. assert is_dict(actual)
  46. expected_keys = ["build", "jsonFile", "source", "projectIndex"]
  47. assert matches(actual["build"], expected["build"])
  48. assert is_int(actual["projectIndex"])
  49. assert is_string(c["projects"][actual["projectIndex"]]["name"], expected["projectName"])
  50. if expected["parentSource"] is not None:
  51. expected_keys.append("parentIndex")
  52. assert is_int(actual["parentIndex"])
  53. assert matches(c["directories"][actual["parentIndex"]]["source"], expected["parentSource"])
  54. if expected["childSources"] is not None:
  55. expected_keys.append("childIndexes")
  56. check_list_match(lambda a, e: matches(c["directories"][a]["source"], e),
  57. actual["childIndexes"], expected["childSources"],
  58. missing_exception=lambda e: "Child source: %s" % e,
  59. extra_exception=lambda a: "Child source: %s" % a["source"])
  60. if expected["targetIds"] is not None:
  61. expected_keys.append("targetIndexes")
  62. check_list_match(lambda a, e: matches(c["targets"][a]["id"], e),
  63. actual["targetIndexes"], expected["targetIds"],
  64. missing_exception=lambda e: "Target ID: %s" % e,
  65. extra_exception=lambda a: "Target ID: %s" % c["targets"][a]["id"])
  66. if expected["minimumCMakeVersion"] is not None:
  67. expected_keys.append("minimumCMakeVersion")
  68. assert is_dict(actual["minimumCMakeVersion"])
  69. assert sorted(actual["minimumCMakeVersion"].keys()) == ["string"]
  70. assert is_string(actual["minimumCMakeVersion"]["string"], expected["minimumCMakeVersion"])
  71. if expected["hasInstallRule"] is not None:
  72. expected_keys.append("hasInstallRule")
  73. assert is_bool(actual["hasInstallRule"], expected["hasInstallRule"])
  74. assert sorted(actual.keys()) == sorted(expected_keys)
  75. assert is_string(actual["jsonFile"])
  76. filepath = os.path.join(reply_dir, actual["jsonFile"])
  77. with open(filepath) as f:
  78. d = json.load(f)
  79. assert is_dict(d)
  80. assert sorted(d.keys()) == ["backtraceGraph", "installers", "paths"]
  81. assert is_string(d["paths"]["source"], actual["source"])
  82. assert is_string(d["paths"]["build"], actual["build"])
  83. check_backtrace_graph(d["backtraceGraph"])
  84. assert is_list(d["installers"])
  85. assert len(d["installers"]) == len(expected["installers"])
  86. for a, e in zip(d["installers"], expected["installers"]):
  87. assert is_dict(a)
  88. expected_keys = ["component", "type"]
  89. assert is_string(a["component"], e["component"])
  90. assert is_string(a["type"], e["type"])
  91. if e["destination"] is not None:
  92. expected_keys.append("destination")
  93. assert is_string(a["destination"], e["destination"])
  94. if e["paths"] is not None:
  95. expected_keys.append("paths")
  96. assert is_list(a["paths"])
  97. assert len(a["paths"]) == len(e["paths"])
  98. for ap, ep in zip(a["paths"], e["paths"]):
  99. if is_string(ep):
  100. assert matches(ap, ep)
  101. else:
  102. assert is_dict(ap)
  103. assert sorted(ap.keys()) == ["from", "to"]
  104. assert matches(ap["from"], ep["from"])
  105. assert matches(ap["to"], ep["to"])
  106. if e["isExcludeFromAll"] is not None:
  107. expected_keys.append("isExcludeFromAll")
  108. assert is_bool(a["isExcludeFromAll"], e["isExcludeFromAll"])
  109. if e["isForAllComponents"] is not None:
  110. expected_keys.append("isForAllComponents")
  111. assert is_bool(a["isForAllComponents"], e["isForAllComponents"])
  112. if e["isOptional"] is not None:
  113. expected_keys.append("isOptional")
  114. assert is_bool(a["isOptional"], e["isOptional"])
  115. if e["targetId"] is not None:
  116. expected_keys.append("targetId")
  117. assert matches(a["targetId"], e["targetId"])
  118. if e["targetIndex"] is not None:
  119. expected_keys.append("targetIndex")
  120. assert is_int(a["targetIndex"])
  121. assert c["targets"][a["targetIndex"]]["name"] == e["targetIndex"]
  122. if e["targetIsImportLibrary"] is not None:
  123. expected_keys.append("targetIsImportLibrary")
  124. assert is_bool(a["targetIsImportLibrary"], e["targetIsImportLibrary"])
  125. if e["targetInstallNamelink"] is not None:
  126. expected_keys.append("targetInstallNamelink")
  127. assert is_string(a["targetInstallNamelink"], e["targetInstallNamelink"])
  128. if e["exportName"] is not None:
  129. expected_keys.append("exportName")
  130. assert is_string(a["exportName"], e["exportName"])
  131. if e["exportTargets"] is not None:
  132. expected_keys.append("exportTargets")
  133. assert is_list(a["exportTargets"])
  134. assert len(a["exportTargets"]) == len(e["exportTargets"])
  135. for at, et in zip(a["exportTargets"], e["exportTargets"]):
  136. assert is_dict(at)
  137. assert sorted(at.keys()) == ["id", "index"]
  138. assert matches(at["id"], et["id"])
  139. assert is_int(at["index"])
  140. assert c["targets"][at["index"]]["name"] == et["index"]
  141. if e["scriptFile"] is not None:
  142. expected_keys.append("scriptFile")
  143. assert is_string(a["scriptFile"], e["scriptFile"])
  144. if e["backtrace"] is not None:
  145. expected_keys.append("backtrace")
  146. check_backtrace(d, a["backtrace"], e["backtrace"])
  147. assert sorted(a.keys()) == sorted(expected_keys)
  148. return _check
  149. def check_backtrace_graph(btg):
  150. assert is_dict(btg)
  151. assert sorted(btg.keys()) == ["commands", "files", "nodes"]
  152. assert is_list(btg["commands"])
  153. for c in btg["commands"]:
  154. assert is_string(c)
  155. for f in btg["files"]:
  156. assert is_string(f)
  157. for n in btg["nodes"]:
  158. expected_keys = ["file"]
  159. assert is_dict(n)
  160. assert is_int(n["file"])
  161. assert 0 <= n["file"] < len(btg["files"])
  162. if "line" in n:
  163. expected_keys.append("line")
  164. assert is_int(n["line"])
  165. if "command" in n:
  166. expected_keys.append("command")
  167. assert is_int(n["command"])
  168. assert 0 <= n["command"] < len(btg["commands"])
  169. if "parent" in n:
  170. expected_keys.append("parent")
  171. assert is_int(n["parent"])
  172. assert 0 <= n["parent"] < len(btg["nodes"])
  173. assert sorted(n.keys()) == sorted(expected_keys)
  174. def check_target(c):
  175. def _check(actual, expected):
  176. assert is_dict(actual)
  177. assert sorted(actual.keys()) == ["directoryIndex", "id", "jsonFile", "name", "projectIndex"]
  178. assert is_int(actual["directoryIndex"])
  179. assert matches(c["directories"][actual["directoryIndex"]]["source"], expected["directorySource"])
  180. assert is_string(actual["name"], expected["name"])
  181. assert is_string(actual["jsonFile"])
  182. assert is_int(actual["projectIndex"])
  183. assert is_string(c["projects"][actual["projectIndex"]]["name"], expected["projectName"])
  184. filepath = os.path.join(reply_dir, actual["jsonFile"])
  185. with open(filepath) as f:
  186. obj = json.load(f)
  187. expected_keys = ["name", "id", "type", "backtraceGraph", "paths", "sources"]
  188. assert is_dict(obj)
  189. assert is_string(obj["name"], expected["name"])
  190. assert matches(obj["id"], expected["id"])
  191. assert is_string(obj["type"], expected["type"])
  192. check_backtrace_graph(obj["backtraceGraph"])
  193. assert is_dict(obj["paths"])
  194. assert sorted(obj["paths"].keys()) == ["build", "source"]
  195. assert matches(obj["paths"]["build"], expected["build"])
  196. assert matches(obj["paths"]["source"], expected["source"])
  197. def check_source(actual, expected):
  198. assert is_dict(actual)
  199. expected_keys = ["path"]
  200. if expected["compileGroupLanguage"] is not None:
  201. expected_keys.append("compileGroupIndex")
  202. assert is_string(obj["compileGroups"][actual["compileGroupIndex"]]["language"], expected["compileGroupLanguage"])
  203. if expected["sourceGroupName"] is not None:
  204. expected_keys.append("sourceGroupIndex")
  205. assert is_string(obj["sourceGroups"][actual["sourceGroupIndex"]]["name"], expected["sourceGroupName"])
  206. if expected["isGenerated"] is not None:
  207. expected_keys.append("isGenerated")
  208. assert is_bool(actual["isGenerated"], expected["isGenerated"])
  209. if expected["backtrace"] is not None:
  210. expected_keys.append("backtrace")
  211. check_backtrace(obj, actual["backtrace"], expected["backtrace"])
  212. assert sorted(actual.keys()) == sorted(expected_keys)
  213. check_list_match(lambda a, e: matches(a["path"], e["path"]), obj["sources"],
  214. expected["sources"], check=check_source,
  215. check_exception=lambda a, e: "Source file: %s" % a["path"],
  216. missing_exception=lambda e: "Source file: %s" % e["path"],
  217. extra_exception=lambda a: "Source file: %s" % a["path"])
  218. if expected["backtrace"] is not None:
  219. expected_keys.append("backtrace")
  220. check_backtrace(obj, obj["backtrace"], expected["backtrace"])
  221. if expected["folder"] is not None:
  222. expected_keys.append("folder")
  223. assert is_dict(obj["folder"])
  224. assert sorted(obj["folder"].keys()) == ["name"]
  225. assert is_string(obj["folder"]["name"], expected["folder"])
  226. if expected["nameOnDisk"] is not None:
  227. expected_keys.append("nameOnDisk")
  228. assert matches(obj["nameOnDisk"], expected["nameOnDisk"])
  229. if expected["artifacts"] is not None:
  230. expected_keys.append("artifacts")
  231. def check_artifact(actual, expected):
  232. assert is_dict(actual)
  233. assert sorted(actual.keys()) == ["path"]
  234. check_list_match(lambda a, e: matches(a["path"], e["path"]),
  235. obj["artifacts"], expected["artifacts"],
  236. check=check_artifact,
  237. check_exception=lambda a, e: "Artifact: %s" % a["path"],
  238. missing_exception=lambda e: "Artifact: %s" % e["path"],
  239. extra_exception=lambda a: "Artifact: %s" % a["path"])
  240. if expected["isGeneratorProvided"] is not None:
  241. expected_keys.append("isGeneratorProvided")
  242. assert is_bool(obj["isGeneratorProvided"], expected["isGeneratorProvided"])
  243. if expected["install"] is not None:
  244. expected_keys.append("install")
  245. assert is_dict(obj["install"])
  246. assert sorted(obj["install"].keys()) == ["destinations", "prefix"]
  247. assert is_dict(obj["install"]["prefix"])
  248. assert sorted(obj["install"]["prefix"].keys()) == ["path"]
  249. assert matches(obj["install"]["prefix"]["path"], expected["install"]["prefix"])
  250. def check_install_destination(actual, expected):
  251. assert is_dict(actual)
  252. expected_keys = ["path"]
  253. if expected["backtrace"] is not None:
  254. expected_keys.append("backtrace")
  255. check_backtrace(obj, actual["backtrace"], expected["backtrace"])
  256. assert sorted(actual.keys()) == sorted(expected_keys)
  257. check_list_match(lambda a, e: matches(a["path"], e["path"]),
  258. obj["install"]["destinations"], expected["install"]["destinations"],
  259. check=check_install_destination,
  260. check_exception=lambda a, e: "Install path: %s" % a["path"],
  261. missing_exception=lambda e: "Install path: %s" % e["path"],
  262. extra_exception=lambda a: "Install path: %s" % a["path"])
  263. if expected["link"] is not None:
  264. expected_keys.append("link")
  265. assert is_dict(obj["link"])
  266. link_keys = ["language"]
  267. assert is_string(obj["link"]["language"], expected["link"]["language"])
  268. if "commandFragments" in obj["link"]:
  269. link_keys.append("commandFragments")
  270. assert is_list(obj["link"]["commandFragments"])
  271. for f in obj["link"]["commandFragments"]:
  272. assert is_dict(f)
  273. assert sorted(f.keys()) == ["fragment", "role"] or sorted(f.keys()) == ["backtrace", "fragment", "role"]
  274. assert is_string(f["fragment"])
  275. assert is_string(f["role"])
  276. assert f["role"] in ("flags", "libraries", "libraryPath", "frameworkPath")
  277. if expected["link"]["commandFragments"] is not None:
  278. def check_link_command_fragments(actual, expected):
  279. assert is_dict(actual)
  280. expected_keys = ["fragment", "role"]
  281. if expected["backtrace"] is not None:
  282. expected_keys.append("backtrace")
  283. assert matches(actual["fragment"], expected["fragment"])
  284. assert actual["role"] == expected["role"]
  285. check_backtrace(obj, actual["backtrace"], expected["backtrace"])
  286. assert sorted(actual.keys()) == sorted(expected_keys)
  287. check_list_match(lambda a, e: matches(a["fragment"], e["fragment"]),
  288. obj["link"]["commandFragments"], expected["link"]["commandFragments"],
  289. check=check_link_command_fragments,
  290. check_exception=lambda a, e: "Link fragment: %s" % a["fragment"],
  291. missing_exception=lambda e: "Link fragment: %s" % e["fragment"],
  292. extra_exception=lambda a: "Link fragment: %s" % a["fragment"],
  293. allow_extra=True)
  294. if expected["link"]["lto"] is not None:
  295. link_keys.append("lto")
  296. assert is_bool(obj["link"]["lto"], expected["link"]["lto"])
  297. # FIXME: Properly test sysroot
  298. if "sysroot" in obj["link"]:
  299. link_keys.append("sysroot")
  300. assert is_string(obj["link"]["sysroot"])
  301. assert sorted(obj["link"].keys()) == sorted(link_keys)
  302. if expected["archive"] is not None:
  303. expected_keys.append("archive")
  304. assert is_dict(obj["archive"])
  305. archive_keys = []
  306. # FIXME: Properly test commandFragments
  307. if "commandFragments" in obj["archive"]:
  308. archive_keys.append("commandFragments")
  309. assert is_list(obj["archive"]["commandFragments"])
  310. for f in obj["archive"]["commandFragments"]:
  311. assert is_dict(f)
  312. assert sorted(f.keys()) == ["fragment", "role"]
  313. assert is_string(f["fragment"])
  314. assert is_string(f["role"])
  315. assert f["role"] in ("flags")
  316. if expected["archive"]["lto"] is not None:
  317. archive_keys.append("lto")
  318. assert is_bool(obj["archive"]["lto"], expected["archive"]["lto"])
  319. assert sorted(obj["archive"].keys()) == sorted(archive_keys)
  320. if expected["dependencies"] is not None:
  321. expected_keys.append("dependencies")
  322. def check_dependency(actual, expected):
  323. assert is_dict(actual)
  324. expected_keys = ["id"]
  325. if expected["backtrace"] is not None:
  326. expected_keys.append("backtrace")
  327. check_backtrace(obj, actual["backtrace"], expected["backtrace"])
  328. assert sorted(actual.keys()) == sorted(expected_keys)
  329. check_list_match(lambda a, e: matches(a["id"], e["id"]),
  330. obj["dependencies"], expected["dependencies"],
  331. check=check_dependency,
  332. check_exception=lambda a, e: "Dependency ID: %s" % a["id"],
  333. missing_exception=lambda e: "Dependency ID: %s" % e["id"],
  334. extra_exception=lambda a: "Dependency ID: %s" % a["id"])
  335. if expected["sourceGroups"] is not None:
  336. expected_keys.append("sourceGroups")
  337. def check_source_group(actual, expected):
  338. assert is_dict(actual)
  339. assert sorted(actual.keys()) == ["name", "sourceIndexes"]
  340. check_list_match(lambda a, e: matches(obj["sources"][a]["path"], e),
  341. actual["sourceIndexes"], expected["sourcePaths"],
  342. missing_exception=lambda e: "Source path: %s" % e,
  343. extra_exception=lambda a: "Source path: %s" % obj["sources"][a]["path"])
  344. check_list_match(lambda a, e: is_string(a["name"], e["name"]),
  345. obj["sourceGroups"], expected["sourceGroups"],
  346. check=check_source_group,
  347. check_exception=lambda a, e: "Source group: %s" % a["name"],
  348. missing_exception=lambda e: "Source group: %s" % e["name"],
  349. extra_exception=lambda a: "Source group: %s" % a["name"])
  350. if expected["compileGroups"] is not None:
  351. expected_keys.append("compileGroups")
  352. def check_compile_group(actual, expected):
  353. assert is_dict(actual)
  354. expected_keys = ["sourceIndexes", "language"]
  355. check_list_match(lambda a, e: matches(obj["sources"][a]["path"], e),
  356. actual["sourceIndexes"], expected["sourcePaths"],
  357. missing_exception=lambda e: "Source path: %s" % e,
  358. extra_exception=lambda a: "Source path: %s" % obj["sources"][a]["path"])
  359. if "compileCommandFragments" in actual:
  360. expected_keys.append("compileCommandFragments")
  361. assert is_list(actual["compileCommandFragments"])
  362. for f in actual["compileCommandFragments"]:
  363. assert is_dict(f)
  364. assert is_string(f["fragment"])
  365. if expected["compileCommandFragments"] is not None:
  366. def check_compile_command_fragments(actual, expected):
  367. assert is_dict(actual)
  368. expected_keys = ["fragment"]
  369. if expected["backtrace"] is not None:
  370. expected_keys.append("backtrace")
  371. assert actual["fragment"] == expected["fragment"]
  372. check_backtrace(obj, actual["backtrace"], expected["backtrace"])
  373. assert sorted(actual.keys()) == sorted(expected_keys)
  374. check_list_match(lambda a, e: is_string(a["fragment"], e["fragment"]),
  375. actual["compileCommandFragments"], expected["compileCommandFragments"],
  376. check=check_compile_command_fragments,
  377. check_exception=lambda a, e: "Compile fragment: %s" % a["fragment"],
  378. missing_exception=lambda e: "Compile fragment: %s" % e["fragment"],
  379. extra_exception=lambda a: "Compile fragment: %s" % a["fragment"],
  380. allow_extra=True)
  381. if expected["includes"] is not None:
  382. expected_keys.append("includes")
  383. def check_include(actual, expected):
  384. assert is_dict(actual)
  385. expected_keys = ["path"]
  386. if expected["isSystem"] is not None:
  387. expected_keys.append("isSystem")
  388. assert is_bool(actual["isSystem"], expected["isSystem"])
  389. if expected["backtrace"] is not None:
  390. expected_keys.append("backtrace")
  391. check_backtrace(obj, actual["backtrace"], expected["backtrace"])
  392. assert sorted(actual.keys()) == sorted(expected_keys)
  393. check_list_match(lambda a, e: matches(a["path"], e["path"]),
  394. actual["includes"], expected["includes"],
  395. check=check_include,
  396. check_exception=lambda a, e: "Include path: %s" % a["path"],
  397. missing_exception=lambda e: "Include path: %s" % e["path"],
  398. extra_exception=lambda a: "Include path: %s" % a["path"])
  399. if "precompileHeaders" in expected:
  400. expected_keys.append("precompileHeaders")
  401. def check_precompile_header(actual, expected):
  402. assert is_dict(actual)
  403. expected_keys = ["backtrace", "header"]
  404. check_backtrace(obj, actual["backtrace"], expected["backtrace"])
  405. assert sorted(actual.keys()) == sorted(expected_keys)
  406. check_list_match(lambda a, e: matches(a["header"], e["header"]),
  407. actual["precompileHeaders"], expected["precompileHeaders"],
  408. check=check_precompile_header,
  409. check_exception=lambda a, e: "Precompile header: %s" % a["header"],
  410. missing_exception=lambda e: "Precompile header: %s" % e["header"],
  411. extra_exception=lambda a: "Precompile header: %s" % a["header"])
  412. if "languageStandard" in expected:
  413. expected_keys.append("languageStandard")
  414. def check_language_standard(actual, expected):
  415. assert is_dict(actual)
  416. expected_keys = ["backtraces", "standard"]
  417. assert actual["standard"] == expected["standard"]
  418. check_backtraces(obj, actual["backtraces"], expected["backtraces"])
  419. assert sorted(actual.keys()) == sorted(expected_keys)
  420. check_language_standard(actual["languageStandard"], expected["languageStandard"])
  421. if expected["defines"] is not None:
  422. expected_keys.append("defines")
  423. def check_define(actual, expected):
  424. assert is_dict(actual)
  425. expected_keys = ["define"]
  426. if expected["backtrace"] is not None:
  427. expected_keys.append("backtrace")
  428. check_backtrace(obj, actual["backtrace"], expected["backtrace"])
  429. assert sorted(actual.keys()) == sorted(expected_keys)
  430. check_list_match(lambda a, e: is_string(a["define"], e["define"]),
  431. actual["defines"], expected["defines"],
  432. check=check_define,
  433. check_exception=lambda a, e: "Define: %s" % a["define"],
  434. missing_exception=lambda e: "Define: %s" % e["define"],
  435. extra_exception=lambda a: "Define: %s" % a["define"])
  436. # FIXME: Properly test sysroot
  437. if "sysroot" in actual:
  438. expected_keys.append("sysroot")
  439. assert is_string(actual["sysroot"])
  440. assert sorted(actual.keys()) == sorted(expected_keys)
  441. check_list_match(lambda a, e: is_string(a["language"], e["language"]),
  442. obj["compileGroups"], expected["compileGroups"],
  443. check=check_compile_group,
  444. check_exception=lambda a, e: "Compile group: %s" % a["language"],
  445. missing_exception=lambda e: "Compile group: %s" % e["language"],
  446. extra_exception=lambda a: "Compile group: %s" % a["language"])
  447. assert sorted(obj.keys()) == sorted(expected_keys)
  448. return _check
  449. def check_project(c):
  450. def _check(actual, expected):
  451. assert is_dict(actual)
  452. expected_keys = ["name", "directoryIndexes"]
  453. check_list_match(lambda a, e: matches(c["directories"][a]["source"], e),
  454. actual["directoryIndexes"], expected["directorySources"],
  455. missing_exception=lambda e: "Directory source: %s" % e,
  456. extra_exception=lambda a: "Directory source: %s" % c["directories"][a]["source"])
  457. if expected["parentName"] is not None:
  458. expected_keys.append("parentIndex")
  459. assert is_int(actual["parentIndex"])
  460. assert is_string(c["projects"][actual["parentIndex"]]["name"], expected["parentName"])
  461. if expected["childNames"] is not None:
  462. expected_keys.append("childIndexes")
  463. check_list_match(lambda a, e: is_string(c["projects"][a]["name"], e),
  464. actual["childIndexes"], expected["childNames"],
  465. missing_exception=lambda e: "Child name: %s" % e,
  466. extra_exception=lambda a: "Child name: %s" % c["projects"][a]["name"])
  467. if expected["targetIds"] is not None:
  468. expected_keys.append("targetIndexes")
  469. check_list_match(lambda a, e: matches(c["targets"][a]["id"], e),
  470. actual["targetIndexes"], expected["targetIds"],
  471. missing_exception=lambda e: "Target ID: %s" % e,
  472. extra_exception=lambda a: "Target ID: %s" % c["targets"][a]["id"])
  473. assert sorted(actual.keys()) == sorted(expected_keys)
  474. return _check
  475. def gen_check_directories(c, g):
  476. expected = [
  477. read_codemodel_json_data("directories/top.json"),
  478. read_codemodel_json_data("directories/alias.json"),
  479. read_codemodel_json_data("directories/custom.json"),
  480. read_codemodel_json_data("directories/cxx.json"),
  481. read_codemodel_json_data("directories/imported.json"),
  482. read_codemodel_json_data("directories/interface.json"),
  483. read_codemodel_json_data("directories/object.json"),
  484. read_codemodel_json_data("directories/dir.json"),
  485. read_codemodel_json_data("directories/dir_dir.json"),
  486. read_codemodel_json_data("directories/external.json"),
  487. ]
  488. if matches(g["name"], "^Visual Studio "):
  489. for e in expected:
  490. if e["parentSource"] is not None:
  491. e["targetIds"] = filter_list(lambda t: not matches(t, "^\\^ZERO_CHECK"), e["targetIds"])
  492. elif g["name"] == "Xcode":
  493. if ';' in os.environ.get("CMAKE_OSX_ARCHITECTURES", ""):
  494. for e in expected:
  495. e["targetIds"] = filter_list(lambda t: not matches(t, "^\\^(link_imported_object_exe)"), e["targetIds"])
  496. else:
  497. for e in expected:
  498. e["targetIds"] = filter_list(lambda t: not matches(t, "^\\^(ALL_BUILD|ZERO_CHECK)"), e["targetIds"])
  499. if sys.platform in ("win32", "cygwin", "msys") or "aix" in sys.platform:
  500. for e in expected:
  501. e["installers"] = list(filter(lambda i: i["targetInstallNamelink"] is None or i["targetInstallNamelink"] == "skip", e["installers"]))
  502. for i in e["installers"]:
  503. i["targetInstallNamelink"] = None
  504. if sys.platform not in ("win32", "cygwin", "msys"):
  505. for e in expected:
  506. e["installers"] = list(filter(lambda i: "_dllExtra" not in i or not i["_dllExtra"], e["installers"]))
  507. if "aix" not in sys.platform:
  508. for i in e["installers"]:
  509. if "pathsNamelink" in i:
  510. i["paths"] = i["pathsNamelink"]
  511. return expected
  512. def check_directories(c, g):
  513. check_list_match(lambda a, e: matches(a["source"], e["source"]), c["directories"], gen_check_directories(c, g),
  514. check=check_directory(c),
  515. check_exception=lambda a, e: "Directory source: %s" % a["source"],
  516. missing_exception=lambda e: "Directory source: %s" % e["source"],
  517. extra_exception=lambda a: "Directory source: %s" % a["source"])
  518. def gen_check_targets(c, g, inSource):
  519. expected = [
  520. read_codemodel_json_data("targets/all_build_top.json"),
  521. read_codemodel_json_data("targets/zero_check_top.json"),
  522. read_codemodel_json_data("targets/interface_exe.json"),
  523. read_codemodel_json_data("targets/c_lib.json"),
  524. read_codemodel_json_data("targets/c_exe.json"),
  525. read_codemodel_json_data("targets/c_shared_lib.json"),
  526. read_codemodel_json_data("targets/c_shared_exe.json"),
  527. read_codemodel_json_data("targets/c_static_lib.json"),
  528. read_codemodel_json_data("targets/c_static_exe.json"),
  529. read_codemodel_json_data("targets/all_build_cxx.json"),
  530. read_codemodel_json_data("targets/zero_check_cxx.json"),
  531. read_codemodel_json_data("targets/cxx_lib.json"),
  532. read_codemodel_json_data("targets/cxx_exe.json"),
  533. read_codemodel_json_data("targets/cxx_standard_compile_feature_exe.json"),
  534. read_codemodel_json_data("targets/cxx_standard_exe.json"),
  535. read_codemodel_json_data("targets/cxx_shared_lib.json"),
  536. read_codemodel_json_data("targets/cxx_shared_exe.json"),
  537. read_codemodel_json_data("targets/cxx_static_lib.json"),
  538. read_codemodel_json_data("targets/cxx_static_exe.json"),
  539. read_codemodel_json_data("targets/all_build_alias.json"),
  540. read_codemodel_json_data("targets/zero_check_alias.json"),
  541. read_codemodel_json_data("targets/c_alias_exe.json"),
  542. read_codemodel_json_data("targets/cxx_alias_exe.json"),
  543. read_codemodel_json_data("targets/all_build_object.json"),
  544. read_codemodel_json_data("targets/zero_check_object.json"),
  545. read_codemodel_json_data("targets/c_object_lib.json"),
  546. read_codemodel_json_data("targets/c_object_exe.json"),
  547. read_codemodel_json_data("targets/cxx_object_lib.json"),
  548. read_codemodel_json_data("targets/cxx_object_exe.json"),
  549. read_codemodel_json_data("targets/all_build_imported.json"),
  550. read_codemodel_json_data("targets/zero_check_imported.json"),
  551. read_codemodel_json_data("targets/link_imported_exe.json"),
  552. read_codemodel_json_data("targets/link_imported_shared_exe.json"),
  553. read_codemodel_json_data("targets/link_imported_static_exe.json"),
  554. read_codemodel_json_data("targets/link_imported_object_exe.json"),
  555. read_codemodel_json_data("targets/link_imported_interface_exe.json"),
  556. read_codemodel_json_data("targets/all_build_interface.json"),
  557. read_codemodel_json_data("targets/zero_check_interface.json"),
  558. read_codemodel_json_data("targets/iface_srcs.json"),
  559. read_codemodel_json_data("targets/all_build_custom.json"),
  560. read_codemodel_json_data("targets/zero_check_custom.json"),
  561. read_codemodel_json_data("targets/custom_tgt.json"),
  562. read_codemodel_json_data("targets/custom_exe.json"),
  563. read_codemodel_json_data("targets/all_build_external.json"),
  564. read_codemodel_json_data("targets/zero_check_external.json"),
  565. read_codemodel_json_data("targets/generated_exe.json"),
  566. ]
  567. if cxx_compiler_id in ['Clang', 'AppleClang', 'GNU', 'Intel', 'IntelLLVM', 'MSVC', 'Embarcadero'] and g["name"] != "Xcode":
  568. for e in expected:
  569. if e["name"] == "cxx_exe":
  570. if matches(g["name"], "^(Visual Studio |Ninja Multi-Config)"):
  571. precompile_header_data = read_codemodel_json_data("targets/cxx_exe_precompileheader_multigen.json")
  572. else:
  573. if ';' in os.environ.get("CMAKE_OSX_ARCHITECTURES", ""):
  574. precompile_header_data = read_codemodel_json_data("targets/cxx_exe_precompileheader_2arch.json")
  575. else:
  576. precompile_header_data = read_codemodel_json_data("targets/cxx_exe_precompileheader.json")
  577. e["compileGroups"] = precompile_header_data["compileGroups"]
  578. e["sources"] = precompile_header_data["sources"]
  579. e["sourceGroups"] = precompile_header_data["sourceGroups"]
  580. if os.path.exists(os.path.join(reply_dir, "..", "..", "..", "..", "cxx", "cxx_std_11.txt")):
  581. for e in expected:
  582. if e["name"] == "cxx_standard_compile_feature_exe":
  583. language_standard_data = read_codemodel_json_data("targets/cxx_standard_compile_feature_exe_languagestandard.json")
  584. e["compileGroups"][0]["languageStandard"] = language_standard_data["languageStandard"]
  585. if not os.path.exists(os.path.join(reply_dir, "..", "..", "..", "..", "ipo_enabled.txt")):
  586. for e in expected:
  587. try:
  588. e["link"]["lto"] = None
  589. except TypeError: # "link" is not a dict, no problem.
  590. pass
  591. try:
  592. e["archive"]["lto"] = None
  593. except TypeError: # "archive" is not a dict, no problem.
  594. pass
  595. if inSource:
  596. for e in expected:
  597. if e["sources"] is not None:
  598. for s in e["sources"]:
  599. s["path"] = s["path"].replace("^.*/Tests/RunCMake/FileAPI/", "^", 1)
  600. if e["sourceGroups"] is not None:
  601. for group in e["sourceGroups"]:
  602. group["sourcePaths"] = [p.replace("^.*/Tests/RunCMake/FileAPI/", "^", 1) for p in group["sourcePaths"]]
  603. if e["compileGroups"] is not None:
  604. for group in e["compileGroups"]:
  605. group["sourcePaths"] = [p.replace("^.*/Tests/RunCMake/FileAPI/", "^", 1) for p in group["sourcePaths"]]
  606. if matches(g["name"], "^Visual Studio "):
  607. expected = filter_list(lambda e: e["name"] not in ("ZERO_CHECK") or e["id"] == "^ZERO_CHECK::@6890427a1f51a3e7e1df$", expected)
  608. for e in expected:
  609. if e["type"] == "UTILITY":
  610. if e["id"] == "^ZERO_CHECK::@6890427a1f51a3e7e1df$":
  611. e["sources"] = [
  612. {
  613. "path": "^.*/Tests/RunCMake/FileAPI/codemodel-v2-build/CMakeFiles/([0-9a-f]+/)?generate\\.stamp\\.rule$",
  614. "isGenerated": True,
  615. "sourceGroupName": "CMake Rules",
  616. "compileGroupLanguage": None,
  617. "backtrace": [
  618. {
  619. "file": "^CMakeLists\\.txt$",
  620. "line": None,
  621. "command": None,
  622. "hasParent": False,
  623. },
  624. ],
  625. },
  626. ]
  627. e["sourceGroups"] = [
  628. {
  629. "name": "CMake Rules",
  630. "sourcePaths": [
  631. "^.*/Tests/RunCMake/FileAPI/codemodel-v2-build/CMakeFiles/([0-9a-f]+/)?generate\\.stamp\\.rule$",
  632. ],
  633. },
  634. ]
  635. elif e["name"] in ("ALL_BUILD"):
  636. e["sources"] = []
  637. e["sourceGroups"] = None
  638. if e["dependencies"] is not None:
  639. for d in e["dependencies"]:
  640. if matches(d["id"], "^\\^ZERO_CHECK::@"):
  641. d["id"] = "^ZERO_CHECK::@6890427a1f51a3e7e1df$"
  642. elif g["name"] == "Xcode":
  643. if ';' in os.environ.get("CMAKE_OSX_ARCHITECTURES", ""):
  644. expected = filter_list(lambda e: e["name"] not in ("link_imported_object_exe"), expected)
  645. for e in expected:
  646. e["dependencies"] = filter_list(lambda d: not matches(d["id"], "^\\^link_imported_object_exe::@"), e["dependencies"])
  647. if e["name"] in ("c_object_lib", "cxx_object_lib"):
  648. e["artifacts"] = None
  649. else:
  650. for e in expected:
  651. e["dependencies"] = filter_list(lambda d: not matches(d["id"], "^\\^ZERO_CHECK::@"), e["dependencies"])
  652. expected = filter_list(lambda t: t["name"] not in ("ALL_BUILD", "ZERO_CHECK"), expected)
  653. if sys.platform not in ("win32", "cygwin", "msys"):
  654. for e in expected:
  655. e["artifacts"] = filter_list(lambda a: not a["_dllExtra"], e["artifacts"])
  656. if e["install"] is not None:
  657. e["install"]["destinations"] = filter_list(lambda d: "_dllExtra" not in d or not d["_dllExtra"], e["install"]["destinations"])
  658. else:
  659. for e in expected:
  660. if e["install"] is not None:
  661. e["install"]["destinations"] = filter_list(lambda d: "_namelink" not in d or not d["_namelink"], e["install"]["destinations"])
  662. if "aix" not in sys.platform:
  663. for e in expected:
  664. e["artifacts"] = filter_list(lambda a: not a.get("_aixExtra", False), e["artifacts"])
  665. return expected
  666. def check_targets(c, g, inSource):
  667. check_list_match(lambda a, e: matches(a["id"], e["id"]),
  668. c["targets"], gen_check_targets(c, g, inSource),
  669. check=check_target(c),
  670. check_exception=lambda a, e: "Target ID: %s" % a["id"],
  671. missing_exception=lambda e: "Target ID: %s" % e["id"],
  672. extra_exception=lambda a: "Target ID: %s" % a["id"])
  673. def gen_check_projects(c, g):
  674. expected = [
  675. read_codemodel_json_data("projects/codemodel-v2.json"),
  676. read_codemodel_json_data("projects/cxx.json"),
  677. read_codemodel_json_data("projects/alias.json"),
  678. read_codemodel_json_data("projects/object.json"),
  679. read_codemodel_json_data("projects/imported.json"),
  680. read_codemodel_json_data("projects/interface.json"),
  681. read_codemodel_json_data("projects/custom.json"),
  682. read_codemodel_json_data("projects/external.json"),
  683. ]
  684. if matches(g["name"], "^Visual Studio "):
  685. for e in expected:
  686. if e["parentName"] is not None:
  687. e["targetIds"] = filter_list(lambda t: not matches(t, "^\\^ZERO_CHECK"), e["targetIds"])
  688. elif g["name"] == "Xcode":
  689. if ';' in os.environ.get("CMAKE_OSX_ARCHITECTURES", ""):
  690. for e in expected:
  691. e["targetIds"] = filter_list(lambda t: not matches(t, "^\\^(link_imported_object_exe)"), e["targetIds"])
  692. else:
  693. for e in expected:
  694. e["targetIds"] = filter_list(lambda t: not matches(t, "^\\^(ALL_BUILD|ZERO_CHECK)"), e["targetIds"])
  695. return expected
  696. def check_projects(c, g):
  697. check_list_match(lambda a, e: is_string(a["name"], e["name"]), c["projects"], gen_check_projects(c, g),
  698. check=check_project(c),
  699. check_exception=lambda a, e: "Project name: %s" % a["name"],
  700. missing_exception=lambda e: "Project name: %s" % e["name"],
  701. extra_exception=lambda a: "Project name: %s" % a["name"])
  702. def check_object_codemodel_configuration(c, g, inSource):
  703. assert sorted(c.keys()) == ["directories", "name", "projects", "targets"]
  704. assert is_string(c["name"])
  705. check_directories(c, g)
  706. check_targets(c, g, inSource)
  707. check_projects(c, g)
  708. def check_object_codemodel(g):
  709. def _check(o):
  710. assert sorted(o.keys()) == ["configurations", "kind", "paths", "version"]
  711. # The "kind" and "version" members are handled by check_index_object.
  712. assert is_dict(o["paths"])
  713. assert sorted(o["paths"].keys()) == ["build", "source"]
  714. assert matches(o["paths"]["build"], "^.*/Tests/RunCMake/FileAPI/codemodel-v2-build$")
  715. assert matches(o["paths"]["source"], "^.*/Tests/RunCMake/FileAPI$")
  716. inSource = os.path.dirname(o["paths"]["build"]) == o["paths"]["source"]
  717. if g["multiConfig"]:
  718. assert sorted([c["name"] for c in o["configurations"]]) == ["Debug", "MinSizeRel", "RelWithDebInfo", "Release"]
  719. else:
  720. assert len(o["configurations"]) == 1
  721. assert o["configurations"][0]["name"] in ("", "Debug", "Release", "RelWithDebInfo", "MinSizeRel")
  722. for c in o["configurations"]:
  723. check_object_codemodel_configuration(c, g, inSource)
  724. return _check
  725. cxx_compiler_id = sys.argv[2]
  726. assert is_dict(index)
  727. assert sorted(index.keys()) == ["cmake", "objects", "reply"]
  728. check_objects(index["objects"], index["cmake"]["generator"])