cmCacheManager.cxx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCacheManager.h"
  4. #include "cmsys/FStream.hxx"
  5. #include "cmsys/Glob.hxx"
  6. #include <algorithm>
  7. #include <sstream>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <string>
  11. #include "cmGeneratedFileStream.h"
  12. #include "cmMessenger.h"
  13. #include "cmState.h"
  14. #include "cmSystemTools.h"
  15. #include "cmVersion.h"
  16. #include "cmake.h"
  17. cmCacheManager::cmCacheManager()
  18. {
  19. this->CacheMajorVersion = 0;
  20. this->CacheMinorVersion = 0;
  21. }
  22. void cmCacheManager::CleanCMakeFiles(const std::string& path)
  23. {
  24. std::string glob = path;
  25. glob += cmake::GetCMakeFilesDirectory();
  26. glob += "/*.cmake";
  27. cmsys::Glob globIt;
  28. globIt.FindFiles(glob);
  29. std::vector<std::string> files = globIt.GetFiles();
  30. std::for_each(files.begin(), files.end(), cmSystemTools::RemoveFile);
  31. }
  32. bool cmCacheManager::LoadCache(const std::string& path, bool internal,
  33. std::set<std::string>& excludes,
  34. std::set<std::string>& includes)
  35. {
  36. std::string cacheFile = path;
  37. cacheFile += "/CMakeCache.txt";
  38. // clear the old cache, if we are reading in internal values
  39. if (internal) {
  40. this->Cache.clear();
  41. }
  42. if (!cmSystemTools::FileExists(cacheFile)) {
  43. this->CleanCMakeFiles(path);
  44. return false;
  45. }
  46. cmsys::ifstream fin(cacheFile.c_str());
  47. if (!fin) {
  48. return false;
  49. }
  50. const char* realbuffer;
  51. std::string buffer;
  52. std::string entryKey;
  53. unsigned int lineno = 0;
  54. while (fin) {
  55. // Format is key:type=value
  56. std::string helpString;
  57. CacheEntry e;
  58. cmSystemTools::GetLineFromStream(fin, buffer);
  59. lineno++;
  60. realbuffer = buffer.c_str();
  61. while (*realbuffer != '0' &&
  62. (*realbuffer == ' ' || *realbuffer == '\t' || *realbuffer == '\r' ||
  63. *realbuffer == '\n')) {
  64. if (*realbuffer == '\n') {
  65. lineno++;
  66. }
  67. realbuffer++;
  68. }
  69. // skip blank lines and comment lines
  70. if (realbuffer[0] == '#' || realbuffer[0] == 0) {
  71. continue;
  72. }
  73. while (realbuffer[0] == '/' && realbuffer[1] == '/') {
  74. if ((realbuffer[2] == '\\') && (realbuffer[3] == 'n')) {
  75. helpString += "\n";
  76. helpString += &realbuffer[4];
  77. } else {
  78. helpString += &realbuffer[2];
  79. }
  80. cmSystemTools::GetLineFromStream(fin, buffer);
  81. lineno++;
  82. realbuffer = buffer.c_str();
  83. if (!fin) {
  84. continue;
  85. }
  86. }
  87. e.SetProperty("HELPSTRING", helpString.c_str());
  88. if (cmState::ParseCacheEntry(realbuffer, entryKey, e.Value, e.Type)) {
  89. if (excludes.find(entryKey) == excludes.end()) {
  90. // Load internal values if internal is set.
  91. // If the entry is not internal to the cache being loaded
  92. // or if it is in the list of internal entries to be
  93. // imported, load it.
  94. if (internal || (e.Type != cmStateEnums::INTERNAL) ||
  95. (includes.find(entryKey) != includes.end())) {
  96. // If we are loading the cache from another project,
  97. // make all loaded entries internal so that it is
  98. // not visible in the gui
  99. if (!internal) {
  100. e.Type = cmStateEnums::INTERNAL;
  101. helpString = "DO NOT EDIT, ";
  102. helpString += entryKey;
  103. helpString += " loaded from external file. "
  104. "To change this value edit this file: ";
  105. helpString += path;
  106. helpString += "/CMakeCache.txt";
  107. e.SetProperty("HELPSTRING", helpString.c_str());
  108. }
  109. if (!this->ReadPropertyEntry(entryKey, e)) {
  110. e.Initialized = true;
  111. this->Cache[entryKey] = e;
  112. }
  113. }
  114. }
  115. } else {
  116. std::ostringstream error;
  117. error << "Parse error in cache file " << cacheFile;
  118. error << " on line " << lineno << ". Offending entry: " << realbuffer;
  119. cmSystemTools::Error(error.str().c_str());
  120. }
  121. }
  122. this->CacheMajorVersion = 0;
  123. this->CacheMinorVersion = 0;
  124. if (const char* cmajor =
  125. this->GetInitializedCacheValue("CMAKE_CACHE_MAJOR_VERSION")) {
  126. unsigned int v = 0;
  127. if (sscanf(cmajor, "%u", &v) == 1) {
  128. this->CacheMajorVersion = v;
  129. }
  130. if (const char* cminor =
  131. this->GetInitializedCacheValue("CMAKE_CACHE_MINOR_VERSION")) {
  132. if (sscanf(cminor, "%u", &v) == 1) {
  133. this->CacheMinorVersion = v;
  134. }
  135. }
  136. } else {
  137. // CMake version not found in the list file.
  138. // Set as version 0.0
  139. this->AddCacheEntry("CMAKE_CACHE_MINOR_VERSION", "0",
  140. "Minor version of cmake used to create the "
  141. "current loaded cache",
  142. cmStateEnums::INTERNAL);
  143. this->AddCacheEntry("CMAKE_CACHE_MAJOR_VERSION", "0",
  144. "Major version of cmake used to create the "
  145. "current loaded cache",
  146. cmStateEnums::INTERNAL);
  147. }
  148. // check to make sure the cache directory has not
  149. // been moved
  150. const char* oldDir = this->GetInitializedCacheValue("CMAKE_CACHEFILE_DIR");
  151. if (internal && oldDir) {
  152. std::string currentcwd = path;
  153. std::string oldcwd = oldDir;
  154. cmSystemTools::ConvertToUnixSlashes(currentcwd);
  155. currentcwd += "/CMakeCache.txt";
  156. oldcwd += "/CMakeCache.txt";
  157. if (!cmSystemTools::SameFile(oldcwd, currentcwd)) {
  158. std::ostringstream message;
  159. message << "The current CMakeCache.txt directory " << currentcwd
  160. << " is different than the directory "
  161. << this->GetInitializedCacheValue("CMAKE_CACHEFILE_DIR")
  162. << " where CMakeCache.txt was created. This may result "
  163. "in binaries being created in the wrong place. If you "
  164. "are not sure, reedit the CMakeCache.txt";
  165. cmSystemTools::Error(message.str().c_str());
  166. }
  167. }
  168. return true;
  169. }
  170. const char* cmCacheManager::PersistentProperties[] = { "ADVANCED", "MODIFIED",
  171. "STRINGS", nullptr };
  172. bool cmCacheManager::ReadPropertyEntry(std::string const& entryKey,
  173. CacheEntry& e)
  174. {
  175. // All property entries are internal.
  176. if (e.Type != cmStateEnums::INTERNAL) {
  177. return false;
  178. }
  179. const char* end = entryKey.c_str() + entryKey.size();
  180. for (const char** p = this->PersistentProperties; *p; ++p) {
  181. std::string::size_type plen = strlen(*p) + 1;
  182. if (entryKey.size() > plen && *(end - plen) == '-' &&
  183. strcmp(end - plen + 1, *p) == 0) {
  184. std::string key = entryKey.substr(0, entryKey.size() - plen);
  185. cmCacheManager::CacheIterator it = this->GetCacheIterator(key.c_str());
  186. if (it.IsAtEnd()) {
  187. // Create an entry and store the property.
  188. CacheEntry& ne = this->Cache[key];
  189. ne.Type = cmStateEnums::UNINITIALIZED;
  190. ne.SetProperty(*p, e.Value.c_str());
  191. } else {
  192. // Store this property on its entry.
  193. it.SetProperty(*p, e.Value.c_str());
  194. }
  195. return true;
  196. }
  197. }
  198. return false;
  199. }
  200. void cmCacheManager::WritePropertyEntries(std::ostream& os, CacheIterator i,
  201. cmMessenger* messenger)
  202. {
  203. for (const char** p = this->PersistentProperties; *p; ++p) {
  204. if (const char* value = i.GetProperty(*p)) {
  205. std::string helpstring = *p;
  206. helpstring += " property for variable: ";
  207. helpstring += i.GetName();
  208. cmCacheManager::OutputHelpString(os, helpstring);
  209. std::string key = i.GetName();
  210. key += "-";
  211. key += *p;
  212. this->OutputKey(os, key);
  213. os << ":INTERNAL=";
  214. this->OutputValue(os, value);
  215. os << "\n";
  216. cmCacheManager::OutputNewlineTruncationWarning(os, key, value,
  217. messenger);
  218. }
  219. }
  220. }
  221. bool cmCacheManager::SaveCache(const std::string& path, cmMessenger* messenger)
  222. {
  223. std::string cacheFile = path;
  224. cacheFile += "/CMakeCache.txt";
  225. cmGeneratedFileStream fout(cacheFile);
  226. fout.SetCopyIfDifferent(true);
  227. if (!fout) {
  228. cmSystemTools::Error("Unable to open cache file for save. ",
  229. cacheFile.c_str());
  230. cmSystemTools::ReportLastSystemError("");
  231. return false;
  232. }
  233. // before writing the cache, update the version numbers
  234. // to the
  235. this->AddCacheEntry("CMAKE_CACHE_MAJOR_VERSION",
  236. std::to_string(cmVersion::GetMajorVersion()).c_str(),
  237. "Major version of cmake used to create the "
  238. "current loaded cache",
  239. cmStateEnums::INTERNAL);
  240. this->AddCacheEntry("CMAKE_CACHE_MINOR_VERSION",
  241. std::to_string(cmVersion::GetMinorVersion()).c_str(),
  242. "Minor version of cmake used to create the "
  243. "current loaded cache",
  244. cmStateEnums::INTERNAL);
  245. this->AddCacheEntry("CMAKE_CACHE_PATCH_VERSION",
  246. std::to_string(cmVersion::GetPatchVersion()).c_str(),
  247. "Patch version of cmake used to create the "
  248. "current loaded cache",
  249. cmStateEnums::INTERNAL);
  250. // Let us store the current working directory so that if somebody
  251. // Copies it, he will not be surprised
  252. std::string currentcwd = path;
  253. if (currentcwd[0] >= 'A' && currentcwd[0] <= 'Z' && currentcwd[1] == ':') {
  254. // Cast added to avoid compiler warning. Cast is ok because
  255. // value is guaranteed to fit in char by the above if...
  256. currentcwd[0] = static_cast<char>(currentcwd[0] - 'A' + 'a');
  257. }
  258. cmSystemTools::ConvertToUnixSlashes(currentcwd);
  259. this->AddCacheEntry("CMAKE_CACHEFILE_DIR", currentcwd.c_str(),
  260. "This is the directory where this CMakeCache.txt"
  261. " was created",
  262. cmStateEnums::INTERNAL);
  263. /* clang-format off */
  264. fout << "# This is the CMakeCache file.\n"
  265. << "# For build in directory: " << currentcwd << "\n"
  266. << "# It was generated by CMake: "
  267. << cmSystemTools::GetCMakeCommand() << std::endl;
  268. /* clang-format on */
  269. /* clang-format off */
  270. fout << "# You can edit this file to change values found and used by cmake."
  271. << std::endl
  272. << "# If you do not want to change any of the values, simply exit the "
  273. "editor." << std::endl
  274. << "# If you do want to change a value, simply edit, save, and exit "
  275. "the editor." << std::endl
  276. << "# The syntax for the file is as follows:\n"
  277. << "# KEY:TYPE=VALUE\n"
  278. << "# KEY is the name of a variable in the cache.\n"
  279. << "# TYPE is a hint to GUIs for the type of VALUE, DO NOT EDIT "
  280. "TYPE!." << std::endl
  281. << "# VALUE is the current value for the KEY.\n\n";
  282. /* clang-format on */
  283. fout << "########################\n";
  284. fout << "# EXTERNAL cache entries\n";
  285. fout << "########################\n";
  286. fout << "\n";
  287. for (auto const& i : this->Cache) {
  288. CacheEntry const& ce = i.second;
  289. cmStateEnums::CacheEntryType t = ce.Type;
  290. if (!ce.Initialized) {
  291. /*
  292. // This should be added in, but is not for now.
  293. cmSystemTools::Error("Cache entry \"", (*i).first.c_str(),
  294. "\" is uninitialized");
  295. */
  296. } else if (t != cmStateEnums::INTERNAL) {
  297. // Format is key:type=value
  298. if (const char* help = ce.GetProperty("HELPSTRING")) {
  299. cmCacheManager::OutputHelpString(fout, help);
  300. } else {
  301. cmCacheManager::OutputHelpString(fout, "Missing description");
  302. }
  303. this->OutputKey(fout, i.first);
  304. fout << ":" << cmState::CacheEntryTypeToString(t) << "=";
  305. this->OutputValue(fout, ce.Value);
  306. fout << "\n";
  307. cmCacheManager::OutputNewlineTruncationWarning(fout, i.first, ce.Value,
  308. messenger);
  309. fout << "\n";
  310. }
  311. }
  312. fout << "\n";
  313. fout << "########################\n";
  314. fout << "# INTERNAL cache entries\n";
  315. fout << "########################\n";
  316. fout << "\n";
  317. for (cmCacheManager::CacheIterator i = this->NewIterator(); !i.IsAtEnd();
  318. i.Next()) {
  319. if (!i.Initialized()) {
  320. continue;
  321. }
  322. cmStateEnums::CacheEntryType t = i.GetType();
  323. this->WritePropertyEntries(fout, i, messenger);
  324. if (t == cmStateEnums::INTERNAL) {
  325. // Format is key:type=value
  326. if (const char* help = i.GetProperty("HELPSTRING")) {
  327. this->OutputHelpString(fout, help);
  328. }
  329. this->OutputKey(fout, i.GetName());
  330. fout << ":" << cmState::CacheEntryTypeToString(t) << "=";
  331. this->OutputValue(fout, i.GetValue());
  332. fout << "\n";
  333. cmCacheManager::OutputNewlineTruncationWarning(fout, i.GetName(),
  334. i.GetValue(), messenger);
  335. }
  336. }
  337. fout << "\n";
  338. fout.Close();
  339. std::string checkCacheFile = path;
  340. checkCacheFile += cmake::GetCMakeFilesDirectory();
  341. cmSystemTools::MakeDirectory(checkCacheFile);
  342. checkCacheFile += "/cmake.check_cache";
  343. cmsys::ofstream checkCache(checkCacheFile.c_str());
  344. if (!checkCache) {
  345. cmSystemTools::Error("Unable to open check cache file for write. ",
  346. checkCacheFile.c_str());
  347. return false;
  348. }
  349. checkCache << "# This file is generated by cmake for dependency checking "
  350. "of the CMakeCache.txt file\n";
  351. return true;
  352. }
  353. bool cmCacheManager::DeleteCache(const std::string& path)
  354. {
  355. std::string cacheFile = path;
  356. cmSystemTools::ConvertToUnixSlashes(cacheFile);
  357. std::string cmakeFiles = cacheFile;
  358. cacheFile += "/CMakeCache.txt";
  359. if (cmSystemTools::FileExists(cacheFile)) {
  360. cmSystemTools::RemoveFile(cacheFile);
  361. // now remove the files in the CMakeFiles directory
  362. // this cleans up language cache files
  363. cmakeFiles += cmake::GetCMakeFilesDirectory();
  364. if (cmSystemTools::FileIsDirectory(cmakeFiles)) {
  365. cmSystemTools::RemoveADirectory(cmakeFiles);
  366. }
  367. }
  368. return true;
  369. }
  370. void cmCacheManager::OutputKey(std::ostream& fout, std::string const& key)
  371. {
  372. // support : in key name by double quoting
  373. const char* q =
  374. (key.find(':') != std::string::npos || key.find("//") == 0) ? "\"" : "";
  375. fout << q << key << q;
  376. }
  377. void cmCacheManager::OutputValue(std::ostream& fout, std::string const& value)
  378. {
  379. // look for and truncate newlines
  380. std::string::size_type newline = value.find('\n');
  381. if (newline != std::string::npos) {
  382. std::string truncated = value.substr(0, newline);
  383. OutputValueNoNewlines(fout, truncated);
  384. } else {
  385. OutputValueNoNewlines(fout, value);
  386. }
  387. }
  388. void cmCacheManager::OutputValueNoNewlines(std::ostream& fout,
  389. std::string const& value)
  390. {
  391. // if value has trailing space or tab, enclose it in single quotes
  392. if (!value.empty() &&
  393. (value[value.size() - 1] == ' ' || value[value.size() - 1] == '\t')) {
  394. fout << '\'' << value << '\'';
  395. } else {
  396. fout << value;
  397. }
  398. }
  399. void cmCacheManager::OutputHelpString(std::ostream& fout,
  400. const std::string& helpString)
  401. {
  402. std::string::size_type end = helpString.size();
  403. if (end == 0) {
  404. return;
  405. }
  406. std::string oneLine;
  407. std::string::size_type pos = 0;
  408. for (std::string::size_type i = 0; i <= end; i++) {
  409. if ((i == end) || (helpString[i] == '\n') ||
  410. ((i - pos >= 60) && (helpString[i] == ' '))) {
  411. fout << "//";
  412. if (helpString[pos] == '\n') {
  413. pos++;
  414. fout << "\\n";
  415. }
  416. oneLine = helpString.substr(pos, i - pos);
  417. fout << oneLine << "\n";
  418. pos = i;
  419. }
  420. }
  421. }
  422. void cmCacheManager::OutputWarningComment(std::ostream& fout,
  423. std::string const& message,
  424. bool wrapSpaces)
  425. {
  426. std::string::size_type end = message.size();
  427. std::string oneLine;
  428. std::string::size_type pos = 0;
  429. for (std::string::size_type i = 0; i <= end; i++) {
  430. if ((i == end) || (message[i] == '\n') ||
  431. ((i - pos >= 60) && (message[i] == ' ') && wrapSpaces)) {
  432. fout << "# ";
  433. if (message[pos] == '\n') {
  434. pos++;
  435. fout << "\\n";
  436. }
  437. oneLine = message.substr(pos, i - pos);
  438. fout << oneLine << "\n";
  439. pos = i;
  440. }
  441. }
  442. }
  443. void cmCacheManager::OutputNewlineTruncationWarning(std::ostream& fout,
  444. std::string const& key,
  445. std::string const& value,
  446. cmMessenger* messenger)
  447. {
  448. if (value.find('\n') != std::string::npos) {
  449. if (messenger) {
  450. std::string message = "Value of ";
  451. message += key;
  452. message += " contained a newline; truncating";
  453. messenger->IssueMessage(cmake::WARNING, message);
  454. }
  455. std::string comment = "WARNING: Value of ";
  456. comment += key;
  457. comment += " contained a newline and was truncated. Original value:";
  458. OutputWarningComment(fout, comment, true);
  459. OutputWarningComment(fout, value, false);
  460. }
  461. }
  462. void cmCacheManager::RemoveCacheEntry(const std::string& key)
  463. {
  464. CacheEntryMap::iterator i = this->Cache.find(key);
  465. if (i != this->Cache.end()) {
  466. this->Cache.erase(i);
  467. }
  468. }
  469. cmCacheManager::CacheEntry* cmCacheManager::GetCacheEntry(
  470. const std::string& key)
  471. {
  472. CacheEntryMap::iterator i = this->Cache.find(key);
  473. if (i != this->Cache.end()) {
  474. return &i->second;
  475. }
  476. return nullptr;
  477. }
  478. cmCacheManager::CacheIterator cmCacheManager::GetCacheIterator(const char* key)
  479. {
  480. return CacheIterator(*this, key);
  481. }
  482. const char* cmCacheManager::GetInitializedCacheValue(
  483. const std::string& key) const
  484. {
  485. CacheEntryMap::const_iterator i = this->Cache.find(key);
  486. if (i != this->Cache.end() && i->second.Initialized) {
  487. return i->second.Value.c_str();
  488. }
  489. return nullptr;
  490. }
  491. void cmCacheManager::PrintCache(std::ostream& out) const
  492. {
  493. out << "=================================================" << std::endl;
  494. out << "CMakeCache Contents:" << std::endl;
  495. for (auto const& i : this->Cache) {
  496. if (i.second.Type != cmStateEnums::INTERNAL) {
  497. out << i.first << " = " << i.second.Value << std::endl;
  498. }
  499. }
  500. out << "\n\n";
  501. out << "To change values in the CMakeCache, " << std::endl
  502. << "edit CMakeCache.txt in your output directory.\n";
  503. out << "=================================================" << std::endl;
  504. }
  505. void cmCacheManager::AddCacheEntry(const std::string& key, const char* value,
  506. const char* helpString,
  507. cmStateEnums::CacheEntryType type)
  508. {
  509. CacheEntry& e = this->Cache[key];
  510. if (value) {
  511. e.Value = value;
  512. e.Initialized = true;
  513. } else {
  514. e.Value.clear();
  515. }
  516. e.Type = type;
  517. // make sure we only use unix style paths
  518. if (type == cmStateEnums::FILEPATH || type == cmStateEnums::PATH) {
  519. if (e.Value.find(';') != std::string::npos) {
  520. std::vector<std::string> paths;
  521. cmSystemTools::ExpandListArgument(e.Value, paths);
  522. const char* sep = "";
  523. e.Value = "";
  524. for (std::string& i : paths) {
  525. cmSystemTools::ConvertToUnixSlashes(i);
  526. e.Value += sep;
  527. e.Value += i;
  528. sep = ";";
  529. }
  530. } else {
  531. cmSystemTools::ConvertToUnixSlashes(e.Value);
  532. }
  533. }
  534. e.SetProperty("HELPSTRING",
  535. helpString
  536. ? helpString
  537. : "(This variable does not exist and should not be used)");
  538. }
  539. bool cmCacheManager::CacheIterator::IsAtEnd() const
  540. {
  541. return this->Position == this->Container.Cache.end();
  542. }
  543. void cmCacheManager::CacheIterator::Begin()
  544. {
  545. this->Position = this->Container.Cache.begin();
  546. }
  547. bool cmCacheManager::CacheIterator::Find(const std::string& key)
  548. {
  549. this->Position = this->Container.Cache.find(key);
  550. return !this->IsAtEnd();
  551. }
  552. void cmCacheManager::CacheIterator::Next()
  553. {
  554. if (!this->IsAtEnd()) {
  555. ++this->Position;
  556. }
  557. }
  558. std::vector<std::string> cmCacheManager::CacheIterator::GetPropertyList() const
  559. {
  560. return this->GetEntry().GetPropertyList();
  561. }
  562. void cmCacheManager::CacheIterator::SetValue(const char* value)
  563. {
  564. if (this->IsAtEnd()) {
  565. return;
  566. }
  567. CacheEntry* entry = &this->GetEntry();
  568. if (value) {
  569. entry->Value = value;
  570. entry->Initialized = true;
  571. } else {
  572. entry->Value.clear();
  573. }
  574. }
  575. bool cmCacheManager::CacheIterator::GetValueAsBool() const
  576. {
  577. return cmSystemTools::IsOn(this->GetEntry().Value);
  578. }
  579. std::vector<std::string> cmCacheManager::CacheEntry::GetPropertyList() const
  580. {
  581. return this->Properties.GetPropertyList();
  582. }
  583. const char* cmCacheManager::CacheEntry::GetProperty(
  584. const std::string& prop) const
  585. {
  586. if (prop == "TYPE") {
  587. return cmState::CacheEntryTypeToString(this->Type);
  588. }
  589. if (prop == "VALUE") {
  590. return this->Value.c_str();
  591. }
  592. return this->Properties.GetPropertyValue(prop);
  593. }
  594. void cmCacheManager::CacheEntry::SetProperty(const std::string& prop,
  595. const char* value)
  596. {
  597. if (prop == "TYPE") {
  598. this->Type = cmState::StringToCacheEntryType(value ? value : "STRING");
  599. } else if (prop == "VALUE") {
  600. this->Value = value ? value : "";
  601. } else {
  602. this->Properties.SetProperty(prop, value);
  603. }
  604. }
  605. void cmCacheManager::CacheEntry::AppendProperty(const std::string& prop,
  606. const char* value,
  607. bool asString)
  608. {
  609. if (prop == "TYPE") {
  610. this->Type = cmState::StringToCacheEntryType(value ? value : "STRING");
  611. } else if (prop == "VALUE") {
  612. if (value) {
  613. if (!this->Value.empty() && *value && !asString) {
  614. this->Value += ";";
  615. }
  616. this->Value += value;
  617. }
  618. } else {
  619. this->Properties.AppendProperty(prop, value, asString);
  620. }
  621. }
  622. const char* cmCacheManager::CacheIterator::GetProperty(
  623. const std::string& prop) const
  624. {
  625. if (!this->IsAtEnd()) {
  626. return this->GetEntry().GetProperty(prop);
  627. }
  628. return nullptr;
  629. }
  630. void cmCacheManager::CacheIterator::SetProperty(const std::string& p,
  631. const char* v)
  632. {
  633. if (!this->IsAtEnd()) {
  634. this->GetEntry().SetProperty(p, v);
  635. }
  636. }
  637. void cmCacheManager::CacheIterator::AppendProperty(const std::string& p,
  638. const char* v,
  639. bool asString)
  640. {
  641. if (!this->IsAtEnd()) {
  642. this->GetEntry().AppendProperty(p, v, asString);
  643. }
  644. }
  645. bool cmCacheManager::CacheIterator::GetPropertyAsBool(
  646. const std::string& prop) const
  647. {
  648. if (const char* value = this->GetProperty(prop)) {
  649. return cmSystemTools::IsOn(value);
  650. }
  651. return false;
  652. }
  653. void cmCacheManager::CacheIterator::SetProperty(const std::string& p, bool v)
  654. {
  655. this->SetProperty(p, v ? "ON" : "OFF");
  656. }
  657. bool cmCacheManager::CacheIterator::PropertyExists(
  658. const std::string& prop) const
  659. {
  660. return this->GetProperty(prop) != nullptr;
  661. }