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 "cmGeneratedFileStream.h"
  11. #include "cmMessenger.h"
  12. #include "cmState.h"
  13. #include "cmSystemTools.h"
  14. #include "cmVersion.h"
  15. #include "cmake.h"
  16. cmCacheManager::cmCacheManager()
  17. {
  18. this->CacheMajorVersion = 0;
  19. this->CacheMinorVersion = 0;
  20. }
  21. void cmCacheManager::CleanCMakeFiles(const std::string& path)
  22. {
  23. std::string glob = path;
  24. glob += cmake::GetCMakeFilesDirectory();
  25. glob += "/*.cmake";
  26. cmsys::Glob globIt;
  27. globIt.FindFiles(glob);
  28. std::vector<std::string> files = globIt.GetFiles();
  29. std::for_each(files.begin(), files.end(), cmSystemTools::RemoveFile);
  30. }
  31. bool cmCacheManager::LoadCache(const std::string& path, bool internal,
  32. std::set<std::string>& excludes,
  33. std::set<std::string>& includes)
  34. {
  35. std::string cacheFile = path;
  36. cacheFile += "/CMakeCache.txt";
  37. // clear the old cache, if we are reading in internal values
  38. if (internal) {
  39. this->Cache.clear();
  40. }
  41. if (!cmSystemTools::FileExists(cacheFile)) {
  42. this->CleanCMakeFiles(path);
  43. return false;
  44. }
  45. cmsys::ifstream fin(cacheFile.c_str());
  46. if (!fin) {
  47. return false;
  48. }
  49. const char* realbuffer;
  50. std::string buffer;
  51. std::string entryKey;
  52. unsigned int lineno = 0;
  53. while (fin) {
  54. // Format is key:type=value
  55. std::string helpString;
  56. CacheEntry e;
  57. cmSystemTools::GetLineFromStream(fin, buffer);
  58. lineno++;
  59. realbuffer = buffer.c_str();
  60. while (*realbuffer != '0' &&
  61. (*realbuffer == ' ' || *realbuffer == '\t' || *realbuffer == '\r' ||
  62. *realbuffer == '\n')) {
  63. if (*realbuffer == '\n') {
  64. lineno++;
  65. }
  66. realbuffer++;
  67. }
  68. // skip blank lines and comment lines
  69. if (realbuffer[0] == '#' || realbuffer[0] == 0) {
  70. continue;
  71. }
  72. while (realbuffer[0] == '/' && realbuffer[1] == '/') {
  73. if ((realbuffer[2] == '\\') && (realbuffer[3] == 'n')) {
  74. helpString += "\n";
  75. helpString += &realbuffer[4];
  76. } else {
  77. helpString += &realbuffer[2];
  78. }
  79. cmSystemTools::GetLineFromStream(fin, buffer);
  80. lineno++;
  81. realbuffer = buffer.c_str();
  82. if (!fin) {
  83. continue;
  84. }
  85. }
  86. e.SetProperty("HELPSTRING", helpString.c_str());
  87. if (cmState::ParseCacheEntry(realbuffer, entryKey, e.Value, e.Type)) {
  88. if (excludes.find(entryKey) == excludes.end()) {
  89. // Load internal values if internal is set.
  90. // If the entry is not internal to the cache being loaded
  91. // or if it is in the list of internal entries to be
  92. // imported, load it.
  93. if (internal || (e.Type != cmStateEnums::INTERNAL) ||
  94. (includes.find(entryKey) != includes.end())) {
  95. // If we are loading the cache from another project,
  96. // make all loaded entries internal so that it is
  97. // not visible in the gui
  98. if (!internal) {
  99. e.Type = cmStateEnums::INTERNAL;
  100. helpString = "DO NOT EDIT, ";
  101. helpString += entryKey;
  102. helpString += " loaded from external file. "
  103. "To change this value edit this file: ";
  104. helpString += path;
  105. helpString += "/CMakeCache.txt";
  106. e.SetProperty("HELPSTRING", helpString.c_str());
  107. }
  108. if (!this->ReadPropertyEntry(entryKey, e)) {
  109. e.Initialized = true;
  110. this->Cache[entryKey] = e;
  111. }
  112. }
  113. }
  114. } else {
  115. std::ostringstream error;
  116. error << "Parse error in cache file " << cacheFile;
  117. error << " on line " << lineno << ". Offending entry: " << realbuffer;
  118. cmSystemTools::Error(error.str().c_str());
  119. }
  120. }
  121. this->CacheMajorVersion = 0;
  122. this->CacheMinorVersion = 0;
  123. if (const char* cmajor =
  124. this->GetInitializedCacheValue("CMAKE_CACHE_MAJOR_VERSION")) {
  125. unsigned int v = 0;
  126. if (sscanf(cmajor, "%u", &v) == 1) {
  127. this->CacheMajorVersion = v;
  128. }
  129. if (const char* cminor =
  130. this->GetInitializedCacheValue("CMAKE_CACHE_MINOR_VERSION")) {
  131. if (sscanf(cminor, "%u", &v) == 1) {
  132. this->CacheMinorVersion = v;
  133. }
  134. }
  135. } else {
  136. // CMake version not found in the list file.
  137. // Set as version 0.0
  138. this->AddCacheEntry("CMAKE_CACHE_MINOR_VERSION", "0",
  139. "Minor version of cmake used to create the "
  140. "current loaded cache",
  141. cmStateEnums::INTERNAL);
  142. this->AddCacheEntry("CMAKE_CACHE_MAJOR_VERSION", "0",
  143. "Major version of cmake used to create the "
  144. "current loaded cache",
  145. cmStateEnums::INTERNAL);
  146. }
  147. // check to make sure the cache directory has not
  148. // been moved
  149. const char* oldDir = this->GetInitializedCacheValue("CMAKE_CACHEFILE_DIR");
  150. if (internal && oldDir) {
  151. std::string currentcwd = path;
  152. std::string oldcwd = oldDir;
  153. cmSystemTools::ConvertToUnixSlashes(currentcwd);
  154. currentcwd += "/CMakeCache.txt";
  155. oldcwd += "/CMakeCache.txt";
  156. if (!cmSystemTools::SameFile(oldcwd, currentcwd)) {
  157. std::string message =
  158. std::string("The current CMakeCache.txt directory ") + currentcwd +
  159. std::string(" is different than the directory ") +
  160. std::string(this->GetInitializedCacheValue("CMAKE_CACHEFILE_DIR")) +
  161. std::string(" where CMakeCache.txt was created. This may result "
  162. "in binaries being created in the wrong place. If you "
  163. "are not sure, reedit the CMakeCache.txt");
  164. cmSystemTools::Error(message.c_str());
  165. }
  166. }
  167. return true;
  168. }
  169. const char* cmCacheManager::PersistentProperties[] = { "ADVANCED", "MODIFIED",
  170. "STRINGS", nullptr };
  171. bool cmCacheManager::ReadPropertyEntry(std::string const& entryKey,
  172. CacheEntry& e)
  173. {
  174. // All property entries are internal.
  175. if (e.Type != cmStateEnums::INTERNAL) {
  176. return false;
  177. }
  178. const char* end = entryKey.c_str() + entryKey.size();
  179. for (const char** p = this->PersistentProperties; *p; ++p) {
  180. std::string::size_type plen = strlen(*p) + 1;
  181. if (entryKey.size() > plen && *(end - plen) == '-' &&
  182. strcmp(end - plen + 1, *p) == 0) {
  183. std::string key = entryKey.substr(0, entryKey.size() - plen);
  184. cmCacheManager::CacheIterator it = this->GetCacheIterator(key.c_str());
  185. if (it.IsAtEnd()) {
  186. // Create an entry and store the property.
  187. CacheEntry& ne = this->Cache[key];
  188. ne.Type = cmStateEnums::UNINITIALIZED;
  189. ne.SetProperty(*p, e.Value.c_str());
  190. } else {
  191. // Store this property on its entry.
  192. it.SetProperty(*p, e.Value.c_str());
  193. }
  194. return true;
  195. }
  196. }
  197. return false;
  198. }
  199. void cmCacheManager::WritePropertyEntries(std::ostream& os, CacheIterator i,
  200. cmMessenger* messenger)
  201. {
  202. for (const char** p = this->PersistentProperties; *p; ++p) {
  203. if (const char* value = i.GetProperty(*p)) {
  204. std::string helpstring = *p;
  205. helpstring += " property for variable: ";
  206. helpstring += i.GetName();
  207. cmCacheManager::OutputHelpString(os, helpstring);
  208. std::string key = i.GetName();
  209. key += "-";
  210. key += *p;
  211. this->OutputKey(os, key);
  212. os << ":INTERNAL=";
  213. this->OutputValue(os, value);
  214. os << "\n";
  215. cmCacheManager::OutputNewlineTruncationWarning(os, key, value,
  216. messenger);
  217. }
  218. }
  219. }
  220. bool cmCacheManager::SaveCache(const std::string& path, cmMessenger* messenger)
  221. {
  222. std::string cacheFile = path;
  223. cacheFile += "/CMakeCache.txt";
  224. cmGeneratedFileStream fout(cacheFile.c_str());
  225. fout.SetCopyIfDifferent(true);
  226. if (!fout) {
  227. cmSystemTools::Error("Unable to open cache file for save. ",
  228. cacheFile.c_str());
  229. cmSystemTools::ReportLastSystemError("");
  230. return false;
  231. }
  232. // before writing the cache, update the version numbers
  233. // to the
  234. char temp[1024];
  235. sprintf(temp, "%d", cmVersion::GetMinorVersion());
  236. this->AddCacheEntry("CMAKE_CACHE_MINOR_VERSION", temp,
  237. "Minor version of cmake used to create the "
  238. "current loaded cache",
  239. cmStateEnums::INTERNAL);
  240. sprintf(temp, "%d", cmVersion::GetMajorVersion());
  241. this->AddCacheEntry("CMAKE_CACHE_MAJOR_VERSION", temp,
  242. "Major version of cmake used to create the "
  243. "current loaded cache",
  244. cmStateEnums::INTERNAL);
  245. sprintf(temp, "%d", cmVersion::GetPatchVersion());
  246. this->AddCacheEntry("CMAKE_CACHE_PATCH_VERSION", temp,
  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", 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.c_str());
  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. }