cmGetDirectoryPropertyCommand.cxx 3.0 KB

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