cmAddTestCommand.cxx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 "cmAddTestCommand.h"
  11. #include "cmTestGenerator.h"
  12. #include "cmTest.h"
  13. // cmExecutableCommand
  14. bool cmAddTestCommand
  15. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  16. {
  17. if(!args.empty() && args[0] == "NAME")
  18. {
  19. return this->HandleNameMode(args);
  20. }
  21. // First argument is the name of the test Second argument is the name of
  22. // the executable to run (a target or external program) Remaining arguments
  23. // are the arguments to pass to the executable
  24. if(args.size() < 2 )
  25. {
  26. this->SetError("called with incorrect number of arguments");
  27. return false;
  28. }
  29. // Collect the command with arguments.
  30. std::vector<std::string> command;
  31. for(std::vector<std::string>::const_iterator it = args.begin() + 1;
  32. it != args.end(); ++it)
  33. {
  34. command.push_back(*it);
  35. }
  36. // Create the test but add a generator only the first time it is
  37. // seen. This preserves behavior from before test generators.
  38. cmTest* test = this->Makefile->GetTest(args[0]);
  39. if(test)
  40. {
  41. // If the test was already added by a new-style signature do not
  42. // allow it to be duplicated.
  43. if(!test->GetOldStyle())
  44. {
  45. cmOStringStream e;
  46. e << " given test name \"" << args[0]
  47. << "\" which already exists in this directory.";
  48. this->SetError(e.str());
  49. return false;
  50. }
  51. }
  52. else
  53. {
  54. test = this->Makefile->CreateTest(args[0]);
  55. test->SetOldStyle(true);
  56. this->Makefile->AddTestGenerator(new cmTestGenerator(test));
  57. }
  58. test->SetCommand(command);
  59. return true;
  60. }
  61. //----------------------------------------------------------------------------
  62. bool cmAddTestCommand::HandleNameMode(std::vector<std::string> const& args)
  63. {
  64. std::string name;
  65. std::vector<std::string> configurations;
  66. std::string working_directory;
  67. std::vector<std::string> command;
  68. // Read the arguments.
  69. enum Doing {
  70. DoingName,
  71. DoingCommand,
  72. DoingConfigs,
  73. DoingWorkingDirectory,
  74. DoingNone
  75. };
  76. Doing doing = DoingName;
  77. for(unsigned int i=1; i < args.size(); ++i)
  78. {
  79. if(args[i] == "COMMAND")
  80. {
  81. if(!command.empty())
  82. {
  83. this->SetError(" may be given at most one COMMAND.");
  84. return false;
  85. }
  86. doing = DoingCommand;
  87. }
  88. else if(args[i] == "CONFIGURATIONS")
  89. {
  90. if(!configurations.empty())
  91. {
  92. this->SetError(" may be given at most one set of CONFIGURATIONS.");
  93. return false;
  94. }
  95. doing = DoingConfigs;
  96. }
  97. else if(args[i] == "WORKING_DIRECTORY")
  98. {
  99. if(!working_directory.empty())
  100. {
  101. this->SetError(" may be given at most one WORKING_DIRECTORY.");
  102. return false;
  103. }
  104. doing = DoingWorkingDirectory;
  105. }
  106. else if(doing == DoingName)
  107. {
  108. name = args[i];
  109. doing = DoingNone;
  110. }
  111. else if(doing == DoingCommand)
  112. {
  113. command.push_back(args[i]);
  114. }
  115. else if(doing == DoingConfigs)
  116. {
  117. configurations.push_back(args[i]);
  118. }
  119. else if(doing == DoingWorkingDirectory)
  120. {
  121. working_directory = args[i];
  122. doing = DoingNone;
  123. }
  124. else
  125. {
  126. cmOStringStream e;
  127. e << " given unknown argument:\n " << args[i] << "\n";
  128. this->SetError(e.str());
  129. return false;
  130. }
  131. }
  132. // Require a test name.
  133. if(name.empty())
  134. {
  135. this->SetError(" must be given non-empty NAME.");
  136. return false;
  137. }
  138. // Require a command.
  139. if(command.empty())
  140. {
  141. this->SetError(" must be given non-empty COMMAND.");
  142. return false;
  143. }
  144. // Require a unique test name within the directory.
  145. if(this->Makefile->GetTest(name))
  146. {
  147. cmOStringStream e;
  148. e << " given test NAME \"" << name
  149. << "\" which already exists in this directory.";
  150. this->SetError(e.str());
  151. return false;
  152. }
  153. // Add the test.
  154. cmTest* test = this->Makefile->CreateTest(name);
  155. test->SetOldStyle(false);
  156. test->SetCommand(command);
  157. if(!working_directory.empty())
  158. {
  159. test->SetProperty("WORKING_DIRECTORY", working_directory.c_str());
  160. }
  161. this->Makefile->AddTestGenerator(new cmTestGenerator(test, configurations));
  162. return true;
  163. }