cmTargetPropertyComputer.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 void IssueLocationPropertyError(std::string const& tgtName,
  31. cmMakefile const& mf);
  32. template <typename Target>
  33. static const std::string& ImportedLocation(Target const* tgt,
  34. std::string const& config);
  35. template <typename Target>
  36. static cmValue GetLocation(Target const* tgt, std::string const& prop,
  37. cmMakefile const& mf)
  38. {
  39. // Watch for special "computed" properties that are dependent on
  40. // other properties or variables. Always recompute them.
  41. if (tgt->GetType() == cmStateEnums::EXECUTABLE ||
  42. tgt->GetType() == cmStateEnums::STATIC_LIBRARY ||
  43. tgt->GetType() == cmStateEnums::SHARED_LIBRARY ||
  44. tgt->GetType() == cmStateEnums::MODULE_LIBRARY ||
  45. tgt->GetType() == cmStateEnums::UNKNOWN_LIBRARY) {
  46. static const std::string propLOCATION = "LOCATION";
  47. if (prop == propLOCATION) {
  48. if (!tgt->IsImported()) {
  49. IssueLocationPropertyError(tgt->GetName(), mf);
  50. return nullptr;
  51. }
  52. return cmValue(ImportedLocation(tgt, std::string()));
  53. }
  54. // Support "LOCATION_<CONFIG>".
  55. if (cmHasLiteralPrefix(prop, "LOCATION_")) {
  56. if (!tgt->IsImported()) {
  57. IssueLocationPropertyError(tgt->GetName(), mf);
  58. return nullptr;
  59. }
  60. std::string configName = prop.substr(9);
  61. return cmValue(ImportedLocation(tgt, configName));
  62. }
  63. // Support "<CONFIG>_LOCATION".
  64. if (cmHasLiteralSuffix(prop, "_LOCATION") &&
  65. !cmHasLiteralPrefix(prop, "XCODE_ATTRIBUTE_")) {
  66. std::string configName(prop.c_str(), prop.size() - 9);
  67. if (configName != "IMPORTED") {
  68. if (!tgt->IsImported()) {
  69. IssueLocationPropertyError(tgt->GetName(), mf);
  70. return nullptr;
  71. }
  72. return cmValue(ImportedLocation(tgt, configName));
  73. }
  74. }
  75. }
  76. return nullptr;
  77. }
  78. template <typename Target>
  79. static cmValue GetSources(Target const* tgt, cmMakefile const& mf);
  80. };