cmaketest.cxx 9.3 KB

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