cmTestGenerator.cxx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmTestGenerator.h"
  11. #include "cmGeneratorExpression.h"
  12. #include "cmOutputConverter.h"
  13. #include "cmMakefile.h"
  14. #include "cmLocalGenerator.h"
  15. #include "cmSystemTools.h"
  16. #include "cmTarget.h"
  17. #include "cmTest.h"
  18. //----------------------------------------------------------------------------
  19. cmTestGenerator
  20. ::cmTestGenerator(cmTest* test,
  21. std::vector<std::string> const& configurations):
  22. cmScriptGenerator("CTEST_CONFIGURATION_TYPE", configurations),
  23. Test(test)
  24. {
  25. this->ActionsPerConfig = !test->GetOldStyle();
  26. this->TestGenerated = false;
  27. this->LG = 0;
  28. }
  29. //----------------------------------------------------------------------------
  30. cmTestGenerator
  31. ::~cmTestGenerator()
  32. {
  33. }
  34. void cmTestGenerator::Compute(cmLocalGenerator* lg)
  35. {
  36. this->LG = lg;
  37. }
  38. //----------------------------------------------------------------------------
  39. void cmTestGenerator::GenerateScriptConfigs(std::ostream& os,
  40. Indent const& indent)
  41. {
  42. // Create the tests.
  43. this->cmScriptGenerator::GenerateScriptConfigs(os, indent);
  44. }
  45. //----------------------------------------------------------------------------
  46. void cmTestGenerator::GenerateScriptActions(std::ostream& os,
  47. Indent const& indent)
  48. {
  49. if(this->ActionsPerConfig)
  50. {
  51. // This is the per-config generation in a single-configuration
  52. // build generator case. The superclass will call our per-config
  53. // method.
  54. this->cmScriptGenerator::GenerateScriptActions(os, indent);
  55. }
  56. else
  57. {
  58. // This is an old-style test, so there is only one config.
  59. //assert(this->Test->GetOldStyle());
  60. this->GenerateOldStyle(os, indent);
  61. }
  62. }
  63. //----------------------------------------------------------------------------
  64. void cmTestGenerator::GenerateScriptForConfig(std::ostream& os,
  65. const std::string& config,
  66. Indent const& indent)
  67. {
  68. this->TestGenerated = true;
  69. // Set up generator expression evaluation context.
  70. cmGeneratorExpression ge(this->Test->GetBacktrace());
  71. // Start the test command.
  72. os << indent << "add_test(" << this->Test->GetName() << " ";
  73. // Get the test command line to be executed.
  74. std::vector<std::string> const& command = this->Test->GetCommand();
  75. // Check whether the command executable is a target whose name is to
  76. // be translated.
  77. std::string exe = command[0];
  78. cmGeneratorTarget* target =
  79. this->LG->FindGeneratorTargetToUse(exe);
  80. if(target && target->GetType() == cmState::EXECUTABLE)
  81. {
  82. // Use the target file on disk.
  83. exe = target->GetFullPath(config);
  84. // Prepend with the emulator when cross compiling if required.
  85. const char * emulator =
  86. target->GetProperty("CROSSCOMPILING_EMULATOR");
  87. if (emulator != 0)
  88. {
  89. std::vector<std::string> emulatorWithArgs;
  90. cmSystemTools::ExpandListArgument(emulator, emulatorWithArgs);
  91. std::string emulatorExe(emulatorWithArgs[0]);
  92. cmSystemTools::ConvertToUnixSlashes(emulatorExe);
  93. os << cmOutputConverter::EscapeForCMake(emulatorExe) << " ";
  94. for(std::vector<std::string>::const_iterator ei =
  95. emulatorWithArgs.begin()+1;
  96. ei != emulatorWithArgs.end();
  97. ++ei)
  98. {
  99. os << cmOutputConverter::EscapeForCMake(*ei) << " ";
  100. }
  101. }
  102. }
  103. else
  104. {
  105. // Use the command name given.
  106. exe = ge.Parse(exe.c_str())->Evaluate(this->LG, config);
  107. cmSystemTools::ConvertToUnixSlashes(exe);
  108. }
  109. // Generate the command line with full escapes.
  110. os << cmOutputConverter::EscapeForCMake(exe);
  111. for(std::vector<std::string>::const_iterator ci = command.begin()+1;
  112. ci != command.end(); ++ci)
  113. {
  114. os << " " << cmOutputConverter::EscapeForCMake(
  115. ge.Parse(*ci)->Evaluate(
  116. this->LG, config));
  117. }
  118. // Finish the test command.
  119. os << ")\n";
  120. // Output properties for the test.
  121. cmPropertyMap& pm = this->Test->GetProperties();
  122. if(!pm.empty())
  123. {
  124. os << indent << "set_tests_properties(" << this->Test->GetName()
  125. << " PROPERTIES ";
  126. for(cmPropertyMap::const_iterator i = pm.begin();
  127. i != pm.end(); ++i)
  128. {
  129. os << " " << i->first
  130. << " " << cmOutputConverter::EscapeForCMake(
  131. ge.Parse(i->second.GetValue())->Evaluate(this->LG,
  132. config));
  133. }
  134. os << ")" << std::endl;
  135. }
  136. }
  137. //----------------------------------------------------------------------------
  138. void cmTestGenerator::GenerateScriptNoConfig(std::ostream& os,
  139. Indent const& indent)
  140. {
  141. os << indent << "add_test(" << this->Test->GetName() << " NOT_AVAILABLE)\n";
  142. }
  143. //----------------------------------------------------------------------------
  144. bool cmTestGenerator::NeedsScriptNoConfig() const
  145. {
  146. return (this->TestGenerated && // test generated for at least one config
  147. this->ActionsPerConfig && // test is config-aware
  148. this->Configurations.empty() && // test runs in all configs
  149. !this->ConfigurationTypes->empty()); // config-dependent command
  150. }
  151. //----------------------------------------------------------------------------
  152. void cmTestGenerator::GenerateOldStyle(std::ostream& fout,
  153. Indent const& indent)
  154. {
  155. this->TestGenerated = true;
  156. // Get the test command line to be executed.
  157. std::vector<std::string> const& command = this->Test->GetCommand();
  158. std::string exe = command[0];
  159. cmSystemTools::ConvertToUnixSlashes(exe);
  160. fout << indent;
  161. fout << "add_test(";
  162. fout << this->Test->GetName() << " \"" << exe << "\"";
  163. for(std::vector<std::string>::const_iterator argit = command.begin()+1;
  164. argit != command.end(); ++argit)
  165. {
  166. // Just double-quote all arguments so they are re-parsed
  167. // correctly by the test system.
  168. fout << " \"";
  169. for(std::string::const_iterator c = argit->begin();
  170. c != argit->end(); ++c)
  171. {
  172. // Escape quotes within arguments. We should escape
  173. // backslashes too but we cannot because it makes the result
  174. // inconsistent with previous behavior of this command.
  175. if((*c == '"'))
  176. {
  177. fout << '\\';
  178. }
  179. fout << *c;
  180. }
  181. fout << "\"";
  182. }
  183. fout << ")" << std::endl;
  184. // Output properties for the test.
  185. cmPropertyMap& pm = this->Test->GetProperties();
  186. if(!pm.empty())
  187. {
  188. fout << indent << "set_tests_properties(" << this->Test->GetName()
  189. << " PROPERTIES ";
  190. for(cmPropertyMap::const_iterator i = pm.begin();
  191. i != pm.end(); ++i)
  192. {
  193. fout << " " << i->first
  194. << " " << cmOutputConverter::EscapeForCMake(i->second.GetValue());
  195. }
  196. fout << ")" << std::endl;
  197. }
  198. }