cmCacheManager.cxx 28 KB

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