cmGlobVerificationManager.cxx 6.0 KB

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