cmaketest.cxx 8.8 KB

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