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 "cmCreateTestSourceList.h"
13 #include "cmSourceFile.h"
15 // cmCreateTestSourceList
16 bool cmCreateTestSourceList
17 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
21 this->SetError("called with wrong number of arguments.");
26 std::vector<std::string>::const_iterator i = args.begin();
27 std::string extraInclude;
29 std::vector<std::string> tests;
30 // extract extra include and function ot
31 for(; i != args.end(); i++)
33 if(*i == "EXTRA_INCLUDE")
38 this->SetError("incorrect arguments to EXTRA_INCLUDE");
41 extraInclude = "#include \"";
43 extraInclude += "\"\n";
45 else if(*i == "FUNCTION")
50 this->SetError("incorrect arguments to FUNCTION");
54 function += "(&ac, &av);\n";
63 // Name of the source list
65 const char* sourceList = i->c_str();
68 // Name of the test driver
69 // make sure they specified an extension
70 if (cmSystemTools::GetFilenameExtension(*i).size() < 2)
73 "You must specify a file extension for the test driver file.");
76 std::string driver = this->Makefile->GetCurrentOutputDirectory();
81 std::string configFile =
82 this->Makefile->GetRequiredDefinition("CMAKE_ROOT");
84 configFile += "/Templates/TestDriver.cxx.in";
85 // Create the test driver file
87 std::vector<std::string>::const_iterator testsBegin = i;
88 std::vector<std::string> tests_func_name;
90 // The rest of the arguments consist of a list of test source files.
91 // Sadly, they can be in directories. Let's find a unique function
92 // name for the corresponding test, and push it to the tests_func_name
95 // - replace spaces ' ', ':' and '/' with underscores '_'
96 std::string forwardDeclareCode;
97 for(i = testsBegin; i != tests.end(); ++i)
99 if(*i == "EXTRA_INCLUDE")
103 std::string func_name;
104 if (cmSystemTools::GetFilenamePath(*i).size() > 0)
106 func_name = cmSystemTools::GetFilenamePath(*i) + "/" +
107 cmSystemTools::GetFilenameWithoutLastExtension(*i);
111 func_name = cmSystemTools::GetFilenameWithoutLastExtension(*i);
113 cmSystemTools::ConvertToUnixSlashes(func_name);
114 cmSystemTools::ReplaceString(func_name, " ", "_");
115 cmSystemTools::ReplaceString(func_name, "/", "_");
116 cmSystemTools::ReplaceString(func_name, ":", "_");
117 tests_func_name.push_back(func_name);
118 forwardDeclareCode += "int ";
119 forwardDeclareCode += func_name;
120 forwardDeclareCode += "(int, char*[]);\n";
123 std::string functionMapCode;
125 std::vector<std::string>::iterator j;
126 for(i = testsBegin, j = tests_func_name.begin(); i != tests.end(); ++i, ++j)
128 std::string func_name;
129 if (cmSystemTools::GetFilenamePath(*i).size() > 0)
131 func_name = cmSystemTools::GetFilenamePath(*i) + "/" +
132 cmSystemTools::GetFilenameWithoutLastExtension(*i);
136 func_name = cmSystemTools::GetFilenameWithoutLastExtension(*i);
138 functionMapCode += " {\n"
140 functionMapCode += func_name;
141 functionMapCode += "\",\n"
143 functionMapCode += *j;
144 functionMapCode += "\n"
148 if(extraInclude.size())
150 this->Makefile->AddDefinition("CMAKE_TESTDRIVER_EXTRA_INCLUDES",
151 extraInclude.c_str());
155 this->Makefile->AddDefinition("CMAKE_TESTDRIVER_ARGVC_FUNCTION",
158 this->Makefile->AddDefinition("CMAKE_FORWARD_DECLARE_TESTS",
159 forwardDeclareCode.c_str());
160 this->Makefile->AddDefinition("CMAKE_FUNCTION_TABLE_ENTIRES",
161 functionMapCode.c_str());
163 if ( !this->Makefile->ConfigureFile(configFile.c_str(), driver.c_str(),
164 false, true, false) )
169 // Construct the source list.
170 std::string sourceListValue;
172 cmSourceFile* sf = this->Makefile->GetOrCreateSource(driver.c_str());
173 sf->SetProperty("ABSTRACT","0");
174 sourceListValue = args[1];
176 for(i = testsBegin; i != tests.end(); ++i)
178 cmSourceFile* sf = this->Makefile->GetOrCreateSource(i->c_str());
179 sf->SetProperty("ABSTRACT","0");
180 sourceListValue += ";";
181 sourceListValue += *i;
184 this->Makefile->AddDefinition(sourceList, sourceListValue.c_str());