cmAddCustomTargetCommand.cxx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 "cmAddCustomTargetCommand.h"
  14. // cmAddCustomTargetCommand
  15. bool cmAddCustomTargetCommand
  16. ::InitialPass(std::vector<std::string> const& args,
  17. cmExecutionStatus&)
  18. {
  19. if(args.size() < 1 )
  20. {
  21. this->SetError("called with incorrect number of arguments");
  22. return false;
  23. }
  24. // Check the target name.
  25. if(args[0].find_first_of("/\\") != args[0].npos)
  26. {
  27. if(!this->Makefile->NeedBackwardsCompatibility(2,2))
  28. {
  29. cmOStringStream e;
  30. e << "called with invalid target name \"" << args[0]
  31. << "\". Target names may not contain a slash. "
  32. << "Use ADD_CUSTOM_COMMAND to generate files. "
  33. << "Set CMAKE_BACKWARDS_COMPATIBILITY to 2.2 "
  34. << "or lower to skip this check.";
  35. this->SetError(e.str().c_str());
  36. return false;
  37. }
  38. }
  39. // Accumulate one command line at a time.
  40. cmCustomCommandLine currentLine;
  41. // Save all command lines.
  42. cmCustomCommandLines commandLines;
  43. // Accumulate dependencies.
  44. std::vector<std::string> depends;
  45. std::string working_directory;
  46. bool verbatim = false;
  47. std::string comment_buffer;
  48. const char* comment = 0;
  49. std::vector<std::string> sources;
  50. // Keep track of parser state.
  51. enum tdoing {
  52. doing_command,
  53. doing_depends,
  54. doing_working_directory,
  55. doing_comment,
  56. doing_source,
  57. doing_verbatim
  58. };
  59. tdoing doing = doing_command;
  60. // Look for the ALL option.
  61. bool excludeFromAll = true;
  62. unsigned int start = 1;
  63. if(args.size() > 1)
  64. {
  65. if(args[1] == "ALL")
  66. {
  67. excludeFromAll = false;
  68. start = 2;
  69. }
  70. }
  71. // Parse the rest of the arguments.
  72. for(unsigned int j = start; j < args.size(); ++j)
  73. {
  74. std::string const& copy = args[j];
  75. if(copy == "DEPENDS")
  76. {
  77. doing = doing_depends;
  78. }
  79. else if(copy == "WORKING_DIRECTORY")
  80. {
  81. doing = doing_working_directory;
  82. }
  83. else if(copy == "VERBATIM")
  84. {
  85. doing = doing_verbatim;
  86. verbatim = true;
  87. }
  88. else if (copy == "COMMENT")
  89. {
  90. doing = doing_comment;
  91. }
  92. else if(copy == "COMMAND")
  93. {
  94. doing = doing_command;
  95. // Save the current command before starting the next command.
  96. if(!currentLine.empty())
  97. {
  98. commandLines.push_back(currentLine);
  99. currentLine.clear();
  100. }
  101. }
  102. else if(copy == "SOURCES")
  103. {
  104. doing = doing_source;
  105. }
  106. else
  107. {
  108. switch (doing)
  109. {
  110. case doing_working_directory:
  111. working_directory = copy;
  112. break;
  113. case doing_command:
  114. currentLine.push_back(copy);
  115. break;
  116. case doing_depends:
  117. depends.push_back(copy);
  118. break;
  119. case doing_comment:
  120. comment_buffer = copy;
  121. comment = comment_buffer.c_str();
  122. break;
  123. case doing_source:
  124. sources.push_back(copy);
  125. break;
  126. default:
  127. this->SetError("Wrong syntax. Unknown type of argument.");
  128. return false;
  129. }
  130. }
  131. }
  132. std::string::size_type pos = args[0].find_first_of("#<>");
  133. if(pos != args[0].npos)
  134. {
  135. cmOStringStream msg;
  136. msg << "called with target name containing a \"" << args[0][pos]
  137. << "\". This character is not allowed.";
  138. this->SetError(msg.str().c_str());
  139. return false;
  140. }
  141. // Store the last command line finished.
  142. if(!currentLine.empty())
  143. {
  144. commandLines.push_back(currentLine);
  145. currentLine.clear();
  146. }
  147. // Enforce name uniqueness.
  148. {
  149. std::string msg;
  150. if(!this->Makefile->EnforceUniqueName(args[0], msg, true))
  151. {
  152. this->SetError(msg.c_str());
  153. return false;
  154. }
  155. }
  156. // Add the utility target to the makefile.
  157. bool escapeOldStyle = !verbatim;
  158. cmTarget* target =
  159. this->Makefile->AddUtilityCommand(args[0].c_str(), excludeFromAll,
  160. working_directory.c_str(), depends,
  161. commandLines, escapeOldStyle, comment);
  162. // Add additional user-specified source files to the target.
  163. target->AddSources(sources);
  164. return true;
  165. }