cmLoadCacheCommand.cxx 4.8 KB

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