cmAddCustomCommandCommand.cxx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmAddCustomCommandCommand.h"
  14. #include "cmTarget.h"
  15. // cmAddCustomCommandCommand
  16. bool cmAddCustomCommandCommand::InitialPass(std::vector<std::string> const& args)
  17. {
  18. /* Let's complain at the end of this function about the lack of a particular
  19. arg. For the moment, let's say that COMMAND, and either TARGET or SOURCE
  20. are required.
  21. */
  22. if (args.size() < 4)
  23. {
  24. this->SetError("called with wrong number of arguments.");
  25. return false;
  26. }
  27. std::string source, target, comment, output, main_dependency,
  28. working;
  29. std::vector<std::string> depends, outputs;
  30. // Accumulate one command line at a time.
  31. cmCustomCommandLine currentLine;
  32. // Save all command lines.
  33. cmCustomCommandLines commandLines;
  34. cmTarget::CustomCommandType cctype = cmTarget::POST_BUILD;
  35. enum tdoing {
  36. doing_source,
  37. doing_command,
  38. doing_target,
  39. doing_depends,
  40. doing_main_dependency,
  41. doing_output,
  42. doing_outputs,
  43. doing_comment,
  44. doing_working_directory,
  45. doing_nothing
  46. };
  47. tdoing doing = doing_nothing;
  48. for (unsigned int j = 0; j < args.size(); ++j)
  49. {
  50. std::string const& copy = args[j];
  51. if(copy == "SOURCE")
  52. {
  53. doing = doing_source;
  54. }
  55. else if(copy == "COMMAND")
  56. {
  57. doing = doing_command;
  58. // Save the current command before starting the next command.
  59. if(!currentLine.empty())
  60. {
  61. commandLines.push_back(currentLine);
  62. currentLine.clear();
  63. }
  64. }
  65. else if(copy == "PRE_BUILD")
  66. {
  67. cctype = cmTarget::PRE_BUILD;
  68. }
  69. else if(copy == "PRE_LINK")
  70. {
  71. cctype = cmTarget::PRE_LINK;
  72. }
  73. else if(copy == "POST_BUILD")
  74. {
  75. cctype = cmTarget::POST_BUILD;
  76. }
  77. else if(copy == "TARGET")
  78. {
  79. doing = doing_target;
  80. }
  81. else if(copy == "ARGS")
  82. {
  83. // Ignore this old keyword.
  84. }
  85. else if (copy == "DEPENDS")
  86. {
  87. doing = doing_depends;
  88. }
  89. else if (copy == "OUTPUTS")
  90. {
  91. doing = doing_outputs;
  92. }
  93. else if (copy == "OUTPUT")
  94. {
  95. doing = doing_output;
  96. }
  97. else if (copy == "WORKING_DIRECTORY")
  98. {
  99. doing = doing_working_directory;
  100. }
  101. else if (copy == "MAIN_DEPENDENCY")
  102. {
  103. doing = doing_main_dependency;
  104. }
  105. else if (copy == "COMMENT")
  106. {
  107. doing = doing_comment;
  108. }
  109. else
  110. {
  111. std::string filename;
  112. switch (doing)
  113. {
  114. case doing_output:
  115. case doing_outputs:
  116. if (!cmSystemTools::FileIsFullPath(copy.c_str()))
  117. {
  118. filename = m_Makefile->GetStartDirectory();
  119. filename += "/";
  120. }
  121. filename += copy;
  122. break;
  123. case doing_source:
  124. // We do not want to convert the argument to SOURCE because
  125. // that option is only available for backward compatibility.
  126. // Old-style use of this command may use the SOURCE==TARGET
  127. // trick which we must preserve. If we convert the source
  128. // to a full path then it will no longer equal the target.
  129. default:
  130. break;
  131. }
  132. switch (doing)
  133. {
  134. case doing_working_directory:
  135. working = copy;
  136. break;
  137. case doing_source:
  138. source = copy;
  139. break;
  140. case doing_output:
  141. output = filename;
  142. break;
  143. case doing_main_dependency:
  144. main_dependency = copy;
  145. break;
  146. case doing_command:
  147. currentLine.push_back(copy);
  148. break;
  149. case doing_target:
  150. target = copy;
  151. break;
  152. case doing_depends:
  153. depends.push_back(copy);
  154. break;
  155. case doing_outputs:
  156. outputs.push_back(filename);
  157. break;
  158. case doing_comment:
  159. comment = copy;
  160. break;
  161. default:
  162. this->SetError("Wrong syntax. Unknown type of argument.");
  163. return false;
  164. }
  165. }
  166. }
  167. // Store the last command line finished.
  168. if(!currentLine.empty())
  169. {
  170. commandLines.push_back(currentLine);
  171. currentLine.clear();
  172. }
  173. // At this point we could complain about the lack of arguments. For
  174. // the moment, let's say that COMMAND, TARGET are always required.
  175. if(output.empty() && target.empty())
  176. {
  177. this->SetError("Wrong syntax. A TARGET or OUTPUT must be specified.");
  178. return false;
  179. }
  180. if(source.empty() && !target.empty() && !output.empty())
  181. {
  182. this->SetError("Wrong syntax. A TARGET and OUTPUT can not both be specified.");
  183. return false;
  184. }
  185. std::string::size_type pos = output.find_first_of("#<>");
  186. if(pos != output.npos)
  187. {
  188. cmOStringStream msg;
  189. msg << "called with OUTPUT containing a \"" << output[pos]
  190. << "\". This character is not allowed.";
  191. this->SetError(msg.str().c_str());
  192. return false;
  193. }
  194. // Choose which mode of the command to use.
  195. if(source.empty() && output.empty())
  196. {
  197. // Source is empty, use the target.
  198. std::vector<std::string> no_depends;
  199. m_Makefile->AddCustomCommandToTarget(target.c_str(), no_depends,
  200. commandLines, cctype,
  201. comment.c_str(), working.c_str());
  202. }
  203. else if(target.empty())
  204. {
  205. // Target is empty, use the output.
  206. m_Makefile->AddCustomCommandToOutput(output.c_str(), depends,
  207. main_dependency.c_str(),
  208. commandLines, comment.c_str(),
  209. working.c_str());
  210. }
  211. else
  212. {
  213. // Use the old-style mode for backward compatibility.
  214. m_Makefile->AddCustomCommandOldStyle(target.c_str(), outputs, depends,
  215. source.c_str(), commandLines,
  216. comment.c_str());
  217. }
  218. return true;
  219. }