cmaketest.cxx 8.6 KB

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