cmAddCustomCommandCommand.cxx 6.5 KB

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