cmTargetPropertyComputer.cxx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 "cmMessenger.h"
  8. #include "cmPolicies.h"
  9. #include "cmStateSnapshot.h"
  10. #include "cmake.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. cmake::MessageType messageType = cmake::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 = cmake::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 != cmake::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> builtIns;
  53. if (builtIns.empty()) {
  54. builtIns.insert("COMPATIBLE_INTERFACE_BOOL");
  55. builtIns.insert("COMPATIBLE_INTERFACE_NUMBER_MAX");
  56. builtIns.insert("COMPATIBLE_INTERFACE_NUMBER_MIN");
  57. builtIns.insert("COMPATIBLE_INTERFACE_STRING");
  58. builtIns.insert("EXPORT_NAME");
  59. builtIns.insert("IMPORTED");
  60. builtIns.insert("IMPORTED_GLOBAL");
  61. builtIns.insert("NAME");
  62. builtIns.insert("TYPE");
  63. }
  64. if (builtIns.count(prop)) {
  65. return true;
  66. }
  67. if (prop == "IMPORTED_CONFIGURATIONS" || prop == "IMPORTED_LIBNAME" ||
  68. cmHasLiteralPrefix(prop, "IMPORTED_LIBNAME_") ||
  69. cmHasLiteralPrefix(prop, "MAP_IMPORTED_CONFIG_")) {
  70. return true;
  71. }
  72. // This property should not be allowed but was incorrectly added in
  73. // CMake 3.8. We can't remove it from the whitelist without breaking
  74. // projects that try to set it. One day we could warn about this, but
  75. // for now silently accept it.
  76. if (prop == "NO_SYSTEM_FROM_IMPORTED") {
  77. return true;
  78. }
  79. return false;
  80. }
  81. bool cmTargetPropertyComputer::PassesWhitelist(
  82. cmStateEnums::TargetType tgtType, std::string const& prop,
  83. cmMessenger* messenger, cmListFileBacktrace const& context)
  84. {
  85. if (tgtType == cmStateEnums::INTERFACE_LIBRARY &&
  86. !WhiteListedInterfaceProperty(prop)) {
  87. std::ostringstream e;
  88. e << "INTERFACE_LIBRARY targets may only have whitelisted properties. "
  89. "The property \""
  90. << prop << "\" is not allowed.";
  91. messenger->IssueMessage(cmake::FATAL_ERROR, e.str(), context);
  92. return false;
  93. }
  94. return true;
  95. }