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& 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. fout <<
  88. "#if defined(_MSC_VER) && defined(_DEBUG)\n"
  89. "/* MSVC debug hook to prevent dialogs when running from DART. */\n"
  90. "# include <crtdbg.h>\n"
  91. "static int TestDriverDebugReport(int type, char* message, int* retVal)\n"
  92. "{\n"
  93. " (void)type; (void)retVal;\n"
  94. " if(getenv(\"DART_TEST_FROM_DART\"))\n"
  95. " {\n"
  96. " fprintf(stderr, message);\n"
  97. " exit(1);\n"
  98. " }\n"
  99. " return 0;\n"
  100. "}\n"
  101. "#endif\n";
  102. if(extraInclude.size())
  103. {
  104. fout << "#include \"" << extraInclude << "\"\n";
  105. }
  106. fout <<
  107. "\n"
  108. "/* Forward declare test functions. */\n"
  109. "\n";
  110. std::vector<std::string>::iterator testsBegin = i;
  111. std::vector<std::string> tests_func_name;
  112. // The rest of the arguments consist of a list of test source files.
  113. // Sadly, they can be in directories. Let's find a unique function
  114. // name for the corresponding test, and push it to the tests_func_name
  115. // list.
  116. // For the moment:
  117. // - replace spaces ' ', ':' and '/' with underscores '_'
  118. for(i = testsBegin; i != tests.end(); ++i)
  119. {
  120. if(*i == "EXTRA_INCLUDE")
  121. {
  122. break;
  123. }
  124. std::string func_name;
  125. if (cmSystemTools::GetFilenamePath(*i).size() > 0)
  126. {
  127. func_name = cmSystemTools::GetFilenamePath(*i) + "/" +
  128. cmSystemTools::GetFilenameWithoutLastExtension(*i);
  129. }
  130. else
  131. {
  132. func_name = cmSystemTools::GetFilenameWithoutLastExtension(*i);
  133. }
  134. cmSystemTools::ConvertToUnixSlashes(func_name);
  135. cmSystemTools::ReplaceString(func_name, " ", "_");
  136. cmSystemTools::ReplaceString(func_name, "/", "_");
  137. cmSystemTools::ReplaceString(func_name, ":", "_");
  138. tests_func_name.push_back(func_name);
  139. fout << "int " << func_name << "(int, char*[]);\n";
  140. }
  141. fout <<
  142. "\n"
  143. "/* Create map. */\n"
  144. "\n"
  145. "typedef int (*MainFuncPointer)(int , char*[]);\n"
  146. "typedef struct\n"
  147. "{\n"
  148. " const char* name;\n"
  149. " MainFuncPointer func;\n"
  150. "} functionMapEntry;\n"
  151. "\n"
  152. "functionMapEntry cmakeGeneratedFunctionMapEntries[] = {\n";
  153. int numTests = 0;
  154. std::vector<std::string>::iterator j;
  155. for(i = testsBegin, j = tests_func_name.begin(); i != tests.end(); ++i, ++j)
  156. {
  157. std::string func_name;
  158. if (cmSystemTools::GetFilenamePath(*i).size() > 0)
  159. {
  160. func_name = cmSystemTools::GetFilenamePath(*i) + "/" +
  161. cmSystemTools::GetFilenameWithoutLastExtension(*i);
  162. }
  163. else
  164. {
  165. func_name = cmSystemTools::GetFilenameWithoutLastExtension(*i);
  166. }
  167. fout <<
  168. " {\n"
  169. " \"" << func_name << "\",\n"
  170. " " << *j << "\n"
  171. " },\n";
  172. numTests++;
  173. }
  174. fout <<
  175. "};\n"
  176. "\n"
  177. "/* Allocate and create a lowercased copy of string\n"
  178. " (note that it has to be free'd manually) */\n"
  179. "\n"
  180. "char* lowercase(const char *string)\n"
  181. "{\n"
  182. " char *new_string, *p;\n"
  183. " new_string = (char *)malloc(sizeof(char) * (size_t)(strlen(string) + 1));\n"
  184. " if (!new_string)\n"
  185. " {\n"
  186. " return NULL;\n"
  187. " }\n"
  188. " strcpy(new_string, string);\n"
  189. " p = new_string;\n"
  190. " while (*p != 0)\n"
  191. " {\n"
  192. " *p = (char)tolower(*p);\n"
  193. " ++p;\n"
  194. " }\n"
  195. " return new_string;\n"
  196. "}\n"
  197. "\n"
  198. "int main(int ac, char *av[])\n"
  199. "{\n"
  200. " int i, NumTests, testNum, partial_match;\n"
  201. " char *arg, *test_name;\n"
  202. " \n"
  203. "#if defined(_MSC_VER) && defined(_DEBUG)\n"
  204. " /* Put in hook for debug library. */\n"
  205. " _CrtSetReportHook(TestDriverDebugReport);\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. }