cmGlobVerificationManager.cxx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmGlobVerificationManager.h"
  4. #include "cmsys/FStream.hxx"
  5. #include <sstream>
  6. #include "cmGeneratedFileStream.h"
  7. #include "cmListFileCache.h"
  8. #include "cmSystemTools.h"
  9. #include "cmVersion.h"
  10. #include "cmake.h"
  11. bool cmGlobVerificationManager::SaveVerificationScript(const std::string& path)
  12. {
  13. if (this->Cache.empty()) {
  14. return true;
  15. }
  16. std::string scriptFile = path;
  17. scriptFile += cmake::GetCMakeFilesDirectory();
  18. std::string stampFile = scriptFile;
  19. cmSystemTools::MakeDirectory(scriptFile);
  20. scriptFile += "/VerifyGlobs.cmake";
  21. stampFile += "/cmake.verify_globs";
  22. cmGeneratedFileStream verifyScriptFile(scriptFile.c_str());
  23. verifyScriptFile.SetCopyIfDifferent(true);
  24. if (!verifyScriptFile) {
  25. cmSystemTools::Error("Unable to open verification script file for save. ",
  26. scriptFile.c_str());
  27. cmSystemTools::ReportLastSystemError("");
  28. return false;
  29. }
  30. verifyScriptFile << std::boolalpha;
  31. verifyScriptFile << "# CMAKE generated file: DO NOT EDIT!\n"
  32. << "# Generated by CMake Version "
  33. << cmVersion::GetMajorVersion() << "."
  34. << cmVersion::GetMinorVersion() << "\n";
  35. for (auto const& i : this->Cache) {
  36. CacheEntryKey k = std::get<0>(i);
  37. CacheEntryValue v = std::get<1>(i);
  38. if (!v.Initialized) {
  39. continue;
  40. }
  41. verifyScriptFile << "\n";
  42. for (auto const& bt : v.Backtraces) {
  43. verifyScriptFile << "# " << std::get<0>(bt);
  44. std::get<1>(bt).PrintTitle(verifyScriptFile);
  45. verifyScriptFile << "\n";
  46. }
  47. k.PrintGlobCommand(verifyScriptFile, "NEW_GLOB");
  48. verifyScriptFile << "\n";
  49. verifyScriptFile << "set(OLD_GLOB\n";
  50. for (const std::string& file : v.Files) {
  51. verifyScriptFile << " \"" << file << "\"\n";
  52. }
  53. verifyScriptFile << " )\n";
  54. verifyScriptFile << "if(NOT \"${NEW_GLOB}\" STREQUAL \"${OLD_GLOB}\")\n"
  55. << " message(\"-- GLOB mismatch!\")\n"
  56. << " file(TOUCH_NOCREATE \"" << stampFile << "\")\n"
  57. << "endif()\n";
  58. }
  59. verifyScriptFile.Close();
  60. cmsys::ofstream verifyStampFile(stampFile.c_str());
  61. if (!verifyStampFile) {
  62. cmSystemTools::Error("Unable to open verification stamp file for write. ",
  63. stampFile.c_str());
  64. return false;
  65. }
  66. verifyStampFile << "# This file is generated by CMake for checking of the "
  67. "VerifyGlobs.cmake file\n";
  68. this->VerifyScript = scriptFile;
  69. this->VerifyStamp = stampFile;
  70. return true;
  71. }
  72. bool cmGlobVerificationManager::DoWriteVerifyTarget() const
  73. {
  74. return !this->VerifyScript.empty() && !this->VerifyStamp.empty();
  75. }
  76. bool cmGlobVerificationManager::CacheEntryKey::operator<(
  77. const CacheEntryKey& r) const
  78. {
  79. if (this->Recurse < r.Recurse) {
  80. return true;
  81. }
  82. if (this->Recurse > r.Recurse) {
  83. return false;
  84. }
  85. if (this->ListDirectories < r.ListDirectories) {
  86. return true;
  87. }
  88. if (this->ListDirectories > r.ListDirectories) {
  89. return false;
  90. }
  91. if (this->FollowSymlinks < r.FollowSymlinks) {
  92. return true;
  93. }
  94. if (this->FollowSymlinks > r.FollowSymlinks) {
  95. return false;
  96. }
  97. if (this->Relative < r.Relative) {
  98. return true;
  99. }
  100. if (this->Relative > r.Relative) {
  101. return false;
  102. }
  103. if (this->Expression < r.Expression) {
  104. return true;
  105. }
  106. if (this->Expression > r.Expression) {
  107. return false;
  108. }
  109. return false;
  110. }
  111. void cmGlobVerificationManager::CacheEntryKey::PrintGlobCommand(
  112. std::ostream& out, const std::string& cmdVar)
  113. {
  114. out << "file(GLOB" << (this->Recurse ? "_RECURSE " : " ");
  115. out << cmdVar << " ";
  116. if (this->Recurse && this->FollowSymlinks) {
  117. out << "FOLLOW_SYMLINKS ";
  118. }
  119. out << "LIST_DIRECTORIES " << this->ListDirectories << " ";
  120. if (!this->Relative.empty()) {
  121. out << "RELATIVE \"" << this->Relative << "\" ";
  122. }
  123. out << "\"" << this->Expression << "\")";
  124. }
  125. void cmGlobVerificationManager::AddCacheEntry(
  126. const bool recurse, const bool listDirectories, const bool followSymlinks,
  127. const std::string& relative, const std::string& expression,
  128. const std::vector<std::string>& files, const std::string& variable,
  129. const cmListFileBacktrace& backtrace)
  130. {
  131. CacheEntryKey key = CacheEntryKey(recurse, listDirectories, followSymlinks,
  132. relative, expression);
  133. CacheEntryValue& value = this->Cache[key];
  134. if (!value.Initialized) {
  135. value.Files = files;
  136. value.Initialized = true;
  137. value.Backtraces.emplace_back(variable, backtrace);
  138. } else if (value.Initialized && value.Files != files) {
  139. std::ostringstream message;
  140. message << std::boolalpha;
  141. message << "The glob expression\n";
  142. key.PrintGlobCommand(message, variable);
  143. backtrace.PrintTitle(message);
  144. message << "\nwas already present in the glob cache but the directory\n"
  145. "contents have changed during the configuration run.\n";
  146. message << "Matching glob expressions:";
  147. for (auto const& bt : value.Backtraces) {
  148. message << "\n " << std::get<0>(bt);
  149. std::get<1>(bt).PrintTitle(message);
  150. }
  151. cmSystemTools::Error(message.str().c_str());
  152. } else {
  153. value.Backtraces.emplace_back(variable, backtrace);
  154. }
  155. }