cmExportBuildPackageInfoGenerator.cxx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmExportBuildPackageInfoGenerator.h"
  4. #include <cassert>
  5. #include <map>
  6. #include <utility>
  7. #include <vector>
  8. #include <cmext/string_view>
  9. #include <cm3p/json/value.h>
  10. #include "cmAlgorithms.h"
  11. #include "cmGeneratorExpression.h"
  12. #include "cmList.h"
  13. #include "cmPackageInfoArguments.h"
  14. #include "cmStateTypes.h"
  15. #include "cmStringAlgorithms.h"
  16. cmExportBuildPackageInfoGenerator::cmExportBuildPackageInfoGenerator(
  17. cmPackageInfoArguments arguments)
  18. : cmExportPackageInfoGenerator(std::move(arguments))
  19. {
  20. this->SetNamespace(cmStrCat(this->GetPackageName(), "::"_s));
  21. }
  22. bool cmExportBuildPackageInfoGenerator::GenerateMainFile(std::ostream& os)
  23. {
  24. if (!this->CollectExports([&](cmGeneratorTarget const*) {})) {
  25. return false;
  26. }
  27. if (!this->CheckDefaultTargets()) {
  28. return false;
  29. }
  30. Json::Value root = this->GeneratePackageInfo();
  31. root["cps_path"] = "@prefix@";
  32. Json::Value& components = root["components"];
  33. // Create all the imported targets.
  34. for (auto const& exp : this->Exports) {
  35. cmGeneratorTarget* const target = exp.Target;
  36. cmStateEnums::TargetType targetType = this->GetExportTargetType(target);
  37. Json::Value* const component =
  38. this->GenerateImportTarget(components, target, targetType);
  39. if (!component) {
  40. return false;
  41. }
  42. ImportPropertyMap properties;
  43. if (!this->PopulateInterfaceProperties(target, properties)) {
  44. return false;
  45. }
  46. this->PopulateInterfaceLinkLibrariesProperty(
  47. target, cmGeneratorExpression::InstallInterface, properties);
  48. if (targetType != cmStateEnums::INTERFACE_LIBRARY) {
  49. auto configurations = Json::Value{ Json::objectValue };
  50. // Add per-configuration properties.
  51. for (std::string const& c : this->Configurations) {
  52. this->GenerateInterfacePropertiesConfig(configurations, target, c);
  53. }
  54. if (!configurations.empty()) {
  55. (*component)["configurations"] = configurations;
  56. }
  57. }
  58. // De-duplicate include directories prior to generation.
  59. auto it = properties.find("INTERFACE_INCLUDE_DIRECTORIES");
  60. if (it != properties.end()) {
  61. auto list = cmList{ it->second };
  62. list = cmList{ list.begin(), cmRemoveDuplicates(list) };
  63. properties["INTERFACE_INCLUDE_DIRECTORIES"] = list.to_string();
  64. }
  65. // Set configuration-agnostic properties for component.
  66. this->GenerateInterfaceProperties(*component, target, properties);
  67. }
  68. this->GeneratePackageRequires(root);
  69. // Write the primary packing information file.
  70. this->WritePackageInfo(root, os);
  71. bool result = true;
  72. return result;
  73. }
  74. void cmExportBuildPackageInfoGenerator::GenerateInterfacePropertiesConfig(
  75. Json::Value& configurations, cmGeneratorTarget* target,
  76. std::string const& config)
  77. {
  78. std::string const& suffix = PropertyConfigSuffix(config);
  79. ImportPropertyMap properties;
  80. assert(this->GetExportTargetType(target) != cmStateEnums::INTERFACE_LIBRARY);
  81. this->SetImportLocationProperty(config, suffix, target, properties);
  82. if (properties.empty()) {
  83. return;
  84. }
  85. this->SetImportDetailProperties(config, suffix, target, properties);
  86. // TODO: PUBLIC_HEADER_LOCATION
  87. Json::Value component =
  88. this->GenerateInterfaceConfigProperties(suffix, properties);
  89. if (!component.empty()) {
  90. configurations[config.empty() ? std::string{ "noconfig" } : config] =
  91. std::move(component);
  92. }
  93. }
  94. std::string cmExportBuildPackageInfoGenerator::GetCxxModulesDirectory() const
  95. {
  96. // TODO: Implement a not-CMake-specific mechanism for providing module
  97. // information.
  98. return {};
  99. }