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 "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 = EXECUTABLE_OUTPUT_PATH;
  91. cmakeCommand += "/";
  92. cmakeCommand += intdir;
  93. cmakeCommand += "/cmake";
  94. cmakeCommand += cmSystemTools::GetExecutableExtension();
  95. std::cout << "*** " << cmakeCommand << "\n";
  96. args.push_back(cmakeCommand.c_str());
  97. args.push_back(sourceDirectory);
  98. std::string generator = "-G";
  99. generator += CMAKE_GENERATOR;
  100. args.push_back(generator);
  101. std::vector<std::string> progArgs;
  102. if(argc > 6)
  103. {
  104. if(strcmp(argv[6] , "CMAKE_ARGS") == 0)
  105. {
  106. for (int j = 7; j < argc ; j++)
  107. {
  108. args.push_back(argv[j]);
  109. }
  110. }
  111. else
  112. {
  113. for(int j = 6; j < argc; j++)
  114. {
  115. progArgs.push_back(argv[j]);
  116. }
  117. }
  118. }
  119. std::cout << "Generating build files...\n";
  120. cmake cm;
  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.\n";
  129. // if the option ONLY_ONE_CONFIG is passed to the program
  130. // only run the config step once
  131. if(!onlyOneConfig)
  132. {
  133. std::cout << "Generating build files (again)...\n";
  134. if (cm.Run(args) != 0)
  135. {
  136. std::cerr << "Error: cmake execution failed\n";
  137. // return to the original directory
  138. cmSystemTools::ChangeDirectory(cwd.c_str());
  139. return 1;
  140. }
  141. std::cout << "Done Generating build files (again).\n";
  142. }
  143. cmListFileCache::GetInstance()->ClearCache();
  144. // now build the test
  145. std::string makeCommand = MAKEPROGRAM;
  146. if(makeCommand.size() == 0)
  147. {
  148. std::cerr << "Error: cmaketest does not have a valid MAKEPROGRAM\n";
  149. }
  150. makeCommand = cmSystemTools::ConvertToOutputPath(makeCommand.c_str());
  151. std::string lowerCaseCommand = cmSystemTools::LowerCase(makeCommand);
  152. // if msdev is the make program then do the following
  153. // MSDEV 6.0
  154. if(lowerCaseCommand.find("msdev") != std::string::npos)
  155. {
  156. // if there are spaces in the makeCommand, assume a full path
  157. // and convert it to a path with no spaces in it as the
  158. // RunCommand does not like spaces
  159. #if defined(_WIN32) && !defined(__CYGWIN__)
  160. if(makeCommand.find(' ') != std::string::npos)
  161. {
  162. cmSystemTools::GetShortPath(makeCommand.c_str(), makeCommand);
  163. }
  164. #endif
  165. makeCommand += " ";
  166. makeCommand += projectName;
  167. makeCommand += ".dsw /MAKE \"ALL_BUILD - ";
  168. makeCommand += intdir + "\" /REBUILD";
  169. }
  170. // MSDEV 7.0 .NET
  171. else if (lowerCaseCommand.find("devenv") != std::string::npos)
  172. {
  173. #if defined(_WIN32) && !defined(__CYGWIN__)
  174. if(makeCommand.find(' ') != std::string::npos)
  175. {
  176. cmSystemTools::GetShortPath(makeCommand.c_str(), makeCommand);
  177. }
  178. #endif
  179. makeCommand += " ";
  180. makeCommand += projectName;
  181. makeCommand += ".sln /rebuild ";
  182. makeCommand += intdir + " /project ALL_BUILD";
  183. }
  184. // command line make program
  185. else
  186. {
  187. // assume a make sytle program
  188. // clean first
  189. std::string cleanCommand = makeCommand;
  190. cleanCommand += " clean";
  191. std::cout << "Running make clean command: " << cleanCommand.c_str() << " ...\n";
  192. if (!cmSystemTools::RunCommand(cleanCommand.c_str(), output))
  193. {
  194. std::cerr << "Error: " << cleanCommand.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 build
  201. }
  202. std::cout << "Running make command: " << makeCommand.c_str() << " ...\n";
  203. if (!cmSystemTools::RunCommand(makeCommand.c_str(), output))
  204. {
  205. std::cerr << "Error: " << makeCommand.c_str() << " execution failed\n";
  206. std::cerr << output.c_str() << "\n";
  207. // return to the original directory
  208. cmSystemTools::ChangeDirectory(cwd.c_str());
  209. return 1;
  210. }
  211. // now run the compiled test if we can find it
  212. // See if the executable exists as written.
  213. std::string fullPath;
  214. if(cmSystemTools::FileExists(executableName))
  215. {
  216. fullPath = cmSystemTools::CollapseFullPath(executableName);
  217. }
  218. std::string tryPath = executableName;
  219. tryPath += cmSystemTools::GetExecutableExtension();
  220. if(cmSystemTools::FileExists(tryPath.c_str()))
  221. {
  222. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  223. }
  224. // try the Debug extension
  225. tryPath = intdir + "/";
  226. tryPath += cmSystemTools::GetFilenameName(executableName);
  227. if(cmSystemTools::FileExists(tryPath.c_str()))
  228. {
  229. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  230. }
  231. tryPath += cmSystemTools::GetExecutableExtension();
  232. if(cmSystemTools::FileExists(tryPath.c_str()))
  233. {
  234. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  235. }
  236. tryPath = executableDirectory;
  237. tryPath += "/";
  238. tryPath += executableName;
  239. tryPath += cmSystemTools::GetExecutableExtension();
  240. if(cmSystemTools::FileExists(tryPath.c_str()))
  241. {
  242. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  243. }
  244. tryPath = executableDirectory;
  245. tryPath += "/";
  246. tryPath += intdir + "/";
  247. tryPath += executableName;
  248. tryPath += cmSystemTools::GetExecutableExtension();
  249. if(cmSystemTools::FileExists(tryPath.c_str()))
  250. {
  251. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  252. }
  253. if(!cmSystemTools::FileExists(fullPath.c_str()))
  254. {
  255. std::cerr << "Could not find path to executable, perhaps it was not built: " <<
  256. executableName << "\n";
  257. std::cerr << "Error: " << fullPath.c_str() << " execution failed\n";
  258. // return to the original directory
  259. cmSystemTools::ChangeDirectory(cwd.c_str());
  260. return 1;
  261. }
  262. fullPath = cmSystemTools::ConvertToOutputPath(fullPath.c_str());
  263. for(std::vector<std::string>::iterator p = progArgs.begin();
  264. p != progArgs.end(); ++p)
  265. {
  266. fullPath += " ";
  267. fullPath += *p;
  268. }
  269. std::cout << "Running test executable: " << fullPath.c_str() << "\n";
  270. int ret = 0;
  271. if (!cmSystemTools::RunCommand(fullPath.c_str(), output, ret, 0, true))
  272. {
  273. std::cerr << "Error: " << fullPath.c_str() << " execution failed\n";
  274. // return to the original directory
  275. cmSystemTools::ChangeDirectory(cwd.c_str());
  276. return 1;
  277. }
  278. std::cout << output << "\n";
  279. // return to the original directory
  280. cmSystemTools::ChangeDirectory(cwd.c_str());
  281. if(ret)
  282. {
  283. cmSystemTools::Error("test executable ", fullPath.c_str(),
  284. "returned a non-zero value");
  285. }
  286. return ret;
  287. }