cmaketest.cxx 8.7 KB

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