codemodel-v2-check.py 45 KB

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