cmCreateTestSourceList.cxx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. "}\n"
  96. "#endif\n";
  97. if(extraInclude.size())
  98. {
  99. fout << "#include \"" << extraInclude << "\"\n";
  100. }
  101. fout <<
  102. "\n"
  103. "/* Forward declare test functions. */\n"
  104. "\n";
  105. std::vector<std::string>::const_iterator testsBegin = i;
  106. std::vector<std::string> tests_func_name;
  107. // The rest of the arguments consist of a list of test source files.
  108. // Sadly, they can be in directories. Let's find a unique function
  109. // name for the corresponding test, and push it to the tests_func_name
  110. // list.
  111. // For the moment:
  112. // - replace spaces ' ', ':' and '/' with underscores '_'
  113. for(i = testsBegin; i != tests.end(); ++i)
  114. {
  115. if(*i == "EXTRA_INCLUDE")
  116. {
  117. break;
  118. }
  119. std::string func_name;
  120. if (cmSystemTools::GetFilenamePath(*i).size() > 0)
  121. {
  122. func_name = cmSystemTools::GetFilenamePath(*i) + "/" +
  123. cmSystemTools::GetFilenameWithoutLastExtension(*i);
  124. }
  125. else
  126. {
  127. func_name = cmSystemTools::GetFilenameWithoutLastExtension(*i);
  128. }
  129. cmSystemTools::ConvertToUnixSlashes(func_name);
  130. cmSystemTools::ReplaceString(func_name, " ", "_");
  131. cmSystemTools::ReplaceString(func_name, "/", "_");
  132. cmSystemTools::ReplaceString(func_name, ":", "_");
  133. tests_func_name.push_back(func_name);
  134. fout << "int " << func_name << "(int, char*[]);\n";
  135. }
  136. fout <<
  137. "\n"
  138. "/* Create map. */\n"
  139. "\n"
  140. "typedef int (*MainFuncPointer)(int , char*[]);\n"
  141. "typedef struct\n"
  142. "{\n"
  143. " const char* name;\n"
  144. " MainFuncPointer func;\n"
  145. "} functionMapEntry;\n"
  146. "\n"
  147. "functionMapEntry cmakeGeneratedFunctionMapEntries[] = {\n";
  148. int numTests = 0;
  149. std::vector<std::string>::iterator j;
  150. for(i = testsBegin, j = tests_func_name.begin(); i != tests.end(); ++i, ++j)
  151. {
  152. std::string func_name;
  153. if (cmSystemTools::GetFilenamePath(*i).size() > 0)
  154. {
  155. func_name = cmSystemTools::GetFilenamePath(*i) + "/" +
  156. cmSystemTools::GetFilenameWithoutLastExtension(*i);
  157. }
  158. else
  159. {
  160. func_name = cmSystemTools::GetFilenameWithoutLastExtension(*i);
  161. }
  162. fout <<
  163. " {\n"
  164. " \"" << func_name << "\",\n"
  165. " " << *j << "\n"
  166. " },\n";
  167. numTests++;
  168. }
  169. // end with an empty struct
  170. fout << " {0,0}\n";
  171. fout <<
  172. "};\n"
  173. "\n"
  174. "/* Allocate and create a lowercased copy of string\n"
  175. " (note that it has to be free'd manually) */\n"
  176. "\n"
  177. "char* lowercase(const char *string)\n"
  178. "{\n"
  179. " char *new_string, *p;\n"
  180. " new_string = (char *)malloc(sizeof(char) * (size_t)(strlen(string) + 1));\n"
  181. " if (!new_string)\n"
  182. " {\n"
  183. " return 0;\n"
  184. " }\n"
  185. " strcpy(new_string, string);\n"
  186. " p = new_string;\n"
  187. " while (*p != 0)\n"
  188. " {\n"
  189. " *p = (char)tolower(*p);\n"
  190. " ++p;\n"
  191. " }\n"
  192. " return new_string;\n"
  193. "}\n"
  194. "\n"
  195. "int main(int ac, char *av[])\n"
  196. "{\n"
  197. " int i, NumTests, testNum, partial_match;\n"
  198. " char *arg, *test_name;\n"
  199. " \n"
  200. "#if defined(_MSC_VER) && defined(_DEBUG)\n"
  201. " /* If running from DART, put in debug hook. */\n"
  202. " if(getenv(\"DART_TEST_FROM_DART\"))\n"
  203. " {\n"
  204. " _CrtSetReportHook(TestDriverDebugReport);\n"
  205. " }\n"
  206. "#endif\n"
  207. " \n"
  208. " NumTests = " << numTests << ";\n"
  209. " \n"
  210. " /* If no test name was given */\n";
  211. if(function.size())
  212. {
  213. fout << " /* process command line with user function. */\n"
  214. << " " << function << "(&ac, &av);\n";
  215. }
  216. fout <<
  217. " if (ac < 2)\n"
  218. " {\n"
  219. " /* Ask for a test. */\n"
  220. " printf(\"Available tests:\\n\");\n"
  221. " for (i =0; i < NumTests; ++i)\n"
  222. " {\n"
  223. " printf(\"%3d. %s\\n\", i, cmakeGeneratedFunctionMapEntries[i].name);\n"
  224. " }\n"
  225. " printf(\"To run a test, enter the test number: \");\n"
  226. " testNum = 0;\n"
  227. " scanf(\"%d\", &testNum);\n"
  228. " if (testNum >= NumTests)\n"
  229. " {\n"
  230. " printf(\"%3d is an invalid test number.\\n\", testNum);\n"
  231. " return -1;\n"
  232. " }\n"
  233. " return (*cmakeGeneratedFunctionMapEntries[testNum].func)(ac-1, av+1);\n"
  234. " }\n"
  235. " \n"
  236. " /* If partial match is requested. */\n"
  237. " partial_match = (strcmp(av[1], \"-R\") == 0) ? 1 : 0;\n"
  238. " if (partial_match && ac < 3)\n"
  239. " {\n"
  240. " printf(\"-R needs an additional parameter.\\n\");\n"
  241. " return -1;\n"
  242. " }\n"
  243. " \n"
  244. " arg = lowercase(av[1 + partial_match]);\n"
  245. " for (i =0; i < NumTests; ++i)\n"
  246. " {\n"
  247. " test_name = lowercase(cmakeGeneratedFunctionMapEntries[i].name);\n"
  248. " if (partial_match && strstr(test_name, arg) != NULL)\n"
  249. " {\n"
  250. " free(arg);\n"
  251. " free(test_name);\n"
  252. " return (*cmakeGeneratedFunctionMapEntries[i].func)(ac - 2, av + 2);\n"
  253. " }\n"
  254. " else if (!partial_match && strcmp(test_name, arg) == 0)\n"
  255. " {\n"
  256. " free(arg);\n"
  257. " free(test_name);\n"
  258. " return (*cmakeGeneratedFunctionMapEntries[i].func)(ac - 1, av + 1);\n"
  259. " }\n"
  260. " free(test_name);\n"
  261. " }\n"
  262. " free(arg);\n"
  263. " \n"
  264. " /* Nothing was run, display the test names. */\n"
  265. " printf(\"Available tests:\\n\");\n"
  266. " for (i =0; i < NumTests; ++i)\n"
  267. " {\n"
  268. " printf(\"%3d. %s\\n\", i, cmakeGeneratedFunctionMapEntries[i].name);\n"
  269. " }\n"
  270. " printf(\"Failed: %s is an invalid test name.\\n\", av[1]);\n"
  271. " \n"
  272. " return -1;\n"
  273. "}\n";
  274. fout.close();
  275. // Create the source list
  276. cmSourceFile cfile;
  277. std::string sourceListValue;
  278. cfile.SetProperty("ABSTRACT","0");
  279. cfile.SetName(cmSystemTools::GetFilenameWithoutExtension(args[1]).c_str(),
  280. m_Makefile->GetCurrentOutputDirectory(),
  281. cmSystemTools::GetFilenameExtension(args[1]).c_str()+1,
  282. false);
  283. m_Makefile->AddSource(cfile);
  284. sourceListValue = args[1];
  285. for(i = testsBegin; i != tests.end(); ++i)
  286. {
  287. cmSourceFile icfile;
  288. icfile.SetProperty("ABSTRACT","0");
  289. icfile.SetName(i->c_str(),
  290. m_Makefile->GetCurrentDirectory(),
  291. m_Makefile->GetSourceExtensions(),
  292. m_Makefile->GetHeaderExtensions());
  293. m_Makefile->AddSource(icfile);
  294. sourceListValue += ";";
  295. sourceListValue += *i;
  296. }
  297. m_Makefile->AddDefinition(sourceList, sourceListValue.c_str());
  298. return true;
  299. }