codemodel-v2-check.py 44 KB

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