cmCacheManager.cxx 24 KB

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