cmCreateTestSourceList.cxx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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, true);
  24. std::vector<std::string>::iterator i = args.begin();
  25. // Name of the source list
  26. const char* sourceList = i->c_str();
  27. ++i;
  28. // Name of the test driver
  29. std::string driver = m_Makefile->GetCurrentOutputDirectory();
  30. driver += "/";
  31. driver += *i;
  32. driver += ".cxx";
  33. ++i;
  34. std::ofstream fout(driver.c_str());
  35. if(!fout)
  36. {
  37. std::string err = "Could not create file ";
  38. err += driver;
  39. err += " for cmCreateTestSourceList command.";
  40. this->SetError(err.c_str());
  41. return false;
  42. }
  43. // Create the test driver file
  44. fout << "#include <stdio.h>\n";
  45. fout << "#include <string.h>\n";
  46. fout << "// forward declare test functions\n";
  47. std::vector<std::string>::iterator testsBegin = i;
  48. std::vector<std::string> tests_filename;
  49. // The rest of the arguments consist of a list of test source files.
  50. // Sadly, they can be in directories. Let's modify each arg to get
  51. // a unique function name for the corresponding test, and push the
  52. // real source filename to the tests_filename var (used at the end).
  53. // For the moment:
  54. // - replace spaces ' ', ':' and '/' with underscores '_'
  55. for(i = testsBegin; i != args.end(); ++i)
  56. {
  57. tests_filename.push_back(*i);
  58. cmSystemTools::ConvertToUnixSlashes(*i);
  59. cmSystemTools::ReplaceString(*i, " ", "_");
  60. cmSystemTools::ReplaceString(*i, "/", "_");
  61. cmSystemTools::ReplaceString(*i, ":", "_");
  62. fout << "int " << *i << "(int, char**);\n";
  63. }
  64. fout << "// Create map \n";
  65. fout << "typedef int (*MainFuncPointer)(int , char**);\n";
  66. fout << "struct functionMapEntry\n"
  67. << "{\n"
  68. << "const char* name;\n"
  69. << "MainFuncPointer func;\n"
  70. << "};\n\n";
  71. fout << "functionMapEntry cmakeGeneratedFunctionMapEntries[] = {\n";
  72. int numTests = 0;
  73. for(i = testsBegin; i != args.end(); ++i)
  74. {
  75. fout << "{\"" << *i << "\", " << *i << "},\n";
  76. numTests++;
  77. }
  78. fout << "};\n";
  79. fout << "int main(int ac, char** av)\n"
  80. << "{\n";
  81. fout << " int NumTests = " << numTests << ";\n";
  82. fout << " int i;\n";
  83. fout << " if(ac < 2)\n";
  84. fout << " {\n";
  85. fout << " // if there is only one test, then run it with the arguments\n";
  86. fout << " if(NumTests == 1)\n";
  87. fout << " { return (*cmakeGeneratedFunctionMapEntries[0].func)(ac, av); }\n";
  88. fout << " printf(\"Available tests:\\n\");\n";
  89. fout << " for(i =0; i < NumTests; ++i)\n";
  90. fout << " {\n";
  91. fout << " printf(\"%d. %s\\n\", i, cmakeGeneratedFunctionMapEntries[i].name);\n";
  92. fout << " }\n";
  93. fout << " printf(\"To run a test, enter the test number: \");\n";
  94. fout << " int testNum = 0;\n";
  95. fout << " scanf(\"%d\", &testNum);\n";
  96. fout << " if(testNum >= NumTests)\n";
  97. fout << " {\n";
  98. fout << " printf(\"%d is an invalid test number.\\n\", testNum);\n";
  99. fout << " return -1;\n";
  100. fout << " }\n";
  101. fout << " return (*cmakeGeneratedFunctionMapEntries[testNum].func)(ac-1, av+1);\n";
  102. fout << " }\n";
  103. fout << " for(i =0; i < NumTests; ++i)\n";
  104. fout << " {\n";
  105. fout << " if(strcmp(cmakeGeneratedFunctionMapEntries[i].name, av[1]) == 0)\n";
  106. fout << " {\n";
  107. fout << " return (*cmakeGeneratedFunctionMapEntries[i].func)(ac-1, av+1);\n";
  108. fout << " }\n";
  109. fout << " }\n";
  110. fout << " // if there is only one test, then run it with the arguments\n";
  111. fout << " if(NumTests == 1)\n";
  112. fout << " { return (*cmakeGeneratedFunctionMapEntries[0].func)(ac, av); }\n";
  113. fout << " printf(\"Available tests:\\n\");\n";
  114. fout << " for(i =0; i < NumTests; ++i)\n";
  115. fout << " {\n";
  116. fout << " printf(\"%d. %s\\n\", i, cmakeGeneratedFunctionMapEntries[i].name);\n";
  117. fout << " }\n";
  118. fout << " printf(\"Failed: %s is an invalid test name.\\n\", av[1]);\n";
  119. fout << " return -1;\n";
  120. fout << "}\n";
  121. fout.close();
  122. // Create the source list
  123. cmSourceFile cfile;
  124. cfile.SetIsAnAbstractClass(false);
  125. cfile.SetName(args[1].c_str(),
  126. m_Makefile->GetCurrentOutputDirectory(),
  127. "cxx",
  128. false);
  129. m_Makefile->AddSource(cfile, sourceList);
  130. for(i = tests_filename.begin(); i != tests_filename.end(); ++i)
  131. {
  132. cmSourceFile cfile;
  133. cfile.SetIsAnAbstractClass(false);
  134. cfile.SetName(i->c_str(),
  135. m_Makefile->GetCurrentDirectory(),
  136. "cxx",
  137. false);
  138. m_Makefile->AddSource(cfile, sourceList);
  139. }
  140. return true;
  141. }