cmGetDirectoryPropertyCommand.cxx 3.1 KB

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