cmGetDirectoryPropertyCommand.cxx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. std::vector<std::string>::const_iterator 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 = *i;
  35. // make sure the start dir is a full path
  36. if (!cmSystemTools::FileIsFullPath(sd)) {
  37. sd = status.GetMakefile().GetCurrentSourceDirectory();
  38. sd += "/";
  39. sd += *i;
  40. }
  41. // The local generators are associated with collapsed paths.
  42. sd = cmSystemTools::CollapseFullPath(sd);
  43. // lookup the makefile from the directory name
  44. dir = status.GetMakefile().GetGlobalGenerator()->FindMakefile(sd);
  45. if (!dir) {
  46. status.SetError(
  47. "DIRECTORY argument provided but requested directory not found. "
  48. "This could be because the directory argument was invalid or, "
  49. "it is valid but has not been processed yet.");
  50. return false;
  51. }
  52. ++i;
  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. const char* prop = nullptr;
  68. if (!i->empty()) {
  69. if (*i == "DEFINITIONS") {
  70. switch (status.GetMakefile().GetPolicyStatus(cmPolicies::CMP0059)) {
  71. case cmPolicies::WARN:
  72. status.GetMakefile().IssueMessage(
  73. MessageType::AUTHOR_WARNING,
  74. cmPolicies::GetPolicyWarning(cmPolicies::CMP0059));
  75. CM_FALLTHROUGH;
  76. case cmPolicies::OLD:
  77. StoreResult(status.GetMakefile(), variable,
  78. status.GetMakefile().GetDefineFlagsCMP0059());
  79. return true;
  80. case cmPolicies::NEW:
  81. case cmPolicies::REQUIRED_ALWAYS:
  82. case cmPolicies::REQUIRED_IF_USED:
  83. break;
  84. }
  85. }
  86. prop = dir->GetProperty(*i);
  87. }
  88. StoreResult(status.GetMakefile(), variable, prop);
  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. }