cmCacheManager.cxx 29 KB

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