cmaketest.cxx 7.8 KB

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