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