cmCacheManager.cxx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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 "cmSystemTools.h"
  16. #include "cmVersion.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 += "/CMakeFiles";
  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());
  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());
  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 = cmCacheManager::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 = cmCacheManager::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. cmCacheManager::OutputKey(os, key);
  215. os << ":INTERNAL=";
  216. cmCacheManager::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. " + cacheFile);
  231. cmSystemTools::ReportLastSystemError("");
  232. return false;
  233. }
  234. // before writing the cache, update the version numbers
  235. // to the
  236. this->AddCacheEntry("CMAKE_CACHE_MAJOR_VERSION",
  237. std::to_string(cmVersion::GetMajorVersion()).c_str(),
  238. "Major version of cmake used to create the "
  239. "current loaded cache",
  240. cmStateEnums::INTERNAL);
  241. this->AddCacheEntry("CMAKE_CACHE_MINOR_VERSION",
  242. std::to_string(cmVersion::GetMinorVersion()).c_str(),
  243. "Minor version of cmake used to create the "
  244. "current loaded cache",
  245. cmStateEnums::INTERNAL);
  246. this->AddCacheEntry("CMAKE_CACHE_PATCH_VERSION",
  247. std::to_string(cmVersion::GetPatchVersion()).c_str(),
  248. "Patch version of cmake used to create the "
  249. "current loaded cache",
  250. cmStateEnums::INTERNAL);
  251. // Let us store the current working directory so that if somebody
  252. // Copies it, he will not be surprised
  253. std::string currentcwd = path;
  254. if (currentcwd[0] >= 'A' && currentcwd[0] <= 'Z' && currentcwd[1] == ':') {
  255. // Cast added to avoid compiler warning. Cast is ok because
  256. // value is guaranteed to fit in char by the above if...
  257. currentcwd[0] = static_cast<char>(currentcwd[0] - 'A' + 'a');
  258. }
  259. cmSystemTools::ConvertToUnixSlashes(currentcwd);
  260. this->AddCacheEntry("CMAKE_CACHEFILE_DIR", currentcwd.c_str(),
  261. "This is the directory where this CMakeCache.txt"
  262. " was created",
  263. cmStateEnums::INTERNAL);
  264. /* clang-format off */
  265. fout << "# This is the CMakeCache file.\n"
  266. << "# For build in directory: " << currentcwd << "\n"
  267. << "# It was generated by CMake: "
  268. << cmSystemTools::GetCMakeCommand() << std::endl;
  269. /* clang-format on */
  270. /* clang-format off */
  271. fout << "# You can edit this file to change values found and used by cmake."
  272. << std::endl
  273. << "# If you do not want to change any of the values, simply exit the "
  274. "editor." << std::endl
  275. << "# If you do want to change a value, simply edit, save, and exit "
  276. "the editor." << std::endl
  277. << "# The syntax for the file is as follows:\n"
  278. << "# KEY:TYPE=VALUE\n"
  279. << "# KEY is the name of a variable in the cache.\n"
  280. << "# TYPE is a hint to GUIs for the type of VALUE, DO NOT EDIT "
  281. "TYPE!." << std::endl
  282. << "# VALUE is the current value for the KEY.\n\n";
  283. /* clang-format on */
  284. fout << "########################\n";
  285. fout << "# EXTERNAL cache entries\n";
  286. fout << "########################\n";
  287. fout << "\n";
  288. for (auto const& i : this->Cache) {
  289. CacheEntry const& ce = i.second;
  290. cmStateEnums::CacheEntryType t = ce.Type;
  291. if (!ce.Initialized) {
  292. /*
  293. // This should be added in, but is not for now.
  294. cmSystemTools::Error("Cache entry \"" + i.first + "\" 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. cmCacheManager::OutputKey(fout, i.first);
  304. fout << ":" << cmState::CacheEntryTypeToString(t) << "=";
  305. cmCacheManager::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. cmCacheManager::OutputHelpString(fout, help);
  328. }
  329. cmCacheManager::OutputKey(fout, i.GetName());
  330. fout << ":" << cmState::CacheEntryTypeToString(t) << "=";
  331. cmCacheManager::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 += "/CMakeFiles";
  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);
  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 += "/CMakeFiles";
  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() && (value.back() == ' ' || value.back() == '\t')) {
  393. fout << '\'' << value << '\'';
  394. } else {
  395. fout << value;
  396. }
  397. }
  398. void cmCacheManager::OutputHelpString(std::ostream& fout,
  399. const std::string& helpString)
  400. {
  401. std::string::size_type end = helpString.size();
  402. if (end == 0) {
  403. return;
  404. }
  405. std::string oneLine;
  406. std::string::size_type pos = 0;
  407. for (std::string::size_type i = 0; i <= end; i++) {
  408. if ((i == end) || (helpString[i] == '\n') ||
  409. ((i - pos >= 60) && (helpString[i] == ' '))) {
  410. fout << "//";
  411. if (helpString[pos] == '\n') {
  412. pos++;
  413. fout << "\\n";
  414. }
  415. oneLine = helpString.substr(pos, i - pos);
  416. fout << oneLine << "\n";
  417. pos = i;
  418. }
  419. }
  420. }
  421. void cmCacheManager::OutputWarningComment(std::ostream& fout,
  422. std::string const& message,
  423. bool wrapSpaces)
  424. {
  425. std::string::size_type end = message.size();
  426. std::string oneLine;
  427. std::string::size_type pos = 0;
  428. for (std::string::size_type i = 0; i <= end; i++) {
  429. if ((i == end) || (message[i] == '\n') ||
  430. ((i - pos >= 60) && (message[i] == ' ') && wrapSpaces)) {
  431. fout << "# ";
  432. if (message[pos] == '\n') {
  433. pos++;
  434. fout << "\\n";
  435. }
  436. oneLine = message.substr(pos, i - pos);
  437. fout << oneLine << "\n";
  438. pos = i;
  439. }
  440. }
  441. }
  442. void cmCacheManager::OutputNewlineTruncationWarning(std::ostream& fout,
  443. std::string const& key,
  444. std::string const& value,
  445. cmMessenger* messenger)
  446. {
  447. if (value.find('\n') != std::string::npos) {
  448. if (messenger) {
  449. std::string message = "Value of ";
  450. message += key;
  451. message += " contained a newline; truncating";
  452. messenger->IssueMessage(MessageType::WARNING, message);
  453. }
  454. std::string comment = "WARNING: Value of ";
  455. comment += key;
  456. comment += " contained a newline and was truncated. Original value:";
  457. OutputWarningComment(fout, comment, true);
  458. OutputWarningComment(fout, value, false);
  459. }
  460. }
  461. void cmCacheManager::RemoveCacheEntry(const std::string& key)
  462. {
  463. CacheEntryMap::iterator i = this->Cache.find(key);
  464. if (i != this->Cache.end()) {
  465. this->Cache.erase(i);
  466. }
  467. }
  468. cmCacheManager::CacheEntry* cmCacheManager::GetCacheEntry(
  469. const std::string& key)
  470. {
  471. CacheEntryMap::iterator i = this->Cache.find(key);
  472. if (i != this->Cache.end()) {
  473. return &i->second;
  474. }
  475. return nullptr;
  476. }
  477. cmCacheManager::CacheIterator cmCacheManager::GetCacheIterator(const char* key)
  478. {
  479. return CacheIterator(*this, key);
  480. }
  481. const std::string* cmCacheManager::GetInitializedCacheValue(
  482. const std::string& key) const
  483. {
  484. CacheEntryMap::const_iterator i = this->Cache.find(key);
  485. if (i != this->Cache.end() && i->second.Initialized) {
  486. return &i->second.Value;
  487. }
  488. return nullptr;
  489. }
  490. void cmCacheManager::PrintCache(std::ostream& out) const
  491. {
  492. out << "=================================================" << std::endl;
  493. out << "CMakeCache Contents:" << std::endl;
  494. for (auto const& i : this->Cache) {
  495. if (i.second.Type != cmStateEnums::INTERNAL) {
  496. out << i.first << " = " << i.second.Value << std::endl;
  497. }
  498. }
  499. out << "\n\n";
  500. out << "To change values in the CMakeCache, " << std::endl
  501. << "edit CMakeCache.txt in your output directory.\n";
  502. out << "=================================================" << std::endl;
  503. }
  504. void cmCacheManager::AddCacheEntry(const std::string& key, const char* value,
  505. const char* helpString,
  506. cmStateEnums::CacheEntryType type)
  507. {
  508. CacheEntry& e = this->Cache[key];
  509. if (value) {
  510. e.Value = value;
  511. e.Initialized = true;
  512. } else {
  513. e.Value.clear();
  514. }
  515. e.Type = type;
  516. // make sure we only use unix style paths
  517. if (type == cmStateEnums::FILEPATH || type == cmStateEnums::PATH) {
  518. if (e.Value.find(';') != std::string::npos) {
  519. std::vector<std::string> paths;
  520. cmSystemTools::ExpandListArgument(e.Value, paths);
  521. const char* sep = "";
  522. e.Value = "";
  523. for (std::string& i : paths) {
  524. cmSystemTools::ConvertToUnixSlashes(i);
  525. e.Value += sep;
  526. e.Value += i;
  527. sep = ";";
  528. }
  529. } else {
  530. cmSystemTools::ConvertToUnixSlashes(e.Value);
  531. }
  532. }
  533. e.SetProperty("HELPSTRING",
  534. helpString
  535. ? helpString
  536. : "(This variable does not exist and should not be used)");
  537. }
  538. bool cmCacheManager::CacheIterator::IsAtEnd() const
  539. {
  540. return this->Position == this->Container.Cache.end();
  541. }
  542. void cmCacheManager::CacheIterator::Begin()
  543. {
  544. this->Position = this->Container.Cache.begin();
  545. }
  546. bool cmCacheManager::CacheIterator::Find(const std::string& key)
  547. {
  548. this->Position = this->Container.Cache.find(key);
  549. return !this->IsAtEnd();
  550. }
  551. void cmCacheManager::CacheIterator::Next()
  552. {
  553. if (!this->IsAtEnd()) {
  554. ++this->Position;
  555. }
  556. }
  557. std::vector<std::string> cmCacheManager::CacheIterator::GetPropertyList() const
  558. {
  559. return this->GetEntry().GetPropertyList();
  560. }
  561. void cmCacheManager::CacheIterator::SetValue(const char* value)
  562. {
  563. if (this->IsAtEnd()) {
  564. return;
  565. }
  566. CacheEntry* entry = &this->GetEntry();
  567. if (value) {
  568. entry->Value = value;
  569. entry->Initialized = true;
  570. } else {
  571. entry->Value.clear();
  572. }
  573. }
  574. bool cmCacheManager::CacheIterator::GetValueAsBool() const
  575. {
  576. return cmSystemTools::IsOn(this->GetEntry().Value);
  577. }
  578. std::vector<std::string> cmCacheManager::CacheEntry::GetPropertyList() const
  579. {
  580. return this->Properties.GetPropertyList();
  581. }
  582. const char* cmCacheManager::CacheEntry::GetProperty(
  583. const std::string& prop) const
  584. {
  585. if (prop == "TYPE") {
  586. return cmState::CacheEntryTypeToString(this->Type);
  587. }
  588. if (prop == "VALUE") {
  589. return this->Value.c_str();
  590. }
  591. return this->Properties.GetPropertyValue(prop);
  592. }
  593. void cmCacheManager::CacheEntry::SetProperty(const std::string& prop,
  594. const char* value)
  595. {
  596. if (prop == "TYPE") {
  597. this->Type = cmState::StringToCacheEntryType(value ? value : "STRING");
  598. } else if (prop == "VALUE") {
  599. this->Value = value ? value : "";
  600. } else {
  601. this->Properties.SetProperty(prop, value);
  602. }
  603. }
  604. void cmCacheManager::CacheEntry::AppendProperty(const std::string& prop,
  605. const char* value,
  606. bool asString)
  607. {
  608. if (prop == "TYPE") {
  609. this->Type = cmState::StringToCacheEntryType(value ? value : "STRING");
  610. } else if (prop == "VALUE") {
  611. if (value) {
  612. if (!this->Value.empty() && *value && !asString) {
  613. this->Value += ";";
  614. }
  615. this->Value += value;
  616. }
  617. } else {
  618. this->Properties.AppendProperty(prop, value, asString);
  619. }
  620. }
  621. const char* cmCacheManager::CacheIterator::GetProperty(
  622. const std::string& prop) const
  623. {
  624. if (!this->IsAtEnd()) {
  625. return this->GetEntry().GetProperty(prop);
  626. }
  627. return nullptr;
  628. }
  629. void cmCacheManager::CacheIterator::SetProperty(const std::string& p,
  630. const char* v)
  631. {
  632. if (!this->IsAtEnd()) {
  633. this->GetEntry().SetProperty(p, v);
  634. }
  635. }
  636. void cmCacheManager::CacheIterator::AppendProperty(const std::string& p,
  637. const char* v,
  638. bool asString)
  639. {
  640. if (!this->IsAtEnd()) {
  641. this->GetEntry().AppendProperty(p, v, asString);
  642. }
  643. }
  644. bool cmCacheManager::CacheIterator::GetPropertyAsBool(
  645. const std::string& prop) const
  646. {
  647. if (const char* value = this->GetProperty(prop)) {
  648. return cmSystemTools::IsOn(value);
  649. }
  650. return false;
  651. }
  652. void cmCacheManager::CacheIterator::SetProperty(const std::string& p, bool v)
  653. {
  654. this->SetProperty(p, v ? "ON" : "OFF");
  655. }
  656. bool cmCacheManager::CacheIterator::PropertyExists(
  657. const std::string& prop) const
  658. {
  659. return this->GetProperty(prop) != nullptr;
  660. }