cmAddTestCommand.cxx 4.2 KB

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