cmCreateTestSourceList.cxx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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_func_name;
  52. // The rest of the arguments consist of a list of test source files.
  53. // Sadly, they can be in directories. Let's find a unique function
  54. // name for the corresponding test, and push it to the tests_func_name
  55. // list.
  56. // For the moment:
  57. // - replace spaces ' ', ':' and '/' with underscores '_'
  58. for(i = testsBegin; i != args.end(); ++i)
  59. {
  60. std::string func_name = *i;
  61. cmSystemTools::ConvertToUnixSlashes(func_name);
  62. cmSystemTools::ReplaceString(func_name, " ", "_");
  63. cmSystemTools::ReplaceString(func_name, "/", "_");
  64. cmSystemTools::ReplaceString(func_name, ":", "_");
  65. tests_func_name.push_back(func_name);
  66. fout << "int " << func_name << "(int, char**);\n";
  67. }
  68. fout <<
  69. "\n"
  70. "// Create map\n"
  71. "\n"
  72. "typedef int (*MainFuncPointer)(int , char**);\n"
  73. "struct functionMapEntry\n"
  74. "{\n"
  75. " const char* name;\n"
  76. " MainFuncPointer func;\n"
  77. "};\n"
  78. "\n"
  79. "functionMapEntry cmakeGeneratedFunctionMapEntries[] = {\n";
  80. int numTests = 0;
  81. std::vector<std::string>::iterator j;
  82. for(i = testsBegin, j = tests_func_name.begin(); i != args.end(); ++i, ++j)
  83. {
  84. fout <<
  85. " {\n"
  86. " \"" << *i << "\",\n"
  87. " " << *j << "\n"
  88. " },\n";
  89. numTests++;
  90. }
  91. fout <<
  92. "};\n"
  93. "\n"
  94. "int main(int ac, char** av)\n"
  95. "{\n"
  96. " int NumTests = " << numTests << ";\n"
  97. " int i;\n"
  98. " \n"
  99. " // If no test name was given\n"
  100. " if (ac < 2)\n"
  101. " {\n"
  102. " // If there is only one test, then run it with the arguments\n"
  103. " if (NumTests == 1)\n"
  104. " {\n"
  105. " return (*cmakeGeneratedFunctionMapEntries[0].func)(ac, av);\n"
  106. " }\n"
  107. " \n"
  108. " // Ask for a test\n"
  109. " printf(\"Available tests:\\n\");\n"
  110. " for (i =0; i < NumTests; ++i)\n"
  111. " {\n"
  112. " printf(\"%d. %s\\n\", i, cmakeGeneratedFunctionMapEntries[i].name);\n"
  113. " }\n"
  114. " printf(\"To run a test, enter the test number: \");\n"
  115. " int testNum = 0;\n"
  116. " scanf(\"%d\", &testNum);\n"
  117. " if (testNum >= NumTests)\n"
  118. " {\n"
  119. " printf(\"%d is an invalid test number.\\n\", testNum);\n"
  120. " return -1;\n"
  121. " }\n"
  122. " return (*cmakeGeneratedFunctionMapEntries[testNum].func)(ac-1, av+1);\n"
  123. " }\n"
  124. " \n"
  125. " // If partial match is requested\n"
  126. " int partial_match = (strcmp(av[1], \"-R\") == 0) ? 1 : 0;\n"
  127. " if (partial_match && ac < 3)\n"
  128. " {\n"
  129. " printf(\"-R needs an additional parameter.\\n\");\n"
  130. " return -1;\n"
  131. " }\n"
  132. " \n"
  133. " // A test name was given, try to find it\n"
  134. " for (i =0; i < NumTests; ++i)\n"
  135. " {\n"
  136. " if (partial_match && \n"
  137. " strstr(cmakeGeneratedFunctionMapEntries[i].name, av[2]) != NULL)\n"
  138. " {\n"
  139. " return (*cmakeGeneratedFunctionMapEntries[i].func)(ac-2, av+2);\n"
  140. " }\n"
  141. " else if (!partial_match && \n"
  142. " strcmp(cmakeGeneratedFunctionMapEntries[i].name, av[1]) == 0)\n"
  143. " {\n"
  144. " return (*cmakeGeneratedFunctionMapEntries[i].func)(ac-1, av+1);\n"
  145. " }\n"
  146. " }\n"
  147. " \n"
  148. " // If the test was not found but there is only one test, then\n"
  149. " // run it with the arguments\n"
  150. " if (NumTests == 1)\n"
  151. " {\n"
  152. " return (*cmakeGeneratedFunctionMapEntries[0].func)(ac, av);\n"
  153. " }\n"
  154. " \n"
  155. " // Nothing was run, display the test names\n"
  156. " printf(\"Available tests:\\n\");\n"
  157. " for (i =0; i < NumTests; ++i)\n"
  158. " {\n"
  159. " printf(\"%d. %s\\n\", i, cmakeGeneratedFunctionMapEntries[i].name);\n"
  160. " }\n"
  161. " printf(\"Failed: %s is an invalid test name.\\n\", av[1]);\n"
  162. " \n"
  163. " return -1;\n"
  164. "}\n";
  165. fout.close();
  166. // Create the source list
  167. cmSourceFile cfile;
  168. cfile.SetIsAnAbstractClass(false);
  169. cfile.SetName(args[1].c_str(),
  170. m_Makefile->GetCurrentOutputDirectory(),
  171. "cxx",
  172. false);
  173. m_Makefile->AddSource(cfile, sourceList);
  174. for(i = testsBegin; i != args.end(); ++i)
  175. {
  176. cmSourceFile cfile;
  177. cfile.SetIsAnAbstractClass(false);
  178. cfile.SetName(i->c_str(),
  179. m_Makefile->GetCurrentDirectory(),
  180. "cxx",
  181. false);
  182. m_Makefile->AddSource(cfile, sourceList);
  183. }
  184. return true;
  185. }