cmCustomCommandGenerator.cxx 5.2 KB

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