cmAddTestCommand.cxx 4.3 KB

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