cmaketest.cxx 9.0 KB

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