cmCustomCommandGenerator.cxx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 "cmCustomCommandGenerator.h"
  4. #include "cmCustomCommand.h"
  5. #include "cmCustomCommandLines.h"
  6. #include "cmGeneratorExpression.h"
  7. #include "cmGeneratorTarget.h"
  8. #include "cmLocalGenerator.h"
  9. #include "cmMakefile.h"
  10. #include "cmOutputConverter.h"
  11. #include "cmStateTypes.h"
  12. #include "cmSystemTools.h"
  13. #include "cm_auto_ptr.hxx"
  14. #include <cmConfigure.h>
  15. cmCustomCommandGenerator::cmCustomCommandGenerator(cmCustomCommand const& cc,
  16. const std::string& config,
  17. cmLocalGenerator* lg)
  18. : CC(cc)
  19. , Config(config)
  20. , LG(lg)
  21. , OldStyle(cc.GetEscapeOldStyle())
  22. , MakeVars(cc.GetEscapeAllowMakeVars())
  23. , GE(new cmGeneratorExpression(cc.GetBacktrace()))
  24. , DependsDone(false)
  25. {
  26. }
  27. cmCustomCommandGenerator::~cmCustomCommandGenerator()
  28. {
  29. delete this->GE;
  30. }
  31. unsigned int cmCustomCommandGenerator::GetNumberOfCommands() const
  32. {
  33. return static_cast<unsigned int>(this->CC.GetCommandLines().size());
  34. }
  35. bool cmCustomCommandGenerator::UseCrossCompilingEmulator(unsigned int c) const
  36. {
  37. std::string const& argv0 = this->CC.GetCommandLines()[c][0];
  38. cmGeneratorTarget* target = this->LG->FindGeneratorTargetToUse(argv0);
  39. if (target && target->GetType() == cmStateEnums::EXECUTABLE) {
  40. return target->GetProperty("CROSSCOMPILING_EMULATOR") != CM_NULLPTR;
  41. }
  42. return false;
  43. }
  44. std::string cmCustomCommandGenerator::GetCommand(unsigned int c) const
  45. {
  46. std::string const& argv0 = this->CC.GetCommandLines()[c][0];
  47. cmGeneratorTarget* target = this->LG->FindGeneratorTargetToUse(argv0);
  48. if (target && target->GetType() == cmStateEnums::EXECUTABLE &&
  49. (target->IsImported() ||
  50. !this->LG->GetMakefile()->IsOn("CMAKE_CROSSCOMPILING"))) {
  51. return target->GetLocation(this->Config);
  52. }
  53. if (target && target->GetType() == cmStateEnums::EXECUTABLE) {
  54. const char* emulator = target->GetProperty("CROSSCOMPILING_EMULATOR");
  55. if (emulator) {
  56. return std::string(emulator);
  57. }
  58. }
  59. CM_AUTO_PTR<cmCompiledGeneratorExpression> cge = this->GE->Parse(argv0);
  60. std::string exe = cge->Evaluate(this->LG, this->Config);
  61. return exe;
  62. }
  63. std::string escapeForShellOldStyle(const std::string& str)
  64. {
  65. std::string result;
  66. #if defined(_WIN32) && !defined(__CYGWIN__)
  67. // if there are spaces
  68. std::string temp = str;
  69. if (temp.find(" ") != std::string::npos &&
  70. temp.find("\"") == std::string::npos) {
  71. result = "\"";
  72. result += str;
  73. result += "\"";
  74. return result;
  75. }
  76. return str;
  77. #else
  78. for (const char* ch = str.c_str(); *ch != '\0'; ++ch) {
  79. if (*ch == ' ') {
  80. result += '\\';
  81. }
  82. result += *ch;
  83. }
  84. return result;
  85. #endif
  86. }
  87. void cmCustomCommandGenerator::AppendArguments(unsigned int c,
  88. std::string& cmd) const
  89. {
  90. unsigned int offset = 1;
  91. if (this->UseCrossCompilingEmulator(c)) {
  92. offset = 0;
  93. }
  94. cmCustomCommandLine const& commandLine = this->CC.GetCommandLines()[c];
  95. for (unsigned int j = offset; j < commandLine.size(); ++j) {
  96. std::string arg =
  97. this->GE->Parse(commandLine[j])->Evaluate(this->LG, this->Config);
  98. cmd += " ";
  99. if (this->OldStyle) {
  100. cmd += escapeForShellOldStyle(arg);
  101. } else {
  102. cmOutputConverter converter(this->LG->GetStateSnapshot());
  103. cmd += converter.EscapeForShell(arg, this->MakeVars);
  104. }
  105. }
  106. }
  107. const char* cmCustomCommandGenerator::GetComment() const
  108. {
  109. return this->CC.GetComment();
  110. }
  111. std::string cmCustomCommandGenerator::GetWorkingDirectory() const
  112. {
  113. return this->CC.GetWorkingDirectory();
  114. }
  115. std::vector<std::string> const& cmCustomCommandGenerator::GetOutputs() const
  116. {
  117. return this->CC.GetOutputs();
  118. }
  119. std::vector<std::string> const& cmCustomCommandGenerator::GetByproducts() const
  120. {
  121. return this->CC.GetByproducts();
  122. }
  123. std::vector<std::string> const& cmCustomCommandGenerator::GetDepends() const
  124. {
  125. if (!this->DependsDone) {
  126. this->DependsDone = true;
  127. std::vector<std::string> depends = this->CC.GetDepends();
  128. for (std::vector<std::string>::const_iterator i = depends.begin();
  129. i != depends.end(); ++i) {
  130. CM_AUTO_PTR<cmCompiledGeneratorExpression> cge = this->GE->Parse(*i);
  131. std::vector<std::string> result;
  132. cmSystemTools::ExpandListArgument(cge->Evaluate(this->LG, this->Config),
  133. result);
  134. for (std::vector<std::string>::iterator it = result.begin();
  135. it != result.end(); ++it) {
  136. if (cmSystemTools::FileIsFullPath(it->c_str())) {
  137. *it = cmSystemTools::CollapseFullPath(*it);
  138. }
  139. }
  140. this->Depends.insert(this->Depends.end(), result.begin(), result.end());
  141. }
  142. }
  143. return this->Depends;
  144. }