cmCacheManager.cxx 15 KB

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