cmLoadCacheCommand.cxx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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<std::string> 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<std::string> 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], 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);
  94. return false;
  95. }
  96. // Prepare the table of variables to read.
  97. this->Prefix = args[2];
  98. this->VariablesToRead.insert(args.begin() + 3, args.end());
  99. // Read the cache file.
  100. cmsys::ifstream fin(cacheFile.c_str());
  101. // This is a big hack read loop to overcome a buggy ifstream
  102. // implementation on HP-UX. This should work on all platforms even
  103. // for small buffer sizes.
  104. const int bufferSize = 4096;
  105. char buffer[bufferSize];
  106. std::string line;
  107. while(fin)
  108. {
  109. // Read a block of the file.
  110. fin.read(buffer, bufferSize);
  111. if(fin.gcount())
  112. {
  113. // Parse for newlines directly.
  114. const char* i = buffer;
  115. const char* end = buffer+fin.gcount();
  116. while(i != end)
  117. {
  118. const char* begin = i;
  119. while(i != end && *i != '\n') { ++i; }
  120. if(i == begin || *(i-1) != '\r')
  121. {
  122. // Include this portion of the line.
  123. line += std::string(begin, i-begin);
  124. }
  125. else
  126. {
  127. // Include this portion of the line.
  128. // Don't include the \r in a \r\n pair.
  129. line += std::string(begin, i-1-begin);
  130. }
  131. if(i != end)
  132. {
  133. // Completed a line.
  134. this->CheckLine(line.c_str());
  135. line = "";
  136. // Skip the newline character.
  137. ++i;
  138. }
  139. }
  140. }
  141. }
  142. if(line.length())
  143. {
  144. // Partial last line.
  145. this->CheckLine(line.c_str());
  146. }
  147. return true;
  148. }
  149. //----------------------------------------------------------------------------
  150. void cmLoadCacheCommand::CheckLine(const char* line)
  151. {
  152. // Check one line of the cache file.
  153. std::string var;
  154. std::string value;
  155. cmCacheManager::CacheEntryType type = cmCacheManager::UNINITIALIZED;
  156. if(cmCacheManager::ParseEntry(line, var, value, type))
  157. {
  158. // Found a real entry. See if this one was requested.
  159. if(this->VariablesToRead.find(var) != this->VariablesToRead.end())
  160. {
  161. // This was requested. Set this variable locally with the given
  162. // prefix.
  163. var = this->Prefix + var;
  164. if(value.length())
  165. {
  166. this->Makefile->AddDefinition(var, value.c_str());
  167. }
  168. else
  169. {
  170. this->Makefile->RemoveDefinition(var);
  171. }
  172. }
  173. }
  174. }