cmaketest.cxx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. // Return value for run command;
  150. int retVal = 0;
  151. int ret = 0;
  152. makeCommand = cmSystemTools::ConvertToOutputPath(makeCommand.c_str());
  153. std::string lowerCaseCommand = cmSystemTools::LowerCase(makeCommand);
  154. // if msdev is the make program then do the following
  155. // MSDEV 6.0
  156. if(lowerCaseCommand.find("msdev") != std::string::npos)
  157. {
  158. // if there are spaces in the makeCommand, assume a full path
  159. // and convert it to a path with no spaces in it as the
  160. // RunSingleCommand does not like spaces
  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 += ".dsw /MAKE \"ALL_BUILD - ";
  170. makeCommand += intdir + "\" /REBUILD";
  171. }
  172. // MSDEV 7.0 .NET
  173. else if (lowerCaseCommand.find("devenv") != std::string::npos)
  174. {
  175. #if defined(_WIN32) && !defined(__CYGWIN__)
  176. if(makeCommand.find(' ') != std::string::npos)
  177. {
  178. cmSystemTools::GetShortPath(makeCommand.c_str(), makeCommand);
  179. }
  180. #endif
  181. makeCommand += " ";
  182. makeCommand += projectName;
  183. makeCommand += ".sln /rebuild ";
  184. makeCommand += intdir + " /project ALL_BUILD";
  185. }
  186. // command line make program
  187. else
  188. {
  189. // assume a make sytle program
  190. // clean first
  191. std::string cleanCommand = makeCommand;
  192. cleanCommand += " clean";
  193. std::cout << "Running make clean command: " << cleanCommand.c_str() << " ...\n";
  194. retVal = 0;
  195. if (!cmSystemTools::RunSingleCommand(cleanCommand.c_str(), &output, &retVal) ||
  196. retVal)
  197. {
  198. std::cerr << "Error: " << cleanCommand.c_str() << " execution failed\n";
  199. std::cerr << output.c_str() << "\n";
  200. // return to the original directory
  201. cmSystemTools::ChangeDirectory(cwd.c_str());
  202. std::cerr << "Return value: " << retVal << std::endl;
  203. return 1;
  204. }
  205. // now build
  206. }
  207. std::cout << "Running make command: " << makeCommand.c_str() << " ...\n";
  208. retVal = 0;
  209. if (!cmSystemTools::RunSingleCommand(makeCommand.c_str(), &output, &retVal))
  210. {
  211. std::cerr << "Error: " << makeCommand.c_str() << " execution failed\n";
  212. std::cerr << output.c_str() << "\n";
  213. // return to the original directory
  214. cmSystemTools::ChangeDirectory(cwd.c_str());
  215. return 1;
  216. }
  217. if ( retVal )
  218. {
  219. cmSystemTools::Error("Building of project failed\n");
  220. std::cerr << output.c_str() << "\n";
  221. // return to the original directory
  222. cmSystemTools::ChangeDirectory(cwd.c_str());
  223. ret = 1;
  224. }
  225. // now run the compiled test if we can find it
  226. // See if the executable exists as written.
  227. std::string fullPath;
  228. if(cmSystemTools::FileExists(executableName))
  229. {
  230. fullPath = cmSystemTools::CollapseFullPath(executableName);
  231. }
  232. std::string tryPath = executableName;
  233. tryPath += cmSystemTools::GetExecutableExtension();
  234. if(cmSystemTools::FileExists(tryPath.c_str()))
  235. {
  236. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  237. }
  238. // try the Debug extension
  239. tryPath = intdir + "/";
  240. tryPath += cmSystemTools::GetFilenameName(executableName);
  241. if(cmSystemTools::FileExists(tryPath.c_str()))
  242. {
  243. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  244. }
  245. tryPath += cmSystemTools::GetExecutableExtension();
  246. if(cmSystemTools::FileExists(tryPath.c_str()))
  247. {
  248. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  249. }
  250. tryPath = executableDirectory;
  251. tryPath += "/";
  252. tryPath += executableName;
  253. tryPath += cmSystemTools::GetExecutableExtension();
  254. if(cmSystemTools::FileExists(tryPath.c_str()))
  255. {
  256. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  257. }
  258. tryPath = executableDirectory;
  259. tryPath += "/";
  260. tryPath += intdir + "/";
  261. tryPath += executableName;
  262. tryPath += cmSystemTools::GetExecutableExtension();
  263. if(cmSystemTools::FileExists(tryPath.c_str()))
  264. {
  265. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  266. }
  267. if(!cmSystemTools::FileExists(fullPath.c_str()))
  268. {
  269. std::cerr << "Could not find path to executable, perhaps it was not built: " <<
  270. executableName << "\n";
  271. std::cerr << "Error: " << fullPath.c_str() << " execution failed\n";
  272. // return to the original directory
  273. cmSystemTools::ChangeDirectory(cwd.c_str());
  274. return 1;
  275. }
  276. fullPath = cmSystemTools::ConvertToOutputPath(fullPath.c_str());
  277. for(std::vector<std::string>::iterator p = progArgs.begin();
  278. p != progArgs.end(); ++p)
  279. {
  280. fullPath += " ";
  281. fullPath += *p;
  282. }
  283. std::cout << "Running test executable: " << fullPath.c_str() << "\n";
  284. retVal = 0;
  285. if (!cmSystemTools::RunSingleCommand(fullPath.c_str(), &output, &retVal))
  286. {
  287. std::cerr << "Error: " << fullPath.c_str() << " execution failed\n";
  288. // return to the original directory
  289. cmSystemTools::ChangeDirectory(cwd.c_str());
  290. return 1;
  291. }
  292. std::cout << output << "\n";
  293. // return to the original directory
  294. cmSystemTools::ChangeDirectory(cwd.c_str());
  295. if(retVal)
  296. {
  297. cmSystemTools::Error("test executable ", fullPath.c_str(),
  298. " returned a non-zero value");
  299. ret = 1;
  300. }
  301. if ( ret )
  302. {
  303. cmSystemTools::Error("Test failed");
  304. }
  305. return ret;
  306. }