cmGetDirectoryPropertyCommand.cxx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 "cmSystemTools.h"
  8. #include "cmValue.h"
  9. namespace {
  10. void StoreResult(cmMakefile& makefile, std::string const& variable,
  11. cmValue prop);
  12. }
  13. // cmGetDirectoryPropertyCommand
  14. bool cmGetDirectoryPropertyCommand(std::vector<std::string> const& args,
  15. cmExecutionStatus& status)
  16. {
  17. if (args.size() < 2) {
  18. status.SetError("called with incorrect number of arguments");
  19. return false;
  20. }
  21. auto i = args.begin();
  22. std::string const& variable = *i;
  23. ++i;
  24. // get the directory argument if there is one
  25. cmMakefile* dir = &status.GetMakefile();
  26. if (*i == "DIRECTORY") {
  27. ++i;
  28. if (i == args.end()) {
  29. status.SetError(
  30. "DIRECTORY argument provided without subsequent arguments");
  31. return false;
  32. }
  33. std::string sd = cmSystemTools::CollapseFullPath(
  34. *i, status.GetMakefile().GetCurrentSourceDirectory());
  35. // lookup the makefile from the directory name
  36. dir = status.GetMakefile().GetGlobalGenerator()->FindMakefile(sd);
  37. if (!dir) {
  38. status.SetError(
  39. "DIRECTORY argument provided but requested directory not found. "
  40. "This could be because the directory argument was invalid or, "
  41. "it is valid but has not been processed yet.");
  42. return false;
  43. }
  44. ++i;
  45. if (i == args.end()) {
  46. status.SetError("called with incorrect number of arguments");
  47. return false;
  48. }
  49. }
  50. // OK, now we have the directory to process, we just get the requested
  51. // information out of it
  52. if (*i == "DEFINITION") {
  53. ++i;
  54. if (i == args.end()) {
  55. status.SetError("A request for a variable definition was made without "
  56. "providing the name of the variable to get.");
  57. return false;
  58. }
  59. std::string const& output = dir->GetSafeDefinition(*i);
  60. status.GetMakefile().AddDefinition(variable, output);
  61. return true;
  62. }
  63. if (i->empty()) {
  64. status.SetError("given empty string for the property name to get");
  65. return false;
  66. }
  67. StoreResult(status.GetMakefile(), variable, dir->GetProperty(*i));
  68. return true;
  69. }
  70. namespace {
  71. void StoreResult(cmMakefile& makefile, std::string const& variable,
  72. cmValue prop)
  73. {
  74. makefile.AddDefinition(variable, prop);
  75. }
  76. }