cmJsonObjects.cxx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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 <algorithm>
  5. #include <cassert>
  6. #include <cstddef>
  7. #include <functional>
  8. #include <limits>
  9. #include <map>
  10. #include <memory>
  11. #include <set>
  12. #include <string>
  13. #include <unordered_map>
  14. #include <utility>
  15. #include <vector>
  16. #include <cmext/algorithm>
  17. #include "cmGeneratorExpression.h"
  18. #include "cmGeneratorTarget.h"
  19. #include "cmGlobalGenerator.h"
  20. #include "cmInstallGenerator.h"
  21. #include "cmInstallSubdirectoryGenerator.h"
  22. #include "cmInstallTargetGenerator.h"
  23. #include "cmJsonObjectDictionary.h"
  24. #include "cmJsonObjects.h"
  25. #include "cmLinkLineComputer.h"
  26. #include "cmLocalGenerator.h"
  27. #include "cmMakefile.h"
  28. #include "cmProperty.h"
  29. #include "cmPropertyMap.h"
  30. #include "cmSourceFile.h"
  31. #include "cmState.h"
  32. #include "cmStateDirectory.h"
  33. #include "cmStateSnapshot.h"
  34. #include "cmStateTypes.h"
  35. #include "cmStringAlgorithms.h"
  36. #include "cmSystemTools.h"
  37. #include "cmTarget.h"
  38. #include "cmTest.h"
  39. #include "cmake.h"
  40. namespace {
  41. std::vector<std::string> getConfigurations(const cmake* cm)
  42. {
  43. std::vector<std::string> configurations;
  44. const auto& makefiles = cm->GetGlobalGenerator()->GetMakefiles();
  45. if (makefiles.empty()) {
  46. return configurations;
  47. }
  48. return makefiles[0]->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  49. }
  50. bool hasString(const Json::Value& v, const std::string& s)
  51. {
  52. return !v.isNull() &&
  53. std::any_of(v.begin(), v.end(),
  54. [s](const Json::Value& i) { return i.asString() == s; });
  55. }
  56. template <class T>
  57. Json::Value fromStringList(const T& in)
  58. {
  59. Json::Value result = Json::arrayValue;
  60. for (std::string const& i : in) {
  61. result.append(i);
  62. }
  63. return result;
  64. }
  65. } // namespace
  66. void cmGetCMakeInputs(const cmGlobalGenerator* gg,
  67. const std::string& sourceDir,
  68. const std::string& buildDir,
  69. std::vector<std::string>* internalFiles,
  70. std::vector<std::string>* explicitFiles,
  71. std::vector<std::string>* tmpFiles)
  72. {
  73. const std::string cmakeRootDir = cmSystemTools::GetCMakeRoot() + '/';
  74. auto const& makefiles = gg->GetMakefiles();
  75. for (const auto& mf : makefiles) {
  76. for (std::string const& lf : mf->GetListFiles()) {
  77. const std::string startOfFile = lf.substr(0, cmakeRootDir.size());
  78. const bool isInternal = (startOfFile == cmakeRootDir);
  79. const bool isTemporary =
  80. !isInternal && (cmHasPrefix(lf, buildDir + '/'));
  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->GetOrDetermineLanguage();
  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 (cmProp 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 (cmProp 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 (cmProp 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 (cmProp 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 (cmProp 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->GetIsGenerated();
  282. std::vector<std::string>& groupFileList = fileGroups[fileData];
  283. groupFileList.push_back(file->ResolveFullPath());
  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. result[kCTEST_COMMAND] =
  309. cmGeneratorExpression::Evaluate(command, lg, config);
  310. // Build up the list of properties that may have been specified
  311. Json::Value properties = Json::arrayValue;
  312. for (auto& prop : testInfo->GetProperties().GetList()) {
  313. Json::Value entry = Json::objectValue;
  314. entry[kKEY_KEY] = prop.first;
  315. // Remove config variables from the value too.
  316. entry[kVALUE_KEY] =
  317. cmGeneratorExpression::Evaluate(prop.second, lg, config);
  318. properties.append(entry);
  319. }
  320. result[kPROPERTIES_KEY] = properties;
  321. return result;
  322. }
  323. static void DumpMakefileTests(cmLocalGenerator* lg, const std::string& config,
  324. Json::Value* result)
  325. {
  326. auto mf = lg->GetMakefile();
  327. std::vector<cmTest*> tests;
  328. mf->GetTests(config, tests);
  329. for (auto test : tests) {
  330. Json::Value tmp = DumpCTestInfo(lg, test, config);
  331. if (!tmp.isNull()) {
  332. result->append(tmp);
  333. }
  334. }
  335. }
  336. static Json::Value DumpCTestProjectList(const cmake* cm,
  337. std::string const& config)
  338. {
  339. Json::Value result = Json::arrayValue;
  340. auto globalGen = cm->GetGlobalGenerator();
  341. for (const auto& projectIt : globalGen->GetProjectMap()) {
  342. Json::Value pObj = Json::objectValue;
  343. pObj[kNAME_KEY] = projectIt.first;
  344. Json::Value tests = Json::arrayValue;
  345. // Gather tests for every generator
  346. for (const auto& lg : projectIt.second) {
  347. // Make sure they're generated.
  348. lg->GenerateTestFiles();
  349. DumpMakefileTests(lg, config, &tests);
  350. }
  351. pObj[kCTEST_INFO] = tests;
  352. result.append(pObj);
  353. }
  354. return result;
  355. }
  356. static Json::Value DumpCTestConfiguration(const cmake* cm,
  357. const std::string& config)
  358. {
  359. Json::Value result = Json::objectValue;
  360. result[kNAME_KEY] = config;
  361. result[kPROJECTS_KEY] = DumpCTestProjectList(cm, config);
  362. return result;
  363. }
  364. static Json::Value DumpCTestConfigurationsList(const cmake* cm)
  365. {
  366. Json::Value result = Json::arrayValue;
  367. for (const std::string& c : getConfigurations(cm)) {
  368. result.append(DumpCTestConfiguration(cm, c));
  369. }
  370. return result;
  371. }
  372. Json::Value cmDumpCTestInfo(const cmake* cm)
  373. {
  374. Json::Value result = Json::objectValue;
  375. result[kCONFIGURATIONS_KEY] = DumpCTestConfigurationsList(cm);
  376. return result;
  377. }
  378. static Json::Value DumpTarget(cmGeneratorTarget* target,
  379. const std::string& config)
  380. {
  381. cmLocalGenerator* lg = target->GetLocalGenerator();
  382. const cmStateEnums::TargetType type = target->GetType();
  383. const std::string typeName = cmState::GetTargetTypeName(type);
  384. Json::Value ttl = Json::arrayValue;
  385. ttl.append("EXECUTABLE");
  386. ttl.append("STATIC_LIBRARY");
  387. ttl.append("SHARED_LIBRARY");
  388. ttl.append("MODULE_LIBRARY");
  389. ttl.append("OBJECT_LIBRARY");
  390. ttl.append("UTILITY");
  391. ttl.append("INTERFACE_LIBRARY");
  392. if (!hasString(ttl, typeName) || target->IsImported()) {
  393. return Json::Value();
  394. }
  395. Json::Value result = Json::objectValue;
  396. result[kNAME_KEY] = target->GetName();
  397. result[kIS_GENERATOR_PROVIDED_KEY] =
  398. target->Target->GetIsGeneratorProvided();
  399. result[kTYPE_KEY] = typeName;
  400. result[kSOURCE_DIRECTORY_KEY] = lg->GetCurrentSourceDirectory();
  401. result[kBUILD_DIRECTORY_KEY] = lg->GetCurrentBinaryDirectory();
  402. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  403. return result;
  404. }
  405. result[kFULL_NAME_KEY] = target->GetFullName(config);
  406. if (target->Target->GetHaveInstallRule()) {
  407. result[kHAS_INSTALL_RULE] = true;
  408. Json::Value installPaths = Json::arrayValue;
  409. for (const auto& installGenerator :
  410. target->Makefile->GetInstallGenerators()) {
  411. auto installTargetGenerator =
  412. dynamic_cast<cmInstallTargetGenerator*>(installGenerator.get());
  413. if (installTargetGenerator != nullptr &&
  414. installTargetGenerator->GetTarget()->Target == target->Target) {
  415. auto dest = installTargetGenerator->GetDestination(config);
  416. std::string installPath;
  417. if (!dest.empty() && cmSystemTools::FileIsFullPath(dest)) {
  418. installPath = dest;
  419. } else {
  420. installPath = cmStrCat(
  421. target->Makefile->GetSafeDefinition("CMAKE_INSTALL_PREFIX"), '/',
  422. dest);
  423. }
  424. installPaths.append(installPath);
  425. }
  426. }
  427. result[kINSTALL_PATHS] = installPaths;
  428. }
  429. if (target->HaveWellDefinedOutputFiles()) {
  430. Json::Value artifacts = Json::arrayValue;
  431. artifacts.append(
  432. target->GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact));
  433. if (target->HasImportLibrary(config)) {
  434. artifacts.append(
  435. target->GetFullPath(config, cmStateEnums::ImportLibraryArtifact));
  436. }
  437. if (target->IsDLLPlatform()) {
  438. const cmGeneratorTarget::OutputInfo* output =
  439. target->GetOutputInfo(config);
  440. if (output && !output->PdbDir.empty()) {
  441. artifacts.append(output->PdbDir + '/' + target->GetPDBName(config));
  442. }
  443. }
  444. result[kARTIFACTS_KEY] = artifacts;
  445. result[kLINKER_LANGUAGE_KEY] = target->GetLinkerLanguage(config);
  446. std::string linkLibs;
  447. std::string linkFlags;
  448. std::string linkLanguageFlags;
  449. std::string frameworkPath;
  450. std::string linkPath;
  451. cmLinkLineComputer linkLineComputer(lg,
  452. lg->GetStateSnapshot().GetDirectory());
  453. lg->GetTargetFlags(&linkLineComputer, config, linkLibs, linkLanguageFlags,
  454. linkFlags, frameworkPath, linkPath, target);
  455. linkLibs = cmTrimWhitespace(linkLibs);
  456. linkFlags = cmTrimWhitespace(linkFlags);
  457. linkLanguageFlags = cmTrimWhitespace(linkLanguageFlags);
  458. frameworkPath = cmTrimWhitespace(frameworkPath);
  459. linkPath = cmTrimWhitespace(linkPath);
  460. if (!cmTrimWhitespace(linkLibs).empty()) {
  461. result[kLINK_LIBRARIES_KEY] = linkLibs;
  462. }
  463. if (!cmTrimWhitespace(linkFlags).empty()) {
  464. result[kLINK_FLAGS_KEY] = linkFlags;
  465. }
  466. if (!cmTrimWhitespace(linkLanguageFlags).empty()) {
  467. result[kLINK_LANGUAGE_FLAGS_KEY] = linkLanguageFlags;
  468. }
  469. if (!frameworkPath.empty()) {
  470. result[kFRAMEWORK_PATH_KEY] = frameworkPath;
  471. }
  472. if (!linkPath.empty()) {
  473. result[kLINK_PATH_KEY] = linkPath;
  474. }
  475. const std::string sysroot =
  476. lg->GetMakefile()->GetSafeDefinition("CMAKE_SYSROOT");
  477. if (!sysroot.empty()) {
  478. result[kSYSROOT_KEY] = sysroot;
  479. }
  480. }
  481. std::set<std::string> languages;
  482. target->GetLanguages(languages, config);
  483. std::map<std::string, LanguageData> languageDataMap;
  484. for (std::string const& lang : languages) {
  485. LanguageData& ld = languageDataMap[lang];
  486. ld.Language = lang;
  487. lg->GetTargetCompileFlags(target, config, lang, ld.Flags);
  488. std::set<std::string> defines;
  489. lg->GetTargetDefines(target, config, lang, defines);
  490. ld.SetDefines(defines);
  491. std::vector<std::string> includePathList;
  492. lg->GetIncludeDirectories(includePathList, target, lang, config);
  493. for (std::string const& i : includePathList) {
  494. ld.IncludePathList.emplace_back(
  495. i, target->IsSystemIncludeDirectory(i, config, lang));
  496. }
  497. }
  498. Json::Value sourceGroupsValue =
  499. DumpSourceFilesList(target, config, languageDataMap);
  500. if (!sourceGroupsValue.empty()) {
  501. result[kFILE_GROUPS_KEY] = sourceGroupsValue;
  502. }
  503. return result;
  504. }
  505. static Json::Value DumpTargetsList(
  506. const std::vector<cmLocalGenerator*>& generators, const std::string& config)
  507. {
  508. Json::Value result = Json::arrayValue;
  509. std::vector<cmGeneratorTarget*> targetList;
  510. for (auto const& lgIt : generators) {
  511. cm::append(targetList, lgIt->GetGeneratorTargets());
  512. }
  513. std::sort(targetList.begin(), targetList.end());
  514. for (cmGeneratorTarget* target : targetList) {
  515. Json::Value tmp = DumpTarget(target, config);
  516. if (!tmp.isNull()) {
  517. result.append(tmp);
  518. }
  519. }
  520. return result;
  521. }
  522. static Json::Value DumpProjectList(const cmake* cm, std::string const& config)
  523. {
  524. Json::Value result = Json::arrayValue;
  525. auto globalGen = cm->GetGlobalGenerator();
  526. for (auto const& projectIt : globalGen->GetProjectMap()) {
  527. Json::Value pObj = Json::objectValue;
  528. pObj[kNAME_KEY] = projectIt.first;
  529. // All Projects must have at least one local generator
  530. assert(!projectIt.second.empty());
  531. const cmLocalGenerator* lg = projectIt.second.at(0);
  532. // Project structure information:
  533. const cmMakefile* mf = lg->GetMakefile();
  534. auto minVersion = mf->GetSafeDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  535. pObj[kMINIMUM_CMAKE_VERSION] = minVersion;
  536. pObj[kSOURCE_DIRECTORY_KEY] = mf->GetCurrentSourceDirectory();
  537. pObj[kBUILD_DIRECTORY_KEY] = mf->GetCurrentBinaryDirectory();
  538. pObj[kTARGETS_KEY] = DumpTargetsList(projectIt.second, config);
  539. // For a project-level install rule it might be defined in any of its
  540. // associated generators.
  541. bool hasInstallRule = false;
  542. for (const auto generator : projectIt.second) {
  543. for (const auto& installGen :
  544. generator->GetMakefile()->GetInstallGenerators()) {
  545. if (!dynamic_cast<cmInstallSubdirectoryGenerator*>(installGen.get())) {
  546. hasInstallRule = true;
  547. break;
  548. }
  549. }
  550. if (hasInstallRule) {
  551. break;
  552. }
  553. }
  554. pObj[kHAS_INSTALL_RULE] = hasInstallRule;
  555. result.append(pObj);
  556. }
  557. return result;
  558. }
  559. static Json::Value DumpConfiguration(const cmake* cm,
  560. const std::string& config)
  561. {
  562. Json::Value result = Json::objectValue;
  563. result[kNAME_KEY] = config;
  564. result[kPROJECTS_KEY] = DumpProjectList(cm, config);
  565. return result;
  566. }
  567. static Json::Value DumpConfigurationsList(const cmake* cm)
  568. {
  569. Json::Value result = Json::arrayValue;
  570. for (std::string const& c : getConfigurations(cm)) {
  571. result.append(DumpConfiguration(cm, c));
  572. }
  573. return result;
  574. }
  575. Json::Value cmDumpCodeModel(const cmake* cm)
  576. {
  577. Json::Value result = Json::objectValue;
  578. result[kCONFIGURATIONS_KEY] = DumpConfigurationsList(cm);
  579. return result;
  580. }