cmAddTestCommand.cxx 4.5 KB

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