cmAddCustomTargetCommand.cxx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. // Keep track of parser state.
  53. enum tdoing {
  54. doing_command,
  55. doing_depends,
  56. doing_working_directory
  57. };
  58. tdoing doing = doing_command;
  59. // Look for the ALL option.
  60. bool all = false;
  61. unsigned int start = 1;
  62. if(args.size() > 1)
  63. {
  64. if(args[1] == "ALL")
  65. {
  66. all = true;
  67. start = 2;
  68. }
  69. }
  70. // Parse the rest of the arguments.
  71. for(unsigned int j = start; j < args.size(); ++j)
  72. {
  73. std::string const& copy = args[j];
  74. if(copy == "DEPENDS")
  75. {
  76. doing = doing_depends;
  77. }
  78. else if(copy == "WORKING_DIRECTORY")
  79. {
  80. doing = doing_working_directory;
  81. }
  82. else if(copy == "COMMAND")
  83. {
  84. doing = doing_command;
  85. // Save the current command before starting the next command.
  86. if(!currentLine.empty())
  87. {
  88. commandLines.push_back(currentLine);
  89. currentLine.clear();
  90. }
  91. }
  92. else
  93. {
  94. switch (doing)
  95. {
  96. case doing_working_directory:
  97. working_directory = copy;
  98. break;
  99. case doing_command:
  100. currentLine.push_back(copy);
  101. break;
  102. case doing_depends:
  103. depends.push_back(copy);
  104. break;
  105. default:
  106. this->SetError("Wrong syntax. Unknown type of argument.");
  107. return false;
  108. }
  109. }
  110. }
  111. std::string::size_type pos = args[0].find_first_of("#<>");
  112. if(pos != args[0].npos)
  113. {
  114. cmOStringStream msg;
  115. msg << "called with target name containing a \"" << args[0][pos]
  116. << "\". This character is not allowed.";
  117. this->SetError(msg.str().c_str());
  118. return false;
  119. }
  120. // Store the last command line finished.
  121. if(!currentLine.empty())
  122. {
  123. commandLines.push_back(currentLine);
  124. currentLine.clear();
  125. }
  126. // Add the utility target to the makefile.
  127. const char* no_output = 0;
  128. this->Makefile->AddUtilityCommand(args[0].c_str(), all, no_output,
  129. working_directory.c_str(), depends,
  130. commandLines);
  131. return true;
  132. }