cmCacheManager.cxx 25 KB

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