cmCreateTestSourceList.cxx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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::string configFile =
  74. m_Makefile->GetDefinition("CMAKE_ROOT");
  75. configFile += "/Templates/TestDriver.cxx.in";
  76. // Create the test driver file
  77. std::vector<std::string>::const_iterator testsBegin = i;
  78. std::vector<std::string> tests_func_name;
  79. // The rest of the arguments consist of a list of test source files.
  80. // Sadly, they can be in directories. Let's find a unique function
  81. // name for the corresponding test, and push it to the tests_func_name
  82. // list.
  83. // For the moment:
  84. // - replace spaces ' ', ':' and '/' with underscores '_'
  85. std::string forwardDeclareCode;
  86. for(i = testsBegin; i != tests.end(); ++i)
  87. {
  88. if(*i == "EXTRA_INCLUDE")
  89. {
  90. break;
  91. }
  92. std::string func_name;
  93. if (cmSystemTools::GetFilenamePath(*i).size() > 0)
  94. {
  95. func_name = cmSystemTools::GetFilenamePath(*i) + "/" +
  96. cmSystemTools::GetFilenameWithoutLastExtension(*i);
  97. }
  98. else
  99. {
  100. func_name = cmSystemTools::GetFilenameWithoutLastExtension(*i);
  101. }
  102. cmSystemTools::ConvertToUnixSlashes(func_name);
  103. cmSystemTools::ReplaceString(func_name, " ", "_");
  104. cmSystemTools::ReplaceString(func_name, "/", "_");
  105. cmSystemTools::ReplaceString(func_name, ":", "_");
  106. tests_func_name.push_back(func_name);
  107. forwardDeclareCode += "int ";
  108. forwardDeclareCode += func_name;
  109. forwardDeclareCode += "(int, char*[]);\n";
  110. }
  111. std::string functionMapCode;
  112. int numTests = 0;
  113. std::vector<std::string>::iterator j;
  114. for(i = testsBegin, j = tests_func_name.begin(); i != tests.end(); ++i, ++j)
  115. {
  116. std::string func_name;
  117. if (cmSystemTools::GetFilenamePath(*i).size() > 0)
  118. {
  119. func_name = cmSystemTools::GetFilenamePath(*i) + "/" +
  120. cmSystemTools::GetFilenameWithoutLastExtension(*i);
  121. }
  122. else
  123. {
  124. func_name = cmSystemTools::GetFilenameWithoutLastExtension(*i);
  125. }
  126. functionMapCode += " {\n"
  127. " \"";
  128. functionMapCode += func_name;
  129. functionMapCode += "\",\n"
  130. " ";
  131. functionMapCode += *j;
  132. functionMapCode += "\n"
  133. " },\n";
  134. numTests++;
  135. }
  136. if(extraInclude.size())
  137. {
  138. m_Makefile->AddDefinition("CMAKE_TESTDRIVER_EXTRA_INCLUDES", extraInclude.c_str());
  139. }
  140. if(function.size())
  141. {
  142. m_Makefile->AddDefinition("CMAKE_TESTDRIVER_ARGVC_FUNCTION", function.c_str());
  143. }
  144. m_Makefile->AddDefinition("CMAKE_FORWARD_DECLARE_TESTS", forwardDeclareCode.c_str());
  145. m_Makefile->AddDefinition("CMAKE_FUNCTION_TABLE_ENTIRES", functionMapCode.c_str());
  146. bool res = true;
  147. if ( !m_Makefile->ConfigureFile(configFile.c_str(), driver.c_str(), false, true, false) )
  148. {
  149. res = false;
  150. }
  151. // Create the source list
  152. cmSourceFile cfile;
  153. std::string sourceListValue;
  154. cfile.SetProperty("ABSTRACT","0");
  155. cfile.SetName(cmSystemTools::GetFilenameWithoutExtension(args[1]).c_str(),
  156. m_Makefile->GetCurrentOutputDirectory(),
  157. cmSystemTools::GetFilenameExtension(args[1]).c_str()+1,
  158. false);
  159. m_Makefile->AddSource(cfile);
  160. sourceListValue = args[1];
  161. for(i = testsBegin; i != tests.end(); ++i)
  162. {
  163. cmSourceFile icfile;
  164. icfile.SetProperty("ABSTRACT","0");
  165. icfile.SetName(i->c_str(),
  166. m_Makefile->GetCurrentDirectory(),
  167. m_Makefile->GetSourceExtensions(),
  168. m_Makefile->GetHeaderExtensions());
  169. m_Makefile->AddSource(icfile);
  170. sourceListValue += ";";
  171. sourceListValue += *i;
  172. }
  173. m_Makefile->AddDefinition(sourceList, sourceListValue.c_str());
  174. return res;
  175. }