cmVariableRequiresCommand.cxx 2.1 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 "cmExecutionStatus.h"
  5. #include "cmMakefile.h"
  6. #include "cmProperty.h"
  7. #include "cmState.h"
  8. #include "cmStringAlgorithms.h"
  9. #include "cmSystemTools.h"
  10. // cmLibraryCommand
  11. bool cmVariableRequiresCommand(std::vector<std::string> const& args,
  12. cmExecutionStatus& status)
  13. {
  14. if (args.size() < 3) {
  15. status.SetError("called with incorrect number of arguments");
  16. return false;
  17. }
  18. std::string const& testVariable = args[0];
  19. if (!status.GetMakefile().IsOn(testVariable)) {
  20. return true;
  21. }
  22. std::string const& resultVariable = args[1];
  23. bool requirementsMet = true;
  24. std::string notSet;
  25. bool hasAdvanced = false;
  26. cmState* state = status.GetMakefile().GetState();
  27. for (unsigned int i = 2; i < args.size(); ++i) {
  28. if (!status.GetMakefile().IsOn(args[i])) {
  29. requirementsMet = false;
  30. notSet += args[i];
  31. notSet += "\n";
  32. if (state->GetCacheEntryValue(args[i]) &&
  33. state->GetCacheEntryPropertyAsBool(args[i], "ADVANCED")) {
  34. hasAdvanced = true;
  35. }
  36. }
  37. }
  38. cmProp reqVar = status.GetMakefile().GetDefinition(resultVariable);
  39. // if reqVar is unset, then set it to requirementsMet
  40. // if reqVar is set to true, but requirementsMet is false , then
  41. // set reqVar to false.
  42. if (!reqVar || (!requirementsMet && status.GetMakefile().IsOn(*reqVar))) {
  43. status.GetMakefile().AddDefinitionBool(resultVariable, requirementsMet);
  44. }
  45. if (!requirementsMet) {
  46. std::string message =
  47. cmStrCat("Variable assertion failed:\n", testVariable,
  48. " Requires that the following unset variables are set:\n",
  49. notSet, "\nPlease set them, or set ", testVariable,
  50. " 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);
  57. }
  58. return true;
  59. }