cmaketest.cxx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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__) && !defined(__BORLANDC__)
  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. std::string lowerCaseCommand = makeCommand;
  68. cmSystemTools::LowerCase(lowerCaseCommand);
  69. // if msdev is the make program then do the following
  70. if(lowerCaseCommand.find("msdev") != std::string::npos)
  71. {
  72. // if there are spaces in the makeCommand, assume a full path
  73. // and convert it to a path with no spaces in it as the
  74. // RunCommand does not like spaces
  75. if(makeCommand.find(' ') != std::string::npos)
  76. {
  77. char *buffer = new char[makeCommand.size()+1];
  78. #if defined(_WIN32) && !defined(__CYGWIN__)
  79. if(GetShortPathName(makeCommand.c_str(), buffer,
  80. makeCommand.size()+1) != 0)
  81. {
  82. makeCommand = buffer;
  83. delete [] buffer;
  84. }
  85. #endif
  86. }
  87. makeCommand += " ";
  88. makeCommand += executableName;
  89. makeCommand += ".dsw /MAKE \"ALL_BUILD - Debug\" /REBUILD";
  90. }
  91. else
  92. {
  93. // assume a make sytle program
  94. makeCommand += " all";
  95. }
  96. if (!cmSystemTools::RunCommand(makeCommand.c_str(), output))
  97. {
  98. std::cerr << "Error: " << makeCommand.c_str() << " execution failed\n";
  99. std::cerr << output.c_str() << "\n";
  100. // return to the original directory
  101. cmSystemTools::ChangeDirectory(cwd.c_str());
  102. return 1;
  103. }
  104. // now run the compiled test if we can find it
  105. // See if the executable exists as written.
  106. std::string fullPath;
  107. if(cmSystemTools::FileExists(executableName))
  108. {
  109. fullPath = cmSystemTools::CollapseFullPath(executableName);
  110. }
  111. std::string tryPath = executableName;
  112. tryPath += cmSystemTools::GetExecutableExtension();
  113. if(cmSystemTools::FileExists(tryPath.c_str()))
  114. {
  115. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  116. }
  117. // try the Debug extension
  118. tryPath = "Debug/";
  119. tryPath += cmSystemTools::GetFilenameName(executableName);
  120. if(cmSystemTools::FileExists(tryPath.c_str()))
  121. {
  122. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  123. }
  124. tryPath += cmSystemTools::GetExecutableExtension();
  125. if(cmSystemTools::FileExists(tryPath.c_str()))
  126. {
  127. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  128. }
  129. tryPath = executableDirectory;
  130. tryPath += "/";
  131. tryPath += executableName;
  132. tryPath += cmSystemTools::GetExecutableExtension();
  133. if(cmSystemTools::FileExists(tryPath.c_str()))
  134. {
  135. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  136. }
  137. tryPath = executableDirectory;
  138. tryPath += "/Debug/";
  139. tryPath += executableName;
  140. tryPath += cmSystemTools::GetExecutableExtension();
  141. if(cmSystemTools::FileExists(tryPath.c_str()))
  142. {
  143. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  144. }
  145. if(!cmSystemTools::FileExists(fullPath.c_str()))
  146. {
  147. std::cerr << "Could not find path to executable, perhaps it was not built: " <<
  148. executableName << "\n";
  149. std::cerr << "Error: " << fullPath.c_str() << " execution failed\n";
  150. // return to the original directory
  151. cmSystemTools::ChangeDirectory(cwd.c_str());
  152. return 1;
  153. }
  154. if (!cmSystemTools::RunCommand(fullPath.c_str(), output))
  155. {
  156. std::cerr << "Error: " << fullPath.c_str() << " execution failed\n";
  157. // return to the original directory
  158. cmSystemTools::ChangeDirectory(cwd.c_str());
  159. return 1;
  160. }
  161. // return to the original directory
  162. cmSystemTools::ChangeDirectory(cwd.c_str());
  163. cmMakefileGenerator::UnRegisterGenerators();
  164. return 0;
  165. }