cmCacheManager.cxx 26 KB

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