1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License for more information.
11 ============================================================================*/
12 #include "cmTestGenerator.h"
14 #include "cmGeneratorExpression.h"
15 #include "cmLocalGenerator.h"
16 #include "cmMakefile.h"
17 #include "cmSystemTools.h"
21 //----------------------------------------------------------------------------
23 ::cmTestGenerator(cmTest* test,
24 std::vector<std::string> const& configurations):
25 cmScriptGenerator("CTEST_CONFIGURATION_TYPE", configurations),
28 this->ActionsPerConfig = !test->GetOldStyle();
29 this->TestGenerated = false;
32 //----------------------------------------------------------------------------
38 //----------------------------------------------------------------------------
39 void cmTestGenerator::GenerateScriptConfigs(std::ostream& os,
42 // First create the tests.
43 this->cmScriptGenerator::GenerateScriptConfigs(os, indent);
45 // Now generate the test properties.
46 if(this->TestGenerated)
48 cmTest* test = this->Test;
49 cmMakefile* mf = test->GetMakefile();
50 cmLocalGenerator* lg = mf->GetLocalGenerator();
51 std::ostream& fout = os;
52 cmPropertyMap::const_iterator pit;
53 cmPropertyMap* mpit = &test->GetProperties();
56 fout << "SET_TESTS_PROPERTIES(" << test->GetName() << " PROPERTIES ";
57 for ( pit = mpit->begin(); pit != mpit->end(); ++ pit )
59 fout << " " << pit->first
60 << " " << lg->EscapeForCMake(pit->second.GetValue());
62 fout << ")" << std::endl;
67 //----------------------------------------------------------------------------
68 void cmTestGenerator::GenerateScriptActions(std::ostream& os,
71 if(this->ActionsPerConfig)
73 // This is the per-config generation in a single-configuration
74 // build generator case. The superclass will call our per-config
76 this->cmScriptGenerator::GenerateScriptActions(os, indent);
80 // This is an old-style test, so there is only one config.
81 //assert(this->Test->GetOldStyle());
82 this->GenerateOldStyle(os, indent);
86 //----------------------------------------------------------------------------
87 void cmTestGenerator::GenerateScriptForConfig(std::ostream& os,
91 this->TestGenerated = true;
93 // Set up generator expression evaluation context.
94 cmMakefile* mf = this->Test->GetMakefile();
95 cmGeneratorExpression ge(mf, config, this->Test->GetBacktrace());
97 // Start the test command.
98 os << indent << "ADD_TEST(" << this->Test->GetName() << " ";
100 // Get the test command line to be executed.
101 std::vector<std::string> const& command = this->Test->GetCommand();
103 // Check whether the command executable is a target whose name is to
105 std::string exe = command[0];
106 cmTarget* target = mf->FindTargetToUse(exe.c_str());
107 if(target && target->GetType() == cmTarget::EXECUTABLE)
109 // Use the target file on disk.
110 exe = target->GetFullPath(config);
114 // Use the command name given.
115 exe = ge.Process(exe.c_str());
116 cmSystemTools::ConvertToUnixSlashes(exe);
119 // Generate the command line with full escapes.
120 cmLocalGenerator* lg = mf->GetLocalGenerator();
121 os << lg->EscapeForCMake(exe.c_str());
122 for(std::vector<std::string>::const_iterator ci = command.begin()+1;
123 ci != command.end(); ++ci)
125 os << " " << lg->EscapeForCMake(ge.Process(*ci));
128 // Finish the test command.
132 //----------------------------------------------------------------------------
133 void cmTestGenerator::GenerateScriptNoConfig(std::ostream& os,
134 Indent const& indent)
136 os << indent << "ADD_TEST(" << this->Test->GetName() << " NOT_AVAILABLE)\n";
139 //----------------------------------------------------------------------------
140 bool cmTestGenerator::NeedsScriptNoConfig() const
142 return (this->TestGenerated && // test generated for at least one config
143 this->ActionsPerConfig && // test is config-aware
144 this->Configurations.empty() && // test runs in all configs
145 !this->ConfigurationTypes->empty()); // config-dependent command
148 //----------------------------------------------------------------------------
149 void cmTestGenerator::GenerateOldStyle(std::ostream& fout,
150 Indent const& indent)
152 this->TestGenerated = true;
154 // Get the test command line to be executed.
155 std::vector<std::string> const& command = this->Test->GetCommand();
157 std::string exe = command[0];
158 cmSystemTools::ConvertToUnixSlashes(exe);
161 fout << this->Test->GetName() << " \"" << exe << "\"";
163 for(std::vector<std::string>::const_iterator argit = command.begin()+1;
164 argit != command.end(); ++argit)
166 // Just double-quote all arguments so they are re-parsed
167 // correctly by the test system.
169 for(std::string::const_iterator c = argit->begin();
170 c != argit->end(); ++c)
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.
183 fout << ")" << std::endl;