You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
4.4 KiB

  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::InitialPass(
  16. std::vector<std::string> const& args)
  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. int major = 0;
  27. int minor = 0;
  28. if(const char* versionValue =
  29. this->Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY"))
  30. {
  31. sscanf(versionValue, "%d.%d", &major, &minor);
  32. }
  33. if(!major || major > 3 || (major == 2 && minor > 2))
  34. {
  35. cmOStringStream e;
  36. e << "called with invalid target name \"" << args[0]
  37. << "\". Target names may not contain a slash. "
  38. << "Use ADD_CUSTOM_COMMAND to generate files. "
  39. << "Set CMAKE_BACKWARDS_COMPATIBILITY to 2.2 "
  40. << "or lower to skip this check.";
  41. this->SetError(e.str().c_str());
  42. return false;
  43. }
  44. }
  45. // Accumulate one command line at a time.
  46. cmCustomCommandLine currentLine;
  47. // Save all command lines.
  48. cmCustomCommandLines commandLines;
  49. // Accumulate dependencies.
  50. std::vector<std::string> depends;
  51. std::string working_directory;
  52. bool verbatim = false;
  53. std::string comment_buffer;
  54. const char* comment = 0;
  55. // Keep track of parser state.
  56. enum tdoing {
  57. doing_command,
  58. doing_depends,
  59. doing_working_directory,
  60. doing_comment,
  61. doing_verbatim
  62. };
  63. tdoing doing = doing_command;
  64. // Look for the ALL option.
  65. bool excludeFromAll = true;
  66. unsigned int start = 1;
  67. if(args.size() > 1)
  68. {
  69. if(args[1] == "ALL")
  70. {
  71. excludeFromAll = false;
  72. start = 2;
  73. }
  74. }
  75. // Parse the rest of the arguments.
  76. for(unsigned int j = start; j < args.size(); ++j)
  77. {
  78. std::string const& copy = args[j];
  79. if(copy == "DEPENDS")
  80. {
  81. doing = doing_depends;
  82. }
  83. else if(copy == "WORKING_DIRECTORY")
  84. {
  85. doing = doing_working_directory;
  86. }
  87. else if(copy == "VERBATIM")
  88. {
  89. doing = doing_verbatim;
  90. verbatim = true;
  91. }
  92. else if (copy == "COMMENT")
  93. {
  94. doing = doing_comment;
  95. }
  96. else if(copy == "COMMAND")
  97. {
  98. doing = doing_command;
  99. // Save the current command before starting the next command.
  100. if(!currentLine.empty())
  101. {
  102. commandLines.push_back(currentLine);
  103. currentLine.clear();
  104. }
  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. 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. // Add the utility target to the makefile.
  145. bool escapeOldStyle = !verbatim;
  146. this->Makefile->AddUtilityCommand(args[0].c_str(), excludeFromAll,
  147. working_directory.c_str(), depends,
  148. commandLines, escapeOldStyle, comment);
  149. return true;
  150. }