cmCTestEmptyBinaryDirectoryCommand.cxx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmCTestEmptyBinaryDirectoryCommand.h"
  4. #include "cmsys/Directory.hxx"
  5. #include "cmExecutionStatus.h"
  6. #include "cmMakefile.h"
  7. #include "cmMessageType.h"
  8. #include "cmMessenger.h"
  9. #include "cmStringAlgorithms.h"
  10. #include "cmSystemTools.h"
  11. namespace {
  12. // Try to remove the binary directory once
  13. cmsys::Status TryToRemoveBinaryDirectoryOnce(std::string const& directoryPath)
  14. {
  15. cmsys::Directory directory;
  16. directory.Load(directoryPath);
  17. // Make sure that CMakeCache.txt is deleted last.
  18. for (unsigned long i = 0; i < directory.GetNumberOfFiles(); ++i) {
  19. std::string path = directory.GetFile(i);
  20. if (path == "." || path == ".." || path == "CMakeCache.txt") {
  21. continue;
  22. }
  23. std::string fullPath = cmStrCat(directoryPath, '/', path);
  24. bool isDirectory = cmSystemTools::FileIsDirectory(fullPath) &&
  25. !cmSystemTools::FileIsSymlink(fullPath);
  26. cmsys::Status status;
  27. if (isDirectory) {
  28. status = cmSystemTools::RemoveADirectory(fullPath);
  29. } else {
  30. status = cmSystemTools::RemoveFile(fullPath);
  31. }
  32. if (!status) {
  33. return status;
  34. }
  35. }
  36. return cmSystemTools::RemoveADirectory(directoryPath);
  37. }
  38. /*
  39. * Empty Binary Directory
  40. */
  41. bool EmptyBinaryDirectory(std::string const& sname, std::string& err)
  42. {
  43. // try to avoid deleting root
  44. if (sname.size() < 2) {
  45. err = "path too short";
  46. return false;
  47. }
  48. // consider non existing target directory a success
  49. if (!cmSystemTools::FileExists(sname)) {
  50. return true;
  51. }
  52. // try to avoid deleting directories that we shouldn't
  53. std::string check = cmStrCat(sname, "/CMakeCache.txt");
  54. if (!cmSystemTools::FileExists(check)) {
  55. err = "path does not contain an existing CMakeCache.txt file";
  56. return false;
  57. }
  58. cmsys::Status status;
  59. for (int i = 0; i < 5; ++i) {
  60. status = TryToRemoveBinaryDirectoryOnce(sname);
  61. if (status) {
  62. return true;
  63. }
  64. cmSystemTools::Delay(100);
  65. }
  66. err = status.GetString();
  67. return false;
  68. }
  69. } // namespace
  70. bool cmCTestEmptyBinaryDirectoryCommand(std::vector<std::string> const& args,
  71. cmExecutionStatus& status)
  72. {
  73. if (args.size() != 1) {
  74. status.SetError("called with incorrect number of arguments");
  75. return false;
  76. }
  77. std::string err;
  78. if (!EmptyBinaryDirectory(args[0], err)) {
  79. cmMakefile& mf = status.GetMakefile();
  80. mf.GetMessenger()->DisplayMessage(
  81. MessageType::FATAL_ERROR,
  82. cmStrCat("Did not remove the binary directory:\n ", args[0],
  83. "\nbecause:\n ", err),
  84. mf.GetBacktrace());
  85. return true;
  86. }
  87. return true;
  88. }