cmCacheManager.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. #include "stdio.h"
  38. const char* cmCacheManagerTypes[] =
  39. { "BOOL",
  40. "PATH",
  41. "FILEPATH",
  42. "STRING",
  43. "INTERNAL",
  44. "STATIC",
  45. 0
  46. };
  47. cmCacheManager::CacheEntryType cmCacheManager::StringToType(const char* s)
  48. {
  49. int i = 0;
  50. while(cmCacheManagerTypes[i])
  51. {
  52. if(strcmp(s, cmCacheManagerTypes[i]) == 0)
  53. {
  54. return static_cast<CacheEntryType>(i);
  55. }
  56. ++i;
  57. }
  58. return STRING;
  59. }
  60. struct CleanUpCacheManager
  61. {
  62. ~CleanUpCacheManager()
  63. {
  64. delete cmCacheManager::GetInstance();
  65. }
  66. void Use() {}
  67. };
  68. CleanUpCacheManager cleanup;
  69. cmCacheManager* cmCacheManager::s_Instance = 0;
  70. cmCacheManager* cmCacheManager::GetInstance()
  71. {
  72. if(!cmCacheManager::s_Instance)
  73. {
  74. cleanup.Use();
  75. cmCacheManager::s_Instance = new cmCacheManager;
  76. }
  77. return cmCacheManager::s_Instance;
  78. }
  79. bool cmCacheManager::LoadCache(cmMakefile* mf)
  80. {
  81. return this->LoadCache(mf->GetHomeOutputDirectory());
  82. }
  83. bool cmCacheManager::LoadCache(const char* path)
  84. {
  85. return this->LoadCache(path,true);
  86. }
  87. bool cmCacheManager::LoadCache(const char* path,
  88. bool internal)
  89. {
  90. std::set<std::string> emptySet;
  91. return this->LoadCache(path, internal, emptySet, emptySet);
  92. }
  93. bool cmCacheManager::LoadCache(const char* path,
  94. bool internal,
  95. std::set<std::string>& excludes,
  96. std::set<std::string>& includes)
  97. {
  98. std::string cacheFile = path;
  99. cacheFile += "/CMakeCache.txt";
  100. // clear the old cache, if we are reading in internal values
  101. if ( internal )
  102. {
  103. m_Cache.clear();
  104. }
  105. std::ifstream fin(cacheFile.c_str());
  106. if(!fin)
  107. {
  108. return false;
  109. }
  110. const int bsize = 4096;
  111. char buffer[bsize];
  112. char *realbuffer;
  113. // input line is: key:type=value
  114. cmRegularExpression reg("^([^:]*):([^=]*)=(.*[^\t ]|[\t ]*)[\t ]*$");
  115. // input line is: "key":type=value
  116. cmRegularExpression regQuoted("^\"([^\"]*)\":([^=]*)=(.*[^\t ]|[\t ]*)[\t ]*$");
  117. std::set<std::string>::const_iterator iter;
  118. std::string entryKey;
  119. while(fin)
  120. {
  121. // Format is key:type=value
  122. CacheEntry e;
  123. fin.getline(buffer, bsize);
  124. realbuffer = buffer;
  125. while(*realbuffer != '0' &&
  126. (*realbuffer == ' ' ||
  127. *realbuffer == '\t' ||
  128. *realbuffer == '\n'))
  129. {
  130. realbuffer++;
  131. }
  132. // skip blank lines and comment lines
  133. if(realbuffer[0] == '#' || realbuffer[0] == 0)
  134. {
  135. continue;
  136. }
  137. while(realbuffer[0] == '/')
  138. {
  139. e.m_HelpString += &realbuffer[2];
  140. fin.getline(realbuffer, bsize);
  141. if(!fin)
  142. {
  143. continue;
  144. }
  145. }
  146. if(regQuoted.find(realbuffer))
  147. {
  148. entryKey = regQuoted.match(1);
  149. if ( excludes.find(entryKey) == excludes.end() )
  150. {
  151. e.m_Type = cmCacheManager::StringToType(regQuoted.match(2).c_str());
  152. // Load internal values if internal is set.
  153. // If the entry is not internal to the cache being loaded
  154. // or if it is in the list of internal entries to be
  155. // imported, load it.
  156. if ( internal || (e.m_Type != INTERNAL) ||
  157. (includes.find(entryKey) != includes.end()) )
  158. {
  159. // If we are loading the cache from another project,
  160. // make all loaded entries internal so that it is
  161. // not visible in the gui
  162. if (!internal)
  163. {
  164. e.m_HelpString = "DO NOT EDIT, ";
  165. e.m_HelpString += entryKey;
  166. e.m_HelpString += " loaded from external file. "
  167. "To change this value edit this file: ";
  168. e.m_HelpString += path;
  169. e.m_HelpString += "/CMakeCache.txt" ;
  170. }
  171. e.m_Value = regQuoted.match(3);
  172. m_Cache[entryKey] = e;
  173. }
  174. }
  175. }
  176. else if (reg.find(realbuffer))
  177. {
  178. entryKey = reg.match(1);
  179. if ( excludes.find(entryKey) == excludes.end() )
  180. {
  181. e.m_Type = cmCacheManager::StringToType(reg.match(2).c_str());
  182. // only load internal values if internal is set
  183. // Load internal values if internal is set.
  184. // If the entry is not internal to the cache being loaded
  185. // or if it is in the list of internal entries to be
  186. // imported, load it.
  187. if ( internal || (e.m_Type != INTERNAL) ||
  188. (includes.find(entryKey) != includes.end()) )
  189. {
  190. // If we are loading the cache from another project,
  191. // make all loaded entries internal so that it is
  192. // not visible in the gui
  193. if (!internal)
  194. {
  195. e.m_Type = INTERNAL;
  196. e.m_HelpString = "DO NOT EDIT, ";
  197. e.m_HelpString += entryKey;
  198. e.m_HelpString += " loaded from external file. "
  199. "To change this value edit this file: ";
  200. e.m_HelpString += path;
  201. e.m_HelpString += "/CMakeCache.txt";
  202. }
  203. e.m_Value = reg.match(3);
  204. m_Cache[entryKey] = e;
  205. }
  206. }
  207. }
  208. else
  209. {
  210. cmSystemTools::Error("Parse error in cache file ", cacheFile.c_str(),
  211. ". Offending entry: ", realbuffer);
  212. }
  213. }
  214. // if CMAKE version not found in the list file
  215. // add them as version 0.0
  216. if(!this->GetCacheValue("CMAKE_CACHE_MINOR_VERSION"))
  217. {
  218. this->AddCacheEntry("CMAKE_CACHE_MINOR_VERSION", "0",
  219. "Minor version of cmake used to create the "
  220. "current loaded cache", cmCacheManager::INTERNAL);
  221. this->AddCacheEntry("CMAKE_CACHE_MAJOR_VERSION", "0",
  222. "Major version of cmake used to create the "
  223. "current loaded cache", cmCacheManager::INTERNAL);
  224. }
  225. if ( internal && this->GetCacheValue("CMAKE_CACHEFILE_DIR") )
  226. {
  227. std::string currentcwd = path;
  228. std::string oldcwd = this->GetCacheValue("CMAKE_CACHEFILE_DIR");
  229. if ( currentcwd[0] >= 'A' && currentcwd[0] <= 'Z' &&
  230. currentcwd[1] == ':' )
  231. {
  232. currentcwd[0] = currentcwd[0] - 'A' + 'a';
  233. }
  234. if ( oldcwd[0] >= 'A' && oldcwd[0] <= 'Z' &&
  235. oldcwd[1] == ':' )
  236. {
  237. oldcwd[0] = oldcwd[0] - 'A' + 'a';
  238. }
  239. cmSystemTools::ConvertToUnixSlashes(currentcwd);
  240. if(cmSystemTools::CollapseFullPath(oldcwd.c_str())
  241. != cmSystemTools::CollapseFullPath(currentcwd.c_str()))
  242. {
  243. std::string message =
  244. std::string("The current CMakeCache.txt directory ") +
  245. currentcwd + std::string(" is different than the directory ") +
  246. std::string(this->GetCacheValue("CMAKE_CACHEFILE_DIR")) +
  247. std::string(" where CMackeCache.txt was created. This may result "
  248. "in binaries being created in the wrong place. If you "
  249. "are not sure, reedit the CMakeCache.txt");
  250. cmSystemTools::Error(message.c_str());
  251. }
  252. }
  253. return true;
  254. }
  255. void cmCacheManager::DefineCache(cmMakefile *mf)
  256. {
  257. if (!mf)
  258. {
  259. return;
  260. }
  261. // add definition to the makefile
  262. for( std::map<cmStdString, CacheEntry>::const_iterator i = m_Cache.begin();
  263. i != m_Cache.end(); ++i)
  264. {
  265. const CacheEntry& ce = (*i).second;
  266. mf->AddDefinition((*i).first.c_str(), ce.m_Value.c_str());
  267. }
  268. }
  269. bool cmCacheManager::SaveCache(cmMakefile* mf)
  270. {
  271. return this->SaveCache(mf->GetHomeOutputDirectory());
  272. }
  273. bool cmCacheManager::SaveCache(const char* path)
  274. {
  275. std::string cacheFile = path;
  276. cacheFile += "/CMakeCache.txt";
  277. std::string tempFile = cacheFile;
  278. tempFile += ".tmp";
  279. std::ofstream fout(tempFile.c_str());
  280. if(!fout)
  281. {
  282. cmSystemTools::Error("Unable to open cache file for save. ",
  283. cacheFile.c_str());
  284. return false;
  285. }
  286. // before writting the cache, update the version numbers
  287. // to the
  288. char temp[1024];
  289. sprintf(temp, "%d", cmMakefile::GetMinorVersion());
  290. this->AddCacheEntry("CMAKE_CACHE_MINOR_VERSION", temp,
  291. "Minor version of cmake used to create the "
  292. "current loaded cache", cmCacheManager::INTERNAL);
  293. sprintf(temp, "%d", cmMakefile::GetMajorVersion());
  294. this->AddCacheEntry("CMAKE_CACHE_MAJOR_VERSION", temp,
  295. "Major version of cmake used to create the "
  296. "current loaded cache", cmCacheManager::INTERNAL);
  297. // Let us store the current working directory so that if somebody
  298. // Copies it, he will not be surprised
  299. std::string currentcwd = path;
  300. if ( currentcwd[0] >= 'A' && currentcwd[0] <= 'Z' &&
  301. currentcwd[1] == ':' )
  302. {
  303. currentcwd[0] = currentcwd[0] - 'A' + 'a';
  304. }
  305. cmSystemTools::ConvertToUnixSlashes(currentcwd);
  306. this->AddCacheEntry("CMAKE_CACHEFILE_DIR", currentcwd.c_str(),
  307. "This is the directory where this CMakeCahe.txt"
  308. " was created", cmCacheManager::INTERNAL);
  309. fout << "# This is the CMakeCache file.\n"
  310. << "# For build in directory: " << path << "\n"
  311. << "# You can edit this file to change values found and used by cmake.\n"
  312. << "# If you do not want to change any of the values, simply exit the editor.\n"
  313. << "# If you do want to change a value, simply edit, save, and exit the editor.\n"
  314. << "# The syntax for the file is as follows:\n"
  315. << "# KEY:TYPE=VALUE\n"
  316. << "# KEY is the name of a varible in the cache.\n"
  317. << "# TYPE is a hint to GUI's for the type of VALUE, DO NOT EDIT TYPE!.\n"
  318. << "# VALUE is the current value for the KEY.\n\n";
  319. fout << "########################\n";
  320. fout << "# EXTERNAL cache entries\n";
  321. fout << "########################\n";
  322. fout << "\n";
  323. for( std::map<cmStdString, CacheEntry>::const_iterator i = m_Cache.begin();
  324. i != m_Cache.end(); ++i)
  325. {
  326. const CacheEntry& ce = (*i).second;
  327. CacheEntryType t = ce.m_Type;
  328. if(t != INTERNAL)
  329. {
  330. // Format is key:type=value
  331. cmCacheManager::OutputHelpString(fout, ce.m_HelpString);
  332. std::string key;
  333. // support : in key name by double quoting
  334. if((*i).first.find(':') != std::string::npos)
  335. {
  336. key = "\"";
  337. key += i->first;
  338. key += "\"";
  339. }
  340. else
  341. {
  342. key = i->first;
  343. }
  344. fout << key.c_str() << ":"
  345. << cmCacheManagerTypes[t] << "="
  346. << ce.m_Value << "\n\n";
  347. }
  348. }
  349. fout << "\n";
  350. fout << "########################\n";
  351. fout << "# INTERNAL cache entries\n";
  352. fout << "########################\n";
  353. fout << "\n";
  354. for( std::map<cmStdString, CacheEntry>::const_iterator i = m_Cache.begin();
  355. i != m_Cache.end(); ++i)
  356. {
  357. const CacheEntry& ce = (*i).second;
  358. CacheEntryType t = ce.m_Type;
  359. if(t == INTERNAL)
  360. {
  361. // Format is key:type=value
  362. cmCacheManager::OutputHelpString(fout, ce.m_HelpString);
  363. std::string key;
  364. // support : in key name by double quoting
  365. if((*i).first.find(':') != std::string::npos)
  366. {
  367. key = "\"";
  368. key += i->first;
  369. key += "\"";
  370. }
  371. else
  372. {
  373. key = i->first;
  374. }
  375. fout << key.c_str() << ":"
  376. << cmCacheManagerTypes[t] << "="
  377. << ce.m_Value << "\n";
  378. }
  379. }
  380. fout << "\n";
  381. fout.close();
  382. cmSystemTools::CopyFileIfDifferent(tempFile.c_str(),
  383. cacheFile.c_str());
  384. cmSystemTools::RemoveFile(tempFile.c_str());
  385. return true;
  386. }
  387. void cmCacheManager::OutputHelpString(std::ofstream& fout,
  388. const std::string& helpString)
  389. {
  390. std::string::size_type end = helpString.size();
  391. if(end == 0)
  392. {
  393. return;
  394. }
  395. std::string oneLine;
  396. std::string::size_type pos = 0;
  397. std::string::size_type nextBreak = 60;
  398. bool done = false;
  399. while(!done)
  400. {
  401. if(nextBreak >= end)
  402. {
  403. nextBreak = end;
  404. done = true;
  405. }
  406. else
  407. {
  408. while(nextBreak < end && helpString[nextBreak] != ' ')
  409. {
  410. nextBreak++;
  411. }
  412. }
  413. oneLine = helpString.substr(pos, nextBreak - pos);
  414. fout << "//" << oneLine.c_str() << "\n";
  415. pos = nextBreak;
  416. nextBreak += 60;
  417. }
  418. }
  419. void cmCacheManager::RemoveCacheEntry(const char* key)
  420. {
  421. if(m_Cache.count(key))
  422. {
  423. m_Cache.erase(key);
  424. }
  425. else
  426. {
  427. std::cerr << "Failed to remove entry" << std::endl;
  428. }
  429. }
  430. cmCacheManager::CacheEntry *cmCacheManager::GetCacheEntry(const char* key)
  431. {
  432. if(m_Cache.count(key))
  433. {
  434. return &(m_Cache.find(key)->second);
  435. }
  436. return 0;
  437. }
  438. const char* cmCacheManager::GetCacheValue(const char* key) const
  439. {
  440. if(m_Cache.count(key))
  441. {
  442. return m_Cache.find(key)->second.m_Value.c_str();
  443. }
  444. return 0;
  445. }
  446. void cmCacheManager::PrintCache(std::ostream& out) const
  447. {
  448. out << "=================================================" << std::endl;
  449. out << "CMakeCache Contents:" << std::endl;
  450. for(std::map<cmStdString, CacheEntry>::const_iterator i = m_Cache.begin();
  451. i != m_Cache.end(); ++i)
  452. {
  453. if((*i).second.m_Type != INTERNAL)
  454. {
  455. out << (*i).first.c_str() << " = " << (*i).second.m_Value.c_str() << std::endl;
  456. }
  457. }
  458. out << "\n\n";
  459. out << "To change values in the CMakeCache, \nedit CMakeCache.txt in your output directory.\n";
  460. out << "=================================================" << std::endl;
  461. }
  462. void cmCacheManager::AddCacheEntry(const char* key,
  463. const char* value,
  464. const char* helpString,
  465. CacheEntryType type)
  466. {
  467. CacheEntry e;
  468. e.m_Value = value;
  469. e.m_Type = type;
  470. // make sure we only use unix style paths
  471. if(type == FILEPATH || type == PATH)
  472. {
  473. cmSystemTools::ConvertToUnixSlashes(e.m_Value);
  474. }
  475. e.m_HelpString = helpString;
  476. m_Cache[key] = e;
  477. }
  478. void cmCacheManager::AddCacheEntry(const char* key, bool v,
  479. const char* helpString)
  480. {
  481. if(v)
  482. {
  483. this->AddCacheEntry(key, "ON", helpString, cmCacheManager::BOOL);
  484. }
  485. else
  486. {
  487. this->AddCacheEntry(key, "OFF", helpString, cmCacheManager::BOOL);
  488. }
  489. }