cmCacheManager.cxx 24 KB

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