cmaketest.cxx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmaketest.h"
  14. #include "cmSystemTools.h"
  15. #include "cmRegularExpression.h"
  16. #include "cmake.h"
  17. #include "cmListFileCache.h"
  18. #include "cmMakefileGenerator.h"
  19. #if defined(_WIN32) && !defined(__CYGWIN__)
  20. #include "windows.h"
  21. #endif
  22. // this is a test driver program for cmake.
  23. int main (int argc, char **argv)
  24. {
  25. if (argc < 4)
  26. {
  27. std::cerr << "Usage: " << argv[0] << " test-src-dir test-bin-dir test-executable\n";
  28. return 1;
  29. }
  30. // does the directory exist ?
  31. if (!cmSystemTools::FileIsDirectory(argv[2]))
  32. {
  33. cmSystemTools::MakeDirectory(argv[2]);
  34. }
  35. const char* sourceDirectory = argv[1];
  36. const char* binaryDirectory = argv[2];
  37. const char* executableName = argv[3];
  38. const char* executableDirectory = "";
  39. if(argc > 4)
  40. {
  41. executableDirectory = argv[4];
  42. }
  43. const char* projectName = executableName;
  44. if(argc > 5)
  45. {
  46. projectName = argv[5];
  47. }
  48. // WARNING: the rest of the args is passed to cmake
  49. /**
  50. * Run an executable command and put the stdout in output.
  51. */
  52. std::string output;
  53. // change to the tests directory and run cmake
  54. // use the cmake object instead of calling cmake
  55. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  56. std::cout << "Changing into directory: " << binaryDirectory << "\n";
  57. cmSystemTools::ChangeDirectory(binaryDirectory);
  58. std::vector<std::string> args;
  59. // make sure the same generator is used
  60. // use this program as the cmake to be run, it should not
  61. // be run that way but the cmake object requires a vailid path
  62. std::string cmakeCommand = CMAKE_COMMAND;
  63. if(cmakeCommand[0] == '\\' && cmakeCommand[1] == '\"')
  64. {
  65. cmakeCommand = cmakeCommand.substr(2, cmakeCommand.size()-4);
  66. }
  67. if(cmakeCommand[0] == '\"')
  68. {
  69. cmakeCommand = cmakeCommand.substr(1, cmakeCommand.size()-2);
  70. }
  71. args.push_back(cmakeCommand.c_str());
  72. args.push_back(sourceDirectory);
  73. std::string generator = "-G";
  74. generator += CMAKE_GENERATOR;
  75. args.push_back(generator);
  76. std::vector<std::string> progArgs;
  77. if(argc > 6)
  78. {
  79. if(strcmp(argv[6] , "CMAKE_ARGS") == 0)
  80. {
  81. for (int j = 7; j < argc ; j++)
  82. {
  83. args.push_back(argv[j]);
  84. }
  85. }
  86. else
  87. {
  88. for(int j = 6; j < argc; j++)
  89. {
  90. progArgs.push_back(argv[j]);
  91. }
  92. }
  93. }
  94. std::cout << "Generating build files...\n";
  95. cmake cm;
  96. if (cm.Generate(args) != 0)
  97. {
  98. std::cerr << "Error: cmake execution failed\n";
  99. // return to the original directory
  100. cmSystemTools::ChangeDirectory(cwd.c_str());
  101. return 1;
  102. }
  103. std::cout << "Done Generating build files.\n";
  104. cmake cm2;
  105. std::cout << "Generating build files (again)...\n";
  106. if (cm2.Generate(args) != 0)
  107. {
  108. std::cerr << "Error: cmake execution failed\n";
  109. // return to the original directory
  110. cmSystemTools::ChangeDirectory(cwd.c_str());
  111. return 1;
  112. }
  113. std::cout << "Done Generating build files (again).\n";
  114. cmListFileCache::GetInstance()->ClearCache();
  115. // now build the test
  116. std::string makeCommand = MAKEPROGRAM;
  117. if(makeCommand.size() == 0)
  118. {
  119. std::cerr << "Error: cmaketest does not have a valid MAKEPROGRAM\n";
  120. }
  121. makeCommand = cmSystemTools::ConvertToOutputPath(makeCommand.c_str());
  122. std::string lowerCaseCommand = makeCommand;
  123. cmSystemTools::LowerCase(lowerCaseCommand);
  124. std::string dartMakeCommand = DART_MAKECOMMAND;
  125. std::string buildtype = "Debug";
  126. #ifdef CMAKE_INTDIR
  127. buildtype = CMAKE_INTDIR;
  128. #endif
  129. // if msdev is the make program then do the following
  130. // MSDEV 6.0
  131. if(lowerCaseCommand.find("msdev") != std::string::npos)
  132. {
  133. // if there are spaces in the makeCommand, assume a full path
  134. // and convert it to a path with no spaces in it as the
  135. // RunCommand does not like spaces
  136. #if defined(_WIN32) && !defined(__CYGWIN__)
  137. if(makeCommand.find(' ') != std::string::npos)
  138. {
  139. char *buffer = new char[makeCommand.size()+1];
  140. if(GetShortPathName(makeCommand.c_str(), buffer,
  141. int(makeCommand.size()+1)) != 0)
  142. {
  143. makeCommand = buffer;
  144. }
  145. delete [] buffer;\
  146. }
  147. #endif
  148. makeCommand += " ";
  149. makeCommand += projectName;
  150. makeCommand += ".dsw /MAKE \"ALL_BUILD - ";
  151. makeCommand += buildtype + "\" /REBUILD";
  152. }
  153. // MSDEV 7.0 .NET
  154. else if (lowerCaseCommand.find("devenv") != std::string::npos)
  155. {
  156. #if defined(_WIN32) && !defined(__CYGWIN__)
  157. if(makeCommand.find(' ') != std::string::npos)
  158. {
  159. char *buffer = new char[makeCommand.size()+1];
  160. if(GetShortPathName(makeCommand.c_str(), buffer,
  161. int(makeCommand.size()+1)) != 0)
  162. {
  163. makeCommand = buffer;
  164. }
  165. delete [] buffer;\
  166. }
  167. #endif
  168. makeCommand += " ";
  169. makeCommand += projectName;
  170. makeCommand += ".sln /rebuild ";
  171. makeCommand += buildtype + " /project ALL_BUILD";
  172. }
  173. // command line make program
  174. else
  175. {
  176. // assume a make sytle program
  177. // clean first
  178. std::string cleanCommand = makeCommand;
  179. cleanCommand += " clean";
  180. std::cout << "Running make clean command: " << cleanCommand.c_str() << " ...\n";
  181. if (!cmSystemTools::RunCommand(cleanCommand.c_str(), output))
  182. {
  183. std::cerr << "Error: " << cleanCommand.c_str() << " execution failed\n";
  184. std::cerr << output.c_str() << "\n";
  185. // return to the original directory
  186. cmSystemTools::ChangeDirectory(cwd.c_str());
  187. return 1;
  188. }
  189. // now build
  190. makeCommand += " all";
  191. }
  192. std::cout << "Running make command: " << makeCommand.c_str() << " ...\n";
  193. if (!cmSystemTools::RunCommand(makeCommand.c_str(), output))
  194. {
  195. std::cerr << "Error: " << makeCommand.c_str() << " execution failed\n";
  196. std::cerr << output.c_str() << "\n";
  197. // return to the original directory
  198. cmSystemTools::ChangeDirectory(cwd.c_str());
  199. return 1;
  200. }
  201. // now run the compiled test if we can find it
  202. // See if the executable exists as written.
  203. std::string fullPath;
  204. if(cmSystemTools::FileExists(executableName))
  205. {
  206. fullPath = cmSystemTools::CollapseFullPath(executableName);
  207. }
  208. std::string tryPath = executableName;
  209. tryPath += cmSystemTools::GetExecutableExtension();
  210. if(cmSystemTools::FileExists(tryPath.c_str()))
  211. {
  212. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  213. }
  214. // try the Debug extension
  215. tryPath = buildtype + "/";
  216. tryPath += cmSystemTools::GetFilenameName(executableName);
  217. if(cmSystemTools::FileExists(tryPath.c_str()))
  218. {
  219. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  220. }
  221. tryPath += cmSystemTools::GetExecutableExtension();
  222. if(cmSystemTools::FileExists(tryPath.c_str()))
  223. {
  224. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  225. }
  226. tryPath = executableDirectory;
  227. tryPath += "/";
  228. tryPath += executableName;
  229. tryPath += cmSystemTools::GetExecutableExtension();
  230. if(cmSystemTools::FileExists(tryPath.c_str()))
  231. {
  232. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  233. }
  234. tryPath = executableDirectory;
  235. tryPath += "/";
  236. tryPath += buildtype + "/";
  237. tryPath += executableName;
  238. tryPath += cmSystemTools::GetExecutableExtension();
  239. if(cmSystemTools::FileExists(tryPath.c_str()))
  240. {
  241. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  242. }
  243. if(!cmSystemTools::FileExists(fullPath.c_str()))
  244. {
  245. std::cerr << "Could not find path to executable, perhaps it was not built: " <<
  246. executableName << "\n";
  247. std::cerr << "Error: " << fullPath.c_str() << " execution failed\n";
  248. // return to the original directory
  249. cmSystemTools::ChangeDirectory(cwd.c_str());
  250. return 1;
  251. }
  252. fullPath = cmSystemTools::ConvertToOutputPath(fullPath.c_str());
  253. for(std::vector<std::string>::iterator p = progArgs.begin();
  254. p != progArgs.end(); ++p)
  255. {
  256. fullPath += " ";
  257. fullPath += *p;
  258. }
  259. std::cout << "Running test executable: " << fullPath.c_str() << "\n";
  260. int ret = 0;
  261. if (!cmSystemTools::RunCommand(fullPath.c_str(), output, ret, 0, true))
  262. {
  263. std::cerr << "Error: " << fullPath.c_str() << " execution failed\n";
  264. // return to the original directory
  265. cmSystemTools::ChangeDirectory(cwd.c_str());
  266. return 1;
  267. }
  268. std::cout << output << "\n";
  269. // return to the original directory
  270. cmSystemTools::ChangeDirectory(cwd.c_str());
  271. cmMakefileGenerator::UnRegisterGenerators();
  272. if(ret)
  273. {
  274. cmSystemTools::Error("test executable ", fullPath.c_str(),
  275. "returned a non-zero value");
  276. }
  277. return ret;
  278. }