cmGetDirectoryPropertyCommand.cxx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 "cmStringAlgorithms.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. std::vector<std::string>::const_iterator 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 = *i;
  36. // make sure the start dir is a full path
  37. if (!cmSystemTools::FileIsFullPath(sd)) {
  38. sd = cmStrCat(status.GetMakefile().GetCurrentSourceDirectory(), '/', *i);
  39. }
  40. // The local generators are associated with collapsed paths.
  41. sd = cmSystemTools::CollapseFullPath(sd);
  42. // lookup the makefile from the directory name
  43. dir = status.GetMakefile().GetGlobalGenerator()->FindMakefile(sd);
  44. if (!dir) {
  45. status.SetError(
  46. "DIRECTORY argument provided but requested directory not found. "
  47. "This could be because the directory argument was invalid or, "
  48. "it is valid but has not been processed yet.");
  49. return false;
  50. }
  51. ++i;
  52. }
  53. // OK, now we have the directory to process, we just get the requested
  54. // information out of it
  55. if (*i == "DEFINITION") {
  56. ++i;
  57. if (i == args.end()) {
  58. status.SetError("A request for a variable definition was made without "
  59. "providing the name of the variable to get.");
  60. return false;
  61. }
  62. std::string const& output = dir->GetSafeDefinition(*i);
  63. status.GetMakefile().AddDefinition(variable, output);
  64. return true;
  65. }
  66. const char* prop = nullptr;
  67. if (!i->empty()) {
  68. if (*i == "DEFINITIONS") {
  69. switch (status.GetMakefile().GetPolicyStatus(cmPolicies::CMP0059)) {
  70. case cmPolicies::WARN:
  71. status.GetMakefile().IssueMessage(
  72. MessageType::AUTHOR_WARNING,
  73. cmPolicies::GetPolicyWarning(cmPolicies::CMP0059));
  74. CM_FALLTHROUGH;
  75. case cmPolicies::OLD:
  76. StoreResult(status.GetMakefile(), variable,
  77. status.GetMakefile().GetDefineFlagsCMP0059());
  78. return true;
  79. case cmPolicies::NEW:
  80. case cmPolicies::REQUIRED_ALWAYS:
  81. case cmPolicies::REQUIRED_IF_USED:
  82. break;
  83. }
  84. }
  85. prop = dir->GetProperty(*i);
  86. }
  87. StoreResult(status.GetMakefile(), variable, prop);
  88. return true;
  89. }
  90. namespace {
  91. void StoreResult(cmMakefile& makefile, std::string const& variable,
  92. const char* prop)
  93. {
  94. makefile.AddDefinition(variable, prop ? prop : "");
  95. }
  96. }