1
0

cmCreateTestSourceList.cxx 8.5 KB

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