cmaketest.cxx 9.3 KB

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