cmaketest.cxx 8.4 KB

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