cmCreateTestSourceList.cxx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 = "#include \"";
  39. extraInclude += *i;
  40. extraInclude += "\"\n";
  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. function += "(&ac, &av);\n";
  52. }
  53. else
  54. {
  55. tests.push_back(*i);
  56. }
  57. }
  58. i = tests.begin();
  59. // Name of the source list
  60. const char* sourceList = i->c_str();
  61. ++i;
  62. // Name of the test driver
  63. // make sure they specified an extension
  64. if (cmSystemTools::GetFilenameExtension(*i).size() < 2)
  65. {
  66. this->SetError("You must specify a file extenion for the test driver file.");
  67. return false;
  68. }
  69. std::string driver = m_Makefile->GetCurrentOutputDirectory();
  70. driver += "/";
  71. driver += *i;
  72. ++i;
  73. std::ofstream fout(driver.c_str());
  74. if (!fout)
  75. {
  76. std::string err = "Could not create file ";
  77. err += driver;
  78. err += " for cmCreateTestSourceList command.";
  79. this->SetError(err.c_str());
  80. return false;
  81. }
  82. std::string configFile =
  83. m_Makefile->GetDefinition("CMAKE_ROOT");
  84. configFile += "/Templates/TestDriver.cxx.in";
  85. // Create the test driver file
  86. std::vector<std::string>::const_iterator testsBegin = i;
  87. std::vector<std::string> tests_func_name;
  88. // The rest of the arguments consist of a list of test source files.
  89. // Sadly, they can be in directories. Let's find a unique function
  90. // name for the corresponding test, and push it to the tests_func_name
  91. // list.
  92. // For the moment:
  93. // - replace spaces ' ', ':' and '/' with underscores '_'
  94. std::string forwardDeclareCode;
  95. for(i = testsBegin; i != tests.end(); ++i)
  96. {
  97. if(*i == "EXTRA_INCLUDE")
  98. {
  99. break;
  100. }
  101. std::string func_name;
  102. if (cmSystemTools::GetFilenamePath(*i).size() > 0)
  103. {
  104. func_name = cmSystemTools::GetFilenamePath(*i) + "/" +
  105. cmSystemTools::GetFilenameWithoutLastExtension(*i);
  106. }
  107. else
  108. {
  109. func_name = cmSystemTools::GetFilenameWithoutLastExtension(*i);
  110. }
  111. cmSystemTools::ConvertToUnixSlashes(func_name);
  112. cmSystemTools::ReplaceString(func_name, " ", "_");
  113. cmSystemTools::ReplaceString(func_name, "/", "_");
  114. cmSystemTools::ReplaceString(func_name, ":", "_");
  115. tests_func_name.push_back(func_name);
  116. forwardDeclareCode += "int ";
  117. forwardDeclareCode += func_name;
  118. forwardDeclareCode += "(int, char*[]);\n";
  119. }
  120. std::string functionMapCode;
  121. int numTests = 0;
  122. std::vector<std::string>::iterator j;
  123. for(i = testsBegin, j = tests_func_name.begin(); i != tests.end(); ++i, ++j)
  124. {
  125. std::string func_name;
  126. if (cmSystemTools::GetFilenamePath(*i).size() > 0)
  127. {
  128. func_name = cmSystemTools::GetFilenamePath(*i) + "/" +
  129. cmSystemTools::GetFilenameWithoutLastExtension(*i);
  130. }
  131. else
  132. {
  133. func_name = cmSystemTools::GetFilenameWithoutLastExtension(*i);
  134. }
  135. functionMapCode += " {\n"
  136. " \"";
  137. functionMapCode += func_name;
  138. functionMapCode += "\",\n"
  139. " ";
  140. functionMapCode += *j;
  141. functionMapCode += "\n"
  142. " },\n";
  143. numTests++;
  144. }
  145. if(extraInclude.size())
  146. {
  147. m_Makefile->AddDefinition("CMAKE_TESTDRIVER_EXTRA_INCLUDES", extraInclude.c_str());
  148. }
  149. if(function.size())
  150. {
  151. m_Makefile->AddDefinition("CMAKE_TESTDRIVER_ARGVC_FUNCTION", function.c_str());
  152. }
  153. m_Makefile->AddDefinition("CMAKE_FORWARD_DECLARE_TESTS", forwardDeclareCode.c_str());
  154. m_Makefile->AddDefinition("CMAKE_FUNCTION_TABLE_ENTIRES", functionMapCode.c_str());
  155. m_Makefile->ConfigureFile(configFile.c_str(), driver.c_str(), false, true, false);
  156. // Create the source list
  157. cmSourceFile cfile;
  158. std::string sourceListValue;
  159. cfile.SetProperty("ABSTRACT","0");
  160. cfile.SetName(cmSystemTools::GetFilenameWithoutExtension(args[1]).c_str(),
  161. m_Makefile->GetCurrentOutputDirectory(),
  162. cmSystemTools::GetFilenameExtension(args[1]).c_str()+1,
  163. false);
  164. m_Makefile->AddSource(cfile);
  165. sourceListValue = args[1];
  166. for(i = testsBegin; i != tests.end(); ++i)
  167. {
  168. cmSourceFile icfile;
  169. icfile.SetProperty("ABSTRACT","0");
  170. icfile.SetName(i->c_str(),
  171. m_Makefile->GetCurrentDirectory(),
  172. m_Makefile->GetSourceExtensions(),
  173. m_Makefile->GetHeaderExtensions());
  174. m_Makefile->AddSource(icfile);
  175. sourceListValue += ";";
  176. sourceListValue += *i;
  177. }
  178. m_Makefile->AddDefinition(sourceList, sourceListValue.c_str());
  179. return true;
  180. }