cmaketest.cxx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. /**
  31. * Run an executable command and put the stdout in output.
  32. */
  33. std::string output;
  34. // change to the tests directory and run cmake
  35. // use the cmake object instead of calling cmake
  36. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  37. cmSystemTools::ChangeDirectory(binaryDirectory);
  38. cmake cm;
  39. std::vector<std::string> args;
  40. // make sure the same generator is used
  41. // use this program as the cmake to be run, it should not
  42. // be run that way but the cmake object requires a vailid path
  43. std::string cmakeCommand = CMAKE_COMMAND;
  44. if(cmakeCommand[0] == '\\' && cmakeCommand[1] == '\"')
  45. {
  46. cmakeCommand = cmakeCommand.substr(2, cmakeCommand.size()-4);
  47. }
  48. if(cmakeCommand[0] == '\"')
  49. {
  50. cmakeCommand = cmakeCommand.substr(1, cmakeCommand.size()-2);
  51. }
  52. args.push_back(cmakeCommand.c_str());
  53. args.push_back(sourceDirectory);
  54. std::string generator = "-G";
  55. generator += CMAKE_GENERATOR;
  56. args.push_back(generator);
  57. if (cm.Generate(args) != 0)
  58. {
  59. std::cerr << "Error: cmake execution failed\n";
  60. // return to the original directory
  61. cmSystemTools::ChangeDirectory(cwd.c_str());
  62. return 1;
  63. }
  64. cmListFileCache::GetInstance()->ClearCache();
  65. // now build the test
  66. std::string makeCommand = MAKEPROGRAM;
  67. if(makeCommand.size() == 0)
  68. {
  69. std::cerr << "Error: cmaketest does not have a valid MAKEPROGRAM\n";
  70. }
  71. std::string lowerCaseCommand = makeCommand;
  72. cmSystemTools::LowerCase(lowerCaseCommand);
  73. // if msdev is the make program then do the following
  74. if(lowerCaseCommand.find("msdev") != std::string::npos)
  75. {
  76. // if there are spaces in the makeCommand, assume a full path
  77. // and convert it to a path with no spaces in it as the
  78. // RunCommand does not like spaces
  79. #if defined(_WIN32) && !defined(__CYGWIN__)
  80. if(makeCommand.find(' ') != std::string::npos)
  81. {
  82. char *buffer = new char[makeCommand.size()+1];
  83. if(GetShortPathName(makeCommand.c_str(), buffer,
  84. makeCommand.size()+1) != 0)
  85. {
  86. makeCommand = buffer;
  87. }
  88. delete [] buffer;\
  89. }
  90. #endif
  91. makeCommand += " ";
  92. makeCommand += executableName;
  93. makeCommand += ".dsw /MAKE \"ALL_BUILD - Debug\" /REBUILD";
  94. }
  95. else
  96. {
  97. // assume a make sytle program
  98. makeCommand += " all";
  99. }
  100. if (!cmSystemTools::RunCommand(makeCommand.c_str(), output))
  101. {
  102. std::cerr << "Error: " << makeCommand.c_str() << " execution failed\n";
  103. std::cerr << output.c_str() << "\n";
  104. // return to the original directory
  105. cmSystemTools::ChangeDirectory(cwd.c_str());
  106. return 1;
  107. }
  108. // now run the compiled test if we can find it
  109. // See if the executable exists as written.
  110. std::string fullPath;
  111. if(cmSystemTools::FileExists(executableName))
  112. {
  113. fullPath = cmSystemTools::CollapseFullPath(executableName);
  114. }
  115. std::string tryPath = executableName;
  116. tryPath += cmSystemTools::GetExecutableExtension();
  117. if(cmSystemTools::FileExists(tryPath.c_str()))
  118. {
  119. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  120. }
  121. // try the Debug extension
  122. tryPath = "Debug/";
  123. tryPath += cmSystemTools::GetFilenameName(executableName);
  124. if(cmSystemTools::FileExists(tryPath.c_str()))
  125. {
  126. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  127. }
  128. tryPath += cmSystemTools::GetExecutableExtension();
  129. if(cmSystemTools::FileExists(tryPath.c_str()))
  130. {
  131. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  132. }
  133. tryPath = executableDirectory;
  134. tryPath += "/";
  135. tryPath += executableName;
  136. tryPath += cmSystemTools::GetExecutableExtension();
  137. if(cmSystemTools::FileExists(tryPath.c_str()))
  138. {
  139. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  140. }
  141. tryPath = executableDirectory;
  142. tryPath += "/Debug/";
  143. tryPath += executableName;
  144. tryPath += cmSystemTools::GetExecutableExtension();
  145. if(cmSystemTools::FileExists(tryPath.c_str()))
  146. {
  147. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  148. }
  149. if(!cmSystemTools::FileExists(fullPath.c_str()))
  150. {
  151. std::cerr << "Could not find path to executable, perhaps it was not built: " <<
  152. executableName << "\n";
  153. std::cerr << "Error: " << fullPath.c_str() << " execution failed\n";
  154. // return to the original directory
  155. cmSystemTools::ChangeDirectory(cwd.c_str());
  156. return 1;
  157. }
  158. if (!cmSystemTools::RunCommand(fullPath.c_str(), output))
  159. {
  160. std::cerr << "Error: " << fullPath.c_str() << " execution failed\n";
  161. // return to the original directory
  162. cmSystemTools::ChangeDirectory(cwd.c_str());
  163. return 1;
  164. }
  165. // return to the original directory
  166. cmSystemTools::ChangeDirectory(cwd.c_str());
  167. cmMakefileGenerator::UnRegisterGenerators();
  168. return 0;
  169. }