cmaketest.cxx 8.4 KB

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