cmaketest.cxx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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::ConvertToOutputPath(makeCommand.c_str());
  102. std::string lowerCaseCommand = makeCommand;
  103. cmSystemTools::LowerCase(lowerCaseCommand);
  104. // if msdev is the make program then do the following
  105. // MSDEV 6.0
  106. if(lowerCaseCommand.find("msdev") != std::string::npos)
  107. {
  108. // if there are spaces in the makeCommand, assume a full path
  109. // and convert it to a path with no spaces in it as the
  110. // RunCommand does not like spaces
  111. #if defined(_WIN32) && !defined(__CYGWIN__)
  112. if(makeCommand.find(' ') != std::string::npos)
  113. {
  114. char *buffer = new char[makeCommand.size()+1];
  115. if(GetShortPathName(makeCommand.c_str(), buffer,
  116. makeCommand.size()+1) != 0)
  117. {
  118. makeCommand = buffer;
  119. }
  120. delete [] buffer;\
  121. }
  122. #endif
  123. makeCommand += " ";
  124. makeCommand += projectName;
  125. makeCommand += ".dsw /MAKE \"ALL_BUILD - Debug\" /REBUILD";
  126. }
  127. // MSDEV 7.0 .NET
  128. else if (lowerCaseCommand.find("devenv") != std::string::npos)
  129. {
  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. makeCommand.size()+1) != 0)
  136. {
  137. makeCommand = buffer;
  138. }
  139. delete [] buffer;\
  140. }
  141. #endif
  142. makeCommand += " ";
  143. makeCommand += projectName;
  144. makeCommand += ".sln /rebuild Debug /project ALL_BUILD";
  145. }
  146. // command line make program
  147. else
  148. {
  149. // assume a make sytle program
  150. // clean first
  151. std::string cleanCommand = makeCommand;
  152. cleanCommand += " clean";
  153. std::cout << "Running make clean command: " << cleanCommand.c_str() << " ...\n";
  154. if (!cmSystemTools::RunCommand(cleanCommand.c_str(), output))
  155. {
  156. std::cerr << "Error: " << cleanCommand.c_str() << " execution failed\n";
  157. std::cerr << output.c_str() << "\n";
  158. // return to the original directory
  159. cmSystemTools::ChangeDirectory(cwd.c_str());
  160. return 1;
  161. }
  162. // now build
  163. makeCommand += " all";
  164. }
  165. std::cout << "Running make command: " << makeCommand.c_str() << " ...\n";
  166. if (!cmSystemTools::RunCommand(makeCommand.c_str(), output))
  167. {
  168. std::cerr << "Error: " << makeCommand.c_str() << " execution failed\n";
  169. std::cerr << output.c_str() << "\n";
  170. // return to the original directory
  171. cmSystemTools::ChangeDirectory(cwd.c_str());
  172. return 1;
  173. }
  174. // now run the compiled test if we can find it
  175. // See if the executable exists as written.
  176. std::string fullPath;
  177. if(cmSystemTools::FileExists(executableName))
  178. {
  179. fullPath = cmSystemTools::CollapseFullPath(executableName);
  180. }
  181. std::string tryPath = executableName;
  182. tryPath += cmSystemTools::GetExecutableExtension();
  183. if(cmSystemTools::FileExists(tryPath.c_str()))
  184. {
  185. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  186. }
  187. // try the Debug extension
  188. tryPath = "Debug/";
  189. tryPath += cmSystemTools::GetFilenameName(executableName);
  190. if(cmSystemTools::FileExists(tryPath.c_str()))
  191. {
  192. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  193. }
  194. tryPath += cmSystemTools::GetExecutableExtension();
  195. if(cmSystemTools::FileExists(tryPath.c_str()))
  196. {
  197. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  198. }
  199. tryPath = executableDirectory;
  200. tryPath += "/";
  201. tryPath += executableName;
  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 += "/Debug/";
  209. tryPath += executableName;
  210. tryPath += cmSystemTools::GetExecutableExtension();
  211. if(cmSystemTools::FileExists(tryPath.c_str()))
  212. {
  213. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  214. }
  215. if(!cmSystemTools::FileExists(fullPath.c_str()))
  216. {
  217. std::cerr << "Could not find path to executable, perhaps it was not built: " <<
  218. executableName << "\n";
  219. std::cerr << "Error: " << fullPath.c_str() << " execution failed\n";
  220. // return to the original directory
  221. cmSystemTools::ChangeDirectory(cwd.c_str());
  222. return 1;
  223. }
  224. fullPath = cmSystemTools::ConvertToOutputPath(fullPath.c_str());
  225. std::cout << "Running test executable: " << fullPath.c_str() << "\n";
  226. int ret = 0;
  227. if (!cmSystemTools::RunCommand(fullPath.c_str(), output, ret, true))
  228. {
  229. std::cerr << "Error: " << fullPath.c_str() << " execution failed\n";
  230. // return to the original directory
  231. cmSystemTools::ChangeDirectory(cwd.c_str());
  232. return 1;
  233. }
  234. // return to the original directory
  235. cmSystemTools::ChangeDirectory(cwd.c_str());
  236. cmMakefileGenerator::UnRegisterGenerators();
  237. if(ret)
  238. {
  239. cmSystemTools::Error("test executable ", fullPath.c_str(),
  240. "returned a non-zero value");
  241. }
  242. return ret;
  243. }