codemodel-v2-check.py 43 KB

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