cmaketest.cxx 5.0 KB

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