cmCacheManager.cxx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  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. cmSystemTools::ReportLastSystemError("");
  309. return false;
  310. }
  311. // before writting the cache, update the version numbers
  312. // to the
  313. char temp[1024];
  314. sprintf(temp, "%d", cmMakefile::GetMinorVersion());
  315. this->AddCacheEntry("CMAKE_CACHE_MINOR_VERSION", temp,
  316. "Minor version of cmake used to create the "
  317. "current loaded cache", cmCacheManager::INTERNAL);
  318. sprintf(temp, "%d", cmMakefile::GetMajorVersion());
  319. this->AddCacheEntry("CMAKE_CACHE_MAJOR_VERSION", temp,
  320. "Major version of cmake used to create the "
  321. "current loaded cache", cmCacheManager::INTERNAL);
  322. this->AddCacheEntry("CMAKE_CACHE_RELEASE_VERSION", cmMakefile::GetReleaseVersion(),
  323. "Major version of cmake used to create the "
  324. "current loaded cache", cmCacheManager::INTERNAL);
  325. // Let us store the current working directory so that if somebody
  326. // Copies it, he will not be surprised
  327. std::string currentcwd = path;
  328. if ( currentcwd[0] >= 'A' && currentcwd[0] <= 'Z' &&
  329. currentcwd[1] == ':' )
  330. {
  331. currentcwd[0] = currentcwd[0] - 'A' + 'a';
  332. }
  333. cmSystemTools::ConvertToUnixSlashes(currentcwd);
  334. this->AddCacheEntry("CMAKE_CACHEFILE_DIR", currentcwd.c_str(),
  335. "This is the directory where this CMakeCahe.txt"
  336. " was created", cmCacheManager::INTERNAL);
  337. fout << "# This is the CMakeCache file.\n"
  338. << "# For build in directory: " << currentcwd << "\n"
  339. << "# You can edit this file to change values found and used by cmake.\n"
  340. << "# If you do not want to change any of the values, simply exit the editor.\n"
  341. << "# If you do want to change a value, simply edit, save, and exit the editor.\n"
  342. << "# The syntax for the file is as follows:\n"
  343. << "# KEY:TYPE=VALUE\n"
  344. << "# KEY is the name of a variable in the cache.\n"
  345. << "# TYPE is a hint to GUI's for the type of VALUE, DO NOT EDIT TYPE!.\n"
  346. << "# VALUE is the current value for the KEY.\n\n";
  347. fout << "########################\n";
  348. fout << "# EXTERNAL cache entries\n";
  349. fout << "########################\n";
  350. fout << "\n";
  351. for( std::map<cmStdString, CacheEntry>::const_iterator i = m_Cache.begin();
  352. i != m_Cache.end(); ++i)
  353. {
  354. const CacheEntry& ce = (*i).second;
  355. CacheEntryType t = ce.m_Type;
  356. if(t == cmCacheManager::UNINITIALIZED || !ce.m_Initialized)
  357. {
  358. /*
  359. // This should be added in, but is not for now.
  360. cmSystemTools::Error("Cache entry \"", (*i).first.c_str(),
  361. "\" is uninitialized");
  362. */
  363. }
  364. else if(t != INTERNAL)
  365. {
  366. // Format is key:type=value
  367. std::map<cmStdString,cmStdString>::const_iterator it =
  368. ce.m_Properties.find("HELPSTRING");
  369. if ( it == ce.m_Properties.end() )
  370. {
  371. cmCacheManager::OutputHelpString(fout, "Missing description");
  372. }
  373. else
  374. {
  375. cmCacheManager::OutputHelpString(fout, it->second);
  376. }
  377. std::string key;
  378. // support : in key name by double quoting
  379. if((*i).first.find(':') != std::string::npos ||
  380. (*i).first.find("//") == 0)
  381. {
  382. key = "\"";
  383. key += i->first;
  384. key += "\"";
  385. }
  386. else
  387. {
  388. key = i->first;
  389. }
  390. fout << key.c_str() << ":"
  391. << cmCacheManagerTypes[t] << "=";
  392. // if value has trailing space or tab, enclose it in single quotes
  393. if (ce.m_Value.size() &&
  394. (ce.m_Value[ce.m_Value.size() - 1] == ' ' ||
  395. ce.m_Value[ce.m_Value.size() - 1] == '\t'))
  396. {
  397. fout << '\'' << ce.m_Value << '\'';
  398. }
  399. else
  400. {
  401. fout << ce.m_Value;
  402. }
  403. fout << "\n\n";
  404. }
  405. }
  406. fout << "\n";
  407. fout << "########################\n";
  408. fout << "# INTERNAL cache entries\n";
  409. fout << "########################\n";
  410. fout << "\n";
  411. for( cmCacheManager::CacheIterator i = this->NewIterator();
  412. !i.IsAtEnd(); i.Next())
  413. {
  414. if ( !i.Initialized() )
  415. {
  416. continue;
  417. }
  418. CacheEntryType t = i.GetType();
  419. bool advanced = i.PropertyExists("ADVANCED");
  420. if ( advanced )
  421. {
  422. // Format is key:type=value
  423. std::string key;
  424. std::string rkey = i.GetName();
  425. std::string helpstring;
  426. // If this is advanced variable, we have to do some magic for
  427. // backward compatibility
  428. helpstring = "Advanced flag for variable: ";
  429. helpstring += i.GetName();
  430. rkey += "-ADVANCED";
  431. cmCacheManager::OutputHelpString(fout, helpstring.c_str());
  432. // support : in key name by double quoting
  433. if(rkey.find(':') != std::string::npos ||
  434. rkey.find("//") == 0)
  435. {
  436. key = "\"";
  437. key += rkey;
  438. key += "\"";
  439. }
  440. else
  441. {
  442. key = rkey;
  443. }
  444. fout << key.c_str() << ":INTERNAL="
  445. << (i.GetPropertyAsBool("ADVANCED") ? "1" : "0") << "\n";
  446. }
  447. bool modified = i.PropertyExists("MODIFIED");
  448. if ( modified )
  449. {
  450. // Format is key:type=value
  451. std::string key;
  452. std::string rkey = i.GetName();
  453. std::string helpstring;
  454. // If this is advanced variable, we have to do some magic for
  455. // backward compatibility
  456. helpstring = "Modified flag for variable: ";
  457. helpstring += i.GetName();
  458. rkey += "-MODIFIED";
  459. cmCacheManager::OutputHelpString(fout, helpstring.c_str());
  460. // support : in key name by double quoting
  461. if(rkey.find(':') != std::string::npos ||
  462. rkey.find("//") == 0)
  463. {
  464. key = "\"";
  465. key += rkey;
  466. key += "\"";
  467. }
  468. else
  469. {
  470. key = rkey;
  471. }
  472. fout << key.c_str() << ":INTERNAL="
  473. << (i.GetPropertyAsBool("MODIFIED") ? "1" : "0") << "\n";
  474. }
  475. if(t == cmCacheManager::INTERNAL)
  476. {
  477. // Format is key:type=value
  478. std::string key;
  479. std::string rkey = i.GetName();
  480. std::string helpstring;
  481. const char* hs = i.GetProperty("HELPSTRING");
  482. if ( hs )
  483. {
  484. helpstring = i.GetProperty("HELPSTRING");
  485. }
  486. else
  487. {
  488. helpstring = "";
  489. }
  490. cmCacheManager::OutputHelpString(fout, helpstring.c_str());
  491. // support : in key name by double quoting
  492. if(rkey.find(':') != std::string::npos ||
  493. rkey.find("//") == 0)
  494. {
  495. key = "\"";
  496. key += rkey;
  497. key += "\"";
  498. }
  499. else
  500. {
  501. key = rkey;
  502. }
  503. fout << key.c_str() << ":"
  504. << cmCacheManagerTypes[t] << "=";
  505. // if value has trailing space or tab, enclose it in single quotes
  506. std::string value = i.GetValue();
  507. if (value.size() &&
  508. (value[value.size() - 1] == ' ' ||
  509. value[value.size() - 1] == '\t'))
  510. {
  511. fout << '\'' << value << '\'';
  512. }
  513. else
  514. {
  515. fout << value;
  516. }
  517. fout << "\n";
  518. }
  519. }
  520. fout << "\n";
  521. fout.close();
  522. cmSystemTools::CopyFileIfDifferent(tempFile.c_str(),
  523. cacheFile.c_str());
  524. cmSystemTools::RemoveFile(tempFile.c_str());
  525. std::string checkCacheFile = path;
  526. checkCacheFile += "/cmake.check_cache";
  527. std::ofstream checkCache(checkCacheFile.c_str());
  528. if(!checkCache)
  529. {
  530. cmSystemTools::Error("Unable to open check cache file for write. ",
  531. checkCacheFile.c_str());
  532. return false;
  533. }
  534. checkCache << "# This file is generated by cmake for dependency checking of the CMakeCache.txt file\n";
  535. return true;
  536. }
  537. bool cmCacheManager::DeleteCache(const char* path)
  538. {
  539. std::string cacheFile = path;
  540. cacheFile += "/CMakeCache.txt";
  541. cmSystemTools::RemoveFile(cacheFile.c_str());
  542. return true;
  543. }
  544. void cmCacheManager::OutputHelpString(std::ofstream& fout,
  545. const std::string& helpString)
  546. {
  547. std::string::size_type end = helpString.size();
  548. if(end == 0)
  549. {
  550. return;
  551. }
  552. std::string oneLine;
  553. std::string::size_type pos = 0;
  554. std::string::size_type nextBreak = 60;
  555. bool done = false;
  556. while(!done)
  557. {
  558. if(nextBreak >= end)
  559. {
  560. nextBreak = end;
  561. done = true;
  562. }
  563. else
  564. {
  565. while(nextBreak < end && helpString[nextBreak] != ' ')
  566. {
  567. nextBreak++;
  568. }
  569. }
  570. oneLine = helpString.substr(pos, nextBreak - pos);
  571. fout << "//" << oneLine.c_str() << "\n";
  572. pos = nextBreak;
  573. nextBreak += 60;
  574. }
  575. }
  576. void cmCacheManager::RemoveCacheEntry(const char* key)
  577. {
  578. CacheEntryMap::iterator i = m_Cache.find(key);
  579. if(i != m_Cache.end())
  580. {
  581. m_Cache.erase(i);
  582. }
  583. else
  584. {
  585. std::cerr << "Failed to remove entry:" << key << std::endl;
  586. }
  587. }
  588. cmCacheManager::CacheEntry *cmCacheManager::GetCacheEntry(const char* key)
  589. {
  590. CacheEntryMap::iterator i = m_Cache.find(key);
  591. if(i != m_Cache.end())
  592. {
  593. return &i->second;
  594. }
  595. return 0;
  596. }
  597. cmCacheManager::CacheIterator cmCacheManager::GetCacheIterator(const char *key)
  598. {
  599. return CacheIterator(*this, key);
  600. }
  601. const char* cmCacheManager::GetCacheValue(const char* key) const
  602. {
  603. CacheEntryMap::const_iterator i = m_Cache.find(key);
  604. if(i != m_Cache.end() &&
  605. i->second.m_Initialized)
  606. {
  607. return i->second.m_Value.c_str();
  608. }
  609. return 0;
  610. }
  611. void cmCacheManager::PrintCache(std::ostream& out) const
  612. {
  613. out << "=================================================" << std::endl;
  614. out << "CMakeCache Contents:" << std::endl;
  615. for(std::map<cmStdString, CacheEntry>::const_iterator i = m_Cache.begin();
  616. i != m_Cache.end(); ++i)
  617. {
  618. if((*i).second.m_Type != INTERNAL)
  619. {
  620. out << (*i).first.c_str() << " = " << (*i).second.m_Value.c_str() << std::endl;
  621. }
  622. }
  623. out << "\n\n";
  624. out << "To change values in the CMakeCache, \nedit CMakeCache.txt in your output directory.\n";
  625. out << "=================================================" << std::endl;
  626. }
  627. void cmCacheManager::AddCacheEntry(const char* key,
  628. const char* value,
  629. const char* helpString,
  630. CacheEntryType type)
  631. {
  632. CacheEntry& e = m_Cache[key];
  633. if ( value )
  634. {
  635. e.m_Value = value;
  636. e.m_Initialized = true;
  637. }
  638. else
  639. {
  640. e.m_Value = "";
  641. }
  642. e.m_Type = type;
  643. // make sure we only use unix style paths
  644. if(type == FILEPATH || type == PATH)
  645. {
  646. cmSystemTools::ConvertToUnixSlashes(e.m_Value);
  647. }
  648. if ( helpString )
  649. {
  650. e.m_Properties["HELPSTRING"] = helpString;
  651. }
  652. else
  653. {
  654. e.m_Properties["HELPSTRING"] = "(This variable does not exists and should not be used)";
  655. }
  656. m_Cache[key] = e;
  657. }
  658. void cmCacheManager::AddCacheEntry(const char* key, bool v,
  659. const char* helpString)
  660. {
  661. if(v)
  662. {
  663. this->AddCacheEntry(key, "ON", helpString, cmCacheManager::BOOL);
  664. }
  665. else
  666. {
  667. this->AddCacheEntry(key, "OFF", helpString, cmCacheManager::BOOL);
  668. }
  669. }
  670. bool cmCacheManager::CacheIterator::IsAtEnd() const
  671. {
  672. return m_Position == m_Container.m_Cache.end();
  673. }
  674. void cmCacheManager::CacheIterator::Begin()
  675. {
  676. m_Position = m_Container.m_Cache.begin();
  677. }
  678. bool cmCacheManager::CacheIterator::Find(const char* key)
  679. {
  680. m_Position = m_Container.m_Cache.find(key);
  681. return !this->IsAtEnd();
  682. }
  683. void cmCacheManager::CacheIterator::Next()
  684. {
  685. if (!this->IsAtEnd())
  686. {
  687. ++m_Position;
  688. }
  689. }
  690. void cmCacheManager::CacheIterator::SetValue(const char* value)
  691. {
  692. if (this->IsAtEnd())
  693. {
  694. return;
  695. }
  696. CacheEntry* entry = &this->GetEntry();
  697. if ( value )
  698. {
  699. entry->m_Value = value;
  700. entry->m_Initialized = true;
  701. }
  702. else
  703. {
  704. entry->m_Value = "";
  705. }
  706. }
  707. const char* cmCacheManager::CacheIterator::GetProperty(const char* property) const
  708. {
  709. // make sure it is not at the end
  710. if (this->IsAtEnd())
  711. {
  712. return 0;
  713. }
  714. if ( !strcmp(property, "TYPE") || !strcmp(property, "VALUE") )
  715. {
  716. cmSystemTools::Error("Property \"", property,
  717. "\" cannot be accessed through the GetProperty()");
  718. return 0;
  719. }
  720. const CacheEntry* ent = &this->GetEntry();
  721. std::map<cmStdString,cmStdString>::const_iterator it =
  722. ent->m_Properties.find(property);
  723. if ( it == ent->m_Properties.end() )
  724. {
  725. return 0;
  726. }
  727. return it->second.c_str();
  728. }
  729. void cmCacheManager::CacheIterator::SetProperty(const char* p, const char* v)
  730. {
  731. // make sure it is not at the end
  732. if (this->IsAtEnd())
  733. {
  734. return;
  735. }
  736. if ( !strcmp(p, "TYPE") || !strcmp(p, "VALUE") )
  737. {
  738. cmSystemTools::Error("Property \"", p,
  739. "\" cannot be accessed through the SetProperty()");
  740. return;
  741. }
  742. CacheEntry* ent = &this->GetEntry();
  743. ent->m_Properties[p] = v;
  744. }
  745. bool cmCacheManager::CacheIterator::GetValueAsBool() const
  746. {
  747. return cmSystemTools::IsOn(this->GetEntry().m_Value.c_str());
  748. }
  749. bool cmCacheManager::CacheIterator::GetPropertyAsBool(const char* property) const
  750. {
  751. // make sure it is not at the end
  752. if (this->IsAtEnd())
  753. {
  754. return false;
  755. }
  756. if ( !strcmp(property, "TYPE") || !strcmp(property, "VALUE") )
  757. {
  758. cmSystemTools::Error("Property \"", property,
  759. "\" cannot be accessed through the GetPropertyAsBool()");
  760. return false;
  761. }
  762. const CacheEntry* ent = &this->GetEntry();
  763. std::map<cmStdString,cmStdString>::const_iterator it =
  764. ent->m_Properties.find(property);
  765. if ( it == ent->m_Properties.end() )
  766. {
  767. return false;
  768. }
  769. return cmSystemTools::IsOn(it->second.c_str());
  770. }
  771. void cmCacheManager::CacheIterator::SetProperty(const char* p, bool v)
  772. {
  773. // make sure it is not at the end
  774. if (this->IsAtEnd())
  775. {
  776. return;
  777. }
  778. if ( !strcmp(p, "TYPE") || !strcmp(p, "VALUE") )
  779. {
  780. cmSystemTools::Error("Property \"", p,
  781. "\" cannot be accessed through the SetProperty()");
  782. return;
  783. }
  784. CacheEntry* ent = &this->GetEntry();
  785. ent->m_Properties[p] = v ? "ON" : "OFF";
  786. }
  787. bool cmCacheManager::CacheIterator::PropertyExists(const char* property) const
  788. {
  789. // make sure it is not at the end
  790. if (this->IsAtEnd())
  791. {
  792. return false;
  793. }
  794. if ( !strcmp(property, "TYPE") || !strcmp(property, "VALUE") )
  795. {
  796. cmSystemTools::Error("Property \"", property,
  797. "\" cannot be accessed through the PropertyExists()");
  798. return false;
  799. }
  800. const CacheEntry* ent = &this->GetEntry();
  801. std::map<cmStdString,cmStdString>::const_iterator it =
  802. ent->m_Properties.find(property);
  803. if ( it == ent->m_Properties.end() )
  804. {
  805. return false;
  806. }
  807. return true;
  808. }