cmaketest.cxx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html 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. #include "cmDynamicLoader.h"
  20. #if defined(_WIN32) && !defined(__CYGWIN__)
  21. #include "windows.h"
  22. #endif
  23. int do_cmaketest(int ac, char** av);
  24. int main(int ac, char** av)
  25. {
  26. cmSystemTools::EnableMSVCDebugHook();
  27. int ret = do_cmaketest(ac, av);
  28. #ifdef CMAKE_BUILD_WITH_CMAKE
  29. cmDynamicLoader::FlushCache();
  30. #endif
  31. cmListFileCache::GetInstance()->ClearCache();
  32. return ret;
  33. }
  34. // this is a test driver program for cmake.
  35. int do_cmaketest (int argc, char **argv)
  36. {
  37. if (argc < 4)
  38. {
  39. std::cerr << "Usage: " << argv[0]
  40. << " test-src-dir test-bin-dir test-executable" << std::endl;
  41. std::cerr << "\tOptional arguments: executable-directory project-name " << std::endl
  42. << "\t CMAKE_ARGS argument ...\n";
  43. return 1;
  44. }
  45. // does the directory exist ?
  46. if (!cmSystemTools::FileIsDirectory(argv[2]))
  47. {
  48. cmSystemTools::MakeDirectory(argv[2]);
  49. }
  50. const char* sourceDirectory = argv[1];
  51. const char* binaryDirectory = argv[2];
  52. const char* executableName = argv[3];
  53. const char* executableDirectory = "";
  54. if(argc > 4)
  55. {
  56. executableDirectory = argv[4];
  57. }
  58. const char* projectName = executableName;
  59. if(argc > 5)
  60. {
  61. projectName = argv[5];
  62. }
  63. // WARNING: the rest of the args is passed to cmake
  64. /**
  65. * Run an executable command and put the stdout in output.
  66. */
  67. std::string output;
  68. // change to the tests directory and run cmake
  69. // use the cmake object instead of calling cmake
  70. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  71. std::cout << "Changing into directory: " << binaryDirectory << "\n";
  72. cmSystemTools::ChangeDirectory(binaryDirectory);
  73. std::vector<std::string> args;
  74. std::string intdir = ".";
  75. #ifdef CMAKE_INTDIR
  76. intdir = CMAKE_INTDIR;
  77. #endif
  78. // make sure the same generator is used
  79. // use this program as the cmake to be run, it should not
  80. // be run that way but the cmake object requires a vailid path
  81. std::string cmakeCommand = CMAKE_BINARY_DIR;
  82. cmakeCommand += "/Source";
  83. cmakeCommand += "/";
  84. cmakeCommand += intdir;
  85. cmakeCommand += "/cmake";
  86. cmakeCommand += cmSystemTools::GetExecutableExtension();
  87. std::cout << "*** " << cmakeCommand << "\n";
  88. args.push_back(cmakeCommand.c_str());
  89. args.push_back(sourceDirectory);
  90. std::string generator = "-G";
  91. generator += CMAKE_GENERATOR;
  92. args.push_back(generator);
  93. std::vector<std::string> progArgs;
  94. if(argc > 6)
  95. {
  96. if(strcmp(argv[6] , "CMAKE_ARGS") == 0)
  97. {
  98. for (int j = 7; j < argc ; j++)
  99. {
  100. args.push_back(argv[j]);
  101. }
  102. }
  103. else
  104. {
  105. for(int j = 6; j < argc; j++)
  106. {
  107. progArgs.push_back(argv[j]);
  108. }
  109. }
  110. }
  111. std::cout << "Generating build files...\n";
  112. cmake cm;
  113. if (cm.Run(args) != 0)
  114. {
  115. std::cerr << "Error: cmake execution failed\n";
  116. // return to the original directory
  117. cmSystemTools::ChangeDirectory(cwd.c_str());
  118. return 1;
  119. }
  120. std::cout << "Done Generating build files.\n";
  121. std::cout << "Generating build files (again)...\n";
  122. if (cm.Run(args) != 0)
  123. {
  124. std::cerr << "Error: cmake execution failed\n";
  125. // return to the original directory
  126. cmSystemTools::ChangeDirectory(cwd.c_str());
  127. return 1;
  128. }
  129. std::cout << "Done Generating build files (again).\n";
  130. cmListFileCache::GetInstance()->ClearCache();
  131. // now build the test
  132. std::string makeCommand = MAKEPROGRAM;
  133. if(makeCommand.size() == 0)
  134. {
  135. std::cerr << "Error: cmaketest does not have a valid MAKEPROGRAM\n";
  136. }
  137. makeCommand = cmSystemTools::ConvertToOutputPath(makeCommand.c_str());
  138. std::string lowerCaseCommand = makeCommand;
  139. cmSystemTools::LowerCase(lowerCaseCommand);
  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. }