cmVariableRequiresCommand.cxx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 "cmVariableRequiresCommand.h"
  4. #include "cmMakefile.h"
  5. #include "cmPolicies.h"
  6. #include "cmState.h"
  7. #include "cmSystemTools.h"
  8. class cmExecutionStatus;
  9. // cmLibraryCommand
  10. bool cmVariableRequiresCommand::InitialPass(
  11. std::vector<std::string> const& args, cmExecutionStatus&)
  12. {
  13. if (args.size() < 3) {
  14. this->SetError("called with incorrect number of arguments");
  15. return false;
  16. }
  17. std::string testVariable = args[0];
  18. if (!this->Makefile->IsOn(testVariable)) {
  19. return true;
  20. }
  21. std::string resultVariable = args[1];
  22. bool requirementsMet = true;
  23. std::string notSet;
  24. bool hasAdvanced = false;
  25. cmState* state = this->Makefile->GetState();
  26. for (unsigned int i = 2; i < args.size(); ++i) {
  27. if (!this->Makefile->IsOn(args[i])) {
  28. requirementsMet = false;
  29. notSet += args[i];
  30. notSet += "\n";
  31. if (state->GetCacheEntryValue(args[i]) &&
  32. state->GetCacheEntryPropertyAsBool(args[i], "ADVANCED")) {
  33. hasAdvanced = true;
  34. }
  35. }
  36. }
  37. const char* reqVar = this->Makefile->GetDefinition(resultVariable);
  38. // if reqVar is unset, then set it to requirementsMet
  39. // if reqVar is set to true, but requirementsMet is false , then
  40. // set reqVar to false.
  41. if (!reqVar || (!requirementsMet && this->Makefile->IsOn(reqVar))) {
  42. this->Makefile->AddDefinition(resultVariable, requirementsMet);
  43. }
  44. if (!requirementsMet) {
  45. std::string message = "Variable assertion failed:\n";
  46. message +=
  47. testVariable + " Requires that the following unset variables are set:\n";
  48. message += notSet;
  49. message += "\nPlease set them, or set ";
  50. message += testVariable + " to false, and re-configure.\n";
  51. if (hasAdvanced) {
  52. message +=
  53. "One or more of the required variables is advanced."
  54. " To set the variable, you must turn on advanced mode in cmake.";
  55. }
  56. cmSystemTools::Error(message.c_str());
  57. }
  58. return true;
  59. }