cmCacheManager.cxx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2001 Insight Consortium
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. * The name of the Insight Consortium, nor the names of any consortium members,
  17. nor of any contributors, may be used to endorse or promote products derived
  18. from this software without specific prior written permission.
  19. * Modified source versions must be plainly marked as such, and must not be
  20. misrepresented as being the original software.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
  25. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. =========================================================================*/
  32. #include "cmCacheManager.h"
  33. #include "cmSystemTools.h"
  34. #include "cmCacheManager.h"
  35. #include "cmMakefile.h"
  36. #include "cmRegularExpression.h"
  37. const char* cmCacheManagerTypes[] =
  38. { "BOOL",
  39. "PATH",
  40. "FILEPATH",
  41. "STRING",
  42. "INTERNAL",
  43. "STATIC",
  44. 0
  45. };
  46. cmCacheManager::CacheEntryType cmCacheManager::StringToType(const char* s)
  47. {
  48. int i = 0;
  49. while(cmCacheManagerTypes[i])
  50. {
  51. if(strcmp(s, cmCacheManagerTypes[i]) == 0)
  52. {
  53. return static_cast<CacheEntryType>(i);
  54. }
  55. ++i;
  56. }
  57. return STRING;
  58. }
  59. struct CleanUpCacheManager
  60. {
  61. ~CleanUpCacheManager()
  62. {
  63. delete cmCacheManager::GetInstance();
  64. }
  65. void Use() {}
  66. };
  67. CleanUpCacheManager cleanup;
  68. cmCacheManager* cmCacheManager::s_Instance = 0;
  69. cmCacheManager* cmCacheManager::GetInstance()
  70. {
  71. if(!cmCacheManager::s_Instance)
  72. {
  73. cleanup.Use();
  74. cmCacheManager::s_Instance = new cmCacheManager;
  75. }
  76. return cmCacheManager::s_Instance;
  77. }
  78. bool cmCacheManager::LoadCache(cmMakefile* mf)
  79. {
  80. return this->LoadCache(mf->GetHomeOutputDirectory());
  81. }
  82. bool cmCacheManager::LoadCache(const char* path)
  83. {
  84. return this->LoadCache(path,true);
  85. }
  86. bool cmCacheManager::LoadCache(const char* path,
  87. bool internal)
  88. {
  89. std::string cacheFile = path;
  90. cacheFile += "/CMakeCache.txt";
  91. // clear the old cache, if we are reading in internal values
  92. if ( internal )
  93. {
  94. m_Cache.clear();
  95. }
  96. std::ifstream fin(cacheFile.c_str());
  97. if(!fin)
  98. {
  99. return false;
  100. }
  101. const int bsize = 4096;
  102. char buffer[bsize];
  103. // input line is: key:type=value
  104. cmRegularExpression reg("^([^:]*):([^=]*)=(.*)$");
  105. while(fin)
  106. {
  107. // Format is key:type=value
  108. CacheEntry e;
  109. fin.getline(buffer, bsize);
  110. // skip blank lines and comment lines
  111. if(buffer[0] == '#' || buffer[0] == 0)
  112. {
  113. continue;
  114. }
  115. while(buffer[0] == '/')
  116. {
  117. e.m_HelpString += &buffer[2];
  118. fin.getline(buffer, bsize);
  119. if(!fin)
  120. {
  121. continue;
  122. }
  123. }
  124. if(reg.find(buffer))
  125. {
  126. e.m_Type = cmCacheManager::StringToType(reg.match(2).c_str());
  127. // only load internal values if internal is set
  128. if (internal || e.m_Type != INTERNAL)
  129. {
  130. e.m_Value = reg.match(3);
  131. m_Cache[reg.match(1)] = e;
  132. }
  133. }
  134. else
  135. {
  136. cmSystemTools::Error("Parse error in cache file ", cacheFile.c_str());
  137. }
  138. }
  139. return true;
  140. }
  141. void cmCacheManager::DefineCache(cmMakefile *mf)
  142. {
  143. if (!mf)
  144. {
  145. return;
  146. }
  147. // add definition to the makefile
  148. for( std::map<std::string, CacheEntry>::const_iterator i = m_Cache.begin();
  149. i != m_Cache.end(); ++i)
  150. {
  151. const CacheEntry& ce = (*i).second;
  152. mf->AddDefinition((*i).first.c_str(), ce.m_Value.c_str());
  153. }
  154. }
  155. bool cmCacheManager::SaveCache(cmMakefile* mf) const
  156. {
  157. return this->SaveCache(mf->GetHomeOutputDirectory());
  158. }
  159. bool cmCacheManager::SaveCache(const char* path) const
  160. {
  161. std::string cacheFile = path;
  162. cacheFile += "/CMakeCache.txt";
  163. std::string tempFile = cacheFile;
  164. tempFile += ".tmp";
  165. std::ofstream fout(tempFile.c_str());
  166. if(!fout)
  167. {
  168. cmSystemTools::Error("Unable to open cache file for save. ",
  169. cacheFile.c_str());
  170. return false;
  171. }
  172. fout << "# This is the CMakeCache file.\n"
  173. << "# For build in directory: " << path << "\n"
  174. << "# You can edit this file to change values found and used by cmake.\n"
  175. << "# If you do not want to change any of the values, simply exit the editor.\n"
  176. << "# If you do want to change a value, simply edit, save, and exit the editor.\n"
  177. << "# The syntax for the file is as follows:\n"
  178. << "# KEY:TYPE=VALUE\n"
  179. << "# KEY is the name of a varible in the cache.\n"
  180. << "# TYPE is a hint to GUI's for the type of VALUE, DO NOT EDIT TYPE!.\n"
  181. << "# VALUE is the current value for the KEY.\n\n";
  182. fout << "########################\n";
  183. fout << "# EXTERNAL cache entries\n";
  184. fout << "########################\n";
  185. fout << "\n";
  186. for( std::map<std::string, CacheEntry>::const_iterator i = m_Cache.begin();
  187. i != m_Cache.end(); ++i)
  188. {
  189. const CacheEntry& ce = (*i).second;
  190. CacheEntryType t = ce.m_Type;
  191. if(t != INTERNAL)
  192. {
  193. // Format is key:type=value
  194. cmCacheManager::OutputHelpString(fout, ce.m_HelpString);
  195. fout << (*i).first.c_str() << ":"
  196. << cmCacheManagerTypes[t] << "="
  197. << ce.m_Value << "\n\n";
  198. }
  199. }
  200. fout << "\n";
  201. fout << "########################\n";
  202. fout << "# INTERNAL cache entries\n";
  203. fout << "########################\n";
  204. fout << "\n";
  205. for( std::map<std::string, CacheEntry>::const_iterator i = m_Cache.begin();
  206. i != m_Cache.end(); ++i)
  207. {
  208. const CacheEntry& ce = (*i).second;
  209. CacheEntryType t = ce.m_Type;
  210. if(t == INTERNAL)
  211. {
  212. // Format is key:type=value
  213. cmCacheManager::OutputHelpString(fout, ce.m_HelpString);
  214. fout << (*i).first.c_str() << ":"
  215. << cmCacheManagerTypes[t] << "="
  216. << ce.m_Value << "\n";
  217. }
  218. }
  219. fout << "\n";
  220. fout.close();
  221. cmSystemTools::CopyFileIfDifferent(tempFile.c_str(),
  222. cacheFile.c_str());
  223. cmSystemTools::RemoveFile(tempFile.c_str());
  224. return true;
  225. }
  226. void cmCacheManager::OutputHelpString(std::ofstream& fout,
  227. const std::string& helpString)
  228. {
  229. std::string::size_type end = helpString.size();
  230. if(end == 0)
  231. {
  232. return;
  233. }
  234. std::string oneLine;
  235. std::string::size_type pos = 0;
  236. std::string::size_type nextBreak = 60;
  237. bool done = false;
  238. while(!done)
  239. {
  240. if(nextBreak >= end)
  241. {
  242. nextBreak = end;
  243. done = true;
  244. }
  245. else
  246. {
  247. while(nextBreak < end && helpString[nextBreak] != ' ')
  248. {
  249. nextBreak++;
  250. }
  251. }
  252. oneLine = helpString.substr(pos, nextBreak - pos);
  253. fout << "//" << oneLine.c_str() << "\n";
  254. pos = nextBreak;
  255. nextBreak += 60;
  256. }
  257. }
  258. void cmCacheManager::RemoveCacheEntry(const char* key)
  259. {
  260. m_Cache.erase(key);
  261. }
  262. cmCacheManager::CacheEntry *cmCacheManager::GetCacheEntry(const char* key)
  263. {
  264. if(m_Cache.count(key))
  265. {
  266. return &(m_Cache.find(key)->second);
  267. }
  268. return 0;
  269. }
  270. const char* cmCacheManager::GetCacheValue(const char* key) const
  271. {
  272. if(m_Cache.count(key))
  273. {
  274. return m_Cache.find(key)->second.m_Value.c_str();
  275. }
  276. return 0;
  277. }
  278. bool cmCacheManager::IsOn(const char* key) const
  279. {
  280. if(!m_Cache.count(key))
  281. {
  282. return false;
  283. }
  284. const std::string &v = m_Cache.find(key)->second.m_Value;
  285. return cmSystemTools::IsOn(v.c_str());
  286. }
  287. void cmCacheManager::PrintCache(std::ostream& out) const
  288. {
  289. out << "=================================================" << std::endl;
  290. out << "CMakeCache Contents:" << std::endl;
  291. for(std::map<std::string, CacheEntry>::const_iterator i = m_Cache.begin();
  292. i != m_Cache.end(); ++i)
  293. {
  294. if((*i).second.m_Type != INTERNAL)
  295. {
  296. out << (*i).first.c_str() << " = " << (*i).second.m_Value.c_str() << std::endl;
  297. }
  298. }
  299. out << "\n\n";
  300. out << "To change values in the CMakeCache, \nedit CMakeCache.txt in your output directory.\n";
  301. out << "=================================================" << std::endl;
  302. }
  303. void cmCacheManager::AddCacheEntry(const char* key,
  304. const char* value,
  305. const char* helpString,
  306. CacheEntryType type)
  307. {
  308. CacheEntry e;
  309. e.m_Value = value;
  310. e.m_Type = type;
  311. // make sure we only use unix style paths
  312. if(type == FILEPATH || type == PATH)
  313. {
  314. cmSystemTools::ConvertToUnixSlashes(e.m_Value);
  315. }
  316. e.m_HelpString = helpString;
  317. m_Cache[key] = e;
  318. }
  319. void cmCacheManager::AddCacheEntry(const char* key, bool v,
  320. const char* helpString)
  321. {
  322. if(v)
  323. {
  324. this->AddCacheEntry(key, "ON", helpString, cmCacheManager::BOOL);
  325. }
  326. else
  327. {
  328. this->AddCacheEntry(key, "OFF", helpString, cmCacheManager::BOOL);
  329. }
  330. }