cmTargetPropertyComputer.cxx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 "cmTargetPropertyComputer.h"
  4. #include <cctype>
  5. #include <sstream>
  6. #include <unordered_set>
  7. #include "cmMessageType.h"
  8. #include "cmMessenger.h"
  9. #include "cmPolicies.h"
  10. #include "cmStateSnapshot.h"
  11. bool cmTargetPropertyComputer::HandleLocationPropertyPolicy(
  12. std::string const& tgtName, cmMessenger* messenger,
  13. cmListFileBacktrace const& context)
  14. {
  15. std::ostringstream e;
  16. const char* modal = nullptr;
  17. MessageType messageType = MessageType::AUTHOR_WARNING;
  18. switch (context.GetBottom().GetPolicy(cmPolicies::CMP0026)) {
  19. case cmPolicies::WARN:
  20. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0026) << "\n";
  21. modal = "should";
  22. case cmPolicies::OLD:
  23. break;
  24. case cmPolicies::REQUIRED_ALWAYS:
  25. case cmPolicies::REQUIRED_IF_USED:
  26. case cmPolicies::NEW:
  27. modal = "may";
  28. messageType = MessageType::FATAL_ERROR;
  29. }
  30. if (modal) {
  31. e << "The LOCATION property " << modal << " not be read from target \""
  32. << tgtName
  33. << "\". Use the target name directly with "
  34. "add_custom_command, or use the generator expression $<TARGET_FILE>, "
  35. "as appropriate.\n";
  36. messenger->IssueMessage(messageType, e.str(), context);
  37. }
  38. return messageType != MessageType::FATAL_ERROR;
  39. }
  40. bool cmTargetPropertyComputer::WhiteListedInterfaceProperty(
  41. const std::string& prop)
  42. {
  43. if (cmHasLiteralPrefix(prop, "INTERFACE_")) {
  44. return true;
  45. }
  46. if (cmHasLiteralPrefix(prop, "_")) {
  47. return true;
  48. }
  49. if (std::islower(prop[0])) {
  50. return true;
  51. }
  52. static std::unordered_set<std::string> const builtIns{
  53. "COMPATIBLE_INTERFACE_BOOL",
  54. "COMPATIBLE_INTERFACE_NUMBER_MAX",
  55. "COMPATIBLE_INTERFACE_NUMBER_MIN",
  56. "COMPATIBLE_INTERFACE_STRING",
  57. "EXPORT_NAME",
  58. "EXPORT_PROPERTIES",
  59. "IMPORTED",
  60. "IMPORTED_GLOBAL",
  61. "MANUALLY_ADDED_DEPENDENCIES",
  62. "NAME",
  63. "PRIVATE_HEADER",
  64. "PUBLIC_HEADER",
  65. "TYPE"
  66. };
  67. if (builtIns.count(prop)) {
  68. return true;
  69. }
  70. if (prop == "IMPORTED_CONFIGURATIONS" || prop == "IMPORTED_LIBNAME" ||
  71. cmHasLiteralPrefix(prop, "IMPORTED_LIBNAME_") ||
  72. cmHasLiteralPrefix(prop, "MAP_IMPORTED_CONFIG_")) {
  73. return true;
  74. }
  75. // This property should not be allowed but was incorrectly added in
  76. // CMake 3.8. We can't remove it from the whitelist without breaking
  77. // projects that try to set it. One day we could warn about this, but
  78. // for now silently accept it.
  79. if (prop == "NO_SYSTEM_FROM_IMPORTED") {
  80. return true;
  81. }
  82. return false;
  83. }
  84. bool cmTargetPropertyComputer::PassesWhitelist(
  85. cmStateEnums::TargetType tgtType, std::string const& prop,
  86. cmMessenger* messenger, cmListFileBacktrace const& context)
  87. {
  88. if (tgtType == cmStateEnums::INTERFACE_LIBRARY &&
  89. !WhiteListedInterfaceProperty(prop)) {
  90. std::ostringstream e;
  91. e << "INTERFACE_LIBRARY targets may only have whitelisted properties. "
  92. "The property \""
  93. << prop << "\" is not allowed.";
  94. messenger->IssueMessage(MessageType::FATAL_ERROR, e.str(), context);
  95. return false;
  96. }
  97. return true;
  98. }