cmCreateTestSourceList.cxx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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 "cmCreateTestSourceList.h"
  14. // cmCreateTestSourceList
  15. bool cmCreateTestSourceList::InitialPass(std::vector<std::string> const& argsIn)
  16. {
  17. if (argsIn.size() < 3)
  18. {
  19. this->SetError("called with wrong number of arguments.");
  20. return false;
  21. }
  22. std::vector<std::string> args;
  23. cmSystemTools::ExpandListArguments(argsIn, args, true);
  24. std::vector<std::string>::iterator i = args.begin();
  25. // Name of the source list
  26. const char* sourceList = i->c_str();
  27. ++i;
  28. // Name of the test driver
  29. std::string driver = m_Makefile->GetCurrentOutputDirectory();
  30. driver += "/";
  31. driver += *i;
  32. driver += ".cxx";
  33. ++i;
  34. std::ofstream fout(driver.c_str());
  35. if (!fout)
  36. {
  37. std::string err = "Could not create file ";
  38. err += driver;
  39. err += " for cmCreateTestSourceList command.";
  40. this->SetError(err.c_str());
  41. return false;
  42. }
  43. // Create the test driver file
  44. fout <<
  45. "#include <stdio.h>\n"
  46. "#include <string.h>\n"
  47. "\n"
  48. "// Forward declare test functions\n"
  49. "\n";
  50. std::vector<std::string>::iterator testsBegin = i;
  51. std::vector<std::string> tests_filename;
  52. // The rest of the arguments consist of a list of test source files.
  53. // Sadly, they can be in directories. Let's modify each arg to get
  54. // a unique function name for the corresponding test, and push the
  55. // real source filename to the tests_filename var (used at the end).
  56. // For the moment:
  57. // - replace spaces ' ', ':' and '/' with underscores '_'
  58. for(i = testsBegin; i != args.end(); ++i)
  59. {
  60. tests_filename.push_back(*i);
  61. cmSystemTools::ConvertToUnixSlashes(*i);
  62. cmSystemTools::ReplaceString(*i, " ", "_");
  63. cmSystemTools::ReplaceString(*i, "/", "_");
  64. cmSystemTools::ReplaceString(*i, ":", "_");
  65. fout << "int " << *i << "(int, char**);\n";
  66. }
  67. fout <<
  68. "\n"
  69. "// Create map\n"
  70. "\n"
  71. "typedef int (*MainFuncPointer)(int , char**);\n"
  72. "struct functionMapEntry\n"
  73. "{\n"
  74. " const char* name;\n"
  75. " MainFuncPointer func;\n"
  76. "};\n"
  77. "\n"
  78. "functionMapEntry cmakeGeneratedFunctionMapEntries[] = {\n";
  79. int numTests = 0;
  80. for(i = testsBegin; i != args.end(); ++i)
  81. {
  82. fout <<
  83. " {\n"
  84. " \"" << *i << "\",\n"
  85. " " << *i << "\n"
  86. " },\n";
  87. numTests++;
  88. }
  89. fout <<
  90. "};\n"
  91. "\n"
  92. "int main(int ac, char** av)\n"
  93. "{\n"
  94. " int NumTests = " << numTests << ";\n"
  95. " int i;\n"
  96. " \n"
  97. " // If no test name was given\n"
  98. " if (ac < 2)\n"
  99. " {\n"
  100. " // If there is only one test, then run it with the arguments\n"
  101. " if (NumTests == 1)\n"
  102. " {\n"
  103. " return (*cmakeGeneratedFunctionMapEntries[0].func)(ac, av);\n"
  104. " }\n"
  105. " \n"
  106. " // Ask for a test\n"
  107. " printf(\"Available tests:\\n\");\n"
  108. " for (i =0; i < NumTests; ++i)\n"
  109. " {\n"
  110. " printf(\"%d. %s\\n\", i, cmakeGeneratedFunctionMapEntries[i].name);\n"
  111. " }\n"
  112. " printf(\"To run a test, enter the test number: \");\n"
  113. " int testNum = 0;\n"
  114. " scanf(\"%d\", &testNum);\n"
  115. " if (testNum >= NumTests)\n"
  116. " {\n"
  117. " printf(\"%d is an invalid test number.\\n\", testNum);\n"
  118. " return -1;\n"
  119. " }\n"
  120. " return (*cmakeGeneratedFunctionMapEntries[testNum].func)(ac-1, av+1);\n"
  121. " }\n"
  122. " \n"
  123. " // If partial match is requested\n"
  124. " int partial_match = (strcmp(av[1], \"-R\") == 0) ? 1 : 0;\n"
  125. " if (partial_match && ac < 3)\n"
  126. " {\n"
  127. " printf(\"-R needs an additional parameter.\\n\");\n"
  128. " return -1;\n"
  129. " }\n"
  130. " \n"
  131. " // A test name was given, try to find it\n"
  132. " for (i =0; i < NumTests; ++i)\n"
  133. " {\n"
  134. " if (partial_match && \n"
  135. " strstr(cmakeGeneratedFunctionMapEntries[i].name, av[2]) != NULL)\n"
  136. " {\n"
  137. " return (*cmakeGeneratedFunctionMapEntries[i].func)(ac-2, av+2);\n"
  138. " }\n"
  139. " else if (!partial_match && \n"
  140. " strcmp(cmakeGeneratedFunctionMapEntries[i].name, av[1]) == 0)\n"
  141. " {\n"
  142. " return (*cmakeGeneratedFunctionMapEntries[i].func)(ac-1, av+1);\n"
  143. " }\n"
  144. " }\n"
  145. " \n"
  146. " // If the test was not found but there is only one test, then\n"
  147. " // run it with the arguments\n"
  148. " if (NumTests == 1)\n"
  149. " {\n"
  150. " return (*cmakeGeneratedFunctionMapEntries[0].func)(ac, av);\n"
  151. " }\n"
  152. " \n"
  153. " // Nothing was run, display the test names\n"
  154. " printf(\"Available tests:\\n\");\n"
  155. " for (i =0; i < NumTests; ++i)\n"
  156. " {\n"
  157. " printf(\"%d. %s\\n\", i, cmakeGeneratedFunctionMapEntries[i].name);\n"
  158. " }\n"
  159. " printf(\"Failed: %s is an invalid test name.\\n\", av[1]);\n"
  160. " \n"
  161. " return -1;\n"
  162. "}\n";
  163. fout.close();
  164. // Create the source list
  165. cmSourceFile cfile;
  166. cfile.SetIsAnAbstractClass(false);
  167. cfile.SetName(args[1].c_str(),
  168. m_Makefile->GetCurrentOutputDirectory(),
  169. "cxx",
  170. false);
  171. m_Makefile->AddSource(cfile, sourceList);
  172. for (i = tests_filename.begin(); i != tests_filename.end(); ++i)
  173. {
  174. cmSourceFile cfile;
  175. cfile.SetIsAnAbstractClass(false);
  176. cfile.SetName(i->c_str(),
  177. m_Makefile->GetCurrentDirectory(),
  178. "cxx",
  179. false);
  180. m_Makefile->AddSource(cfile, sourceList);
  181. }
  182. return true;
  183. }