cmAddCustomTargetCommand.cxx 4.9 KB

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