cmTargetPropertyComputer.cxx 3.0 KB

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