You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
5.4 KiB

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