cmaketest.cxx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. std::cout << "Generating build files (again)...\n";
  107. if (cm.Run(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 (again).\n";
  115. cmListFileCache::GetInstance()->ClearCache();
  116. // now build the test
  117. std::string makeCommand = MAKEPROGRAM;
  118. if(makeCommand.size() == 0)
  119. {
  120. std::cerr << "Error: cmaketest does not have a valid MAKEPROGRAM\n";
  121. }
  122. makeCommand = cmSystemTools::ConvertToOutputPath(makeCommand.c_str());
  123. std::string lowerCaseCommand = makeCommand;
  124. cmSystemTools::LowerCase(lowerCaseCommand);
  125. // if msdev is the make program then do the following
  126. // MSDEV 6.0
  127. if(lowerCaseCommand.find("msdev") != std::string::npos)
  128. {
  129. // if there are spaces in the makeCommand, assume a full path
  130. // and convert it to a path with no spaces in it as the
  131. // RunCommand does not like spaces
  132. #if defined(_WIN32) && !defined(__CYGWIN__)
  133. if(makeCommand.find(' ') != std::string::npos)
  134. {
  135. cmSystemTools::GetShortPath(makeCommand.c_str(), makeCommand);
  136. }
  137. #endif
  138. makeCommand += " ";
  139. makeCommand += projectName;
  140. makeCommand += ".dsw /MAKE \"ALL_BUILD - ";
  141. makeCommand += intdir + "\" /REBUILD";
  142. }
  143. // MSDEV 7.0 .NET
  144. else if (lowerCaseCommand.find("devenv") != std::string::npos)
  145. {
  146. #if defined(_WIN32) && !defined(__CYGWIN__)
  147. if(makeCommand.find(' ') != std::string::npos)
  148. {
  149. cmSystemTools::GetShortPath(makeCommand.c_str(), makeCommand);
  150. }
  151. #endif
  152. makeCommand += " ";
  153. makeCommand += projectName;
  154. makeCommand += ".sln /rebuild ";
  155. makeCommand += intdir + " /project ALL_BUILD";
  156. }
  157. // command line make program
  158. else
  159. {
  160. // assume a make sytle program
  161. // clean first
  162. std::string cleanCommand = makeCommand;
  163. cleanCommand += " clean";
  164. std::cout << "Running make clean command: " << cleanCommand.c_str() << " ...\n";
  165. if (!cmSystemTools::RunCommand(cleanCommand.c_str(), output))
  166. {
  167. std::cerr << "Error: " << cleanCommand.c_str() << " execution failed\n";
  168. std::cerr << output.c_str() << "\n";
  169. // return to the original directory
  170. cmSystemTools::ChangeDirectory(cwd.c_str());
  171. return 1;
  172. }
  173. // now build
  174. makeCommand += " all";
  175. }
  176. std::cout << "Running make command: " << makeCommand.c_str() << " ...\n";
  177. if (!cmSystemTools::RunCommand(makeCommand.c_str(), output))
  178. {
  179. std::cerr << "Error: " << makeCommand.c_str() << " execution failed\n";
  180. std::cerr << output.c_str() << "\n";
  181. // return to the original directory
  182. cmSystemTools::ChangeDirectory(cwd.c_str());
  183. return 1;
  184. }
  185. // now run the compiled test if we can find it
  186. // See if the executable exists as written.
  187. std::string fullPath;
  188. if(cmSystemTools::FileExists(executableName))
  189. {
  190. fullPath = cmSystemTools::CollapseFullPath(executableName);
  191. }
  192. std::string tryPath = executableName;
  193. tryPath += cmSystemTools::GetExecutableExtension();
  194. if(cmSystemTools::FileExists(tryPath.c_str()))
  195. {
  196. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  197. }
  198. // try the Debug extension
  199. tryPath = intdir + "/";
  200. tryPath += cmSystemTools::GetFilenameName(executableName);
  201. if(cmSystemTools::FileExists(tryPath.c_str()))
  202. {
  203. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  204. }
  205. tryPath += cmSystemTools::GetExecutableExtension();
  206. if(cmSystemTools::FileExists(tryPath.c_str()))
  207. {
  208. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  209. }
  210. tryPath = executableDirectory;
  211. tryPath += "/";
  212. tryPath += executableName;
  213. tryPath += cmSystemTools::GetExecutableExtension();
  214. if(cmSystemTools::FileExists(tryPath.c_str()))
  215. {
  216. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  217. }
  218. tryPath = executableDirectory;
  219. tryPath += "/";
  220. tryPath += intdir + "/";
  221. tryPath += executableName;
  222. tryPath += cmSystemTools::GetExecutableExtension();
  223. if(cmSystemTools::FileExists(tryPath.c_str()))
  224. {
  225. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  226. }
  227. if(!cmSystemTools::FileExists(fullPath.c_str()))
  228. {
  229. std::cerr << "Could not find path to executable, perhaps it was not built: " <<
  230. executableName << "\n";
  231. std::cerr << "Error: " << fullPath.c_str() << " execution failed\n";
  232. // return to the original directory
  233. cmSystemTools::ChangeDirectory(cwd.c_str());
  234. return 1;
  235. }
  236. fullPath = cmSystemTools::ConvertToOutputPath(fullPath.c_str());
  237. for(std::vector<std::string>::iterator p = progArgs.begin();
  238. p != progArgs.end(); ++p)
  239. {
  240. fullPath += " ";
  241. fullPath += *p;
  242. }
  243. std::cout << "Running test executable: " << fullPath.c_str() << "\n";
  244. int ret = 0;
  245. if (!cmSystemTools::RunCommand(fullPath.c_str(), output, ret, 0, true))
  246. {
  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. std::cout << output << "\n";
  253. // return to the original directory
  254. cmSystemTools::ChangeDirectory(cwd.c_str());
  255. if(ret)
  256. {
  257. cmSystemTools::Error("test executable ", fullPath.c_str(),
  258. "returned a non-zero value");
  259. }
  260. return ret;
  261. }