cmTargetPropertyComputer.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <string>
  6. #include "cmListFileCache.h"
  7. #include "cmProperty.h"
  8. #include "cmStateTypes.h"
  9. #include "cmStringAlgorithms.h"
  10. #include "cmSystemTools.h"
  11. class cmMessenger;
  12. class cmTargetPropertyComputer
  13. {
  14. public:
  15. template <typename Target>
  16. static cmProp GetProperty(Target const* tgt, const std::string& prop,
  17. cmMessenger* messenger,
  18. cmListFileBacktrace const& context)
  19. {
  20. if (cmProp loc = GetLocation(tgt, prop, messenger, context)) {
  21. return loc;
  22. }
  23. if (cmSystemTools::GetFatalErrorOccured()) {
  24. return nullptr;
  25. }
  26. if (prop == "SOURCES") {
  27. return GetSources(tgt, messenger, context);
  28. }
  29. return nullptr;
  30. }
  31. private:
  32. static bool HandleLocationPropertyPolicy(std::string const& tgtName,
  33. cmMessenger* messenger,
  34. cmListFileBacktrace const& context);
  35. template <typename Target>
  36. static const std::string& ComputeLocationForBuild(Target const* tgt);
  37. template <typename Target>
  38. static const std::string& ComputeLocation(Target const* tgt,
  39. std::string const& config);
  40. template <typename Target>
  41. static cmProp GetLocation(Target const* tgt, std::string const& prop,
  42. cmMessenger* messenger,
  43. cmListFileBacktrace const& context)
  44. {
  45. // Watch for special "computed" properties that are dependent on
  46. // other properties or variables. Always recompute them.
  47. if (tgt->GetType() == cmStateEnums::EXECUTABLE ||
  48. tgt->GetType() == cmStateEnums::STATIC_LIBRARY ||
  49. tgt->GetType() == cmStateEnums::SHARED_LIBRARY ||
  50. tgt->GetType() == cmStateEnums::MODULE_LIBRARY ||
  51. tgt->GetType() == cmStateEnums::UNKNOWN_LIBRARY) {
  52. static const std::string propLOCATION = "LOCATION";
  53. if (prop == propLOCATION) {
  54. if (!tgt->IsImported() &&
  55. !HandleLocationPropertyPolicy(tgt->GetName(), messenger,
  56. context)) {
  57. return nullptr;
  58. }
  59. return cmProp(ComputeLocationForBuild(tgt));
  60. }
  61. // Support "LOCATION_<CONFIG>".
  62. if (cmHasLiteralPrefix(prop, "LOCATION_")) {
  63. if (!tgt->IsImported() &&
  64. !HandleLocationPropertyPolicy(tgt->GetName(), messenger,
  65. context)) {
  66. return nullptr;
  67. }
  68. std::string configName = prop.substr(9);
  69. return cmProp(ComputeLocation(tgt, configName));
  70. }
  71. // Support "<CONFIG>_LOCATION".
  72. if (cmHasLiteralSuffix(prop, "_LOCATION") &&
  73. !cmHasLiteralPrefix(prop, "XCODE_ATTRIBUTE_")) {
  74. std::string configName(prop.c_str(), prop.size() - 9);
  75. if (configName != "IMPORTED") {
  76. if (!tgt->IsImported() &&
  77. !HandleLocationPropertyPolicy(tgt->GetName(), messenger,
  78. context)) {
  79. return nullptr;
  80. }
  81. return cmProp(ComputeLocation(tgt, configName));
  82. }
  83. }
  84. }
  85. return nullptr;
  86. }
  87. template <typename Target>
  88. static cmProp GetSources(Target const* tgt, cmMessenger* messenger,
  89. cmListFileBacktrace const& context);
  90. };