cmCacheManager.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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 "cmRegularExpression.h"
  18. #include "stdio.h"
  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. 0
  30. };
  31. cmCacheManager::CacheEntryType cmCacheManager::StringToType(const char* s)
  32. {
  33. int i = 0;
  34. while(cmCacheManagerTypes[i])
  35. {
  36. if(strcmp(s, cmCacheManagerTypes[i]) == 0)
  37. {
  38. return static_cast<CacheEntryType>(i);
  39. }
  40. ++i;
  41. }
  42. return STRING;
  43. }
  44. bool cmCacheManager::LoadCache(cmMakefile* mf)
  45. {
  46. return this->LoadCache(mf->GetHomeOutputDirectory());
  47. }
  48. bool cmCacheManager::LoadCache(const char* path)
  49. {
  50. return this->LoadCache(path,true);
  51. }
  52. bool cmCacheManager::LoadCache(const char* path,
  53. bool internal)
  54. {
  55. std::set<std::string> emptySet;
  56. return this->LoadCache(path, internal, emptySet, emptySet);
  57. }
  58. bool cmCacheManager::ParseEntry(const char* entry,
  59. std::string& var,
  60. std::string& value,
  61. CacheEntryType& type)
  62. {
  63. // input line is: key:type=value
  64. cmRegularExpression reg("^([^:]*):([^=]*)=(.*[^\t ]|[\t ]*)[\t ]*$");
  65. // input line is: "key":type=value
  66. cmRegularExpression regQuoted("^\"([^\"]*)\":([^=]*)=(.*[^\t ]|[\t ]*)[\t ]*$");
  67. bool flag = false;
  68. if(regQuoted.find(entry))
  69. {
  70. var = regQuoted.match(1);
  71. type = cmCacheManager::StringToType(regQuoted.match(2).c_str());
  72. value = regQuoted.match(3);
  73. flag = true;
  74. }
  75. else if (reg.find(entry))
  76. {
  77. var = reg.match(1);
  78. type = cmCacheManager::StringToType(reg.match(2).c_str());
  79. value = reg.match(3);
  80. flag = true;
  81. }
  82. // if value is enclosed in single quotes ('foo') then remove them
  83. // it is used to enclose trailing space or tab
  84. if (flag &&
  85. value.size() >= 2 &&
  86. value[0] == '\'' &&
  87. value[value.size() - 1] == '\'')
  88. {
  89. value = value.substr(1,
  90. value.size() - 2);
  91. }
  92. return flag;
  93. }
  94. bool cmCacheManager::LoadCache(const char* path,
  95. bool internal,
  96. std::set<std::string>& excludes,
  97. std::set<std::string>& includes)
  98. {
  99. std::string cacheFile = path;
  100. cacheFile += "/CMakeCache.txt";
  101. // clear the old cache, if we are reading in internal values
  102. if ( internal )
  103. {
  104. m_Cache.clear();
  105. }
  106. std::ifstream fin(cacheFile.c_str());
  107. if(!fin)
  108. {
  109. return false;
  110. }
  111. const int bsize = 4096;
  112. char buffer[bsize];
  113. char *realbuffer;
  114. // input line is: key:type=value
  115. cmRegularExpression reg("^([^:]*):([^=]*)=(.*[^\t ]|[\t ]*)[\t ]*$");
  116. // input line is: "key":type=value
  117. cmRegularExpression regQuoted("^\"([^\"]*)\":([^=]*)=(.*[^\t ]|[\t ]*)[\t ]*$");
  118. std::string entryKey;
  119. while(fin)
  120. {
  121. // Format is key:type=value
  122. CacheEntry e;
  123. fin.getline(buffer, bsize);
  124. realbuffer = buffer;
  125. while(*realbuffer != '0' &&
  126. (*realbuffer == ' ' ||
  127. *realbuffer == '\t' ||
  128. *realbuffer == '\n'))
  129. {
  130. realbuffer++;
  131. }
  132. // skip blank lines and comment lines
  133. if(realbuffer[0] == '#' || realbuffer[0] == 0)
  134. {
  135. continue;
  136. }
  137. while(realbuffer[0] == '/' && realbuffer[1] == '/')
  138. {
  139. e.m_HelpString += &realbuffer[2];
  140. fin.getline(realbuffer, bsize);
  141. if(!fin)
  142. {
  143. continue;
  144. }
  145. }
  146. if(cmCacheManager::ParseEntry(realbuffer, entryKey, e.m_Value, e.m_Type))
  147. {
  148. if ( excludes.find(entryKey) == excludes.end() )
  149. {
  150. // Load internal values if internal is set.
  151. // If the entry is not internal to the cache being loaded
  152. // or if it is in the list of internal entries to be
  153. // imported, load it.
  154. if ( internal || (e.m_Type != INTERNAL) ||
  155. (includes.find(entryKey) != includes.end()) )
  156. {
  157. // If we are loading the cache from another project,
  158. // make all loaded entries internal so that it is
  159. // not visible in the gui
  160. if (!internal)
  161. {
  162. e.m_Type = INTERNAL;
  163. e.m_HelpString = "DO NOT EDIT, ";
  164. e.m_HelpString += entryKey;
  165. e.m_HelpString += " loaded from external file. "
  166. "To change this value edit this file: ";
  167. e.m_HelpString += path;
  168. e.m_HelpString += "/CMakeCache.txt" ;
  169. }
  170. m_Cache[entryKey] = e;
  171. }
  172. }
  173. }
  174. else
  175. {
  176. cmSystemTools::Error("Parse error in cache file ", cacheFile.c_str(),
  177. ". Offending entry: ", realbuffer);
  178. }
  179. }
  180. // if CMAKE version not found in the list file
  181. // add them as version 0.0
  182. if(!this->GetCacheValue("CMAKE_CACHE_MINOR_VERSION"))
  183. {
  184. this->AddCacheEntry("CMAKE_CACHE_MINOR_VERSION", "0",
  185. "Minor version of cmake used to create the "
  186. "current loaded cache", cmCacheManager::INTERNAL);
  187. this->AddCacheEntry("CMAKE_CACHE_MAJOR_VERSION", "0",
  188. "Major version of cmake used to create the "
  189. "current loaded cache", cmCacheManager::INTERNAL);
  190. }
  191. // check to make sure the cache directory has not
  192. // been moved
  193. if ( internal && this->GetCacheValue("CMAKE_CACHEFILE_DIR") )
  194. {
  195. std::string currentcwd = path;
  196. std::string oldcwd = this->GetCacheValue("CMAKE_CACHEFILE_DIR");
  197. cmSystemTools::ConvertToUnixSlashes(currentcwd);
  198. currentcwd += "/CMakeCache.txt";
  199. oldcwd += "/CMakeCache.txt";
  200. if(!cmSystemTools::SameFile(oldcwd.c_str(), currentcwd.c_str()))
  201. {
  202. std::string message =
  203. std::string("The current CMakeCache.txt directory ") +
  204. currentcwd + std::string(" is different than the directory ") +
  205. std::string(this->GetCacheValue("CMAKE_CACHEFILE_DIR")) +
  206. std::string(" where CMackeCache.txt was created. This may result "
  207. "in binaries being created in the wrong place. If you "
  208. "are not sure, reedit the CMakeCache.txt");
  209. cmSystemTools::Error(message.c_str());
  210. }
  211. }
  212. return true;
  213. }
  214. bool cmCacheManager::SaveCache(cmMakefile* mf)
  215. {
  216. return this->SaveCache(mf->GetHomeOutputDirectory());
  217. }
  218. bool cmCacheManager::SaveCache(const char* path)
  219. {
  220. std::string cacheFile = path;
  221. cacheFile += "/CMakeCache.txt";
  222. std::string tempFile = cacheFile;
  223. tempFile += ".tmp";
  224. std::ofstream fout(tempFile.c_str());
  225. if(!fout)
  226. {
  227. cmSystemTools::Error("Unable to open cache file for save. ",
  228. cacheFile.c_str());
  229. return false;
  230. }
  231. // before writting the cache, update the version numbers
  232. // to the
  233. char temp[1024];
  234. sprintf(temp, "%d", cmMakefile::GetMinorVersion());
  235. this->AddCacheEntry("CMAKE_CACHE_MINOR_VERSION", temp,
  236. "Minor version of cmake used to create the "
  237. "current loaded cache", cmCacheManager::INTERNAL);
  238. sprintf(temp, "%d", cmMakefile::GetMajorVersion());
  239. this->AddCacheEntry("CMAKE_CACHE_MAJOR_VERSION", temp,
  240. "Major version of cmake used to create the "
  241. "current loaded cache", cmCacheManager::INTERNAL);
  242. // Let us store the current working directory so that if somebody
  243. // Copies it, he will not be surprised
  244. std::string currentcwd = path;
  245. if ( currentcwd[0] >= 'A' && currentcwd[0] <= 'Z' &&
  246. currentcwd[1] == ':' )
  247. {
  248. currentcwd[0] = currentcwd[0] - 'A' + 'a';
  249. }
  250. cmSystemTools::ConvertToUnixSlashes(currentcwd);
  251. this->AddCacheEntry("CMAKE_CACHEFILE_DIR", currentcwd.c_str(),
  252. "This is the directory where this CMakeCahe.txt"
  253. " was created", cmCacheManager::INTERNAL);
  254. fout << "# This is the CMakeCache file.\n"
  255. << "# For build in directory: " << currentcwd << "\n"
  256. << "# You can edit this file to change values found and used by cmake.\n"
  257. << "# If you do not want to change any of the values, simply exit the editor.\n"
  258. << "# If you do want to change a value, simply edit, save, and exit the editor.\n"
  259. << "# The syntax for the file is as follows:\n"
  260. << "# KEY:TYPE=VALUE\n"
  261. << "# KEY is the name of a varible in the cache.\n"
  262. << "# TYPE is a hint to GUI's for the type of VALUE, DO NOT EDIT TYPE!.\n"
  263. << "# VALUE is the current value for the KEY.\n\n";
  264. fout << "########################\n";
  265. fout << "# EXTERNAL cache entries\n";
  266. fout << "########################\n";
  267. fout << "\n";
  268. for( std::map<cmStdString, CacheEntry>::const_iterator i = m_Cache.begin();
  269. i != m_Cache.end(); ++i)
  270. {
  271. const CacheEntry& ce = (*i).second;
  272. CacheEntryType t = ce.m_Type;
  273. if(t != INTERNAL)
  274. {
  275. // Format is key:type=value
  276. cmCacheManager::OutputHelpString(fout, ce.m_HelpString);
  277. std::string key;
  278. // support : in key name by double quoting
  279. if((*i).first.find(':') != std::string::npos ||
  280. (*i).first.find("//") == 0)
  281. {
  282. key = "\"";
  283. key += i->first;
  284. key += "\"";
  285. }
  286. else
  287. {
  288. key = i->first;
  289. }
  290. fout << key.c_str() << ":"
  291. << cmCacheManagerTypes[t] << "=";
  292. // if value has trailing space or tab, enclose it in single quotes
  293. if (ce.m_Value.size() &&
  294. (ce.m_Value[ce.m_Value.size() - 1] == ' ' ||
  295. ce.m_Value[ce.m_Value.size() - 1] == '\t'))
  296. {
  297. fout << '\'' << ce.m_Value << '\'';
  298. }
  299. else
  300. {
  301. fout << ce.m_Value;
  302. }
  303. fout << "\n\n";
  304. }
  305. }
  306. fout << "\n";
  307. fout << "########################\n";
  308. fout << "# INTERNAL cache entries\n";
  309. fout << "########################\n";
  310. fout << "\n";
  311. for( std::map<cmStdString, CacheEntry>::const_iterator i = m_Cache.begin();
  312. i != m_Cache.end(); ++i)
  313. {
  314. const CacheEntry& ce = (*i).second;
  315. CacheEntryType t = ce.m_Type;
  316. if(t == INTERNAL)
  317. {
  318. // Format is key:type=value
  319. cmCacheManager::OutputHelpString(fout, ce.m_HelpString);
  320. std::string key;
  321. // support : in key name by double quoting
  322. if((*i).first.find(':') != std::string::npos ||
  323. (*i).first.find("//") == 0)
  324. {
  325. key = "\"";
  326. key += i->first;
  327. key += "\"";
  328. }
  329. else
  330. {
  331. key = i->first;
  332. }
  333. fout << key.c_str() << ":"
  334. << cmCacheManagerTypes[t] << "=";
  335. // if value has trailing space or tab, enclose it in single quotes
  336. if (ce.m_Value.size() &&
  337. (ce.m_Value[ce.m_Value.size() - 1] == ' ' ||
  338. ce.m_Value[ce.m_Value.size() - 1] == '\t'))
  339. {
  340. fout << '\'' << ce.m_Value << '\'';
  341. }
  342. else
  343. {
  344. fout << ce.m_Value;
  345. }
  346. fout << "\n";
  347. }
  348. }
  349. fout << "\n";
  350. fout.close();
  351. cmSystemTools::CopyFileIfDifferent(tempFile.c_str(),
  352. cacheFile.c_str());
  353. cmSystemTools::RemoveFile(tempFile.c_str());
  354. return true;
  355. }
  356. void cmCacheManager::OutputHelpString(std::ofstream& fout,
  357. const std::string& helpString)
  358. {
  359. std::string::size_type end = helpString.size();
  360. if(end == 0)
  361. {
  362. return;
  363. }
  364. std::string oneLine;
  365. std::string::size_type pos = 0;
  366. std::string::size_type nextBreak = 60;
  367. bool done = false;
  368. while(!done)
  369. {
  370. if(nextBreak >= end)
  371. {
  372. nextBreak = end;
  373. done = true;
  374. }
  375. else
  376. {
  377. while(nextBreak < end && helpString[nextBreak] != ' ')
  378. {
  379. nextBreak++;
  380. }
  381. }
  382. oneLine = helpString.substr(pos, nextBreak - pos);
  383. fout << "//" << oneLine.c_str() << "\n";
  384. pos = nextBreak;
  385. nextBreak += 60;
  386. }
  387. }
  388. void cmCacheManager::RemoveCacheEntry(const char* key)
  389. {
  390. CacheEntryMap::iterator i = m_Cache.find(key);
  391. if(i != m_Cache.end())
  392. {
  393. m_Cache.erase(i);
  394. }
  395. else
  396. {
  397. std::cerr << "Failed to remove entry:" << key << std::endl;
  398. }
  399. }
  400. cmCacheManager::CacheEntry *cmCacheManager::GetCacheEntry(const char* key)
  401. {
  402. CacheEntryMap::iterator i = m_Cache.find(key);
  403. if(i != m_Cache.end())
  404. {
  405. return &i->second;
  406. }
  407. return 0;
  408. }
  409. const char* cmCacheManager::GetCacheValue(const char* key) const
  410. {
  411. CacheEntryMap::const_iterator i = m_Cache.find(key);
  412. if(i != m_Cache.end())
  413. {
  414. return i->second.m_Value.c_str();
  415. }
  416. return 0;
  417. }
  418. void cmCacheManager::PrintCache(std::ostream& out) const
  419. {
  420. out << "=================================================" << std::endl;
  421. out << "CMakeCache Contents:" << std::endl;
  422. for(std::map<cmStdString, CacheEntry>::const_iterator i = m_Cache.begin();
  423. i != m_Cache.end(); ++i)
  424. {
  425. if((*i).second.m_Type != INTERNAL)
  426. {
  427. out << (*i).first.c_str() << " = " << (*i).second.m_Value.c_str() << std::endl;
  428. }
  429. }
  430. out << "\n\n";
  431. out << "To change values in the CMakeCache, \nedit CMakeCache.txt in your output directory.\n";
  432. out << "=================================================" << std::endl;
  433. }
  434. void cmCacheManager::AddCacheEntry(const char* key,
  435. const char* value,
  436. const char* helpString,
  437. CacheEntryType type)
  438. {
  439. CacheEntry e;
  440. e.m_Value = value;
  441. e.m_Type = type;
  442. // make sure we only use unix style paths
  443. if(type == FILEPATH || type == PATH)
  444. {
  445. cmSystemTools::ConvertToUnixSlashes(e.m_Value);
  446. }
  447. e.m_HelpString = helpString;
  448. m_Cache[key] = e;
  449. }
  450. void cmCacheManager::AddCacheEntry(const char* key, bool v,
  451. const char* helpString)
  452. {
  453. if(v)
  454. {
  455. this->AddCacheEntry(key, "ON", helpString, cmCacheManager::BOOL);
  456. }
  457. else
  458. {
  459. this->AddCacheEntry(key, "OFF", helpString, cmCacheManager::BOOL);
  460. }
  461. }
  462. bool cmCacheManager::IsAdvanced(const char* key)
  463. {
  464. std::string advancedVar = key;
  465. advancedVar += "-ADVANCED";
  466. const char* value = this->GetCacheValue(advancedVar.c_str());
  467. if(value)
  468. {
  469. return cmSystemTools::IsOn(value);
  470. }
  471. return false;
  472. }
  473. bool cmCacheManager::CacheIterator::IsAtEnd()
  474. {
  475. return position == m_Container.m_Cache.end();
  476. }
  477. void cmCacheManager::CacheIterator::Begin()
  478. {
  479. position = m_Container.m_Cache.begin();
  480. }
  481. void cmCacheManager::CacheIterator::Next()
  482. {
  483. ++position;
  484. }