cmJsonObjects.cxx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmJsonObjects.h" // IWYU pragma: keep
  4. #include "cmGeneratorExpression.h"
  5. #include "cmGeneratorTarget.h"
  6. #include "cmGlobalGenerator.h"
  7. #include "cmInstallGenerator.h"
  8. #include "cmInstallSubdirectoryGenerator.h"
  9. #include "cmInstallTargetGenerator.h"
  10. #include "cmJsonObjectDictionary.h"
  11. #include "cmJsonObjects.h"
  12. #include "cmLinkLineComputer.h"
  13. #include "cmLocalGenerator.h"
  14. #include "cmMakefile.h"
  15. #include "cmProperty.h"
  16. #include "cmPropertyMap.h"
  17. #include "cmSourceFile.h"
  18. #include "cmState.h"
  19. #include "cmStateDirectory.h"
  20. #include "cmStateSnapshot.h"
  21. #include "cmStateTypes.h"
  22. #include "cmSystemTools.h"
  23. #include "cmTarget.h"
  24. #include "cmTest.h"
  25. #include "cmake.h"
  26. #include <algorithm>
  27. #include <cassert>
  28. #include <cstddef>
  29. #include <functional>
  30. #include <limits>
  31. #include <map>
  32. #include <set>
  33. #include <string>
  34. #include <unordered_map>
  35. #include <utility>
  36. #include <vector>
  37. namespace {
  38. std::vector<std::string> getConfigurations(const cmake* cm)
  39. {
  40. std::vector<std::string> configurations;
  41. auto makefiles = cm->GetGlobalGenerator()->GetMakefiles();
  42. if (makefiles.empty()) {
  43. return configurations;
  44. }
  45. makefiles[0]->GetConfigurations(configurations);
  46. if (configurations.empty()) {
  47. configurations.emplace_back();
  48. }
  49. return configurations;
  50. }
  51. bool hasString(const Json::Value& v, const std::string& s)
  52. {
  53. return !v.isNull() &&
  54. std::any_of(v.begin(), v.end(),
  55. [s](const Json::Value& i) { return i.asString() == s; });
  56. }
  57. template <class T>
  58. Json::Value fromStringList(const T& in)
  59. {
  60. Json::Value result = Json::arrayValue;
  61. for (std::string const& i : in) {
  62. result.append(i);
  63. }
  64. return result;
  65. }
  66. } // namespace
  67. void cmGetCMakeInputs(const cmGlobalGenerator* gg,
  68. const std::string& sourceDir,
  69. const std::string& buildDir,
  70. std::vector<std::string>* internalFiles,
  71. std::vector<std::string>* explicitFiles,
  72. std::vector<std::string>* tmpFiles)
  73. {
  74. const std::string cmakeRootDir = cmSystemTools::GetCMakeRoot() + '/';
  75. std::vector<cmMakefile*> const& makefiles = gg->GetMakefiles();
  76. for (cmMakefile const* mf : makefiles) {
  77. for (std::string const& lf : mf->GetListFiles()) {
  78. const std::string startOfFile = lf.substr(0, cmakeRootDir.size());
  79. const bool isInternal = (startOfFile == cmakeRootDir);
  80. const bool isTemporary = !isInternal && (lf.find(buildDir + '/') == 0);
  81. std::string toAdd = lf;
  82. if (!sourceDir.empty()) {
  83. const std::string& relative =
  84. cmSystemTools::RelativePath(sourceDir, lf);
  85. if (toAdd.size() > relative.size()) {
  86. toAdd = relative;
  87. }
  88. }
  89. if (isInternal) {
  90. if (internalFiles) {
  91. internalFiles->push_back(std::move(toAdd));
  92. }
  93. } else {
  94. if (isTemporary) {
  95. if (tmpFiles) {
  96. tmpFiles->push_back(std::move(toAdd));
  97. }
  98. } else {
  99. if (explicitFiles) {
  100. explicitFiles->push_back(std::move(toAdd));
  101. }
  102. }
  103. }
  104. }
  105. }
  106. }
  107. Json::Value cmDumpCMakeInputs(const cmake* cm)
  108. {
  109. const cmGlobalGenerator* gg = cm->GetGlobalGenerator();
  110. const std::string& buildDir = cm->GetHomeOutputDirectory();
  111. const std::string& sourceDir = cm->GetHomeDirectory();
  112. std::vector<std::string> internalFiles;
  113. std::vector<std::string> explicitFiles;
  114. std::vector<std::string> tmpFiles;
  115. cmGetCMakeInputs(gg, sourceDir, buildDir, &internalFiles, &explicitFiles,
  116. &tmpFiles);
  117. Json::Value array = Json::arrayValue;
  118. Json::Value tmp = Json::objectValue;
  119. tmp[kIS_CMAKE_KEY] = true;
  120. tmp[kIS_TEMPORARY_KEY] = false;
  121. tmp[kSOURCES_KEY] = fromStringList(internalFiles);
  122. array.append(tmp);
  123. tmp = Json::objectValue;
  124. tmp[kIS_CMAKE_KEY] = false;
  125. tmp[kIS_TEMPORARY_KEY] = false;
  126. tmp[kSOURCES_KEY] = fromStringList(explicitFiles);
  127. array.append(tmp);
  128. tmp = Json::objectValue;
  129. tmp[kIS_CMAKE_KEY] = false;
  130. tmp[kIS_TEMPORARY_KEY] = true;
  131. tmp[kSOURCES_KEY] = fromStringList(tmpFiles);
  132. array.append(tmp);
  133. return array;
  134. }
  135. class LanguageData
  136. {
  137. public:
  138. bool operator==(const LanguageData& other) const;
  139. void SetDefines(const std::set<std::string>& defines);
  140. bool IsGenerated = false;
  141. std::string Language;
  142. std::string Flags;
  143. std::vector<std::string> Defines;
  144. std::vector<std::pair<std::string, bool>> IncludePathList;
  145. };
  146. bool LanguageData::operator==(const LanguageData& other) const
  147. {
  148. return Language == other.Language && Defines == other.Defines &&
  149. Flags == other.Flags && IncludePathList == other.IncludePathList &&
  150. IsGenerated == other.IsGenerated;
  151. }
  152. void LanguageData::SetDefines(const std::set<std::string>& defines)
  153. {
  154. std::vector<std::string> result;
  155. result.reserve(defines.size());
  156. for (std::string const& i : defines) {
  157. result.push_back(i);
  158. }
  159. std::sort(result.begin(), result.end());
  160. Defines = std::move(result);
  161. }
  162. namespace std {
  163. template <>
  164. struct hash<LanguageData>
  165. {
  166. std::size_t operator()(const LanguageData& in) const
  167. {
  168. using std::hash;
  169. size_t result =
  170. hash<std::string>()(in.Language) ^ hash<std::string>()(in.Flags);
  171. for (auto const& i : in.IncludePathList) {
  172. result = result ^
  173. (hash<std::string>()(i.first) ^
  174. (i.second ? std::numeric_limits<size_t>::max() : 0));
  175. }
  176. for (auto const& i : in.Defines) {
  177. result = result ^ hash<std::string>()(i);
  178. }
  179. result =
  180. result ^ (in.IsGenerated ? std::numeric_limits<size_t>::max() : 0);
  181. return result;
  182. }
  183. };
  184. } // namespace std
  185. static Json::Value DumpSourceFileGroup(const LanguageData& data,
  186. const std::vector<std::string>& files,
  187. const std::string& baseDir)
  188. {
  189. Json::Value result = Json::objectValue;
  190. if (!data.Language.empty()) {
  191. result[kLANGUAGE_KEY] = data.Language;
  192. if (!data.Flags.empty()) {
  193. result[kCOMPILE_FLAGS_KEY] = data.Flags;
  194. }
  195. if (!data.IncludePathList.empty()) {
  196. Json::Value includes = Json::arrayValue;
  197. for (auto const& i : data.IncludePathList) {
  198. Json::Value tmp = Json::objectValue;
  199. tmp[kPATH_KEY] = i.first;
  200. if (i.second) {
  201. tmp[kIS_SYSTEM_KEY] = i.second;
  202. }
  203. includes.append(tmp);
  204. }
  205. result[kINCLUDE_PATH_KEY] = includes;
  206. }
  207. if (!data.Defines.empty()) {
  208. result[kDEFINES_KEY] = fromStringList(data.Defines);
  209. }
  210. }
  211. result[kIS_GENERATED_KEY] = data.IsGenerated;
  212. Json::Value sourcesValue = Json::arrayValue;
  213. for (auto const& i : files) {
  214. const std::string relPath = cmSystemTools::RelativePath(baseDir, i);
  215. sourcesValue.append(relPath.size() < i.size() ? relPath : i);
  216. }
  217. result[kSOURCES_KEY] = sourcesValue;
  218. return result;
  219. }
  220. static Json::Value DumpSourceFilesList(
  221. cmGeneratorTarget* target, const std::string& config,
  222. const std::map<std::string, LanguageData>& languageDataMap)
  223. {
  224. // Collect sourcefile groups:
  225. std::vector<cmSourceFile*> files;
  226. target->GetSourceFiles(files, config);
  227. std::unordered_map<LanguageData, std::vector<std::string>> fileGroups;
  228. for (cmSourceFile* file : files) {
  229. LanguageData fileData;
  230. fileData.Language = file->GetLanguage();
  231. if (!fileData.Language.empty()) {
  232. const LanguageData& ld = languageDataMap.at(fileData.Language);
  233. cmLocalGenerator* lg = target->GetLocalGenerator();
  234. cmGeneratorExpressionInterpreter genexInterpreter(lg, config, target,
  235. fileData.Language);
  236. std::string compileFlags = ld.Flags;
  237. const std::string COMPILE_FLAGS("COMPILE_FLAGS");
  238. if (const char* cflags = file->GetProperty(COMPILE_FLAGS)) {
  239. lg->AppendFlags(compileFlags,
  240. genexInterpreter.Evaluate(cflags, COMPILE_FLAGS));
  241. }
  242. const std::string COMPILE_OPTIONS("COMPILE_OPTIONS");
  243. if (const char* coptions = file->GetProperty(COMPILE_OPTIONS)) {
  244. lg->AppendCompileOptions(
  245. compileFlags, genexInterpreter.Evaluate(coptions, COMPILE_OPTIONS));
  246. }
  247. fileData.Flags = compileFlags;
  248. // Add include directories from source file properties.
  249. std::vector<std::string> includes;
  250. const std::string INCLUDE_DIRECTORIES("INCLUDE_DIRECTORIES");
  251. if (const char* cincludes = file->GetProperty(INCLUDE_DIRECTORIES)) {
  252. const std::string& evaluatedIncludes =
  253. genexInterpreter.Evaluate(cincludes, INCLUDE_DIRECTORIES);
  254. lg->AppendIncludeDirectories(includes, evaluatedIncludes, *file);
  255. for (const auto& include : includes) {
  256. fileData.IncludePathList.emplace_back(
  257. include,
  258. target->IsSystemIncludeDirectory(include, config,
  259. fileData.Language));
  260. }
  261. }
  262. fileData.IncludePathList.insert(fileData.IncludePathList.end(),
  263. ld.IncludePathList.begin(),
  264. ld.IncludePathList.end());
  265. const std::string COMPILE_DEFINITIONS("COMPILE_DEFINITIONS");
  266. std::set<std::string> defines;
  267. if (const char* defs = file->GetProperty(COMPILE_DEFINITIONS)) {
  268. lg->AppendDefines(
  269. defines, genexInterpreter.Evaluate(defs, COMPILE_DEFINITIONS));
  270. }
  271. const std::string defPropName =
  272. "COMPILE_DEFINITIONS_" + cmSystemTools::UpperCase(config);
  273. if (const char* config_defs = file->GetProperty(defPropName)) {
  274. lg->AppendDefines(
  275. defines,
  276. genexInterpreter.Evaluate(config_defs, COMPILE_DEFINITIONS));
  277. }
  278. defines.insert(ld.Defines.begin(), ld.Defines.end());
  279. fileData.SetDefines(defines);
  280. }
  281. fileData.IsGenerated = file->GetPropertyAsBool("GENERATED");
  282. std::vector<std::string>& groupFileList = fileGroups[fileData];
  283. groupFileList.push_back(file->GetFullPath());
  284. }
  285. const std::string& baseDir = target->Makefile->GetCurrentSourceDirectory();
  286. Json::Value result = Json::arrayValue;
  287. for (auto const& it : fileGroups) {
  288. Json::Value group = DumpSourceFileGroup(it.first, it.second, baseDir);
  289. if (!group.isNull()) {
  290. result.append(group);
  291. }
  292. }
  293. return result;
  294. }
  295. static Json::Value DumpCTestInfo(cmLocalGenerator* lg, cmTest* testInfo,
  296. const std::string& config)
  297. {
  298. Json::Value result = Json::objectValue;
  299. result[kCTEST_NAME] = testInfo->GetName();
  300. // Concat command entries together. After the first should be the arguments
  301. // for the command
  302. std::string command;
  303. for (auto const& cmd : testInfo->GetCommand()) {
  304. command.append(cmd);
  305. command.append(" ");
  306. }
  307. // Remove any config specific variables from the output.
  308. cmGeneratorExpression ge;
  309. auto cge = ge.Parse(command);
  310. const std::string& processed = cge->Evaluate(lg, config);
  311. result[kCTEST_COMMAND] = processed;
  312. // Build up the list of properties that may have been specified
  313. Json::Value properties = Json::arrayValue;
  314. for (auto& prop : testInfo->GetProperties()) {
  315. Json::Value entry = Json::objectValue;
  316. entry[kKEY_KEY] = prop.first;
  317. // Remove config variables from the value too.
  318. auto cge_value = ge.Parse(prop.second.GetValue());
  319. const std::string& processed_value = cge_value->Evaluate(lg, config);
  320. entry[kVALUE_KEY] = processed_value;
  321. properties.append(entry);
  322. }
  323. result[kPROPERTIES_KEY] = properties;
  324. return result;
  325. }
  326. static void DumpMakefileTests(cmLocalGenerator* lg, const std::string& config,
  327. Json::Value* result)
  328. {
  329. auto mf = lg->GetMakefile();
  330. std::vector<cmTest*> tests;
  331. mf->GetTests(config, tests);
  332. for (auto test : tests) {
  333. Json::Value tmp = DumpCTestInfo(lg, test, config);
  334. if (!tmp.isNull()) {
  335. result->append(tmp);
  336. }
  337. }
  338. }
  339. static Json::Value DumpCTestProjectList(const cmake* cm,
  340. std::string const& config)
  341. {
  342. Json::Value result = Json::arrayValue;
  343. auto globalGen = cm->GetGlobalGenerator();
  344. for (const auto& projectIt : globalGen->GetProjectMap()) {
  345. Json::Value pObj = Json::objectValue;
  346. pObj[kNAME_KEY] = projectIt.first;
  347. Json::Value tests = Json::arrayValue;
  348. // Gather tests for every generator
  349. for (const auto& lg : projectIt.second) {
  350. // Make sure they're generated.
  351. lg->GenerateTestFiles();
  352. DumpMakefileTests(lg, config, &tests);
  353. }
  354. pObj[kCTEST_INFO] = tests;
  355. result.append(pObj);
  356. }
  357. return result;
  358. }
  359. static Json::Value DumpCTestConfiguration(const cmake* cm,
  360. const std::string& config)
  361. {
  362. Json::Value result = Json::objectValue;
  363. result[kNAME_KEY] = config;
  364. result[kPROJECTS_KEY] = DumpCTestProjectList(cm, config);
  365. return result;
  366. }
  367. static Json::Value DumpCTestConfigurationsList(const cmake* cm)
  368. {
  369. Json::Value result = Json::arrayValue;
  370. for (const std::string& c : getConfigurations(cm)) {
  371. result.append(DumpCTestConfiguration(cm, c));
  372. }
  373. return result;
  374. }
  375. Json::Value cmDumpCTestInfo(const cmake* cm)
  376. {
  377. Json::Value result = Json::objectValue;
  378. result[kCONFIGURATIONS_KEY] = DumpCTestConfigurationsList(cm);
  379. return result;
  380. }
  381. static Json::Value DumpTarget(cmGeneratorTarget* target,
  382. const std::string& config)
  383. {
  384. cmLocalGenerator* lg = target->GetLocalGenerator();
  385. const cmStateEnums::TargetType type = target->GetType();
  386. const std::string typeName = cmState::GetTargetTypeName(type);
  387. Json::Value ttl = Json::arrayValue;
  388. ttl.append("EXECUTABLE");
  389. ttl.append("STATIC_LIBRARY");
  390. ttl.append("SHARED_LIBRARY");
  391. ttl.append("MODULE_LIBRARY");
  392. ttl.append("OBJECT_LIBRARY");
  393. ttl.append("UTILITY");
  394. ttl.append("INTERFACE_LIBRARY");
  395. if (!hasString(ttl, typeName) || target->IsImported()) {
  396. return Json::Value();
  397. }
  398. Json::Value result = Json::objectValue;
  399. result[kNAME_KEY] = target->GetName();
  400. result[kIS_GENERATOR_PROVIDED_KEY] =
  401. target->Target->GetIsGeneratorProvided();
  402. result[kTYPE_KEY] = typeName;
  403. result[kSOURCE_DIRECTORY_KEY] = lg->GetCurrentSourceDirectory();
  404. result[kBUILD_DIRECTORY_KEY] = lg->GetCurrentBinaryDirectory();
  405. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  406. return result;
  407. }
  408. result[kFULL_NAME_KEY] = target->GetFullName(config);
  409. if (target->Target->GetHaveInstallRule()) {
  410. result[kHAS_INSTALL_RULE] = true;
  411. Json::Value installPaths = Json::arrayValue;
  412. auto targetGenerators = target->Makefile->GetInstallGenerators();
  413. for (auto installGenerator : targetGenerators) {
  414. auto installTargetGenerator =
  415. dynamic_cast<cmInstallTargetGenerator*>(installGenerator);
  416. if (installTargetGenerator != nullptr &&
  417. installTargetGenerator->GetTarget()->Target == target->Target) {
  418. auto dest = installTargetGenerator->GetDestination(config);
  419. std::string installPath;
  420. if (!dest.empty() && cmSystemTools::FileIsFullPath(dest)) {
  421. installPath = dest;
  422. } else {
  423. std::string installPrefix =
  424. target->Makefile->GetSafeDefinition("CMAKE_INSTALL_PREFIX");
  425. installPath = installPrefix + '/' + dest;
  426. }
  427. installPaths.append(installPath);
  428. }
  429. }
  430. result[kINSTALL_PATHS] = installPaths;
  431. }
  432. if (target->HaveWellDefinedOutputFiles()) {
  433. Json::Value artifacts = Json::arrayValue;
  434. artifacts.append(
  435. target->GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact));
  436. if (target->IsDLLPlatform()) {
  437. artifacts.append(
  438. target->GetFullPath(config, cmStateEnums::ImportLibraryArtifact));
  439. const cmGeneratorTarget::OutputInfo* output =
  440. target->GetOutputInfo(config);
  441. if (output && !output->PdbDir.empty()) {
  442. artifacts.append(output->PdbDir + '/' + target->GetPDBName(config));
  443. }
  444. }
  445. result[kARTIFACTS_KEY] = artifacts;
  446. result[kLINKER_LANGUAGE_KEY] = target->GetLinkerLanguage(config);
  447. std::string linkLibs;
  448. std::string linkFlags;
  449. std::string linkLanguageFlags;
  450. std::string frameworkPath;
  451. std::string linkPath;
  452. cmLinkLineComputer linkLineComputer(lg,
  453. lg->GetStateSnapshot().GetDirectory());
  454. lg->GetTargetFlags(&linkLineComputer, config, linkLibs, linkLanguageFlags,
  455. linkFlags, frameworkPath, linkPath, target);
  456. linkLibs = cmSystemTools::TrimWhitespace(linkLibs);
  457. linkFlags = cmSystemTools::TrimWhitespace(linkFlags);
  458. linkLanguageFlags = cmSystemTools::TrimWhitespace(linkLanguageFlags);
  459. frameworkPath = cmSystemTools::TrimWhitespace(frameworkPath);
  460. linkPath = cmSystemTools::TrimWhitespace(linkPath);
  461. if (!cmSystemTools::TrimWhitespace(linkLibs).empty()) {
  462. result[kLINK_LIBRARIES_KEY] = linkLibs;
  463. }
  464. if (!cmSystemTools::TrimWhitespace(linkFlags).empty()) {
  465. result[kLINK_FLAGS_KEY] = linkFlags;
  466. }
  467. if (!cmSystemTools::TrimWhitespace(linkLanguageFlags).empty()) {
  468. result[kLINK_LANGUAGE_FLAGS_KEY] = linkLanguageFlags;
  469. }
  470. if (!frameworkPath.empty()) {
  471. result[kFRAMEWORK_PATH_KEY] = frameworkPath;
  472. }
  473. if (!linkPath.empty()) {
  474. result[kLINK_PATH_KEY] = linkPath;
  475. }
  476. const std::string sysroot =
  477. lg->GetMakefile()->GetSafeDefinition("CMAKE_SYSROOT");
  478. if (!sysroot.empty()) {
  479. result[kSYSROOT_KEY] = sysroot;
  480. }
  481. }
  482. std::set<std::string> languages;
  483. target->GetLanguages(languages, config);
  484. std::map<std::string, LanguageData> languageDataMap;
  485. for (std::string const& lang : languages) {
  486. LanguageData& ld = languageDataMap[lang];
  487. ld.Language = lang;
  488. lg->GetTargetCompileFlags(target, config, lang, ld.Flags);
  489. std::set<std::string> defines;
  490. lg->GetTargetDefines(target, config, lang, defines);
  491. ld.SetDefines(defines);
  492. std::vector<std::string> includePathList;
  493. lg->GetIncludeDirectories(includePathList, target, lang, config, true);
  494. for (std::string const& i : includePathList) {
  495. ld.IncludePathList.emplace_back(
  496. i, target->IsSystemIncludeDirectory(i, config, lang));
  497. }
  498. }
  499. Json::Value sourceGroupsValue =
  500. DumpSourceFilesList(target, config, languageDataMap);
  501. if (!sourceGroupsValue.empty()) {
  502. result[kFILE_GROUPS_KEY] = sourceGroupsValue;
  503. }
  504. return result;
  505. }
  506. static Json::Value DumpTargetsList(
  507. const std::vector<cmLocalGenerator*>& generators, const std::string& config)
  508. {
  509. Json::Value result = Json::arrayValue;
  510. std::vector<cmGeneratorTarget*> targetList;
  511. for (auto const& lgIt : generators) {
  512. const auto& list = lgIt->GetGeneratorTargets();
  513. targetList.insert(targetList.end(), list.begin(), list.end());
  514. }
  515. std::sort(targetList.begin(), targetList.end());
  516. for (cmGeneratorTarget* target : targetList) {
  517. Json::Value tmp = DumpTarget(target, config);
  518. if (!tmp.isNull()) {
  519. result.append(tmp);
  520. }
  521. }
  522. return result;
  523. }
  524. static Json::Value DumpProjectList(const cmake* cm, std::string const& config)
  525. {
  526. Json::Value result = Json::arrayValue;
  527. auto globalGen = cm->GetGlobalGenerator();
  528. for (auto const& projectIt : globalGen->GetProjectMap()) {
  529. Json::Value pObj = Json::objectValue;
  530. pObj[kNAME_KEY] = projectIt.first;
  531. // All Projects must have at least one local generator
  532. assert(!projectIt.second.empty());
  533. const cmLocalGenerator* lg = projectIt.second.at(0);
  534. // Project structure information:
  535. const cmMakefile* mf = lg->GetMakefile();
  536. auto minVersion = mf->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  537. pObj[kMINIMUM_CMAKE_VERSION] = minVersion ? minVersion : "";
  538. pObj[kSOURCE_DIRECTORY_KEY] = mf->GetCurrentSourceDirectory();
  539. pObj[kBUILD_DIRECTORY_KEY] = mf->GetCurrentBinaryDirectory();
  540. pObj[kTARGETS_KEY] = DumpTargetsList(projectIt.second, config);
  541. // For a project-level install rule it might be defined in any of its
  542. // associated generators.
  543. bool hasInstallRule = false;
  544. for (const auto generator : projectIt.second) {
  545. for (const auto installGen :
  546. generator->GetMakefile()->GetInstallGenerators()) {
  547. if (!dynamic_cast<cmInstallSubdirectoryGenerator*>(installGen)) {
  548. hasInstallRule = true;
  549. break;
  550. }
  551. }
  552. if (hasInstallRule) {
  553. break;
  554. }
  555. }
  556. pObj[kHAS_INSTALL_RULE] = hasInstallRule;
  557. result.append(pObj);
  558. }
  559. return result;
  560. }
  561. static Json::Value DumpConfiguration(const cmake* cm,
  562. const std::string& config)
  563. {
  564. Json::Value result = Json::objectValue;
  565. result[kNAME_KEY] = config;
  566. result[kPROJECTS_KEY] = DumpProjectList(cm, config);
  567. return result;
  568. }
  569. static Json::Value DumpConfigurationsList(const cmake* cm)
  570. {
  571. Json::Value result = Json::arrayValue;
  572. for (std::string const& c : getConfigurations(cm)) {
  573. result.append(DumpConfiguration(cm, c));
  574. }
  575. return result;
  576. }
  577. Json::Value cmDumpCodeModel(const cmake* cm)
  578. {
  579. Json::Value result = Json::objectValue;
  580. result[kCONFIGURATIONS_KEY] = DumpConfigurationsList(cm);
  581. return result;
  582. }