cmAddTestCommand.cxx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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].c_str());
  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().c_str());
  49. return false;
  50. }
  51. }
  52. else
  53. {
  54. test = this->Makefile->CreateTest(args[0].c_str());
  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 = this->Makefile->GetCurrentOutputDirectory();
  67. bool working_directory_set = false;
  68. std::vector<std::string> command;
  69. // Read the arguments.
  70. enum Doing {
  71. DoingName,
  72. DoingCommand,
  73. DoingConfigs,
  74. DoingWorkingDirectory,
  75. DoingNone
  76. };
  77. Doing doing = DoingName;
  78. for(unsigned int i=1; i < args.size(); ++i)
  79. {
  80. if(args[i] == "COMMAND")
  81. {
  82. if(!command.empty())
  83. {
  84. this->SetError(" may be given at most one COMMAND.");
  85. return false;
  86. }
  87. doing = DoingCommand;
  88. }
  89. else if(args[i] == "CONFIGURATIONS")
  90. {
  91. if(!configurations.empty())
  92. {
  93. this->SetError(" may be given at most one set of CONFIGURATIONS.");
  94. return false;
  95. }
  96. doing = DoingConfigs;
  97. }
  98. else if(args[i] == "WORKING_DIRECTORY")
  99. {
  100. if(working_directory_set)
  101. {
  102. this->SetError(" may be given at most one WORKING_DIRECTORY.");
  103. return false;
  104. }
  105. doing = DoingWorkingDirectory;
  106. working_directory_set = true;
  107. }
  108. else if(doing == DoingName)
  109. {
  110. name = args[i];
  111. doing = DoingNone;
  112. }
  113. else if(doing == DoingCommand)
  114. {
  115. command.push_back(args[i]);
  116. }
  117. else if(doing == DoingConfigs)
  118. {
  119. configurations.push_back(args[i]);
  120. }
  121. else if(doing == DoingWorkingDirectory)
  122. {
  123. working_directory = args[i];
  124. doing = DoingNone;
  125. }
  126. else
  127. {
  128. cmOStringStream e;
  129. e << " given unknown argument:\n " << args[i] << "\n";
  130. this->SetError(e.str().c_str());
  131. return false;
  132. }
  133. }
  134. // Require a test name.
  135. if(name.empty())
  136. {
  137. this->SetError(" must be given non-empty NAME.");
  138. return false;
  139. }
  140. // Require a command.
  141. if(command.empty())
  142. {
  143. this->SetError(" must be given non-empty COMMAND.");
  144. return false;
  145. }
  146. // Require a unique test name within the directory.
  147. if(this->Makefile->GetTest(name.c_str()))
  148. {
  149. cmOStringStream e;
  150. e << " given test NAME \"" << name
  151. << "\" which already exists in this directory.";
  152. this->SetError(e.str().c_str());
  153. return false;
  154. }
  155. // Add the test.
  156. cmTest* test = this->Makefile->CreateTest(name.c_str());
  157. test->SetOldStyle(false);
  158. test->SetCommand(command);
  159. test->SetProperty("WORKING_DIRECTORY", working_directory.c_str());
  160. this->Makefile->AddTestGenerator(new cmTestGenerator(test, configurations));
  161. return true;
  162. }