cmCreateTestSourceList.cxx 9.1 KB

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