1
0

cmCacheManager.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. void cmCacheManager::DefineCache(cmMakefile *mf)
  234. {
  235. if (!mf)
  236. {
  237. return;
  238. }
  239. // add definition to the makefile
  240. for( std::map<cmStdString, CacheEntry>::const_iterator i = m_Cache.begin();
  241. i != m_Cache.end(); ++i)
  242. {
  243. const CacheEntry& ce = (*i).second;
  244. mf->AddDefinition((*i).first.c_str(), ce.m_Value.c_str());
  245. }
  246. }
  247. bool cmCacheManager::SaveCache(cmMakefile* mf)
  248. {
  249. return this->SaveCache(mf->GetHomeOutputDirectory());
  250. }
  251. bool cmCacheManager::SaveCache(const char* path)
  252. {
  253. std::string cacheFile = path;
  254. cacheFile += "/CMakeCache.txt";
  255. std::string tempFile = cacheFile;
  256. tempFile += ".tmp";
  257. std::ofstream fout(tempFile.c_str());
  258. if(!fout)
  259. {
  260. cmSystemTools::Error("Unable to open cache file for save. ",
  261. cacheFile.c_str());
  262. return false;
  263. }
  264. // before writting the cache, update the version numbers
  265. // to the
  266. char temp[1024];
  267. sprintf(temp, "%d", cmMakefile::GetMinorVersion());
  268. this->AddCacheEntry("CMAKE_CACHE_MINOR_VERSION", temp,
  269. "Minor version of cmake used to create the "
  270. "current loaded cache", cmCacheManager::INTERNAL);
  271. sprintf(temp, "%d", cmMakefile::GetMajorVersion());
  272. this->AddCacheEntry("CMAKE_CACHE_MAJOR_VERSION", temp,
  273. "Major version of cmake used to create the "
  274. "current loaded cache", cmCacheManager::INTERNAL);
  275. // Let us store the current working directory so that if somebody
  276. // Copies it, he will not be surprised
  277. std::string currentcwd = path;
  278. if ( currentcwd[0] >= 'A' && currentcwd[0] <= 'Z' &&
  279. currentcwd[1] == ':' )
  280. {
  281. currentcwd[0] = currentcwd[0] - 'A' + 'a';
  282. }
  283. cmSystemTools::ConvertToUnixSlashes(currentcwd);
  284. this->AddCacheEntry("CMAKE_CACHEFILE_DIR", currentcwd.c_str(),
  285. "This is the directory where this CMakeCahe.txt"
  286. " was created", cmCacheManager::INTERNAL);
  287. fout << "# This is the CMakeCache file.\n"
  288. << "# For build in directory: " << currentcwd << "\n"
  289. << "# You can edit this file to change values found and used by cmake.\n"
  290. << "# If you do not want to change any of the values, simply exit the editor.\n"
  291. << "# If you do want to change a value, simply edit, save, and exit the editor.\n"
  292. << "# The syntax for the file is as follows:\n"
  293. << "# KEY:TYPE=VALUE\n"
  294. << "# KEY is the name of a varible in the cache.\n"
  295. << "# TYPE is a hint to GUI's for the type of VALUE, DO NOT EDIT TYPE!.\n"
  296. << "# VALUE is the current value for the KEY.\n\n";
  297. fout << "########################\n";
  298. fout << "# EXTERNAL cache entries\n";
  299. fout << "########################\n";
  300. fout << "\n";
  301. for( std::map<cmStdString, CacheEntry>::const_iterator i = m_Cache.begin();
  302. i != m_Cache.end(); ++i)
  303. {
  304. const CacheEntry& ce = (*i).second;
  305. CacheEntryType t = ce.m_Type;
  306. if(t != INTERNAL)
  307. {
  308. // Format is key:type=value
  309. cmCacheManager::OutputHelpString(fout, ce.m_HelpString);
  310. std::string key;
  311. // support : in key name by double quoting
  312. if((*i).first.find(':') != std::string::npos ||
  313. (*i).first.find("//") == 0)
  314. {
  315. key = "\"";
  316. key += i->first;
  317. key += "\"";
  318. }
  319. else
  320. {
  321. key = i->first;
  322. }
  323. fout << key.c_str() << ":"
  324. << cmCacheManagerTypes[t] << "=";
  325. // if value has trailing space or tab, enclose it in single quotes
  326. if (ce.m_Value.size() &&
  327. (ce.m_Value[ce.m_Value.size() - 1] == ' ' ||
  328. ce.m_Value[ce.m_Value.size() - 1] == '\t'))
  329. {
  330. fout << '\'' << ce.m_Value << '\'';
  331. }
  332. else
  333. {
  334. fout << ce.m_Value;
  335. }
  336. fout << "\n\n";
  337. }
  338. }
  339. fout << "\n";
  340. fout << "########################\n";
  341. fout << "# INTERNAL cache entries\n";
  342. fout << "########################\n";
  343. fout << "\n";
  344. for( std::map<cmStdString, CacheEntry>::const_iterator i = m_Cache.begin();
  345. i != m_Cache.end(); ++i)
  346. {
  347. const CacheEntry& ce = (*i).second;
  348. CacheEntryType t = ce.m_Type;
  349. if(t == INTERNAL)
  350. {
  351. // Format is key:type=value
  352. cmCacheManager::OutputHelpString(fout, ce.m_HelpString);
  353. std::string key;
  354. // support : in key name by double quoting
  355. if((*i).first.find(':') != std::string::npos ||
  356. (*i).first.find("//") == 0)
  357. {
  358. key = "\"";
  359. key += i->first;
  360. key += "\"";
  361. }
  362. else
  363. {
  364. key = i->first;
  365. }
  366. fout << key.c_str() << ":"
  367. << cmCacheManagerTypes[t] << "=";
  368. // if value has trailing space or tab, enclose it in single quotes
  369. if (ce.m_Value.size() &&
  370. (ce.m_Value[ce.m_Value.size() - 1] == ' ' ||
  371. ce.m_Value[ce.m_Value.size() - 1] == '\t'))
  372. {
  373. fout << '\'' << ce.m_Value << '\'';
  374. }
  375. else
  376. {
  377. fout << ce.m_Value;
  378. }
  379. fout << "\n";
  380. }
  381. }
  382. fout << "\n";
  383. fout.close();
  384. cmSystemTools::CopyFileIfDifferent(tempFile.c_str(),
  385. cacheFile.c_str());
  386. cmSystemTools::RemoveFile(tempFile.c_str());
  387. return true;
  388. }
  389. void cmCacheManager::OutputHelpString(std::ofstream& fout,
  390. const std::string& helpString)
  391. {
  392. std::string::size_type end = helpString.size();
  393. if(end == 0)
  394. {
  395. return;
  396. }
  397. std::string oneLine;
  398. std::string::size_type pos = 0;
  399. std::string::size_type nextBreak = 60;
  400. bool done = false;
  401. while(!done)
  402. {
  403. if(nextBreak >= end)
  404. {
  405. nextBreak = end;
  406. done = true;
  407. }
  408. else
  409. {
  410. while(nextBreak < end && helpString[nextBreak] != ' ')
  411. {
  412. nextBreak++;
  413. }
  414. }
  415. oneLine = helpString.substr(pos, nextBreak - pos);
  416. fout << "//" << oneLine.c_str() << "\n";
  417. pos = nextBreak;
  418. nextBreak += 60;
  419. }
  420. }
  421. void cmCacheManager::RemoveCacheEntry(const char* key)
  422. {
  423. if(m_Cache.count(key))
  424. {
  425. m_Cache.erase(key);
  426. }
  427. else
  428. {
  429. std::cerr << "Failed to remove entry" << std::endl;
  430. }
  431. }
  432. cmCacheManager::CacheEntry *cmCacheManager::GetCacheEntry(const char* key)
  433. {
  434. if(m_Cache.count(key))
  435. {
  436. return &(m_Cache.find(key)->second);
  437. }
  438. return 0;
  439. }
  440. const char* cmCacheManager::GetCacheValue(const char* key) const
  441. {
  442. if(m_Cache.count(key))
  443. {
  444. return m_Cache.find(key)->second.m_Value.c_str();
  445. }
  446. return 0;
  447. }
  448. void cmCacheManager::PrintCache(std::ostream& out) const
  449. {
  450. out << "=================================================" << std::endl;
  451. out << "CMakeCache Contents:" << std::endl;
  452. for(std::map<cmStdString, CacheEntry>::const_iterator i = m_Cache.begin();
  453. i != m_Cache.end(); ++i)
  454. {
  455. if((*i).second.m_Type != INTERNAL)
  456. {
  457. out << (*i).first.c_str() << " = " << (*i).second.m_Value.c_str() << std::endl;
  458. }
  459. }
  460. out << "\n\n";
  461. out << "To change values in the CMakeCache, \nedit CMakeCache.txt in your output directory.\n";
  462. out << "=================================================" << std::endl;
  463. }
  464. void cmCacheManager::AddCacheEntry(const char* key,
  465. const char* value,
  466. const char* helpString,
  467. CacheEntryType type)
  468. {
  469. CacheEntry e;
  470. e.m_Value = value;
  471. e.m_Type = type;
  472. // make sure we only use unix style paths
  473. if(type == FILEPATH || type == PATH)
  474. {
  475. cmSystemTools::ConvertToUnixSlashes(e.m_Value);
  476. }
  477. e.m_HelpString = helpString;
  478. m_Cache[key] = e;
  479. }
  480. void cmCacheManager::AddCacheEntry(const char* key, bool v,
  481. const char* helpString)
  482. {
  483. if(v)
  484. {
  485. this->AddCacheEntry(key, "ON", helpString, cmCacheManager::BOOL);
  486. }
  487. else
  488. {
  489. this->AddCacheEntry(key, "OFF", helpString, cmCacheManager::BOOL);
  490. }
  491. }
  492. bool cmCacheManager::IsAdvanced(const char* key)
  493. {
  494. std::string advancedVar = key;
  495. advancedVar += "-ADVANCED";
  496. const char* value =
  497. cmCacheManager::GetInstance()->GetCacheValue(advancedVar.c_str());
  498. if(value)
  499. {
  500. return cmSystemTools::IsOn(value);
  501. }
  502. return false;
  503. }