cmaketest.cxx 8.9 KB

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