cmCustomCommandGenerator.cxx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2010 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCustomCommandGenerator.h"
  11. #include "cmCustomCommand.h"
  12. #include "cmGeneratorExpression.h"
  13. #include "cmLocalGenerator.h"
  14. #include "cmMakefile.h"
  15. #include "cmOutputConverter.h"
  16. cmCustomCommandGenerator::cmCustomCommandGenerator(
  17. cmCustomCommand const& cc, const std::string& config, cmLocalGenerator* lg):
  18. CC(cc), Config(config), LG(lg),
  19. OldStyle(cc.GetEscapeOldStyle()), MakeVars(cc.GetEscapeAllowMakeVars()),
  20. GE(new cmGeneratorExpression(cc.GetBacktrace())), DependsDone(false)
  21. {
  22. }
  23. cmCustomCommandGenerator::~cmCustomCommandGenerator()
  24. {
  25. delete this->GE;
  26. }
  27. unsigned int cmCustomCommandGenerator::GetNumberOfCommands() const
  28. {
  29. return static_cast<unsigned int>(this->CC.GetCommandLines().size());
  30. }
  31. bool cmCustomCommandGenerator::UseCrossCompilingEmulator(unsigned int c) const
  32. {
  33. std::string const& argv0 = this->CC.GetCommandLines()[c][0];
  34. cmGeneratorTarget* target =
  35. this->LG->FindGeneratorTargetToUse(argv0);
  36. if(target && target->GetType() == cmState::EXECUTABLE)
  37. {
  38. return target->GetProperty("CROSSCOMPILING_EMULATOR") != 0;
  39. }
  40. return false;
  41. }
  42. std::string cmCustomCommandGenerator::GetCommand(unsigned int c) const
  43. {
  44. std::string const& argv0 = this->CC.GetCommandLines()[c][0];
  45. cmGeneratorTarget* target =
  46. this->LG->FindGeneratorTargetToUse(argv0);
  47. if(target && target->GetType() == cmState::EXECUTABLE &&
  48. (target->IsImported()
  49. || !this->LG->GetMakefile()->IsOn("CMAKE_CROSSCOMPILING")))
  50. {
  51. return target->GetLocation(this->Config);
  52. }
  53. if (target && target->GetType() == cmState::EXECUTABLE)
  54. {
  55. const char* emulator = target->GetProperty("CROSSCOMPILING_EMULATOR");
  56. if (emulator)
  57. {
  58. return std::string(emulator);
  59. }
  60. }
  61. cmsys::auto_ptr<cmCompiledGeneratorExpression> cge = this->GE->Parse(argv0);
  62. std::string exe = cge->Evaluate(this->LG, this->Config);
  63. return exe;
  64. }
  65. std::string escapeForShellOldStyle(const std::string& str)
  66. {
  67. std::string result;
  68. #if defined(_WIN32) && !defined(__CYGWIN__)
  69. // if there are spaces
  70. std::string temp = str;
  71. if (temp.find(" ") != std::string::npos &&
  72. temp.find("\"")==std::string::npos)
  73. {
  74. result = "\"";
  75. result += str;
  76. result += "\"";
  77. return result;
  78. }
  79. return str;
  80. #else
  81. for(const char* ch = str.c_str(); *ch != '\0'; ++ch)
  82. {
  83. if(*ch == ' ')
  84. {
  85. result += '\\';
  86. }
  87. result += *ch;
  88. }
  89. return result;
  90. #endif
  91. }
  92. void
  93. cmCustomCommandGenerator
  94. ::AppendArguments(unsigned int c, std::string& cmd) const
  95. {
  96. unsigned int offset = 1;
  97. if (this->UseCrossCompilingEmulator(c))
  98. {
  99. offset = 0;
  100. }
  101. cmCustomCommandLine const& commandLine = this->CC.GetCommandLines()[c];
  102. for(unsigned int j=offset;j < commandLine.size(); ++j)
  103. {
  104. std::string arg =
  105. this->GE->Parse(commandLine[j])->Evaluate(this->LG,
  106. this->Config);
  107. cmd += " ";
  108. if(this->OldStyle)
  109. {
  110. cmd += escapeForShellOldStyle(arg);
  111. }
  112. else
  113. {
  114. cmOutputConverter converter(this->LG->GetStateSnapshot());
  115. cmd += converter.EscapeForShell(arg, this->MakeVars);
  116. }
  117. }
  118. }
  119. const char* cmCustomCommandGenerator::GetComment() const
  120. {
  121. return this->CC.GetComment();
  122. }
  123. std::string cmCustomCommandGenerator::GetWorkingDirectory() const
  124. {
  125. return this->CC.GetWorkingDirectory();
  126. }
  127. std::vector<std::string> const& cmCustomCommandGenerator::GetOutputs() const
  128. {
  129. return this->CC.GetOutputs();
  130. }
  131. std::vector<std::string> const& cmCustomCommandGenerator::GetByproducts() const
  132. {
  133. return this->CC.GetByproducts();
  134. }
  135. std::vector<std::string> const& cmCustomCommandGenerator::GetDepends() const
  136. {
  137. if (!this->DependsDone)
  138. {
  139. this->DependsDone = true;
  140. std::vector<std::string> depends = this->CC.GetDepends();
  141. for(std::vector<std::string>::const_iterator
  142. i = depends.begin();
  143. i != depends.end(); ++i)
  144. {
  145. cmsys::auto_ptr<cmCompiledGeneratorExpression> cge
  146. = this->GE->Parse(*i);
  147. std::vector<std::string> result;
  148. cmSystemTools::ExpandListArgument(
  149. cge->Evaluate(this->LG, this->Config), result);
  150. for (std::vector<std::string>::iterator it = result.begin();
  151. it != result.end(); ++it)
  152. {
  153. if (cmSystemTools::FileIsFullPath(it->c_str()))
  154. {
  155. *it = cmSystemTools::CollapseFullPath(*it);
  156. }
  157. }
  158. this->Depends.insert(this->Depends.end(), result.begin(), result.end());
  159. }
  160. }
  161. return this->Depends;
  162. }