cmaketest.cxx 8.8 KB

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