cmDefinePropertyCommand.cxx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 "cmDefinePropertyCommand.h"
  4. #include <algorithm>
  5. #include <iterator>
  6. #include <cmext/string_view>
  7. #include "cmArgumentParser.h"
  8. #include "cmExecutionStatus.h"
  9. #include "cmMakefile.h"
  10. #include "cmProperty.h"
  11. #include "cmRange.h"
  12. #include "cmState.h"
  13. #include "cmStringAlgorithms.h"
  14. bool cmDefinePropertyCommand(std::vector<std::string> const& args,
  15. cmExecutionStatus& status)
  16. {
  17. if (args.empty()) {
  18. status.SetError("called with incorrect number of arguments");
  19. return false;
  20. }
  21. // Get the scope in which to define the property.
  22. cmProperty::ScopeType scope;
  23. std::string const& scope_arg = args[0];
  24. if (scope_arg == "GLOBAL") {
  25. scope = cmProperty::GLOBAL;
  26. } else if (scope_arg == "DIRECTORY") {
  27. scope = cmProperty::DIRECTORY;
  28. } else if (scope_arg == "TARGET") {
  29. scope = cmProperty::TARGET;
  30. } else if (scope_arg == "SOURCE") {
  31. scope = cmProperty::SOURCE_FILE;
  32. } else if (scope_arg == "TEST") {
  33. scope = cmProperty::TEST;
  34. } else if (scope_arg == "VARIABLE") {
  35. scope = cmProperty::VARIABLE;
  36. } else if (scope_arg == "CACHED_VARIABLE") {
  37. scope = cmProperty::CACHED_VARIABLE;
  38. } else {
  39. status.SetError(cmStrCat("given invalid scope ", scope_arg,
  40. ". Valid scopes are GLOBAL, DIRECTORY, TARGET, "
  41. "SOURCE, TEST, VARIABLE, CACHED_VARIABLE."));
  42. return false;
  43. }
  44. // Parse remaining arguments.
  45. bool inherited = false;
  46. std::string PropertyName;
  47. std::vector<std::string> BriefDocs;
  48. std::vector<std::string> FullDocs;
  49. std::string initializeFromVariable;
  50. cmArgumentParser<void> parser;
  51. parser.Bind("PROPERTY"_s, PropertyName);
  52. parser.Bind("BRIEF_DOCS"_s, BriefDocs);
  53. parser.Bind("FULL_DOCS"_s, FullDocs);
  54. parser.Bind("INHERITED"_s, inherited);
  55. parser.Bind("INITIALIZE_FROM_VARIABLE"_s, initializeFromVariable);
  56. std::vector<std::string> invalidArgs;
  57. parser.Parse(cmMakeRange(args).advance(1), &invalidArgs);
  58. if (!invalidArgs.empty()) {
  59. status.SetError(
  60. cmStrCat("given invalid argument \"", invalidArgs.front(), "\"."));
  61. return false;
  62. }
  63. // Make sure a property name was found.
  64. if (PropertyName.empty()) {
  65. status.SetError("not given a PROPERTY <name> argument.");
  66. return false;
  67. }
  68. if (!initializeFromVariable.empty()) {
  69. // Make sure property scope is TARGET.
  70. if (scope != cmProperty::TARGET) {
  71. status.SetError(
  72. "Scope must be TARGET if INITIALIZE_FROM_VARIABLE is specified");
  73. return false;
  74. }
  75. // Make sure the variable has the property name as a suffix.
  76. if (!cmHasSuffix(initializeFromVariable, PropertyName)) {
  77. status.SetError(cmStrCat("Variable name \"", initializeFromVariable,
  78. "\" does not end with property name \"",
  79. PropertyName, "\""));
  80. return false;
  81. }
  82. if (initializeFromVariable == PropertyName) {
  83. status.SetError(cmStrCat(
  84. "Variable name must have a non-empty prefix before property name \"",
  85. PropertyName, "\""));
  86. return false;
  87. }
  88. // Make sure the variable is not reserved.
  89. static constexpr const char* reservedPrefixes[] = {
  90. "CMAKE_",
  91. "_CMAKE_",
  92. };
  93. if (std::any_of(std::begin(reservedPrefixes), std::end(reservedPrefixes),
  94. [&initializeFromVariable](const char* prefix) {
  95. return cmHasPrefix(initializeFromVariable, prefix);
  96. })) {
  97. status.SetError(cmStrCat("variable name \"", initializeFromVariable,
  98. "\" is reserved"));
  99. return false;
  100. }
  101. }
  102. // Actually define the property.
  103. status.GetMakefile().GetState()->DefineProperty(
  104. PropertyName, scope, cmJoin(BriefDocs, ""), cmJoin(FullDocs, ""),
  105. inherited, initializeFromVariable);
  106. return true;
  107. }