cmGetDirectoryPropertyCommand.cxx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "cmGetDirectoryPropertyCommand.h"
  4. #include "cmExecutionStatus.h"
  5. #include "cmGlobalGenerator.h"
  6. #include "cmMakefile.h"
  7. #include "cmMessageType.h"
  8. #include "cmPolicies.h"
  9. #include "cmProperty.h"
  10. #include "cmSystemTools.h"
  11. namespace {
  12. void StoreResult(cmMakefile& makefile, std::string const& variable,
  13. const char* prop);
  14. void StoreResult(cmMakefile& makefile, std::string const& variable,
  15. cmProp prop);
  16. }
  17. // cmGetDirectoryPropertyCommand
  18. bool cmGetDirectoryPropertyCommand(std::vector<std::string> const& args,
  19. cmExecutionStatus& status)
  20. {
  21. if (args.size() < 2) {
  22. status.SetError("called with incorrect number of arguments");
  23. return false;
  24. }
  25. auto i = args.begin();
  26. std::string const& variable = *i;
  27. ++i;
  28. // get the directory argument if there is one
  29. cmMakefile* dir = &status.GetMakefile();
  30. if (*i == "DIRECTORY") {
  31. ++i;
  32. if (i == args.end()) {
  33. status.SetError(
  34. "DIRECTORY argument provided without subsequent arguments");
  35. return false;
  36. }
  37. std::string sd = cmSystemTools::CollapseFullPath(
  38. *i, status.GetMakefile().GetCurrentSourceDirectory());
  39. // lookup the makefile from the directory name
  40. dir = status.GetMakefile().GetGlobalGenerator()->FindMakefile(sd);
  41. if (!dir) {
  42. status.SetError(
  43. "DIRECTORY argument provided but requested directory not found. "
  44. "This could be because the directory argument was invalid or, "
  45. "it is valid but has not been processed yet.");
  46. return false;
  47. }
  48. ++i;
  49. if (i == args.end()) {
  50. status.SetError("called with incorrect number of arguments");
  51. return false;
  52. }
  53. }
  54. // OK, now we have the directory to process, we just get the requested
  55. // information out of it
  56. if (*i == "DEFINITION") {
  57. ++i;
  58. if (i == args.end()) {
  59. status.SetError("A request for a variable definition was made without "
  60. "providing the name of the variable to get.");
  61. return false;
  62. }
  63. std::string const& output = dir->GetSafeDefinition(*i);
  64. status.GetMakefile().AddDefinition(variable, output);
  65. return true;
  66. }
  67. if (i->empty()) {
  68. status.SetError("given empty string for the property name to get");
  69. return false;
  70. }
  71. if (*i == "DEFINITIONS") {
  72. switch (status.GetMakefile().GetPolicyStatus(cmPolicies::CMP0059)) {
  73. case cmPolicies::WARN:
  74. status.GetMakefile().IssueMessage(
  75. MessageType::AUTHOR_WARNING,
  76. cmPolicies::GetPolicyWarning(cmPolicies::CMP0059));
  77. CM_FALLTHROUGH;
  78. case cmPolicies::OLD:
  79. StoreResult(status.GetMakefile(), variable,
  80. status.GetMakefile().GetDefineFlagsCMP0059());
  81. return true;
  82. case cmPolicies::NEW:
  83. case cmPolicies::REQUIRED_ALWAYS:
  84. case cmPolicies::REQUIRED_IF_USED:
  85. break;
  86. }
  87. }
  88. StoreResult(status.GetMakefile(), variable, dir->GetProperty(*i));
  89. return true;
  90. }
  91. namespace {
  92. void StoreResult(cmMakefile& makefile, std::string const& variable,
  93. const char* prop)
  94. {
  95. makefile.AddDefinition(variable, prop ? prop : "");
  96. }
  97. void StoreResult(cmMakefile& makefile, std::string const& variable,
  98. cmProp prop)
  99. {
  100. makefile.AddDefinition(variable, prop);
  101. }
  102. }