cmaketest.cxx 8.8 KB

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