cmLoadCacheCommand.cxx 5.1 KB

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