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