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 "cmMessageType.h"
  13. #include "cmMessenger.h"
  14. #include "cmState.h"
  15. #include "cmStringAlgorithms.h"
  16. #include "cmSystemTools.h"
  17. #include "cmVersion.h"
  18. cmCacheManager::cmCacheManager()
  19. {
  20. this->CacheMajorVersion = 0;
  21. this->CacheMinorVersion = 0;
  22. }
  23. void cmCacheManager::CleanCMakeFiles(const std::string& path)
  24. {
  25. std::string glob = path;
  26. glob += "/CMakeFiles";
  27. glob += "/*.cmake";
  28. cmsys::Glob globIt;
  29. globIt.FindFiles(glob);
  30. std::vector<std::string> files = globIt.GetFiles();
  31. std::for_each(files.begin(), files.end(), cmSystemTools::RemoveFile);
  32. }
  33. bool cmCacheManager::LoadCache(const std::string& path, bool internal,
  34. std::set<std::string>& excludes,
  35. std::set<std::string>& includes)
  36. {
  37. std::string cacheFile = path;
  38. cacheFile += "/CMakeCache.txt";
  39. // clear the old cache, if we are reading in internal values
  40. if (internal) {
  41. this->Cache.clear();
  42. }
  43. if (!cmSystemTools::FileExists(cacheFile)) {
  44. this->CleanCMakeFiles(path);
  45. return false;
  46. }
  47. cmsys::ifstream fin(cacheFile.c_str());
  48. if (!fin) {
  49. return false;
  50. }
  51. const char* realbuffer;
  52. std::string buffer;
  53. std::string entryKey;
  54. unsigned int lineno = 0;
  55. while (fin) {
  56. // Format is key:type=value
  57. std::string helpString;
  58. CacheEntry e;
  59. cmSystemTools::GetLineFromStream(fin, buffer);
  60. lineno++;
  61. realbuffer = buffer.c_str();
  62. while (*realbuffer != '0' &&
  63. (*realbuffer == ' ' || *realbuffer == '\t' || *realbuffer == '\r' ||
  64. *realbuffer == '\n')) {
  65. if (*realbuffer == '\n') {
  66. lineno++;
  67. }
  68. realbuffer++;
  69. }
  70. // skip blank lines and comment lines
  71. if (realbuffer[0] == '#' || realbuffer[0] == 0) {
  72. continue;
  73. }
  74. while (realbuffer[0] == '/' && realbuffer[1] == '/') {
  75. if ((realbuffer[2] == '\\') && (realbuffer[3] == 'n')) {
  76. helpString += "\n";
  77. helpString += &realbuffer[4];
  78. } else {
  79. helpString += &realbuffer[2];
  80. }
  81. cmSystemTools::GetLineFromStream(fin, buffer);
  82. lineno++;
  83. realbuffer = buffer.c_str();
  84. if (!fin) {
  85. continue;
  86. }
  87. }
  88. e.SetProperty("HELPSTRING", helpString.c_str());
  89. if (cmState::ParseCacheEntry(realbuffer, entryKey, e.Value, e.Type)) {
  90. if (excludes.find(entryKey) == excludes.end()) {
  91. // Load internal values if internal is set.
  92. // If the entry is not internal to the cache being loaded
  93. // or if it is in the list of internal entries to be
  94. // imported, load it.
  95. if (internal || (e.Type != cmStateEnums::INTERNAL) ||
  96. (includes.find(entryKey) != includes.end())) {
  97. // If we are loading the cache from another project,
  98. // make all loaded entries internal so that it is
  99. // not visible in the gui
  100. if (!internal) {
  101. e.Type = cmStateEnums::INTERNAL;
  102. helpString = "DO NOT EDIT, ";
  103. helpString += entryKey;
  104. helpString += " loaded from external file. "
  105. "To change this value edit this file: ";
  106. helpString += path;
  107. helpString += "/CMakeCache.txt";
  108. e.SetProperty("HELPSTRING", helpString.c_str());
  109. }
  110. if (!this->ReadPropertyEntry(entryKey, e)) {
  111. e.Initialized = true;
  112. this->Cache[entryKey] = e;
  113. }
  114. }
  115. }
  116. } else {
  117. std::ostringstream error;
  118. error << "Parse error in cache file " << cacheFile;
  119. error << " on line " << lineno << ". Offending entry: " << realbuffer;
  120. cmSystemTools::Error(error.str());
  121. }
  122. }
  123. this->CacheMajorVersion = 0;
  124. this->CacheMinorVersion = 0;
  125. if (const std::string* cmajor =
  126. this->GetInitializedCacheValue("CMAKE_CACHE_MAJOR_VERSION")) {
  127. unsigned int v = 0;
  128. if (sscanf(cmajor->c_str(), "%u", &v) == 1) {
  129. this->CacheMajorVersion = v;
  130. }
  131. if (const std::string* cminor =
  132. this->GetInitializedCacheValue("CMAKE_CACHE_MINOR_VERSION")) {
  133. if (sscanf(cminor->c_str(), "%u", &v) == 1) {
  134. this->CacheMinorVersion = v;
  135. }
  136. }
  137. } else {
  138. // CMake version not found in the list file.
  139. // Set as version 0.0
  140. this->AddCacheEntry("CMAKE_CACHE_MINOR_VERSION", "0",
  141. "Minor version of cmake used to create the "
  142. "current loaded cache",
  143. cmStateEnums::INTERNAL);
  144. this->AddCacheEntry("CMAKE_CACHE_MAJOR_VERSION", "0",
  145. "Major version of cmake used to create the "
  146. "current loaded cache",
  147. cmStateEnums::INTERNAL);
  148. }
  149. // check to make sure the cache directory has not
  150. // been moved
  151. const std::string* oldDir =
  152. this->GetInitializedCacheValue("CMAKE_CACHEFILE_DIR");
  153. if (internal && oldDir) {
  154. std::string currentcwd = path;
  155. std::string oldcwd = *oldDir;
  156. cmSystemTools::ConvertToUnixSlashes(currentcwd);
  157. currentcwd += "/CMakeCache.txt";
  158. oldcwd += "/CMakeCache.txt";
  159. if (!cmSystemTools::SameFile(oldcwd, currentcwd)) {
  160. const std::string* dir =
  161. this->GetInitializedCacheValue("CMAKE_CACHEFILE_DIR");
  162. std::ostringstream message;
  163. message << "The current CMakeCache.txt directory " << currentcwd
  164. << " is different than the directory " << (dir ? *dir : "")
  165. << " where CMakeCache.txt was created. This may result "
  166. "in binaries being created in the wrong place. If you "
  167. "are not sure, reedit the CMakeCache.txt";
  168. cmSystemTools::Error(message.str());
  169. }
  170. }
  171. return true;
  172. }
  173. const char* cmCacheManager::PersistentProperties[] = { "ADVANCED", "MODIFIED",
  174. "STRINGS", nullptr };
  175. bool cmCacheManager::ReadPropertyEntry(std::string const& entryKey,
  176. CacheEntry& e)
  177. {
  178. // All property entries are internal.
  179. if (e.Type != cmStateEnums::INTERNAL) {
  180. return false;
  181. }
  182. const char* end = entryKey.c_str() + entryKey.size();
  183. for (const char** p = cmCacheManager::PersistentProperties; *p; ++p) {
  184. std::string::size_type plen = strlen(*p) + 1;
  185. if (entryKey.size() > plen && *(end - plen) == '-' &&
  186. strcmp(end - plen + 1, *p) == 0) {
  187. std::string key = entryKey.substr(0, entryKey.size() - plen);
  188. cmCacheManager::CacheIterator it = this->GetCacheIterator(key.c_str());
  189. if (it.IsAtEnd()) {
  190. // Create an entry and store the property.
  191. CacheEntry& ne = this->Cache[key];
  192. ne.Type = cmStateEnums::UNINITIALIZED;
  193. ne.SetProperty(*p, e.Value.c_str());
  194. } else {
  195. // Store this property on its entry.
  196. it.SetProperty(*p, e.Value.c_str());
  197. }
  198. return true;
  199. }
  200. }
  201. return false;
  202. }
  203. void cmCacheManager::WritePropertyEntries(std::ostream& os, CacheIterator i,
  204. cmMessenger* messenger)
  205. {
  206. for (const char** p = cmCacheManager::PersistentProperties; *p; ++p) {
  207. if (const char* value = i.GetProperty(*p)) {
  208. std::string helpstring = *p;
  209. helpstring += " property for variable: ";
  210. helpstring += i.GetName();
  211. cmCacheManager::OutputHelpString(os, helpstring);
  212. std::string key = i.GetName();
  213. key += "-";
  214. key += *p;
  215. cmCacheManager::OutputKey(os, key);
  216. os << ":INTERNAL=";
  217. cmCacheManager::OutputValue(os, value);
  218. os << "\n";
  219. cmCacheManager::OutputNewlineTruncationWarning(os, key, value,
  220. messenger);
  221. }
  222. }
  223. }
  224. bool cmCacheManager::SaveCache(const std::string& path, cmMessenger* messenger)
  225. {
  226. std::string cacheFile = path;
  227. cacheFile += "/CMakeCache.txt";
  228. cmGeneratedFileStream fout(cacheFile);
  229. fout.SetCopyIfDifferent(true);
  230. if (!fout) {
  231. cmSystemTools::Error("Unable to open cache file for save. " + cacheFile);
  232. cmSystemTools::ReportLastSystemError("");
  233. return false;
  234. }
  235. // before writing the cache, update the version numbers
  236. // to the
  237. this->AddCacheEntry("CMAKE_CACHE_MAJOR_VERSION",
  238. std::to_string(cmVersion::GetMajorVersion()).c_str(),
  239. "Major version of cmake used to create the "
  240. "current loaded cache",
  241. cmStateEnums::INTERNAL);
  242. this->AddCacheEntry("CMAKE_CACHE_MINOR_VERSION",
  243. std::to_string(cmVersion::GetMinorVersion()).c_str(),
  244. "Minor version of cmake used to create the "
  245. "current loaded cache",
  246. cmStateEnums::INTERNAL);
  247. this->AddCacheEntry("CMAKE_CACHE_PATCH_VERSION",
  248. std::to_string(cmVersion::GetPatchVersion()).c_str(),
  249. "Patch version of cmake used to create the "
  250. "current loaded cache",
  251. cmStateEnums::INTERNAL);
  252. // Let us store the current working directory so that if somebody
  253. // Copies it, he will not be surprised
  254. std::string currentcwd = path;
  255. if (currentcwd[0] >= 'A' && currentcwd[0] <= 'Z' && currentcwd[1] == ':') {
  256. // Cast added to avoid compiler warning. Cast is ok because
  257. // value is guaranteed to fit in char by the above if...
  258. currentcwd[0] = static_cast<char>(currentcwd[0] - 'A' + 'a');
  259. }
  260. cmSystemTools::ConvertToUnixSlashes(currentcwd);
  261. this->AddCacheEntry("CMAKE_CACHEFILE_DIR", currentcwd.c_str(),
  262. "This is the directory where this CMakeCache.txt"
  263. " was created",
  264. cmStateEnums::INTERNAL);
  265. /* clang-format off */
  266. fout << "# This is the CMakeCache file.\n"
  267. << "# For build in directory: " << currentcwd << "\n"
  268. << "# It was generated by CMake: "
  269. << cmSystemTools::GetCMakeCommand() << std::endl;
  270. /* clang-format on */
  271. /* clang-format off */
  272. fout << "# You can edit this file to change values found and used by cmake."
  273. << std::endl
  274. << "# If you do not want to change any of the values, simply exit the "
  275. "editor." << std::endl
  276. << "# If you do want to change a value, simply edit, save, and exit "
  277. "the editor." << std::endl
  278. << "# The syntax for the file is as follows:\n"
  279. << "# KEY:TYPE=VALUE\n"
  280. << "# KEY is the name of a variable in the cache.\n"
  281. << "# TYPE is a hint to GUIs for the type of VALUE, DO NOT EDIT "
  282. "TYPE!." << std::endl
  283. << "# VALUE is the current value for the KEY.\n\n";
  284. /* clang-format on */
  285. fout << "########################\n";
  286. fout << "# EXTERNAL cache entries\n";
  287. fout << "########################\n";
  288. fout << "\n";
  289. for (auto const& i : this->Cache) {
  290. CacheEntry const& ce = i.second;
  291. cmStateEnums::CacheEntryType t = ce.Type;
  292. if (!ce.Initialized) {
  293. /*
  294. // This should be added in, but is not for now.
  295. cmSystemTools::Error("Cache entry \"" + i.first + "\" is uninitialized");
  296. */
  297. } else if (t != cmStateEnums::INTERNAL) {
  298. // Format is key:type=value
  299. if (const char* help = ce.GetProperty("HELPSTRING")) {
  300. cmCacheManager::OutputHelpString(fout, help);
  301. } else {
  302. cmCacheManager::OutputHelpString(fout, "Missing description");
  303. }
  304. cmCacheManager::OutputKey(fout, i.first);
  305. fout << ":" << cmState::CacheEntryTypeToString(t) << "=";
  306. cmCacheManager::OutputValue(fout, ce.Value);
  307. fout << "\n";
  308. cmCacheManager::OutputNewlineTruncationWarning(fout, i.first, ce.Value,
  309. messenger);
  310. fout << "\n";
  311. }
  312. }
  313. fout << "\n";
  314. fout << "########################\n";
  315. fout << "# INTERNAL cache entries\n";
  316. fout << "########################\n";
  317. fout << "\n";
  318. for (cmCacheManager::CacheIterator i = this->NewIterator(); !i.IsAtEnd();
  319. i.Next()) {
  320. if (!i.Initialized()) {
  321. continue;
  322. }
  323. cmStateEnums::CacheEntryType t = i.GetType();
  324. this->WritePropertyEntries(fout, i, messenger);
  325. if (t == cmStateEnums::INTERNAL) {
  326. // Format is key:type=value
  327. if (const char* help = i.GetProperty("HELPSTRING")) {
  328. cmCacheManager::OutputHelpString(fout, help);
  329. }
  330. cmCacheManager::OutputKey(fout, i.GetName());
  331. fout << ":" << cmState::CacheEntryTypeToString(t) << "=";
  332. cmCacheManager::OutputValue(fout, i.GetValue());
  333. fout << "\n";
  334. cmCacheManager::OutputNewlineTruncationWarning(fout, i.GetName(),
  335. i.GetValue(), messenger);
  336. }
  337. }
  338. fout << "\n";
  339. fout.Close();
  340. std::string checkCacheFile = path;
  341. checkCacheFile += "/CMakeFiles";
  342. cmSystemTools::MakeDirectory(checkCacheFile);
  343. checkCacheFile += "/cmake.check_cache";
  344. cmsys::ofstream checkCache(checkCacheFile.c_str());
  345. if (!checkCache) {
  346. cmSystemTools::Error("Unable to open check cache file for write. " +
  347. checkCacheFile);
  348. return false;
  349. }
  350. checkCache << "# This file is generated by cmake for dependency checking "
  351. "of the CMakeCache.txt file\n";
  352. return true;
  353. }
  354. bool cmCacheManager::DeleteCache(const std::string& path)
  355. {
  356. std::string cacheFile = path;
  357. cmSystemTools::ConvertToUnixSlashes(cacheFile);
  358. std::string cmakeFiles = cacheFile;
  359. cacheFile += "/CMakeCache.txt";
  360. if (cmSystemTools::FileExists(cacheFile)) {
  361. cmSystemTools::RemoveFile(cacheFile);
  362. // now remove the files in the CMakeFiles directory
  363. // this cleans up language cache files
  364. cmakeFiles += "/CMakeFiles";
  365. if (cmSystemTools::FileIsDirectory(cmakeFiles)) {
  366. cmSystemTools::RemoveADirectory(cmakeFiles);
  367. }
  368. }
  369. return true;
  370. }
  371. void cmCacheManager::OutputKey(std::ostream& fout, std::string const& key)
  372. {
  373. // support : in key name by double quoting
  374. const char* q =
  375. (key.find(':') != std::string::npos || key.find("//") == 0) ? "\"" : "";
  376. fout << q << key << q;
  377. }
  378. void cmCacheManager::OutputValue(std::ostream& fout, std::string const& value)
  379. {
  380. // look for and truncate newlines
  381. std::string::size_type newline = value.find('\n');
  382. if (newline != std::string::npos) {
  383. std::string truncated = value.substr(0, newline);
  384. OutputValueNoNewlines(fout, truncated);
  385. } else {
  386. OutputValueNoNewlines(fout, value);
  387. }
  388. }
  389. void cmCacheManager::OutputValueNoNewlines(std::ostream& fout,
  390. std::string const& value)
  391. {
  392. // if value has trailing space or tab, enclose it in single quotes
  393. if (!value.empty() && (value.back() == ' ' || value.back() == '\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(MessageType::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 std::string* 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;
  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. cmExpandList(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.GetKeys();
  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. }