cmCacheManager.cxx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html 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 <cmsys/RegularExpression.hxx>
  18. #if defined(_WIN32) || defined(__CYGWIN__)
  19. # include <windows.h>
  20. #endif // _WIN32
  21. const char* cmCacheManagerTypes[] =
  22. { "BOOL",
  23. "PATH",
  24. "FILEPATH",
  25. "STRING",
  26. "INTERNAL",
  27. "STATIC",
  28. "UNINITIALIZED",
  29. 0
  30. };
  31. const char* cmCacheManager::TypeToString(cmCacheManager::CacheEntryType type)
  32. {
  33. if ( type > 6 || type < 0 )
  34. {
  35. return cmCacheManagerTypes[6];
  36. }
  37. return cmCacheManagerTypes[type];
  38. }
  39. cmCacheManager::CacheEntryType cmCacheManager::StringToType(const char* s)
  40. {
  41. int i = 0;
  42. while(cmCacheManagerTypes[i])
  43. {
  44. if(strcmp(s, cmCacheManagerTypes[i]) == 0)
  45. {
  46. return static_cast<CacheEntryType>(i);
  47. }
  48. ++i;
  49. }
  50. return STRING;
  51. }
  52. bool cmCacheManager::LoadCache(cmMakefile* mf)
  53. {
  54. return this->LoadCache(mf->GetHomeOutputDirectory());
  55. }
  56. bool cmCacheManager::LoadCache(const char* path)
  57. {
  58. return this->LoadCache(path,true);
  59. }
  60. bool cmCacheManager::LoadCache(const char* path,
  61. bool internal)
  62. {
  63. std::set<std::string> emptySet;
  64. return this->LoadCache(path, internal, emptySet, emptySet);
  65. }
  66. bool cmCacheManager::ParseEntry(const char* entry,
  67. std::string& var,
  68. std::string& value)
  69. {
  70. // input line is: key:type=value
  71. cmsys::RegularExpression reg("^([^:]*)=(.*[^\t ]|[\t ]*)[\t ]*$");
  72. // input line is: "key":type=value
  73. cmsys::RegularExpression regQuoted("^\"([^\"]*)\"=(.*[^\t ]|[\t ]*)[\t ]*$");
  74. bool flag = false;
  75. if(regQuoted.find(entry))
  76. {
  77. var = regQuoted.match(1);
  78. value = regQuoted.match(2);
  79. flag = true;
  80. }
  81. else if (reg.find(entry))
  82. {
  83. var = reg.match(1);
  84. value = reg.match(2);
  85. flag = true;
  86. }
  87. // if value is enclosed in single quotes ('foo') then remove them
  88. // it is used to enclose trailing space or tab
  89. if (flag &&
  90. value.size() >= 2 &&
  91. value[0] == '\'' &&
  92. value[value.size() - 1] == '\'')
  93. {
  94. value = value.substr(1,
  95. value.size() - 2);
  96. }
  97. return flag;
  98. }
  99. bool cmCacheManager::ParseEntry(const char* entry,
  100. std::string& var,
  101. std::string& value,
  102. CacheEntryType& type)
  103. {
  104. // input line is: key:type=value
  105. cmsys::RegularExpression reg("^([^:]*):([^=]*)=(.*[^\t ]|[\t ]*)[\t ]*$");
  106. // input line is: "key":type=value
  107. cmsys::RegularExpression regQuoted("^\"([^\"]*)\":([^=]*)=(.*[^\t ]|[\t ]*)[\t ]*$");
  108. bool flag = false;
  109. if(regQuoted.find(entry))
  110. {
  111. var = regQuoted.match(1);
  112. type = cmCacheManager::StringToType(regQuoted.match(2).c_str());
  113. value = regQuoted.match(3);
  114. flag = true;
  115. }
  116. else if (reg.find(entry))
  117. {
  118. var = reg.match(1);
  119. type = cmCacheManager::StringToType(reg.match(2).c_str());
  120. value = reg.match(3);
  121. flag = true;
  122. }
  123. // if value is enclosed in single quotes ('foo') then remove them
  124. // it is used to enclose trailing space or tab
  125. if (flag &&
  126. value.size() >= 2 &&
  127. value[0] == '\'' &&
  128. value[value.size() - 1] == '\'')
  129. {
  130. value = value.substr(1,
  131. value.size() - 2);
  132. }
  133. return flag;
  134. }
  135. bool cmCacheManager::LoadCache(const char* path,
  136. bool internal,
  137. std::set<std::string>& excludes,
  138. std::set<std::string>& includes)
  139. {
  140. std::string cacheFile = path;
  141. cacheFile += "/CMakeCache.txt";
  142. // clear the old cache, if we are reading in internal values
  143. if ( internal )
  144. {
  145. m_Cache.clear();
  146. }
  147. std::ifstream fin(cacheFile.c_str());
  148. if(!fin)
  149. {
  150. return false;
  151. }
  152. const char *realbuffer;
  153. std::string buffer;
  154. std::string entryKey;
  155. while(fin)
  156. {
  157. // Format is key:type=value
  158. CacheEntry e;
  159. cmSystemTools::GetLineFromStream(fin, buffer);
  160. realbuffer = buffer.c_str();
  161. while(*realbuffer != '0' &&
  162. (*realbuffer == ' ' ||
  163. *realbuffer == '\t' ||
  164. *realbuffer == '\n'))
  165. {
  166. realbuffer++;
  167. }
  168. // skip blank lines and comment lines
  169. if(realbuffer[0] == '#' || realbuffer[0] == 0)
  170. {
  171. continue;
  172. }
  173. while(realbuffer[0] == '/' && realbuffer[1] == '/')
  174. {
  175. e.m_Properties["HELPSTRING"] += &realbuffer[2];
  176. cmSystemTools::GetLineFromStream(fin, buffer);
  177. realbuffer = buffer.c_str();
  178. if(!fin)
  179. {
  180. continue;
  181. }
  182. }
  183. if(cmCacheManager::ParseEntry(realbuffer, entryKey, e.m_Value, e.m_Type))
  184. {
  185. if ( excludes.find(entryKey) == excludes.end() )
  186. {
  187. // Load internal values if internal is set.
  188. // If the entry is not internal to the cache being loaded
  189. // or if it is in the list of internal entries to be
  190. // imported, load it.
  191. if ( internal || (e.m_Type != INTERNAL) ||
  192. (includes.find(entryKey) != includes.end()) )
  193. {
  194. // If we are loading the cache from another project,
  195. // make all loaded entries internal so that it is
  196. // not visible in the gui
  197. if (!internal)
  198. {
  199. e.m_Type = INTERNAL;
  200. e.m_Properties["HELPSTRING"] = "DO NOT EDIT, ";
  201. e.m_Properties["HELPSTRING"] += entryKey;
  202. e.m_Properties["HELPSTRING"] += " loaded from external file. "
  203. "To change this value edit this file: ";
  204. e.m_Properties["HELPSTRING"] += path;
  205. e.m_Properties["HELPSTRING"] += "/CMakeCache.txt" ;
  206. }
  207. if ( e.m_Type == cmCacheManager::INTERNAL &&
  208. (entryKey.size() > strlen("-ADVANCED")) &&
  209. strcmp(entryKey.c_str() + (entryKey.size() - strlen("-ADVANCED")),
  210. "-ADVANCED") == 0 )
  211. {
  212. std::string value = e.m_Value;
  213. std::string akey = entryKey.substr(0, (entryKey.size() - strlen("-ADVANCED")));
  214. cmCacheManager::CacheIterator it = this->GetCacheIterator(akey.c_str());
  215. if ( it.IsAtEnd() )
  216. {
  217. e.m_Type = cmCacheManager::UNINITIALIZED;
  218. m_Cache[akey] = e;
  219. }
  220. if (!it.Find(akey.c_str()))
  221. {
  222. cmSystemTools::Error("Internal CMake error when reading cache");
  223. }
  224. it.SetProperty("ADVANCED", value.c_str());
  225. }
  226. else if ( e.m_Type == cmCacheManager::INTERNAL &&
  227. (entryKey.size() > strlen("-MODIFIED")) &&
  228. strcmp(entryKey.c_str() + (entryKey.size() - strlen("-MODIFIED")),
  229. "-MODIFIED") == 0 )
  230. {
  231. std::string value = e.m_Value;
  232. std::string akey = entryKey.substr(0, (entryKey.size() - strlen("-MODIFIED")));
  233. cmCacheManager::CacheIterator it = this->GetCacheIterator(akey.c_str());
  234. if ( it.IsAtEnd() )
  235. {
  236. e.m_Type = cmCacheManager::UNINITIALIZED;
  237. m_Cache[akey] = e;
  238. }
  239. if (!it.Find(akey.c_str()))
  240. {
  241. cmSystemTools::Error("Internal CMake error when reading cache");
  242. }
  243. it.SetProperty("MODIFIED", value.c_str());
  244. }
  245. else
  246. {
  247. e.m_Initialized = true;
  248. m_Cache[entryKey] = e;
  249. }
  250. }
  251. }
  252. }
  253. else
  254. {
  255. cmSystemTools::Error("Parse error in cache file ", cacheFile.c_str(),
  256. ". Offending entry: ", realbuffer);
  257. }
  258. }
  259. // if CMAKE version not found in the list file
  260. // add them as version 0.0
  261. if(!this->GetCacheValue("CMAKE_CACHE_MINOR_VERSION"))
  262. {
  263. this->AddCacheEntry("CMAKE_CACHE_MINOR_VERSION", "0",
  264. "Minor version of cmake used to create the "
  265. "current loaded cache", cmCacheManager::INTERNAL);
  266. this->AddCacheEntry("CMAKE_CACHE_MAJOR_VERSION", "0",
  267. "Major version of cmake used to create the "
  268. "current loaded cache", cmCacheManager::INTERNAL);
  269. }
  270. // check to make sure the cache directory has not
  271. // been moved
  272. if ( internal && this->GetCacheValue("CMAKE_CACHEFILE_DIR") )
  273. {
  274. std::string currentcwd = path;
  275. std::string oldcwd = this->GetCacheValue("CMAKE_CACHEFILE_DIR");
  276. cmSystemTools::ConvertToUnixSlashes(currentcwd);
  277. currentcwd += "/CMakeCache.txt";
  278. oldcwd += "/CMakeCache.txt";
  279. if(!cmSystemTools::SameFile(oldcwd.c_str(), currentcwd.c_str()))
  280. {
  281. std::string message =
  282. std::string("The current CMakeCache.txt directory ") +
  283. currentcwd + std::string(" is different than the directory ") +
  284. std::string(this->GetCacheValue("CMAKE_CACHEFILE_DIR")) +
  285. std::string(" where CMackeCache.txt was created. This may result "
  286. "in binaries being created in the wrong place. If you "
  287. "are not sure, reedit the CMakeCache.txt");
  288. cmSystemTools::Error(message.c_str());
  289. }
  290. }
  291. return true;
  292. }
  293. bool cmCacheManager::SaveCache(cmMakefile* mf)
  294. {
  295. return this->SaveCache(mf->GetHomeOutputDirectory());
  296. }
  297. bool cmCacheManager::SaveCache(const char* path)
  298. {
  299. std::string cacheFile = path;
  300. cacheFile += "/CMakeCache.txt";
  301. std::string tempFile = cacheFile;
  302. tempFile += ".tmp";
  303. std::ofstream fout(tempFile.c_str());
  304. if(!fout)
  305. {
  306. cmSystemTools::Error("Unable to open cache file for save. ",
  307. cacheFile.c_str());
  308. return false;
  309. }
  310. // before writting the cache, update the version numbers
  311. // to the
  312. char temp[1024];
  313. sprintf(temp, "%d", cmMakefile::GetMinorVersion());
  314. this->AddCacheEntry("CMAKE_CACHE_MINOR_VERSION", temp,
  315. "Minor version of cmake used to create the "
  316. "current loaded cache", cmCacheManager::INTERNAL);
  317. sprintf(temp, "%d", cmMakefile::GetMajorVersion());
  318. this->AddCacheEntry("CMAKE_CACHE_MAJOR_VERSION", temp,
  319. "Major version of cmake used to create the "
  320. "current loaded cache", cmCacheManager::INTERNAL);
  321. this->AddCacheEntry("CMAKE_CACHE_RELEASE_VERSION", cmMakefile::GetReleaseVersion(),
  322. "Major version of cmake used to create the "
  323. "current loaded cache", cmCacheManager::INTERNAL);
  324. // Let us store the current working directory so that if somebody
  325. // Copies it, he will not be surprised
  326. std::string currentcwd = path;
  327. if ( currentcwd[0] >= 'A' && currentcwd[0] <= 'Z' &&
  328. currentcwd[1] == ':' )
  329. {
  330. currentcwd[0] = currentcwd[0] - 'A' + 'a';
  331. }
  332. cmSystemTools::ConvertToUnixSlashes(currentcwd);
  333. this->AddCacheEntry("CMAKE_CACHEFILE_DIR", currentcwd.c_str(),
  334. "This is the directory where this CMakeCahe.txt"
  335. " was created", cmCacheManager::INTERNAL);
  336. fout << "# This is the CMakeCache file.\n"
  337. << "# For build in directory: " << currentcwd << "\n"
  338. << "# You can edit this file to change values found and used by cmake.\n"
  339. << "# If you do not want to change any of the values, simply exit the editor.\n"
  340. << "# If you do want to change a value, simply edit, save, and exit the editor.\n"
  341. << "# The syntax for the file is as follows:\n"
  342. << "# KEY:TYPE=VALUE\n"
  343. << "# KEY is the name of a variable in the cache.\n"
  344. << "# TYPE is a hint to GUI's for the type of VALUE, DO NOT EDIT TYPE!.\n"
  345. << "# VALUE is the current value for the KEY.\n\n";
  346. fout << "########################\n";
  347. fout << "# EXTERNAL cache entries\n";
  348. fout << "########################\n";
  349. fout << "\n";
  350. for( std::map<cmStdString, CacheEntry>::const_iterator i = m_Cache.begin();
  351. i != m_Cache.end(); ++i)
  352. {
  353. const CacheEntry& ce = (*i).second;
  354. CacheEntryType t = ce.m_Type;
  355. if(t == cmCacheManager::UNINITIALIZED || !ce.m_Initialized)
  356. {
  357. /*
  358. // This should be added in, but is not for now.
  359. cmSystemTools::Error("Cache entry \"", (*i).first.c_str(),
  360. "\" is uninitialized");
  361. */
  362. }
  363. else if(t != INTERNAL)
  364. {
  365. // Format is key:type=value
  366. std::map<cmStdString,cmStdString>::const_iterator it =
  367. ce.m_Properties.find("HELPSTRING");
  368. if ( it == ce.m_Properties.end() )
  369. {
  370. cmCacheManager::OutputHelpString(fout, "Missing description");
  371. }
  372. else
  373. {
  374. cmCacheManager::OutputHelpString(fout, it->second);
  375. }
  376. std::string key;
  377. // support : in key name by double quoting
  378. if((*i).first.find(':') != std::string::npos ||
  379. (*i).first.find("//") == 0)
  380. {
  381. key = "\"";
  382. key += i->first;
  383. key += "\"";
  384. }
  385. else
  386. {
  387. key = i->first;
  388. }
  389. fout << key.c_str() << ":"
  390. << cmCacheManagerTypes[t] << "=";
  391. // if value has trailing space or tab, enclose it in single quotes
  392. if (ce.m_Value.size() &&
  393. (ce.m_Value[ce.m_Value.size() - 1] == ' ' ||
  394. ce.m_Value[ce.m_Value.size() - 1] == '\t'))
  395. {
  396. fout << '\'' << ce.m_Value << '\'';
  397. }
  398. else
  399. {
  400. fout << ce.m_Value;
  401. }
  402. fout << "\n\n";
  403. }
  404. }
  405. fout << "\n";
  406. fout << "########################\n";
  407. fout << "# INTERNAL cache entries\n";
  408. fout << "########################\n";
  409. fout << "\n";
  410. for( cmCacheManager::CacheIterator i = this->NewIterator();
  411. !i.IsAtEnd(); i.Next())
  412. {
  413. if ( !i.Initialized() )
  414. {
  415. continue;
  416. }
  417. CacheEntryType t = i.GetType();
  418. bool advanced = i.PropertyExists("ADVANCED");
  419. if ( advanced )
  420. {
  421. // Format is key:type=value
  422. std::string key;
  423. std::string rkey = i.GetName();
  424. std::string helpstring;
  425. // If this is advanced variable, we have to do some magic for
  426. // backward compatibility
  427. helpstring = "Advanced flag for variable: ";
  428. helpstring += i.GetName();
  429. rkey += "-ADVANCED";
  430. cmCacheManager::OutputHelpString(fout, helpstring.c_str());
  431. // support : in key name by double quoting
  432. if(rkey.find(':') != std::string::npos ||
  433. rkey.find("//") == 0)
  434. {
  435. key = "\"";
  436. key += rkey;
  437. key += "\"";
  438. }
  439. else
  440. {
  441. key = rkey;
  442. }
  443. fout << key.c_str() << ":INTERNAL="
  444. << (i.GetPropertyAsBool("ADVANCED") ? "1" : "0") << "\n";
  445. }
  446. bool modified = i.PropertyExists("MODIFIED");
  447. if ( modified )
  448. {
  449. // Format is key:type=value
  450. std::string key;
  451. std::string rkey = i.GetName();
  452. std::string helpstring;
  453. // If this is advanced variable, we have to do some magic for
  454. // backward compatibility
  455. helpstring = "Modified flag for variable: ";
  456. helpstring += i.GetName();
  457. rkey += "-MODIFIED";
  458. cmCacheManager::OutputHelpString(fout, helpstring.c_str());
  459. // support : in key name by double quoting
  460. if(rkey.find(':') != std::string::npos ||
  461. rkey.find("//") == 0)
  462. {
  463. key = "\"";
  464. key += rkey;
  465. key += "\"";
  466. }
  467. else
  468. {
  469. key = rkey;
  470. }
  471. fout << key.c_str() << ":INTERNAL="
  472. << (i.GetPropertyAsBool("MODIFIED") ? "1" : "0") << "\n";
  473. }
  474. if(t == cmCacheManager::INTERNAL)
  475. {
  476. // Format is key:type=value
  477. std::string key;
  478. std::string rkey = i.GetName();
  479. std::string helpstring;
  480. const char* hs = i.GetProperty("HELPSTRING");
  481. if ( hs )
  482. {
  483. helpstring = i.GetProperty("HELPSTRING");
  484. }
  485. else
  486. {
  487. helpstring = "";
  488. }
  489. cmCacheManager::OutputHelpString(fout, helpstring.c_str());
  490. // support : in key name by double quoting
  491. if(rkey.find(':') != std::string::npos ||
  492. rkey.find("//") == 0)
  493. {
  494. key = "\"";
  495. key += rkey;
  496. key += "\"";
  497. }
  498. else
  499. {
  500. key = rkey;
  501. }
  502. fout << key.c_str() << ":"
  503. << cmCacheManagerTypes[t] << "=";
  504. // if value has trailing space or tab, enclose it in single quotes
  505. std::string value = i.GetValue();
  506. if (value.size() &&
  507. (value[value.size() - 1] == ' ' ||
  508. value[value.size() - 1] == '\t'))
  509. {
  510. fout << '\'' << value << '\'';
  511. }
  512. else
  513. {
  514. fout << value;
  515. }
  516. fout << "\n";
  517. }
  518. }
  519. fout << "\n";
  520. fout.close();
  521. cmSystemTools::CopyFileIfDifferent(tempFile.c_str(),
  522. cacheFile.c_str());
  523. cmSystemTools::RemoveFile(tempFile.c_str());
  524. std::string checkCacheFile = path;
  525. checkCacheFile += "/cmake.check_cache";
  526. std::ofstream checkCache(checkCacheFile.c_str());
  527. if(!checkCache)
  528. {
  529. cmSystemTools::Error("Unable to open check cache file for write. ",
  530. checkCacheFile.c_str());
  531. return false;
  532. }
  533. checkCache << "# This file is generated by cmake for dependency checking of the CMakeCache.txt file\n";
  534. return true;
  535. }
  536. bool cmCacheManager::DeleteCache(const char* path)
  537. {
  538. std::string cacheFile = path;
  539. cacheFile += "/CMakeCache.txt";
  540. cmSystemTools::RemoveFile(cacheFile.c_str());
  541. return true;
  542. }
  543. void cmCacheManager::OutputHelpString(std::ofstream& fout,
  544. const std::string& helpString)
  545. {
  546. std::string::size_type end = helpString.size();
  547. if(end == 0)
  548. {
  549. return;
  550. }
  551. std::string oneLine;
  552. std::string::size_type pos = 0;
  553. std::string::size_type nextBreak = 60;
  554. bool done = false;
  555. while(!done)
  556. {
  557. if(nextBreak >= end)
  558. {
  559. nextBreak = end;
  560. done = true;
  561. }
  562. else
  563. {
  564. while(nextBreak < end && helpString[nextBreak] != ' ')
  565. {
  566. nextBreak++;
  567. }
  568. }
  569. oneLine = helpString.substr(pos, nextBreak - pos);
  570. fout << "//" << oneLine.c_str() << "\n";
  571. pos = nextBreak;
  572. nextBreak += 60;
  573. }
  574. }
  575. void cmCacheManager::RemoveCacheEntry(const char* key)
  576. {
  577. CacheEntryMap::iterator i = m_Cache.find(key);
  578. if(i != m_Cache.end())
  579. {
  580. m_Cache.erase(i);
  581. }
  582. else
  583. {
  584. std::cerr << "Failed to remove entry:" << key << std::endl;
  585. }
  586. }
  587. cmCacheManager::CacheEntry *cmCacheManager::GetCacheEntry(const char* key)
  588. {
  589. CacheEntryMap::iterator i = m_Cache.find(key);
  590. if(i != m_Cache.end())
  591. {
  592. return &i->second;
  593. }
  594. return 0;
  595. }
  596. cmCacheManager::CacheIterator cmCacheManager::GetCacheIterator(const char *key)
  597. {
  598. return CacheIterator(*this, key);
  599. }
  600. const char* cmCacheManager::GetCacheValue(const char* key) const
  601. {
  602. CacheEntryMap::const_iterator i = m_Cache.find(key);
  603. if(i != m_Cache.end() &&
  604. i->second.m_Initialized)
  605. {
  606. return i->second.m_Value.c_str();
  607. }
  608. return 0;
  609. }
  610. void cmCacheManager::PrintCache(std::ostream& out) const
  611. {
  612. out << "=================================================" << std::endl;
  613. out << "CMakeCache Contents:" << std::endl;
  614. for(std::map<cmStdString, CacheEntry>::const_iterator i = m_Cache.begin();
  615. i != m_Cache.end(); ++i)
  616. {
  617. if((*i).second.m_Type != INTERNAL)
  618. {
  619. out << (*i).first.c_str() << " = " << (*i).second.m_Value.c_str() << std::endl;
  620. }
  621. }
  622. out << "\n\n";
  623. out << "To change values in the CMakeCache, \nedit CMakeCache.txt in your output directory.\n";
  624. out << "=================================================" << std::endl;
  625. }
  626. void cmCacheManager::AddCacheEntry(const char* key,
  627. const char* value,
  628. const char* helpString,
  629. CacheEntryType type)
  630. {
  631. CacheEntry& e = m_Cache[key];
  632. if ( value )
  633. {
  634. e.m_Value = value;
  635. e.m_Initialized = true;
  636. }
  637. else
  638. {
  639. e.m_Value = "";
  640. }
  641. e.m_Type = type;
  642. // make sure we only use unix style paths
  643. if(type == FILEPATH || type == PATH)
  644. {
  645. cmSystemTools::ConvertToUnixSlashes(e.m_Value);
  646. }
  647. if ( helpString )
  648. {
  649. e.m_Properties["HELPSTRING"] = helpString;
  650. }
  651. else
  652. {
  653. e.m_Properties["HELPSTRING"] = "(This variable does not exists and should not be used)";
  654. }
  655. m_Cache[key] = e;
  656. }
  657. void cmCacheManager::AddCacheEntry(const char* key, bool v,
  658. const char* helpString)
  659. {
  660. if(v)
  661. {
  662. this->AddCacheEntry(key, "ON", helpString, cmCacheManager::BOOL);
  663. }
  664. else
  665. {
  666. this->AddCacheEntry(key, "OFF", helpString, cmCacheManager::BOOL);
  667. }
  668. }
  669. bool cmCacheManager::CacheIterator::IsAtEnd() const
  670. {
  671. return m_Position == m_Container.m_Cache.end();
  672. }
  673. void cmCacheManager::CacheIterator::Begin()
  674. {
  675. m_Position = m_Container.m_Cache.begin();
  676. }
  677. bool cmCacheManager::CacheIterator::Find(const char* key)
  678. {
  679. m_Position = m_Container.m_Cache.find(key);
  680. return !this->IsAtEnd();
  681. }
  682. void cmCacheManager::CacheIterator::Next()
  683. {
  684. if (!this->IsAtEnd())
  685. {
  686. ++m_Position;
  687. }
  688. }
  689. void cmCacheManager::CacheIterator::SetValue(const char* value)
  690. {
  691. if (this->IsAtEnd())
  692. {
  693. return;
  694. }
  695. CacheEntry* entry = &this->GetEntry();
  696. if ( value )
  697. {
  698. entry->m_Value = value;
  699. entry->m_Initialized = true;
  700. }
  701. else
  702. {
  703. entry->m_Value = "";
  704. }
  705. }
  706. const char* cmCacheManager::CacheIterator::GetProperty(const char* property) const
  707. {
  708. // make sure it is not at the end
  709. if (this->IsAtEnd())
  710. {
  711. return 0;
  712. }
  713. if ( !strcmp(property, "TYPE") || !strcmp(property, "VALUE") )
  714. {
  715. cmSystemTools::Error("Property \"", property,
  716. "\" cannot be accessed through the GetProperty()");
  717. return 0;
  718. }
  719. const CacheEntry* ent = &this->GetEntry();
  720. std::map<cmStdString,cmStdString>::const_iterator it =
  721. ent->m_Properties.find(property);
  722. if ( it == ent->m_Properties.end() )
  723. {
  724. return 0;
  725. }
  726. return it->second.c_str();
  727. }
  728. void cmCacheManager::CacheIterator::SetProperty(const char* p, const char* v)
  729. {
  730. // make sure it is not at the end
  731. if (this->IsAtEnd())
  732. {
  733. return;
  734. }
  735. if ( !strcmp(p, "TYPE") || !strcmp(p, "VALUE") )
  736. {
  737. cmSystemTools::Error("Property \"", p,
  738. "\" cannot be accessed through the SetProperty()");
  739. return;
  740. }
  741. CacheEntry* ent = &this->GetEntry();
  742. ent->m_Properties[p] = v;
  743. }
  744. bool cmCacheManager::CacheIterator::GetValueAsBool() const
  745. {
  746. return cmSystemTools::IsOn(this->GetEntry().m_Value.c_str());
  747. }
  748. bool cmCacheManager::CacheIterator::GetPropertyAsBool(const char* property) const
  749. {
  750. // make sure it is not at the end
  751. if (this->IsAtEnd())
  752. {
  753. return false;
  754. }
  755. if ( !strcmp(property, "TYPE") || !strcmp(property, "VALUE") )
  756. {
  757. cmSystemTools::Error("Property \"", property,
  758. "\" cannot be accessed through the GetPropertyAsBool()");
  759. return false;
  760. }
  761. const CacheEntry* ent = &this->GetEntry();
  762. std::map<cmStdString,cmStdString>::const_iterator it =
  763. ent->m_Properties.find(property);
  764. if ( it == ent->m_Properties.end() )
  765. {
  766. return false;
  767. }
  768. return cmSystemTools::IsOn(it->second.c_str());
  769. }
  770. void cmCacheManager::CacheIterator::SetProperty(const char* p, bool v)
  771. {
  772. // make sure it is not at the end
  773. if (this->IsAtEnd())
  774. {
  775. return;
  776. }
  777. if ( !strcmp(p, "TYPE") || !strcmp(p, "VALUE") )
  778. {
  779. cmSystemTools::Error("Property \"", p,
  780. "\" cannot be accessed through the SetProperty()");
  781. return;
  782. }
  783. CacheEntry* ent = &this->GetEntry();
  784. ent->m_Properties[p] = v ? "ON" : "OFF";
  785. }
  786. bool cmCacheManager::CacheIterator::PropertyExists(const char* property) const
  787. {
  788. // make sure it is not at the end
  789. if (this->IsAtEnd())
  790. {
  791. return false;
  792. }
  793. if ( !strcmp(property, "TYPE") || !strcmp(property, "VALUE") )
  794. {
  795. cmSystemTools::Error("Property \"", property,
  796. "\" cannot be accessed through the PropertyExists()");
  797. return false;
  798. }
  799. const CacheEntry* ent = &this->GetEntry();
  800. std::map<cmStdString,cmStdString>::const_iterator it =
  801. ent->m_Properties.find(property);
  802. if ( it == ent->m_Properties.end() )
  803. {
  804. return false;
  805. }
  806. return true;
  807. }