1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmCreateTestSourceList.cxx,v $
6 Date: $Date: 2008-01-23 15:27:59 $
7 Version: $Revision: 1.44 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmCreateTestSourceList.h"
18 #include "cmSourceFile.h"
20 // cmCreateTestSourceList
21 bool cmCreateTestSourceList
22 ::InitialPass(std::vector
<std::string
> const& args
, cmExecutionStatus
&)
26 this->SetError("called with wrong number of arguments.");
31 std::vector
<std::string
>::const_iterator i
= args
.begin();
32 std::string extraInclude
;
34 std::vector
<std::string
> tests
;
35 // extract extra include and function ot
36 for(; i
!= args
.end(); i
++)
38 if(*i
== "EXTRA_INCLUDE")
43 this->SetError("incorrect arguments to EXTRA_INCLUDE");
46 extraInclude
= "#include \"";
48 extraInclude
+= "\"\n";
50 else if(*i
== "FUNCTION")
55 this->SetError("incorrect arguments to FUNCTION");
59 function
+= "(&ac, &av);\n";
68 // Name of the source list
70 const char* sourceList
= i
->c_str();
73 // Name of the test driver
74 // make sure they specified an extension
75 if (cmSystemTools::GetFilenameExtension(*i
).size() < 2)
78 "You must specify a file extenion for the test driver file.");
81 std::string driver
= this->Makefile
->GetCurrentOutputDirectory();
86 std::string configFile
=
87 this->Makefile
->GetRequiredDefinition("CMAKE_ROOT");
89 configFile
+= "/Templates/TestDriver.cxx.in";
90 // Create the test driver file
92 std::vector
<std::string
>::const_iterator testsBegin
= i
;
93 std::vector
<std::string
> tests_func_name
;
95 // The rest of the arguments consist of a list of test source files.
96 // Sadly, they can be in directories. Let's find a unique function
97 // name for the corresponding test, and push it to the tests_func_name
100 // - replace spaces ' ', ':' and '/' with underscores '_'
101 std::string forwardDeclareCode
;
102 for(i
= testsBegin
; i
!= tests
.end(); ++i
)
104 if(*i
== "EXTRA_INCLUDE")
108 std::string func_name
;
109 if (cmSystemTools::GetFilenamePath(*i
).size() > 0)
111 func_name
= cmSystemTools::GetFilenamePath(*i
) + "/" +
112 cmSystemTools::GetFilenameWithoutLastExtension(*i
);
116 func_name
= cmSystemTools::GetFilenameWithoutLastExtension(*i
);
118 cmSystemTools::ConvertToUnixSlashes(func_name
);
119 cmSystemTools::ReplaceString(func_name
, " ", "_");
120 cmSystemTools::ReplaceString(func_name
, "/", "_");
121 cmSystemTools::ReplaceString(func_name
, ":", "_");
122 tests_func_name
.push_back(func_name
);
123 forwardDeclareCode
+= "int ";
124 forwardDeclareCode
+= func_name
;
125 forwardDeclareCode
+= "(int, char*[]);\n";
128 std::string functionMapCode
;
130 std::vector
<std::string
>::iterator j
;
131 for(i
= testsBegin
, j
= tests_func_name
.begin(); i
!= tests
.end(); ++i
, ++j
)
133 std::string func_name
;
134 if (cmSystemTools::GetFilenamePath(*i
).size() > 0)
136 func_name
= cmSystemTools::GetFilenamePath(*i
) + "/" +
137 cmSystemTools::GetFilenameWithoutLastExtension(*i
);
141 func_name
= cmSystemTools::GetFilenameWithoutLastExtension(*i
);
143 functionMapCode
+= " {\n"
145 functionMapCode
+= func_name
;
146 functionMapCode
+= "\",\n"
148 functionMapCode
+= *j
;
149 functionMapCode
+= "\n"
153 if(extraInclude
.size())
155 this->Makefile
->AddDefinition("CMAKE_TESTDRIVER_EXTRA_INCLUDES",
156 extraInclude
.c_str());
160 this->Makefile
->AddDefinition("CMAKE_TESTDRIVER_ARGVC_FUNCTION",
163 this->Makefile
->AddDefinition("CMAKE_FORWARD_DECLARE_TESTS",
164 forwardDeclareCode
.c_str());
165 this->Makefile
->AddDefinition("CMAKE_FUNCTION_TABLE_ENTIRES",
166 functionMapCode
.c_str());
168 if ( !this->Makefile
->ConfigureFile(configFile
.c_str(), driver
.c_str(),
169 false, true, false) )
174 // Construct the source list.
175 std::string sourceListValue
;
177 cmSourceFile
* sf
= this->Makefile
->GetOrCreateSource(driver
.c_str());
178 sf
->SetProperty("ABSTRACT","0");
179 sourceListValue
= args
[1];
181 for(i
= testsBegin
; i
!= tests
.end(); ++i
)
183 cmSourceFile
* sf
= this->Makefile
->GetOrCreateSource(i
->c_str());
184 sf
->SetProperty("ABSTRACT","0");
185 sourceListValue
+= ";";
186 sourceListValue
+= *i
;
189 this->Makefile
->AddDefinition(sourceList
, sourceListValue
.c_str());