cmCreateTestSourceList.cxx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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() < 5)
  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);
  24. std::vector<std::string>::iterator i = args.begin();
  25. const char* sourceList = i->c_str();
  26. ++i;
  27. std::string driver = m_Makefile->GetCurrentOutputDirectory();
  28. driver += "/";
  29. driver += *i;
  30. driver += ".cxx";
  31. ++i;
  32. std::vector<std::string>::iterator testsBegin = i;
  33. std::ofstream fout(driver.c_str());
  34. if(!fout)
  35. {
  36. std::string err = "Could not create file ";
  37. err += driver;
  38. err += " for cmCreateTestSourceList command.";
  39. this->SetError(err.c_str());
  40. return false;
  41. }
  42. // Create the test driver file
  43. fout << "#include <stdio.h>\n";
  44. fout << "#include <string.h>\n";
  45. fout << "// forward declare test functions\n";
  46. for(i = testsBegin; i != args.end(); ++i)
  47. {
  48. fout << "int " << *i << "(int, char**);\n";
  49. }
  50. fout << "// Create map \n";
  51. fout << "typedef int (*MainFuncPointer)(int , char**);\n";
  52. fout << "struct functionMapEntry\n"
  53. << "{\n"
  54. << "const char* name;\n"
  55. << "MainFuncPointer func;\n"
  56. << "};\n\n";
  57. fout << "functionMapEntry cmakeGeneratedFunctionMapEntries[] = {\n";
  58. int numTests = 0;
  59. for(i = testsBegin; i != args.end(); ++i)
  60. {
  61. fout << "{\"" << *i << "\", " << *i << "},\n";
  62. numTests++;
  63. }
  64. fout << "};\n";
  65. fout << "int main(int ac, char** av)\n"
  66. << "{\n";
  67. fout << " int NumTests = " << numTests << ";\n";
  68. fout << " int i;\n";
  69. fout << " if(ac < 2)\n";
  70. fout << " {\n";
  71. fout << " printf(\"Available tests:\\n\");\n";
  72. fout << " for(i =0; i < NumTests; ++i)\n";
  73. fout << " {\n";
  74. fout << " printf(\"%d. %s\\n\", i, cmakeGeneratedFunctionMapEntries[i].name);\n";
  75. fout << " }\n";
  76. fout << " printf(\"To run a test, enter the test number: \");\n";
  77. fout << " int testNum = 0;\n";
  78. fout << " scanf(\"%d\", &testNum);\n";
  79. fout << " if(testNum >= NumTests)\n";
  80. fout << " {\n";
  81. fout << " printf(\"%d is an invalid test number.\\n\", testNum);\n";
  82. fout << " return -1;\n";
  83. fout << " }\n";
  84. fout << " return (*cmakeGeneratedFunctionMapEntries[testNum].func)(ac-1, av+1);\n";
  85. fout << " }\n";
  86. fout << " for(i =0; i < NumTests; ++i)\n";
  87. fout << " {\n";
  88. fout << " if(strcmp(cmakeGeneratedFunctionMapEntries[i].name, av[1]) == 0)\n";
  89. fout << " {\n";
  90. fout << " return (*cmakeGeneratedFunctionMapEntries[i].func)(ac-1, av+1);\n";
  91. fout << " }\n";
  92. fout << " }\n";
  93. fout << " printf(\"Available tests:\\n\");\n";
  94. fout << " for(i =0; i < NumTests; ++i)\n";
  95. fout << " {\n";
  96. fout << " printf(\"%d. %s\\n\", i, cmakeGeneratedFunctionMapEntries[i].name);\n";
  97. fout << " }\n";
  98. fout << " printf(\"Failed: %s is an invalid test name.\\n\", av[1]);\n";
  99. fout << " return -1;\n";
  100. fout << "}\n";
  101. fout.close();
  102. // create the source list
  103. cmSourceFile cfile;
  104. cfile.SetIsAnAbstractClass(false);
  105. cfile.SetName(args[1].c_str(), m_Makefile->GetCurrentOutputDirectory(),
  106. "cxx", false);
  107. m_Makefile->AddSource(cfile, sourceList);
  108. for(i = testsBegin; i != args.end(); ++i)
  109. {
  110. cmSourceFile cfile;
  111. cfile.SetIsAnAbstractClass(false);
  112. cfile.SetName(i->c_str(), m_Makefile->GetCurrentDirectory(),
  113. "cxx", false);
  114. m_Makefile->AddSource(cfile, sourceList);
  115. }
  116. return true;
  117. }