cmCacheManager.cxx 15 KB

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