cmExportBuildPackageInfoGenerator.cxx 3.7 KB

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