cmLoadCacheCommand.cxx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 "cmLoadCacheCommand.h"
  4. #include "cmsys/FStream.hxx"
  5. #include "cmMakefile.h"
  6. #include "cmStateTypes.h"
  7. #include "cmSystemTools.h"
  8. #include "cmake.h"
  9. class cmExecutionStatus;
  10. // cmLoadCacheCommand
  11. bool cmLoadCacheCommand::InitialPass(std::vector<std::string> const& args,
  12. cmExecutionStatus&)
  13. {
  14. if (args.empty()) {
  15. this->SetError("called with wrong number of arguments.");
  16. }
  17. if (args.size() >= 2 && args[1] == "READ_WITH_PREFIX") {
  18. return this->ReadWithPrefix(args);
  19. }
  20. // Cache entries to be excluded from the import list.
  21. // If this set is empty, all cache entries are brought in
  22. // and they can not be overridden.
  23. bool excludeFiles = false;
  24. std::set<std::string> excludes;
  25. for (std::string const& arg : args) {
  26. if (excludeFiles) {
  27. excludes.insert(arg);
  28. }
  29. if (arg == "EXCLUDE") {
  30. excludeFiles = true;
  31. }
  32. if (excludeFiles && (arg == "INCLUDE_INTERNALS")) {
  33. break;
  34. }
  35. }
  36. // Internal cache entries to be imported.
  37. // If this set is empty, no internal cache entries are
  38. // brought in.
  39. bool includeFiles = false;
  40. std::set<std::string> includes;
  41. for (std::string const& arg : args) {
  42. if (includeFiles) {
  43. includes.insert(arg);
  44. }
  45. if (arg == "INCLUDE_INTERNALS") {
  46. includeFiles = true;
  47. }
  48. if (includeFiles && (arg == "EXCLUDE")) {
  49. break;
  50. }
  51. }
  52. // Loop over each build directory listed in the arguments. Each
  53. // directory has a cache file.
  54. for (std::string const& arg : args) {
  55. if ((arg == "EXCLUDE") || (arg == "INCLUDE_INTERNALS")) {
  56. break;
  57. }
  58. this->Makefile->GetCMakeInstance()->LoadCache(arg, false, excludes,
  59. includes);
  60. }
  61. return true;
  62. }
  63. bool cmLoadCacheCommand::ReadWithPrefix(std::vector<std::string> const& args)
  64. {
  65. // Make sure we have a prefix.
  66. if (args.size() < 3) {
  67. this->SetError("READ_WITH_PREFIX form must specify a prefix.");
  68. return false;
  69. }
  70. // Make sure the cache file exists.
  71. std::string cacheFile = args[0] + "/CMakeCache.txt";
  72. if (!cmSystemTools::FileExists(cacheFile)) {
  73. std::string e = "Cannot load cache file from " + cacheFile;
  74. this->SetError(e);
  75. return false;
  76. }
  77. // Prepare the table of variables to read.
  78. this->Prefix = args[2];
  79. this->VariablesToRead.insert(args.begin() + 3, args.end());
  80. // Read the cache file.
  81. cmsys::ifstream fin(cacheFile.c_str());
  82. // This is a big hack read loop to overcome a buggy ifstream
  83. // implementation on HP-UX. This should work on all platforms even
  84. // for small buffer sizes.
  85. const int bufferSize = 4096;
  86. char buffer[bufferSize];
  87. std::string line;
  88. while (fin) {
  89. // Read a block of the file.
  90. fin.read(buffer, bufferSize);
  91. if (fin.gcount()) {
  92. // Parse for newlines directly.
  93. const char* i = buffer;
  94. const char* end = buffer + fin.gcount();
  95. while (i != end) {
  96. const char* begin = i;
  97. while (i != end && *i != '\n') {
  98. ++i;
  99. }
  100. if (i == begin || *(i - 1) != '\r') {
  101. // Include this portion of the line.
  102. line += std::string(begin, i - begin);
  103. } else {
  104. // Include this portion of the line.
  105. // Don't include the \r in a \r\n pair.
  106. line += std::string(begin, i - 1 - begin);
  107. }
  108. if (i != end) {
  109. // Completed a line.
  110. this->CheckLine(line.c_str());
  111. line.clear();
  112. // Skip the newline character.
  113. ++i;
  114. }
  115. }
  116. }
  117. }
  118. if (!line.empty()) {
  119. // Partial last line.
  120. this->CheckLine(line.c_str());
  121. }
  122. return true;
  123. }
  124. void cmLoadCacheCommand::CheckLine(const char* line)
  125. {
  126. // Check one line of the cache file.
  127. std::string var;
  128. std::string value;
  129. cmStateEnums::CacheEntryType type = cmStateEnums::UNINITIALIZED;
  130. if (cmake::ParseCacheEntry(line, var, value, type)) {
  131. // Found a real entry. See if this one was requested.
  132. if (this->VariablesToRead.find(var) != this->VariablesToRead.end()) {
  133. // This was requested. Set this variable locally with the given
  134. // prefix.
  135. var = this->Prefix + var;
  136. if (!value.empty()) {
  137. this->Makefile->AddDefinition(var, value.c_str());
  138. } else {
  139. this->Makefile->RemoveDefinition(var);
  140. }
  141. }
  142. }
  143. }