cmCacheManager.cxx 26 KB

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