cmaketest.cxx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "cmaketest.h"
  2. #include "cmSystemTools.h"
  3. // this is a test driver program for cmake.
  4. int main (int argc, char *argv[])
  5. {
  6. if (argc < 4)
  7. {
  8. std::cerr << "Usage: " << argv[0] << " test-src-dir test-bin-dir test-executable\n";
  9. return 1;
  10. }
  11. // does the directory exist ?
  12. if (!cmSystemTools::FileIsDirectory(argv[2]))
  13. {
  14. cmSystemTools::MakeDirectory(argv[2]);
  15. }
  16. const char* sourceDirectory = argv[1];
  17. const char* binaryDirectory = argv[2];
  18. const char* executableName = argv[3];
  19. const char* executableDirectory = "";
  20. if(argc > 4)
  21. {
  22. executableDirectory = argv[4];
  23. }
  24. /**
  25. * Run an executable command and put the stdout in output.
  26. */
  27. std::string output;
  28. // change to the tests directory and run cmake
  29. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  30. cmSystemTools::ChangeDirectory(binaryDirectory);
  31. std::string ccmd = CMAKE_COMMAND;
  32. ccmd += " ";
  33. ccmd += sourceDirectory;
  34. if (!cmSystemTools::RunCommand(ccmd.c_str(), output))
  35. {
  36. std::cerr << "Error: cmake execution failed\n";
  37. std::cerr << output.c_str() << "\n";
  38. // return to the original directory
  39. cmSystemTools::ChangeDirectory(cwd.c_str());
  40. return 1;
  41. }
  42. // now build the test
  43. std::string makeCommand = MAKEPROGRAM;
  44. makeCommand += " ";
  45. #ifdef _WIN32
  46. makeCommand += executableName;
  47. makeCommand += ".dsw /MAKE \"ALL_BUILD - Release\" /REBUILD";
  48. #else
  49. makeCommand += " all";
  50. #endif
  51. if (!cmSystemTools::RunCommand(makeCommand.c_str(), output))
  52. {
  53. std::cerr << "Error: " << makeCommand.c_str() << " execution failed\n";
  54. std::cerr << output.c_str() << "\n";
  55. // return to the original directory
  56. cmSystemTools::ChangeDirectory(cwd.c_str());
  57. return 1;
  58. }
  59. // now run the compiled test if we can find it
  60. // See if the executable exists as written.
  61. std::string fullPath;
  62. if(cmSystemTools::FileExists(executableName))
  63. {
  64. fullPath = cmSystemTools::CollapseFullPath(executableName);
  65. }
  66. std::string tryPath = executableName;
  67. tryPath += cmSystemTools::GetExecutableExtension();
  68. if(cmSystemTools::FileExists(tryPath.c_str()))
  69. {
  70. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  71. }
  72. // try the release extension
  73. tryPath = "Release/";
  74. tryPath += cmSystemTools::GetFilenameName(executableName);
  75. if(cmSystemTools::FileExists(tryPath.c_str()))
  76. {
  77. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  78. }
  79. tryPath += cmSystemTools::GetExecutableExtension();
  80. if(cmSystemTools::FileExists(tryPath.c_str()))
  81. {
  82. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  83. }
  84. tryPath = executableDirectory;
  85. tryPath += "/";
  86. tryPath += executableName;
  87. tryPath += cmSystemTools::GetExecutableExtension();
  88. if(cmSystemTools::FileExists(tryPath.c_str()))
  89. {
  90. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  91. }
  92. tryPath = executableDirectory;
  93. tryPath += "/Release/";
  94. tryPath += executableName;
  95. tryPath += cmSystemTools::GetExecutableExtension();
  96. if(cmSystemTools::FileExists(tryPath.c_str()))
  97. {
  98. fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  99. }
  100. if (!cmSystemTools::RunCommand(fullPath.c_str(), output))
  101. {
  102. std::cerr << "Error: " << fullPath.c_str() << " execution failed\n";
  103. // return to the original directory
  104. cmSystemTools::ChangeDirectory(cwd.c_str());
  105. return 1;
  106. }
  107. // return to the original directory
  108. cmSystemTools::ChangeDirectory(cwd.c_str());
  109. return 0;
  110. }