cmaketest.cxx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include "cmaketest.h"
  2. #include "cmSystemTools.h"
  3. #include "cmake.h"
  4. #include "cmListFileCache.h"
  5. #include "cmMakefileGenerator.h"
  6. #if defined(_WIN32) && !defined(__CYGWIN__)
  7. #include "windows.h"
  8. #endif
  9. // this is a test driver program for cmake.
  10. int main (int argc, char *argv[])
  11. {
  12. if (argc < 4)
  13. {
  14. std::cerr << "Usage: " << argv[0] << " test-src-dir test-bin-dir test-executable\n";
  15. return 1;
  16. }
  17. // does the directory exist ?
  18. if (!cmSystemTools::FileIsDirectory(argv[2]))
  19. {
  20. cmSystemTools::MakeDirectory(argv[2]);
  21. }
  22. const char* sourceDirectory = argv[1];
  23. const char* binaryDirectory = argv[2];
  24. const char* executableName = argv[3];
  25. const char* executableDirectory = "";
  26. if(argc > 4)
  27. {
  28. executableDirectory = argv[4];
  29. }
  30. const char* projectName = executableName;
  31. if(argc > 5)
  32. {
  33. projectName = argv[5];
  34. }
  35. /**
  36. * Run an executable command and put the stdout in output.
  37. */
  38. std::string output;
  39. // change to the tests directory and run cmake
  40. // use the cmake object instead of calling cmake
  41. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  42. std::cout << "Changing into directory: " << binaryDirectory << "\n";
  43. cmSystemTools::ChangeDirectory(binaryDirectory);
  44. cmake cm;
  45. std::vector<std::string> args;
  46. // make sure the same generator is used
  47. // use this program as the cmake to be run, it should not
  48. // be run that way but the cmake object requires a vailid path
  49. std::string cmakeCommand = CMAKE_COMMAND;
  50. if(cmakeCommand[0] == '\\' && cmakeCommand[1] == '\"')
  51. {
  52. cmakeCommand = cmakeCommand.substr(2, cmakeCommand.size()-4);
  53. }
  54. if(cmakeCommand[0] == '\"')
  55. {
  56. cmakeCommand = cmakeCommand.substr(1, cmakeCommand.size()-2);
  57. }
  58. args.push_back(cmakeCommand.c_str());
  59. args.push_back(sourceDirectory);
  60. std::string generator = "-G";
  61. generator += CMAKE_GENERATOR;
  62. args.push_back(generator);
  63. std::cout << "Generating build files...\n";
  64. if (cm.Generate(args) != 0)
  65. {
  66. std::cerr << "Error: cmake execution failed\n";
  67. // return to the original directory
  68. cmSystemTools::ChangeDirectory(cwd.c_str());
  69. return 1;
  70. }
  71. std::cout << "Done Generating build files.\n";
  72. cmListFileCache::GetInstance()->ClearCache();
  73. // now build the test
  74. std::string makeCommand = MAKEPROGRAM;
  75. if(makeCommand.size() == 0)
  76. {
  77. std::cerr << "Error: cmaketest does not have a valid MAKEPROGRAM\n";
  78. }
  79. makeCommand = cmSystemTools::EscapeSpaces(makeCommand.c_str());
  80. #if defined(_WIN32) && !defined(__CYGWIN__)
  81. cmSystemTools::ConvertToWindowsSlashes(makeCommand);
  82. #endif
  83. std::string lowerCaseCommand = makeCommand;
  84. cmSystemTools::LowerCase(lowerCaseCommand);
  85. // if msdev is the make program then do the following
  86. if(lowerCaseCommand.find("msdev") != std::string::npos)
  87. {
  88. // if there are spaces in the makeCommand, assume a full path
  89. // and convert it to a path with no spaces in it as the
  90. // RunCommand does not like spaces
  91. #if defined(_WIN32) && !defined(__CYGWIN__)
  92. if(makeCommand.find(' ') != std::string::npos)
  93. {
  94. char *buffer = new char[makeCommand.size()+1];
  95. if(GetShortPathName(makeCommand.c_str(), buffer,
  96. makeCommand.size()+1) != 0)
  97. {
  98. makeCommand = buffer;
  99. }
  100. delete [] buffer;\
  101. }
  102. #endif
  103. makeCommand += " ";
  104. makeCommand += projectName;
  105. makeCommand += ".dsw /MAKE \"ALL_BUILD - Debug\" /REBUILD";
  106. }
  107. else
  108. {
  109. // assume a make sytle program
  110. // clean first
  111. std::string cleanCommand = makeCommand;
  112. cleanCommand += " clean";
  113. std::cout << "Running make command: " << cleanCommand.c_str() << " ...\n";
  114. if (!cmSystemTools::RunCommand(cleanCommand.c_str(), output))
  115. {
  116. std::cerr << "Error: " << cleanCommand.c_str() << " execution failed\n";
  117. std::cerr << output.c_str() << "\n";
  118. // return to the original directory
  119. cmSystemTools::ChangeDirectory(cwd.c_str());
  120. return 1;
  121. }
  122. // now build
  123. makeCommand += " all";
  124. }
  125. std::cout << "Running make command: " << makeCommand.c_str() << " ...\n";
  126. if (!cmSystemTools::RunCommand(makeCommand.c_str(), output))
  127. {
  128. std::cerr << "Error: " << makeCommand.c_str() << " execution failed\n";
  129. std::cerr << output.c_str() << "\n";
  130. // return to the original directory
  131. cmSystemTools::ChangeDirectory(cwd.c_str());
  132. return 1;
  133. }
  134. // now run the compiled test if we can find it
  135. // See if the executable exists as written.
  136. std::string fullPath;
  137. if(cmSystemTools::FileExists(executableName))
  138. {
  139. fullPath = cmSystemTools::CollapseFullPath(executableName);
  140. }
  141. std::string tryPath = executableName;
  142. tryPath += cmSystemTools::GetExecutableExtension();
  143. if(cmSystemTools::FileExists(tryPath.c_str()))
  144. {
  145. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  146. }
  147. // try the Debug extension
  148. tryPath = "Debug/";
  149. tryPath += cmSystemTools::GetFilenameName(executableName);
  150. if(cmSystemTools::FileExists(tryPath.c_str()))
  151. {
  152. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  153. }
  154. tryPath += cmSystemTools::GetExecutableExtension();
  155. if(cmSystemTools::FileExists(tryPath.c_str()))
  156. {
  157. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  158. }
  159. tryPath = executableDirectory;
  160. tryPath += "/";
  161. tryPath += executableName;
  162. tryPath += cmSystemTools::GetExecutableExtension();
  163. if(cmSystemTools::FileExists(tryPath.c_str()))
  164. {
  165. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  166. }
  167. tryPath = executableDirectory;
  168. tryPath += "/Debug/";
  169. tryPath += executableName;
  170. tryPath += cmSystemTools::GetExecutableExtension();
  171. if(cmSystemTools::FileExists(tryPath.c_str()))
  172. {
  173. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  174. }
  175. if(!cmSystemTools::FileExists(fullPath.c_str()))
  176. {
  177. std::cerr << "Could not find path to executable, perhaps it was not built: " <<
  178. executableName << "\n";
  179. std::cerr << "Error: " << fullPath.c_str() << " execution failed\n";
  180. // return to the original directory
  181. cmSystemTools::ChangeDirectory(cwd.c_str());
  182. return 1;
  183. }
  184. fullPath = cmSystemTools::EscapeSpaces(fullPath.c_str());
  185. #if defined(_WIN32) && !defined(__CYGWIN__)
  186. cmSystemTools::ConvertToWindowsSlashes(fullPath);
  187. #endif
  188. std::cout << "Running test executable: " << fullPath.c_str() << "\n";
  189. int ret = 0;
  190. if (!cmSystemTools::RunCommand(fullPath.c_str(), output, ret, true))
  191. {
  192. std::cerr << "Error: " << fullPath.c_str() << " execution failed\n";
  193. // return to the original directory
  194. cmSystemTools::ChangeDirectory(cwd.c_str());
  195. return 1;
  196. }
  197. // return to the original directory
  198. cmSystemTools::ChangeDirectory(cwd.c_str());
  199. cmMakefileGenerator::UnRegisterGenerators();
  200. if(ret)
  201. {
  202. cmSystemTools::Error("test executable ", fullPath.c_str(),
  203. "returned a non-zero value");
  204. }
  205. return ret;
  206. }