cmExportBuildPackageInfoGenerator.cxx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 "cmExportBuildPackageInfoGenerator.h"
  4. #include <cassert>
  5. #include <utility>
  6. #include <vector>
  7. #include <cmext/string_view>
  8. #include <cm3p/json/value.h>
  9. #include "cmGeneratorExpression.h"
  10. #include "cmStateTypes.h"
  11. #include "cmStringAlgorithms.h"
  12. cmExportBuildPackageInfoGenerator::cmExportBuildPackageInfoGenerator(
  13. std::string packageName, std::string version, std::string versionCompat,
  14. std::string versionSchema, std::vector<std::string> defaultTargets,
  15. std::vector<std::string> defaultConfigurations)
  16. : cmExportPackageInfoGenerator(
  17. std::move(packageName), std::move(version), std::move(versionCompat),
  18. std::move(versionSchema), std::move(defaultTargets),
  19. std::move(defaultConfigurations))
  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. // Set configuration-agnostic properties for component.
  60. this->GenerateInterfaceProperties(*component, target, properties);
  61. }
  62. this->GeneratePackageRequires(root);
  63. // Write the primary packing information file.
  64. this->WritePackageInfo(root, os);
  65. bool result = true;
  66. return result;
  67. }
  68. void cmExportBuildPackageInfoGenerator::GenerateInterfacePropertiesConfig(
  69. Json::Value& configurations, cmGeneratorTarget* target,
  70. std::string const& config)
  71. {
  72. std::string const& suffix = PropertyConfigSuffix(config);
  73. ImportPropertyMap properties;
  74. assert(this->GetExportTargetType(target) != cmStateEnums::INTERFACE_LIBRARY);
  75. this->SetImportLocationProperty(config, suffix, target, properties);
  76. if (properties.empty()) {
  77. return;
  78. }
  79. this->SetImportDetailProperties(config, suffix, target, properties);
  80. // TODO: PUBLIC_HEADER_LOCATION
  81. Json::Value component =
  82. this->GenerateInterfaceConfigProperties(suffix, properties);
  83. if (!component.empty()) {
  84. configurations[config] = std::move(component);
  85. }
  86. }
  87. std::string cmExportBuildPackageInfoGenerator::GetCxxModulesDirectory() const
  88. {
  89. // TODO: Implement a not-CMake-specific mechanism for providing module
  90. // information.
  91. return {};
  92. }