1
0

cmCacheManager.cxx 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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 std::string* cmajor =
  125. this->GetInitializedCacheValue("CMAKE_CACHE_MAJOR_VERSION")) {
  126. unsigned int v = 0;
  127. if (sscanf(cmajor->c_str(), "%u", &v) == 1) {
  128. this->CacheMajorVersion = v;
  129. }
  130. if (const std::string* cminor =
  131. this->GetInitializedCacheValue("CMAKE_CACHE_MINOR_VERSION")) {
  132. if (sscanf(cminor->c_str(), "%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 std::string* oldDir =
  151. this->GetInitializedCacheValue("CMAKE_CACHEFILE_DIR");
  152. if (internal && oldDir) {
  153. std::string currentcwd = path;
  154. std::string oldcwd = *oldDir;
  155. cmSystemTools::ConvertToUnixSlashes(currentcwd);
  156. currentcwd += "/CMakeCache.txt";
  157. oldcwd += "/CMakeCache.txt";
  158. if (!cmSystemTools::SameFile(oldcwd, currentcwd)) {
  159. const std::string* dir =
  160. this->GetInitializedCacheValue("CMAKE_CACHEFILE_DIR");
  161. std::ostringstream message;
  162. message << "The current CMakeCache.txt directory " << currentcwd
  163. << " is different than the directory " << (dir ? *dir : "")
  164. << " where CMakeCache.txt was created. This may result "
  165. "in binaries being created in the wrong place. If you "
  166. "are not sure, reedit the CMakeCache.txt";
  167. cmSystemTools::Error(message.str().c_str());
  168. }
  169. }
  170. return true;
  171. }
  172. const char* cmCacheManager::PersistentProperties[] = { "ADVANCED", "MODIFIED",
  173. "STRINGS", nullptr };
  174. bool cmCacheManager::ReadPropertyEntry(std::string const& entryKey,
  175. CacheEntry& e)
  176. {
  177. // All property entries are internal.
  178. if (e.Type != cmStateEnums::INTERNAL) {
  179. return false;
  180. }
  181. const char* end = entryKey.c_str() + entryKey.size();
  182. for (const char** p = this->PersistentProperties; *p; ++p) {
  183. std::string::size_type plen = strlen(*p) + 1;
  184. if (entryKey.size() > plen && *(end - plen) == '-' &&
  185. strcmp(end - plen + 1, *p) == 0) {
  186. std::string key = entryKey.substr(0, entryKey.size() - plen);
  187. cmCacheManager::CacheIterator it = this->GetCacheIterator(key.c_str());
  188. if (it.IsAtEnd()) {
  189. // Create an entry and store the property.
  190. CacheEntry& ne = this->Cache[key];
  191. ne.Type = cmStateEnums::UNINITIALIZED;
  192. ne.SetProperty(*p, e.Value.c_str());
  193. } else {
  194. // Store this property on its entry.
  195. it.SetProperty(*p, e.Value.c_str());
  196. }
  197. return true;
  198. }
  199. }
  200. return false;
  201. }
  202. void cmCacheManager::WritePropertyEntries(std::ostream& os, CacheIterator i,
  203. cmMessenger* messenger)
  204. {
  205. for (const char** p = this->PersistentProperties; *p; ++p) {
  206. if (const char* value = i.GetProperty(*p)) {
  207. std::string helpstring = *p;
  208. helpstring += " property for variable: ";
  209. helpstring += i.GetName();
  210. cmCacheManager::OutputHelpString(os, helpstring);
  211. std::string key = i.GetName();
  212. key += "-";
  213. key += *p;
  214. this->OutputKey(os, key);
  215. os << ":INTERNAL=";
  216. this->OutputValue(os, value);
  217. os << "\n";
  218. cmCacheManager::OutputNewlineTruncationWarning(os, key, value,
  219. messenger);
  220. }
  221. }
  222. }
  223. bool cmCacheManager::SaveCache(const std::string& path, cmMessenger* messenger)
  224. {
  225. std::string cacheFile = path;
  226. cacheFile += "/CMakeCache.txt";
  227. cmGeneratedFileStream fout(cacheFile);
  228. fout.SetCopyIfDifferent(true);
  229. if (!fout) {
  230. cmSystemTools::Error("Unable to open cache file for save. ",
  231. cacheFile.c_str());
  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.c_str(),
  296. "\" is uninitialized");
  297. */
  298. } else if (t != cmStateEnums::INTERNAL) {
  299. // Format is key:type=value
  300. if (const char* help = ce.GetProperty("HELPSTRING")) {
  301. cmCacheManager::OutputHelpString(fout, help);
  302. } else {
  303. cmCacheManager::OutputHelpString(fout, "Missing description");
  304. }
  305. this->OutputKey(fout, i.first);
  306. fout << ":" << cmState::CacheEntryTypeToString(t) << "=";
  307. this->OutputValue(fout, ce.Value);
  308. fout << "\n";
  309. cmCacheManager::OutputNewlineTruncationWarning(fout, i.first, ce.Value,
  310. messenger);
  311. fout << "\n";
  312. }
  313. }
  314. fout << "\n";
  315. fout << "########################\n";
  316. fout << "# INTERNAL cache entries\n";
  317. fout << "########################\n";
  318. fout << "\n";
  319. for (cmCacheManager::CacheIterator i = this->NewIterator(); !i.IsAtEnd();
  320. i.Next()) {
  321. if (!i.Initialized()) {
  322. continue;
  323. }
  324. cmStateEnums::CacheEntryType t = i.GetType();
  325. this->WritePropertyEntries(fout, i, messenger);
  326. if (t == cmStateEnums::INTERNAL) {
  327. // Format is key:type=value
  328. if (const char* help = i.GetProperty("HELPSTRING")) {
  329. this->OutputHelpString(fout, help);
  330. }
  331. this->OutputKey(fout, i.GetName());
  332. fout << ":" << cmState::CacheEntryTypeToString(t) << "=";
  333. this->OutputValue(fout, i.GetValue());
  334. fout << "\n";
  335. cmCacheManager::OutputNewlineTruncationWarning(fout, i.GetName(),
  336. i.GetValue(), messenger);
  337. }
  338. }
  339. fout << "\n";
  340. fout.Close();
  341. std::string checkCacheFile = path;
  342. checkCacheFile += cmake::GetCMakeFilesDirectory();
  343. cmSystemTools::MakeDirectory(checkCacheFile);
  344. checkCacheFile += "/cmake.check_cache";
  345. cmsys::ofstream checkCache(checkCacheFile.c_str());
  346. if (!checkCache) {
  347. cmSystemTools::Error("Unable to open check cache file for write. ",
  348. checkCacheFile.c_str());
  349. return false;
  350. }
  351. checkCache << "# This file is generated by cmake for dependency checking "
  352. "of the CMakeCache.txt file\n";
  353. return true;
  354. }
  355. bool cmCacheManager::DeleteCache(const std::string& path)
  356. {
  357. std::string cacheFile = path;
  358. cmSystemTools::ConvertToUnixSlashes(cacheFile);
  359. std::string cmakeFiles = cacheFile;
  360. cacheFile += "/CMakeCache.txt";
  361. if (cmSystemTools::FileExists(cacheFile)) {
  362. cmSystemTools::RemoveFile(cacheFile);
  363. // now remove the files in the CMakeFiles directory
  364. // this cleans up language cache files
  365. cmakeFiles += cmake::GetCMakeFilesDirectory();
  366. if (cmSystemTools::FileIsDirectory(cmakeFiles)) {
  367. cmSystemTools::RemoveADirectory(cmakeFiles);
  368. }
  369. }
  370. return true;
  371. }
  372. void cmCacheManager::OutputKey(std::ostream& fout, std::string const& key)
  373. {
  374. // support : in key name by double quoting
  375. const char* q =
  376. (key.find(':') != std::string::npos || key.find("//") == 0) ? "\"" : "";
  377. fout << q << key << q;
  378. }
  379. void cmCacheManager::OutputValue(std::ostream& fout, std::string const& value)
  380. {
  381. // look for and truncate newlines
  382. std::string::size_type newline = value.find('\n');
  383. if (newline != std::string::npos) {
  384. std::string truncated = value.substr(0, newline);
  385. OutputValueNoNewlines(fout, truncated);
  386. } else {
  387. OutputValueNoNewlines(fout, value);
  388. }
  389. }
  390. void cmCacheManager::OutputValueNoNewlines(std::ostream& fout,
  391. std::string const& value)
  392. {
  393. // if value has trailing space or tab, enclose it in single quotes
  394. if (!value.empty() && (value.back() == ' ' || value.back() == '\t')) {
  395. fout << '\'' << value << '\'';
  396. } else {
  397. fout << value;
  398. }
  399. }
  400. void cmCacheManager::OutputHelpString(std::ostream& fout,
  401. const std::string& helpString)
  402. {
  403. std::string::size_type end = helpString.size();
  404. if (end == 0) {
  405. return;
  406. }
  407. std::string oneLine;
  408. std::string::size_type pos = 0;
  409. for (std::string::size_type i = 0; i <= end; i++) {
  410. if ((i == end) || (helpString[i] == '\n') ||
  411. ((i - pos >= 60) && (helpString[i] == ' '))) {
  412. fout << "//";
  413. if (helpString[pos] == '\n') {
  414. pos++;
  415. fout << "\\n";
  416. }
  417. oneLine = helpString.substr(pos, i - pos);
  418. fout << oneLine << "\n";
  419. pos = i;
  420. }
  421. }
  422. }
  423. void cmCacheManager::OutputWarningComment(std::ostream& fout,
  424. std::string const& message,
  425. bool wrapSpaces)
  426. {
  427. std::string::size_type end = message.size();
  428. std::string oneLine;
  429. std::string::size_type pos = 0;
  430. for (std::string::size_type i = 0; i <= end; i++) {
  431. if ((i == end) || (message[i] == '\n') ||
  432. ((i - pos >= 60) && (message[i] == ' ') && wrapSpaces)) {
  433. fout << "# ";
  434. if (message[pos] == '\n') {
  435. pos++;
  436. fout << "\\n";
  437. }
  438. oneLine = message.substr(pos, i - pos);
  439. fout << oneLine << "\n";
  440. pos = i;
  441. }
  442. }
  443. }
  444. void cmCacheManager::OutputNewlineTruncationWarning(std::ostream& fout,
  445. std::string const& key,
  446. std::string const& value,
  447. cmMessenger* messenger)
  448. {
  449. if (value.find('\n') != std::string::npos) {
  450. if (messenger) {
  451. std::string message = "Value of ";
  452. message += key;
  453. message += " contained a newline; truncating";
  454. messenger->IssueMessage(cmake::WARNING, message);
  455. }
  456. std::string comment = "WARNING: Value of ";
  457. comment += key;
  458. comment += " contained a newline and was truncated. Original value:";
  459. OutputWarningComment(fout, comment, true);
  460. OutputWarningComment(fout, value, false);
  461. }
  462. }
  463. void cmCacheManager::RemoveCacheEntry(const std::string& key)
  464. {
  465. CacheEntryMap::iterator i = this->Cache.find(key);
  466. if (i != this->Cache.end()) {
  467. this->Cache.erase(i);
  468. }
  469. }
  470. cmCacheManager::CacheEntry* cmCacheManager::GetCacheEntry(
  471. const std::string& key)
  472. {
  473. CacheEntryMap::iterator i = this->Cache.find(key);
  474. if (i != this->Cache.end()) {
  475. return &i->second;
  476. }
  477. return nullptr;
  478. }
  479. cmCacheManager::CacheIterator cmCacheManager::GetCacheIterator(const char* key)
  480. {
  481. return CacheIterator(*this, key);
  482. }
  483. const std::string* cmCacheManager::GetInitializedCacheValue(
  484. const std::string& key) const
  485. {
  486. CacheEntryMap::const_iterator i = this->Cache.find(key);
  487. if (i != this->Cache.end() && i->second.Initialized) {
  488. return &i->second.Value;
  489. }
  490. return nullptr;
  491. }
  492. void cmCacheManager::PrintCache(std::ostream& out) const
  493. {
  494. out << "=================================================" << std::endl;
  495. out << "CMakeCache Contents:" << std::endl;
  496. for (auto const& i : this->Cache) {
  497. if (i.second.Type != cmStateEnums::INTERNAL) {
  498. out << i.first << " = " << i.second.Value << std::endl;
  499. }
  500. }
  501. out << "\n\n";
  502. out << "To change values in the CMakeCache, " << std::endl
  503. << "edit CMakeCache.txt in your output directory.\n";
  504. out << "=================================================" << std::endl;
  505. }
  506. void cmCacheManager::AddCacheEntry(const std::string& key, const char* value,
  507. const char* helpString,
  508. cmStateEnums::CacheEntryType type)
  509. {
  510. CacheEntry& e = this->Cache[key];
  511. if (value) {
  512. e.Value = value;
  513. e.Initialized = true;
  514. } else {
  515. e.Value.clear();
  516. }
  517. e.Type = type;
  518. // make sure we only use unix style paths
  519. if (type == cmStateEnums::FILEPATH || type == cmStateEnums::PATH) {
  520. if (e.Value.find(';') != std::string::npos) {
  521. std::vector<std::string> paths;
  522. cmSystemTools::ExpandListArgument(e.Value, paths);
  523. const char* sep = "";
  524. e.Value = "";
  525. for (std::string& i : paths) {
  526. cmSystemTools::ConvertToUnixSlashes(i);
  527. e.Value += sep;
  528. e.Value += i;
  529. sep = ";";
  530. }
  531. } else {
  532. cmSystemTools::ConvertToUnixSlashes(e.Value);
  533. }
  534. }
  535. e.SetProperty("HELPSTRING",
  536. helpString
  537. ? helpString
  538. : "(This variable does not exist and should not be used)");
  539. }
  540. bool cmCacheManager::CacheIterator::IsAtEnd() const
  541. {
  542. return this->Position == this->Container.Cache.end();
  543. }
  544. void cmCacheManager::CacheIterator::Begin()
  545. {
  546. this->Position = this->Container.Cache.begin();
  547. }
  548. bool cmCacheManager::CacheIterator::Find(const std::string& key)
  549. {
  550. this->Position = this->Container.Cache.find(key);
  551. return !this->IsAtEnd();
  552. }
  553. void cmCacheManager::CacheIterator::Next()
  554. {
  555. if (!this->IsAtEnd()) {
  556. ++this->Position;
  557. }
  558. }
  559. std::vector<std::string> cmCacheManager::CacheIterator::GetPropertyList() const
  560. {
  561. return this->GetEntry().GetPropertyList();
  562. }
  563. void cmCacheManager::CacheIterator::SetValue(const char* value)
  564. {
  565. if (this->IsAtEnd()) {
  566. return;
  567. }
  568. CacheEntry* entry = &this->GetEntry();
  569. if (value) {
  570. entry->Value = value;
  571. entry->Initialized = true;
  572. } else {
  573. entry->Value.clear();
  574. }
  575. }
  576. bool cmCacheManager::CacheIterator::GetValueAsBool() const
  577. {
  578. return cmSystemTools::IsOn(this->GetEntry().Value);
  579. }
  580. std::vector<std::string> cmCacheManager::CacheEntry::GetPropertyList() const
  581. {
  582. return this->Properties.GetPropertyList();
  583. }
  584. const char* cmCacheManager::CacheEntry::GetProperty(
  585. const std::string& prop) const
  586. {
  587. if (prop == "TYPE") {
  588. return cmState::CacheEntryTypeToString(this->Type);
  589. }
  590. if (prop == "VALUE") {
  591. return this->Value.c_str();
  592. }
  593. return this->Properties.GetPropertyValue(prop);
  594. }
  595. void cmCacheManager::CacheEntry::SetProperty(const std::string& prop,
  596. const char* value)
  597. {
  598. if (prop == "TYPE") {
  599. this->Type = cmState::StringToCacheEntryType(value ? value : "STRING");
  600. } else if (prop == "VALUE") {
  601. this->Value = value ? value : "";
  602. } else {
  603. this->Properties.SetProperty(prop, value);
  604. }
  605. }
  606. void cmCacheManager::CacheEntry::AppendProperty(const std::string& prop,
  607. const char* value,
  608. bool asString)
  609. {
  610. if (prop == "TYPE") {
  611. this->Type = cmState::StringToCacheEntryType(value ? value : "STRING");
  612. } else if (prop == "VALUE") {
  613. if (value) {
  614. if (!this->Value.empty() && *value && !asString) {
  615. this->Value += ";";
  616. }
  617. this->Value += value;
  618. }
  619. } else {
  620. this->Properties.AppendProperty(prop, value, asString);
  621. }
  622. }
  623. const char* cmCacheManager::CacheIterator::GetProperty(
  624. const std::string& prop) const
  625. {
  626. if (!this->IsAtEnd()) {
  627. return this->GetEntry().GetProperty(prop);
  628. }
  629. return nullptr;
  630. }
  631. void cmCacheManager::CacheIterator::SetProperty(const std::string& p,
  632. const char* v)
  633. {
  634. if (!this->IsAtEnd()) {
  635. this->GetEntry().SetProperty(p, v);
  636. }
  637. }
  638. void cmCacheManager::CacheIterator::AppendProperty(const std::string& p,
  639. const char* v,
  640. bool asString)
  641. {
  642. if (!this->IsAtEnd()) {
  643. this->GetEntry().AppendProperty(p, v, asString);
  644. }
  645. }
  646. bool cmCacheManager::CacheIterator::GetPropertyAsBool(
  647. const std::string& prop) const
  648. {
  649. if (const char* value = this->GetProperty(prop)) {
  650. return cmSystemTools::IsOn(value);
  651. }
  652. return false;
  653. }
  654. void cmCacheManager::CacheIterator::SetProperty(const std::string& p, bool v)
  655. {
  656. this->SetProperty(p, v ? "ON" : "OFF");
  657. }
  658. bool cmCacheManager::CacheIterator::PropertyExists(
  659. const std::string& prop) const
  660. {
  661. return this->GetProperty(prop) != nullptr;
  662. }