new arch
[cmake.git] / Source / cmaketest.cxx
blob6d650aff987f8ed0259571b6f5eed7b18e3da149
1 /*=========================================================================
3 Program: Insight Segmentation & Registration Toolkit
4 Module: $RCSfile: cmaketest.cxx,v $
5 Language: C++
6 Date: $Date: 2002-09-06 17:01:46 $
7 Version: $Revision: 1.41 $
9 Copyright (c) 2002 Insight Consortium. All rights reserved.
10 See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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 "cmaketest.h"
18 #include "cmSystemTools.h"
19 #include "cmRegularExpression.h"
20 #include "cmake.h"
21 #include "cmListFileCache.h"
22 #include "cmMakefileGenerator.h"
23 #include "cmCacheManager.h"
24 #if defined(_WIN32) && !defined(__CYGWIN__)
25 #include "windows.h"
26 #endif
29 // this is a test driver program for cmake.
30 int main (int argc, char **argv)
32 if (argc < 4)
34 std::cerr << "Usage: " << argv[0] << " test-src-dir test-bin-dir test-executable\n";
35 return 1;
38 // does the directory exist ?
39 if (!cmSystemTools::FileIsDirectory(argv[2]))
41 cmSystemTools::MakeDirectory(argv[2]);
44 const char* sourceDirectory = argv[1];
45 const char* binaryDirectory = argv[2];
46 const char* executableName = argv[3];
47 const char* executableDirectory = "";
49 if(argc > 4)
51 executableDirectory = argv[4];
54 const char* projectName = executableName;
55 if(argc > 5)
57 projectName = argv[5];
60 // WARNING: the rest of the args is passed to cmake
62 /**
63 * Run an executable command and put the stdout in output.
65 std::string output;
67 // change to the tests directory and run cmake
68 // use the cmake object instead of calling cmake
69 std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
70 std::cout << "Changing into directory: " << binaryDirectory << "\n";
71 cmSystemTools::ChangeDirectory(binaryDirectory);
73 std::vector<std::string> args;
74 std::string intdir = ".";
75 #ifdef CMAKE_INTDIR
76 intdir = CMAKE_INTDIR;
77 #endif
79 // make sure the same generator is used
80 // use this program as the cmake to be run, it should not
81 // be run that way but the cmake object requires a vailid path
82 std::string cmakeCommand = CMAKE_BINARY_DIR;
83 cmakeCommand += "/Source";
84 cmakeCommand += "/";
85 cmakeCommand += intdir;
86 cmakeCommand += "/cmake";
87 cmakeCommand += cmSystemTools::GetExecutableExtension();
89 std::cout << "*** " << cmakeCommand << "\n";
90 args.push_back(cmakeCommand.c_str());
91 args.push_back(sourceDirectory);
92 std::string generator = "-G";
93 generator += CMAKE_GENERATOR;
94 args.push_back(generator);
96 std::vector<std::string> progArgs;
97 if(argc > 6)
99 if(strcmp(argv[6] , "CMAKE_ARGS") == 0)
101 for (int j = 7; j < argc ; j++)
103 args.push_back(argv[j]);
106 else
108 for(int j = 6; j < argc; j++)
110 progArgs.push_back(argv[j]);
115 std::cout << "Generating build files...\n";
117 cmake cm;
118 if (cm.Run(args) != 0)
120 std::cerr << "Error: cmake execution failed\n";
121 // return to the original directory
122 cmSystemTools::ChangeDirectory(cwd.c_str());
123 return 1;
125 std::cout << "Done Generating build files.\n";
127 cmake cm2;
128 std::cout << "Generating build files (again)...\n";
129 if (cm2.Run(args) != 0)
131 std::cerr << "Error: cmake execution failed\n";
132 // return to the original directory
133 cmSystemTools::ChangeDirectory(cwd.c_str());
134 return 1;
136 std::cout << "Done Generating build files (again).\n";
138 cmListFileCache::GetInstance()->ClearCache();
140 // now build the test
141 std::string makeCommand = MAKEPROGRAM;
142 if(makeCommand.size() == 0)
144 std::cerr << "Error: cmaketest does not have a valid MAKEPROGRAM\n";
146 makeCommand = cmSystemTools::ConvertToOutputPath(makeCommand.c_str());
147 std::string lowerCaseCommand = makeCommand;
148 cmSystemTools::LowerCase(lowerCaseCommand);
149 std::string dartMakeCommand = DART_MAKECOMMAND;
150 // if msdev is the make program then do the following
151 // MSDEV 6.0
152 if(lowerCaseCommand.find("msdev") != std::string::npos)
154 // if there are spaces in the makeCommand, assume a full path
155 // and convert it to a path with no spaces in it as the
156 // RunCommand does not like spaces
157 #if defined(_WIN32) && !defined(__CYGWIN__)
158 if(makeCommand.find(' ') != std::string::npos)
160 cmSystemTools::GetShortPath(makeCommand.c_str(), makeCommand);
162 #endif
163 makeCommand += " ";
164 makeCommand += projectName;
165 makeCommand += ".dsw /MAKE \"ALL_BUILD - ";
166 makeCommand += intdir + "\" /REBUILD";
168 // MSDEV 7.0 .NET
169 else if (lowerCaseCommand.find("devenv") != std::string::npos)
171 #if defined(_WIN32) && !defined(__CYGWIN__)
172 if(makeCommand.find(' ') != std::string::npos)
174 cmSystemTools::GetShortPath(makeCommand.c_str(), makeCommand);
176 #endif
177 makeCommand += " ";
178 makeCommand += projectName;
179 makeCommand += ".sln /rebuild ";
180 makeCommand += intdir + " /project ALL_BUILD";
182 // command line make program
183 else
185 // assume a make sytle program
186 // clean first
187 std::string cleanCommand = makeCommand;
188 cleanCommand += " clean";
189 std::cout << "Running make clean command: " << cleanCommand.c_str() << " ...\n";
190 if (!cmSystemTools::RunCommand(cleanCommand.c_str(), output))
192 std::cerr << "Error: " << cleanCommand.c_str() << " execution failed\n";
193 std::cerr << output.c_str() << "\n";
194 // return to the original directory
195 cmSystemTools::ChangeDirectory(cwd.c_str());
196 return 1;
199 // now build
200 makeCommand += " all";
203 std::cout << "Running make command: " << makeCommand.c_str() << " ...\n";
204 if (!cmSystemTools::RunCommand(makeCommand.c_str(), output))
206 std::cerr << "Error: " << makeCommand.c_str() << " execution failed\n";
207 std::cerr << output.c_str() << "\n";
208 // return to the original directory
209 cmSystemTools::ChangeDirectory(cwd.c_str());
210 return 1;
213 // now run the compiled test if we can find it
214 // See if the executable exists as written.
215 std::string fullPath;
216 if(cmSystemTools::FileExists(executableName))
218 fullPath = cmSystemTools::CollapseFullPath(executableName);
220 std::string tryPath = executableName;
221 tryPath += cmSystemTools::GetExecutableExtension();
222 if(cmSystemTools::FileExists(tryPath.c_str()))
224 fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
226 // try the Debug extension
227 tryPath = intdir + "/";
228 tryPath += cmSystemTools::GetFilenameName(executableName);
229 if(cmSystemTools::FileExists(tryPath.c_str()))
231 fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
233 tryPath += cmSystemTools::GetExecutableExtension();
234 if(cmSystemTools::FileExists(tryPath.c_str()))
236 fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
238 tryPath = executableDirectory;
239 tryPath += "/";
240 tryPath += executableName;
241 tryPath += cmSystemTools::GetExecutableExtension();
242 if(cmSystemTools::FileExists(tryPath.c_str()))
244 fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
246 tryPath = executableDirectory;
247 tryPath += "/";
248 tryPath += intdir + "/";
249 tryPath += executableName;
250 tryPath += cmSystemTools::GetExecutableExtension();
251 if(cmSystemTools::FileExists(tryPath.c_str()))
253 fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
255 if(!cmSystemTools::FileExists(fullPath.c_str()))
257 std::cerr << "Could not find path to executable, perhaps it was not built: " <<
258 executableName << "\n";
259 std::cerr << "Error: " << fullPath.c_str() << " execution failed\n";
260 // return to the original directory
261 cmSystemTools::ChangeDirectory(cwd.c_str());
262 return 1;
264 fullPath = cmSystemTools::ConvertToOutputPath(fullPath.c_str());
265 for(std::vector<std::string>::iterator p = progArgs.begin();
266 p != progArgs.end(); ++p)
268 fullPath += " ";
269 fullPath += *p;
271 std::cout << "Running test executable: " << fullPath.c_str() << "\n";
272 int ret = 0;
273 if (!cmSystemTools::RunCommand(fullPath.c_str(), output, ret, 0, true))
275 std::cerr << "Error: " << fullPath.c_str() << " execution failed\n";
276 // return to the original directory
277 cmSystemTools::ChangeDirectory(cwd.c_str());
278 return 1;
280 std::cout << output << "\n";
281 // return to the original directory
282 cmSystemTools::ChangeDirectory(cwd.c_str());
283 if(ret)
285 cmSystemTools::Error("test executable ", fullPath.c_str(),
286 "returned a non-zero value");
288 return ret;