cmJsonObjects.cxx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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 cmState* state = lg->GetState();
  384. const cmStateEnums::TargetType type = target->GetType();
  385. const std::string typeName = state->GetTargetTypeName(type);
  386. Json::Value ttl = Json::arrayValue;
  387. ttl.append("EXECUTABLE");
  388. ttl.append("STATIC_LIBRARY");
  389. ttl.append("SHARED_LIBRARY");
  390. ttl.append("MODULE_LIBRARY");
  391. ttl.append("OBJECT_LIBRARY");
  392. ttl.append("UTILITY");
  393. ttl.append("INTERFACE_LIBRARY");
  394. if (!hasString(ttl, typeName) || target->IsImported()) {
  395. return Json::Value();
  396. }
  397. Json::Value result = Json::objectValue;
  398. result[kNAME_KEY] = target->GetName();
  399. result[kIS_GENERATOR_PROVIDED_KEY] =
  400. target->Target->GetIsGeneratorProvided();
  401. result[kTYPE_KEY] = typeName;
  402. result[kSOURCE_DIRECTORY_KEY] = lg->GetCurrentSourceDirectory();
  403. result[kBUILD_DIRECTORY_KEY] = lg->GetCurrentBinaryDirectory();
  404. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  405. return result;
  406. }
  407. result[kFULL_NAME_KEY] = target->GetFullName(config);
  408. if (target->Target->GetHaveInstallRule()) {
  409. result[kHAS_INSTALL_RULE] = true;
  410. Json::Value installPaths = Json::arrayValue;
  411. auto targetGenerators = target->Makefile->GetInstallGenerators();
  412. for (auto installGenerator : targetGenerators) {
  413. auto installTargetGenerator =
  414. dynamic_cast<cmInstallTargetGenerator*>(installGenerator);
  415. if (installTargetGenerator != nullptr &&
  416. installTargetGenerator->GetTarget()->Target == target->Target) {
  417. auto dest = installTargetGenerator->GetDestination(config);
  418. std::string installPath;
  419. if (!dest.empty() && cmSystemTools::FileIsFullPath(dest)) {
  420. installPath = dest;
  421. } else {
  422. std::string installPrefix =
  423. target->Makefile->GetSafeDefinition("CMAKE_INSTALL_PREFIX");
  424. installPath = installPrefix + '/' + dest;
  425. }
  426. installPaths.append(installPath);
  427. }
  428. }
  429. result[kINSTALL_PATHS] = installPaths;
  430. }
  431. if (target->HaveWellDefinedOutputFiles()) {
  432. Json::Value artifacts = Json::arrayValue;
  433. artifacts.append(
  434. target->GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact));
  435. if (target->IsDLLPlatform()) {
  436. artifacts.append(
  437. target->GetFullPath(config, cmStateEnums::ImportLibraryArtifact));
  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 = cmSystemTools::TrimWhitespace(linkLibs);
  456. linkFlags = cmSystemTools::TrimWhitespace(linkFlags);
  457. linkLanguageFlags = cmSystemTools::TrimWhitespace(linkLanguageFlags);
  458. frameworkPath = cmSystemTools::TrimWhitespace(frameworkPath);
  459. linkPath = cmSystemTools::TrimWhitespace(linkPath);
  460. if (!cmSystemTools::TrimWhitespace(linkLibs).empty()) {
  461. result[kLINK_LIBRARIES_KEY] = linkLibs;
  462. }
  463. if (!cmSystemTools::TrimWhitespace(linkFlags).empty()) {
  464. result[kLINK_FLAGS_KEY] = linkFlags;
  465. }
  466. if (!cmSystemTools::TrimWhitespace(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, true);
  493. for (std::string const& i : includePathList) {
  494. ld.IncludePathList.push_back(
  495. std::make_pair(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. const auto& list = lgIt->GetGeneratorTargets();
  512. targetList.insert(targetList.end(), list.begin(), list.end());
  513. }
  514. std::sort(targetList.begin(), targetList.end());
  515. for (cmGeneratorTarget* target : targetList) {
  516. Json::Value tmp = DumpTarget(target, config);
  517. if (!tmp.isNull()) {
  518. result.append(tmp);
  519. }
  520. }
  521. return result;
  522. }
  523. static Json::Value DumpProjectList(const cmake* cm, std::string const& config)
  524. {
  525. Json::Value result = Json::arrayValue;
  526. auto globalGen = cm->GetGlobalGenerator();
  527. for (auto const& projectIt : globalGen->GetProjectMap()) {
  528. Json::Value pObj = Json::objectValue;
  529. pObj[kNAME_KEY] = projectIt.first;
  530. // All Projects must have at least one local generator
  531. assert(!projectIt.second.empty());
  532. const cmLocalGenerator* lg = projectIt.second.at(0);
  533. // Project structure information:
  534. const cmMakefile* mf = lg->GetMakefile();
  535. auto minVersion = mf->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  536. pObj[kMINIMUM_CMAKE_VERSION] = minVersion ? minVersion : "";
  537. pObj[kSOURCE_DIRECTORY_KEY] = mf->GetCurrentSourceDirectory();
  538. pObj[kBUILD_DIRECTORY_KEY] = mf->GetCurrentBinaryDirectory();
  539. pObj[kTARGETS_KEY] = DumpTargetsList(projectIt.second, config);
  540. // For a project-level install rule it might be defined in any of its
  541. // associated generators.
  542. bool hasInstallRule = false;
  543. for (const auto generator : projectIt.second) {
  544. hasInstallRule =
  545. generator->GetMakefile()->GetInstallGenerators().empty() == false;
  546. if (hasInstallRule) {
  547. break;
  548. }
  549. }
  550. pObj[kHAS_INSTALL_RULE] = hasInstallRule;
  551. result.append(pObj);
  552. }
  553. return result;
  554. }
  555. static Json::Value DumpConfiguration(const cmake* cm,
  556. const std::string& config)
  557. {
  558. Json::Value result = Json::objectValue;
  559. result[kNAME_KEY] = config;
  560. result[kPROJECTS_KEY] = DumpProjectList(cm, config);
  561. return result;
  562. }
  563. static Json::Value DumpConfigurationsList(const cmake* cm)
  564. {
  565. Json::Value result = Json::arrayValue;
  566. for (std::string const& c : getConfigurations(cm)) {
  567. result.append(DumpConfiguration(cm, c));
  568. }
  569. return result;
  570. }
  571. Json::Value cmDumpCodeModel(const cmake* cm)
  572. {
  573. Json::Value result = Json::objectValue;
  574. result[kCONFIGURATIONS_KEY] = DumpConfigurationsList(cm);
  575. return result;
  576. }