cmFileAPIToolchains.cxx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "cmFileAPIToolchains.h"
  4. #include <memory>
  5. #include <string>
  6. #include <vector>
  7. #include <cm3p/json/value.h>
  8. #include "cmFileAPI.h"
  9. #include "cmGlobalGenerator.h"
  10. #include "cmMakefile.h"
  11. #include "cmState.h"
  12. #include "cmStringAlgorithms.h"
  13. #include "cmValue.h"
  14. #include "cmake.h"
  15. namespace {
  16. struct ToolchainVariable
  17. {
  18. std::string ObjectKey;
  19. std::string VariableSuffix;
  20. bool IsList;
  21. };
  22. class Toolchains
  23. {
  24. cmFileAPI& FileAPI;
  25. unsigned long Version;
  26. static const std::vector<ToolchainVariable> CompilerVariables;
  27. static const std::vector<ToolchainVariable> CompilerImplicitVariables;
  28. static const ToolchainVariable SourceFileExtensionsVariable;
  29. Json::Value DumpToolchains();
  30. Json::Value DumpToolchain(std::string const& lang);
  31. Json::Value DumpToolchainVariables(
  32. cmMakefile const* mf, std::string const& lang,
  33. std::vector<ToolchainVariable> const& variables);
  34. void DumpToolchainVariable(cmMakefile const* mf, Json::Value& object,
  35. std::string const& lang,
  36. ToolchainVariable const& variable);
  37. public:
  38. Toolchains(cmFileAPI& fileAPI, unsigned long version);
  39. Json::Value Dump();
  40. };
  41. const std::vector<ToolchainVariable> Toolchains::CompilerVariables{
  42. { "path", "COMPILER", false },
  43. { "id", "COMPILER_ID", false },
  44. { "version", "COMPILER_VERSION", false },
  45. { "target", "COMPILER_TARGET", false },
  46. };
  47. const std::vector<ToolchainVariable> Toolchains::CompilerImplicitVariables{
  48. { "includeDirectories", "IMPLICIT_INCLUDE_DIRECTORIES", true },
  49. { "linkDirectories", "IMPLICIT_LINK_DIRECTORIES", true },
  50. { "linkFrameworkDirectories", "IMPLICIT_LINK_FRAMEWORK_DIRECTORIES", true },
  51. { "linkLibraries", "IMPLICIT_LINK_LIBRARIES", true },
  52. };
  53. const ToolchainVariable Toolchains::SourceFileExtensionsVariable{
  54. "sourceFileExtensions", "SOURCE_FILE_EXTENSIONS", true
  55. };
  56. Toolchains::Toolchains(cmFileAPI& fileAPI, unsigned long version)
  57. : FileAPI(fileAPI)
  58. , Version(version)
  59. {
  60. static_cast<void>(this->Version);
  61. }
  62. Json::Value Toolchains::Dump()
  63. {
  64. Json::Value toolchains = Json::objectValue;
  65. toolchains["toolchains"] = this->DumpToolchains();
  66. return toolchains;
  67. }
  68. Json::Value Toolchains::DumpToolchains()
  69. {
  70. Json::Value toolchains = Json::arrayValue;
  71. for (std::string const& lang :
  72. this->FileAPI.GetCMakeInstance()->GetState()->GetEnabledLanguages()) {
  73. toolchains.append(this->DumpToolchain(lang));
  74. }
  75. return toolchains;
  76. }
  77. Json::Value Toolchains::DumpToolchain(std::string const& lang)
  78. {
  79. const auto& mf =
  80. this->FileAPI.GetCMakeInstance()->GetGlobalGenerator()->GetMakefiles()[0];
  81. Json::Value toolchain = Json::objectValue;
  82. toolchain["language"] = lang;
  83. toolchain["compiler"] =
  84. this->DumpToolchainVariables(mf.get(), lang, CompilerVariables);
  85. toolchain["compiler"]["implicit"] =
  86. this->DumpToolchainVariables(mf.get(), lang, CompilerImplicitVariables);
  87. this->DumpToolchainVariable(mf.get(), toolchain, lang,
  88. SourceFileExtensionsVariable);
  89. return toolchain;
  90. }
  91. Json::Value Toolchains::DumpToolchainVariables(
  92. cmMakefile const* mf, std::string const& lang,
  93. std::vector<ToolchainVariable> const& variables)
  94. {
  95. Json::Value object = Json::objectValue;
  96. for (const auto& variable : variables) {
  97. this->DumpToolchainVariable(mf, object, lang, variable);
  98. }
  99. return object;
  100. }
  101. void Toolchains::DumpToolchainVariable(cmMakefile const* mf,
  102. Json::Value& object,
  103. std::string const& lang,
  104. ToolchainVariable const& variable)
  105. {
  106. std::string const variableName =
  107. cmStrCat("CMAKE_", lang, "_", variable.VariableSuffix);
  108. if (variable.IsList) {
  109. std::vector<std::string> values;
  110. if (mf->GetDefExpandList(variableName, values)) {
  111. Json::Value jsonArray = Json::arrayValue;
  112. for (std::string const& value : values) {
  113. jsonArray.append(value);
  114. }
  115. object[variable.ObjectKey] = jsonArray;
  116. }
  117. } else {
  118. cmValue def = mf->GetDefinition(variableName);
  119. if (def) {
  120. object[variable.ObjectKey] = *def;
  121. }
  122. }
  123. }
  124. }
  125. Json::Value cmFileAPIToolchainsDump(cmFileAPI& fileAPI, unsigned long version)
  126. {
  127. Toolchains toolchains(fileAPI, version);
  128. return toolchains.Dump();
  129. }