cmMarkAsAdvancedCommand.cxx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 "cmMarkAsAdvancedCommand.h"
  4. #include "cmExecutionStatus.h"
  5. #include "cmMakefile.h"
  6. #include "cmMessageType.h"
  7. #include "cmPolicies.h"
  8. #include "cmProperty.h"
  9. #include "cmState.h"
  10. #include "cmStateTypes.h"
  11. #include "cmStringAlgorithms.h"
  12. #include "cmSystemTools.h"
  13. #include "cmake.h"
  14. // cmMarkAsAdvancedCommand
  15. bool cmMarkAsAdvancedCommand(std::vector<std::string> const& args,
  16. cmExecutionStatus& status)
  17. {
  18. if (args.empty()) {
  19. status.SetError("called with incorrect number of arguments");
  20. return false;
  21. }
  22. unsigned int i = 0;
  23. const char* value = "1";
  24. bool overwrite = false;
  25. if (args[0] == "CLEAR" || args[0] == "FORCE") {
  26. overwrite = true;
  27. if (args[0] == "CLEAR") {
  28. value = "0";
  29. }
  30. i = 1;
  31. }
  32. cmMakefile& mf = status.GetMakefile();
  33. cmState* state = mf.GetState();
  34. for (; i < args.size(); ++i) {
  35. std::string const& variable = args[i];
  36. bool issueMessage = false;
  37. bool oldBehavior = false;
  38. bool ignoreVariable = false;
  39. switch (mf.GetPolicyStatus(cmPolicies::CMP0102)) {
  40. case cmPolicies::WARN:
  41. if (mf.PolicyOptionalWarningEnabled("CMAKE_POLICY_WARNING_CMP0102")) {
  42. if (!state->GetCacheEntryValue(variable)) {
  43. issueMessage = true;
  44. }
  45. }
  46. CM_FALLTHROUGH;
  47. case cmPolicies::OLD:
  48. oldBehavior = true;
  49. break;
  50. case cmPolicies::NEW:
  51. case cmPolicies::REQUIRED_IF_USED:
  52. case cmPolicies::REQUIRED_ALWAYS:
  53. if (!state->GetCacheEntryValue(variable)) {
  54. ignoreVariable = true;
  55. }
  56. break;
  57. }
  58. // First see if we should issue a message about CMP0102
  59. if (issueMessage) {
  60. std::string err = cmStrCat(
  61. "Policy CMP0102 is not set: The variable named \"", variable,
  62. "\" is not in the cache. This results in an empty cache entry which "
  63. "is no longer created when policy CMP0102 is set to NEW. Run \"cmake "
  64. "--help-policy CMP0102\" for policy details. Use the cmake_policy "
  65. "command to set the policy and suppress this warning.");
  66. mf.IssueMessage(MessageType::AUTHOR_WARNING, err);
  67. }
  68. // If it's not in the cache and we're using the new behavior, nothing to
  69. // see here.
  70. if (ignoreVariable) {
  71. continue;
  72. }
  73. // Check if we want the old behavior of making a dummy cache entry.
  74. if (oldBehavior) {
  75. if (!state->GetCacheEntryValue(variable)) {
  76. status.GetMakefile().GetCMakeInstance()->AddCacheEntry(
  77. variable, nullptr, nullptr, cmStateEnums::UNINITIALIZED);
  78. overwrite = true;
  79. }
  80. }
  81. // We need a cache entry to do this.
  82. if (!state->GetCacheEntryValue(variable)) {
  83. cmSystemTools::Error("This should never happen...");
  84. return false;
  85. }
  86. if (!state->GetCacheEntryProperty(variable, "ADVANCED") || overwrite) {
  87. state->SetCacheEntryProperty(variable, "ADVANCED", value);
  88. }
  89. }
  90. return true;
  91. }