cmCacheManager.cxx 25 KB

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