cmCacheManager.cxx 25 KB

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