cmTargetPropertyComputer.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 "cmStateTypes.h"
  7. #include "cmStringAlgorithms.h"
  8. #include "cmSystemTools.h"
  9. #include "cmValue.h"
  10. class cmMakefile;
  11. class cmTargetPropertyComputer
  12. {
  13. public:
  14. template <typename Target>
  15. static cmValue GetProperty(Target const* tgt, const std::string& prop,
  16. cmMakefile const& mf)
  17. {
  18. if (cmValue loc = GetLocation(tgt, prop, mf)) {
  19. return loc;
  20. }
  21. if (cmSystemTools::GetFatalErrorOccurred()) {
  22. return nullptr;
  23. }
  24. if (prop == "SOURCES") {
  25. return GetSources(tgt, mf);
  26. }
  27. return nullptr;
  28. }
  29. private:
  30. static bool HandleLocationPropertyPolicy(std::string const& tgtName,
  31. cmMakefile const& mf);
  32. template <typename Target>
  33. static const std::string& ComputeLocationForBuild(Target const* tgt);
  34. template <typename Target>
  35. static const std::string& ComputeLocation(Target const* tgt,
  36. std::string const& config);
  37. template <typename Target>
  38. static cmValue GetLocation(Target const* tgt, std::string const& prop,
  39. cmMakefile const& mf)
  40. {
  41. // Watch for special "computed" properties that are dependent on
  42. // other properties or variables. Always recompute them.
  43. if (tgt->GetType() == cmStateEnums::EXECUTABLE ||
  44. tgt->GetType() == cmStateEnums::STATIC_LIBRARY ||
  45. tgt->GetType() == cmStateEnums::SHARED_LIBRARY ||
  46. tgt->GetType() == cmStateEnums::MODULE_LIBRARY ||
  47. tgt->GetType() == cmStateEnums::UNKNOWN_LIBRARY) {
  48. static const std::string propLOCATION = "LOCATION";
  49. if (prop == propLOCATION) {
  50. if (!tgt->IsImported() &&
  51. !HandleLocationPropertyPolicy(tgt->GetName(), mf)) {
  52. return nullptr;
  53. }
  54. return cmValue(ComputeLocationForBuild(tgt));
  55. }
  56. // Support "LOCATION_<CONFIG>".
  57. if (cmHasLiteralPrefix(prop, "LOCATION_")) {
  58. if (!tgt->IsImported() &&
  59. !HandleLocationPropertyPolicy(tgt->GetName(), mf)) {
  60. return nullptr;
  61. }
  62. std::string configName = prop.substr(9);
  63. return cmValue(ComputeLocation(tgt, configName));
  64. }
  65. // Support "<CONFIG>_LOCATION".
  66. if (cmHasLiteralSuffix(prop, "_LOCATION") &&
  67. !cmHasLiteralPrefix(prop, "XCODE_ATTRIBUTE_")) {
  68. std::string configName(prop.c_str(), prop.size() - 9);
  69. if (configName != "IMPORTED") {
  70. if (!tgt->IsImported() &&
  71. !HandleLocationPropertyPolicy(tgt->GetName(), mf)) {
  72. return nullptr;
  73. }
  74. return cmValue(ComputeLocation(tgt, configName));
  75. }
  76. }
  77. }
  78. return nullptr;
  79. }
  80. template <typename Target>
  81. static cmValue GetSources(Target const* tgt, cmMakefile const& mf);
  82. };