cmCreateTestSourceList.cxx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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);
  24. std::vector<std::string>::iterator i = args.begin();
  25. std::string extraInclude;
  26. std::string function;
  27. std::vector<std::string> tests;
  28. // extract extra include and function ot
  29. for(; i != args.end(); i++)
  30. {
  31. if(*i == "EXTRA_INCLUDE")
  32. {
  33. ++i;
  34. if(i == args.end())
  35. {
  36. this->SetError("incorrect arguments to EXTRA_INCLUDE");
  37. return false;
  38. }
  39. extraInclude = *i;
  40. }
  41. else if(*i == "FUNCTION")
  42. {
  43. ++i;
  44. if(i == args.end())
  45. {
  46. this->SetError("incorrect arguments to FUNCTION");
  47. return false;
  48. }
  49. function = *i;
  50. }
  51. else
  52. {
  53. tests.push_back(*i);
  54. }
  55. }
  56. i = tests.begin();
  57. // Name of the source list
  58. const char* sourceList = i->c_str();
  59. ++i;
  60. // Name of the test driver
  61. std::string driver = m_Makefile->GetCurrentOutputDirectory();
  62. driver += "/";
  63. driver += *i;
  64. driver += ".cxx";
  65. ++i;
  66. std::ofstream fout(driver.c_str());
  67. if (!fout)
  68. {
  69. std::string err = "Could not create file ";
  70. err += driver;
  71. err += " for cmCreateTestSourceList command.";
  72. this->SetError(err.c_str());
  73. return false;
  74. }
  75. // Create the test driver file
  76. fout <<
  77. "#include <ctype.h>\n"
  78. "#include <stdio.h>\n"
  79. "#include <string.h>\n"
  80. "#include <malloc.h>\n";
  81. if(extraInclude.size())
  82. {
  83. fout << "#include \"" << extraInclude << "\"\n";
  84. }
  85. fout <<
  86. "\n"
  87. "// Forward declare test functions\n"
  88. "\n";
  89. std::vector<std::string>::iterator testsBegin = i;
  90. std::vector<std::string> tests_func_name;
  91. // The rest of the arguments consist of a list of test source files.
  92. // Sadly, they can be in directories. Let's find a unique function
  93. // name for the corresponding test, and push it to the tests_func_name
  94. // list.
  95. // For the moment:
  96. // - replace spaces ' ', ':' and '/' with underscores '_'
  97. for(i = testsBegin; i != tests.end(); ++i)
  98. {
  99. if(*i == "EXTRA_INCLUDE")
  100. {
  101. break;
  102. }
  103. std::string func_name = *i;
  104. cmSystemTools::ConvertToUnixSlashes(func_name);
  105. cmSystemTools::ReplaceString(func_name, " ", "_");
  106. cmSystemTools::ReplaceString(func_name, "/", "_");
  107. cmSystemTools::ReplaceString(func_name, ":", "_");
  108. tests_func_name.push_back(func_name);
  109. fout << "int " << func_name << "(int, char**);\n";
  110. }
  111. fout <<
  112. "\n"
  113. "// Create map\n"
  114. "\n"
  115. "typedef int (*MainFuncPointer)(int , char**);\n"
  116. "struct functionMapEntry\n"
  117. "{\n"
  118. " const char* name;\n"
  119. " MainFuncPointer func;\n"
  120. "};\n"
  121. "\n"
  122. "functionMapEntry cmakeGeneratedFunctionMapEntries[] = {\n";
  123. int numTests = 0;
  124. std::vector<std::string>::iterator j;
  125. for(i = testsBegin, j = tests_func_name.begin(); i != tests.end(); ++i, ++j)
  126. {
  127. fout <<
  128. " {\n"
  129. " \"" << *i << "\",\n"
  130. " " << *j << "\n"
  131. " },\n";
  132. numTests++;
  133. }
  134. fout <<
  135. "};\n"
  136. "\n"
  137. "// Allocate and create a lowercased copy of string\n"
  138. "// (note that it has to be free'd manually)\n"
  139. "\n"
  140. "char* lowercase(const char *string)\n"
  141. "{\n"
  142. " char *new_string, *p;\n"
  143. " new_string = (char *)malloc(sizeof(char) * (size_t)(strlen(string) + 1));\n"
  144. " if (!new_string)\n"
  145. " {\n"
  146. " return NULL;\n"
  147. " }\n"
  148. " strcpy(new_string, string);\n"
  149. " p = new_string;\n"
  150. " while (*p != 0)\n"
  151. " {\n"
  152. " *p = tolower(*p);\n"
  153. " ++p;\n"
  154. " }\n"
  155. " return new_string;\n"
  156. "}\n"
  157. "\n"
  158. "int main(int ac, char** av)\n"
  159. "{\n"
  160. " int NumTests, i, testNum, partial_match;\n"
  161. " char *arg, *test_name;\n"
  162. " \n"
  163. " NumTests = " << numTests << ";\n"
  164. " \n"
  165. " // If no test name was given\n";
  166. if(function.size())
  167. {
  168. fout << " // process command line with user function\n"
  169. << " " << function << "(&ac, &av);\n";
  170. }
  171. fout <<
  172. " if (ac < 2)\n"
  173. " {\n"
  174. " // If there is only one test, then run it with the arguments\n"
  175. " if (NumTests == 1)\n"
  176. " {\n"
  177. " return (*cmakeGeneratedFunctionMapEntries[0].func)(ac, av);\n"
  178. " }\n"
  179. " \n"
  180. " // Ask for a test\n"
  181. " printf(\"Available tests:\\n\");\n"
  182. " for (i =0; i < NumTests; ++i)\n"
  183. " {\n"
  184. " printf(\"%3d. %s\\n\", i, cmakeGeneratedFunctionMapEntries[i].name);\n"
  185. " }\n"
  186. " printf(\"To run a test, enter the test number: \");\n"
  187. " testNum = 0;\n"
  188. " scanf(\"%d\", &testNum);\n"
  189. " if (testNum >= NumTests)\n"
  190. " {\n"
  191. " printf(\"%3d is an invalid test number.\\n\", testNum);\n"
  192. " return -1;\n"
  193. " }\n"
  194. " return (*cmakeGeneratedFunctionMapEntries[testNum].func)(ac-1, av+1);\n"
  195. " }\n"
  196. " \n"
  197. " // If partial match is requested\n"
  198. " partial_match = (strcmp(av[1], \"-R\") == 0) ? 1 : 0;\n"
  199. " if (partial_match && ac < 3)\n"
  200. " {\n"
  201. " printf(\"-R needs an additional parameter.\\n\");\n"
  202. " return -1;\n"
  203. " }\n"
  204. " \n"
  205. " arg = lowercase(av[1 + partial_match]);\n"
  206. " for (i =0; i < NumTests; ++i)\n"
  207. " {\n"
  208. " test_name = lowercase(cmakeGeneratedFunctionMapEntries[i].name);\n"
  209. " if (partial_match && strstr(test_name, arg) != NULL)\n"
  210. " {\n"
  211. " return (*cmakeGeneratedFunctionMapEntries[i].func)(ac - 2, av + 2);\n"
  212. " }\n"
  213. " else if (!partial_match && strcmp(test_name, arg) == 0)\n"
  214. " {\n"
  215. " return (*cmakeGeneratedFunctionMapEntries[i].func)(ac - 1, av + 1);\n"
  216. " }\n"
  217. " free(test_name);\n"
  218. " }\n"
  219. " free(arg);\n"
  220. " \n"
  221. " // If the test was not found but there is only one test, then\n"
  222. " // run it with the arguments\n"
  223. " if (NumTests == 1)\n"
  224. " {\n"
  225. " return (*cmakeGeneratedFunctionMapEntries[0].func)(ac, av);\n"
  226. " }\n"
  227. " \n"
  228. " // Nothing was run, display the test names\n"
  229. " printf(\"Available tests:\\n\");\n"
  230. " for (i =0; i < NumTests; ++i)\n"
  231. " {\n"
  232. " printf(\"%3d. %s\\n\", i, cmakeGeneratedFunctionMapEntries[i].name);\n"
  233. " }\n"
  234. " printf(\"Failed: %s is an invalid test name.\\n\", av[1]);\n"
  235. " \n"
  236. " return -1;\n"
  237. "}\n";
  238. fout.close();
  239. // Create the source list
  240. cmSourceFile cfile;
  241. std::string sourceListValue;
  242. cfile.SetIsAnAbstractClass(false);
  243. cfile.SetName(args[1].c_str(),
  244. m_Makefile->GetCurrentOutputDirectory(),
  245. "cxx",
  246. false);
  247. m_Makefile->AddSource(cfile);
  248. sourceListValue = args[1] + ".cxx";
  249. for(i = testsBegin; i != tests.end(); ++i)
  250. {
  251. cmSourceFile cfile;
  252. cfile.SetIsAnAbstractClass(false);
  253. cfile.SetName(i->c_str(),
  254. m_Makefile->GetCurrentDirectory(),
  255. m_Makefile->GetSourceExtensions(),
  256. m_Makefile->GetHeaderExtensions());
  257. m_Makefile->AddSource(cfile);
  258. sourceListValue += ";";
  259. sourceListValue += *i;
  260. }
  261. m_Makefile->AddDefinition(sourceList, sourceListValue.c_str());
  262. return true;
  263. }