cmaketest.cxx 4.0 KB

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