cmVariableRequiresCommand.cxx 2.0 KB

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