cmJsonObjects.cxx 21 KB

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