cmCacheManager.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2001 Insight Consortium
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. * The name of the Insight Consortium, nor the names of any consortium members,
  17. nor of any contributors, may be used to endorse or promote products derived
  18. from this software without specific prior written permission.
  19. * Modified source versions must be plainly marked as such, and must not be
  20. misrepresented as being the original software.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
  25. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. =========================================================================*/
  32. #include "cmCacheManager.h"
  33. #include "cmSystemTools.h"
  34. #include "cmCacheManager.h"
  35. #include "cmMakefile.h"
  36. #include "cmRegularExpression.h"
  37. #include "stdio.h"
  38. #if defined(_WIN32) || defined(__CYGWIN__)
  39. # include <windows.h>
  40. #endif // _WIN32
  41. const char* cmCacheManagerTypes[] =
  42. { "BOOL",
  43. "PATH",
  44. "FILEPATH",
  45. "STRING",
  46. "INTERNAL",
  47. "STATIC",
  48. 0
  49. };
  50. cmCacheManager::CacheEntryType cmCacheManager::StringToType(const char* s)
  51. {
  52. int i = 0;
  53. while(cmCacheManagerTypes[i])
  54. {
  55. if(strcmp(s, cmCacheManagerTypes[i]) == 0)
  56. {
  57. return static_cast<CacheEntryType>(i);
  58. }
  59. ++i;
  60. }
  61. return STRING;
  62. }
  63. struct CleanUpCacheManager
  64. {
  65. ~CleanUpCacheManager()
  66. {
  67. delete cmCacheManager::GetInstance();
  68. }
  69. void Use() {}
  70. };
  71. CleanUpCacheManager cleanup;
  72. cmCacheManager* cmCacheManager::s_Instance = 0;
  73. cmCacheManager* cmCacheManager::GetInstance()
  74. {
  75. if(!cmCacheManager::s_Instance)
  76. {
  77. cleanup.Use();
  78. cmCacheManager::s_Instance = new cmCacheManager;
  79. }
  80. return cmCacheManager::s_Instance;
  81. }
  82. bool cmCacheManager::LoadCache(cmMakefile* mf)
  83. {
  84. return this->LoadCache(mf->GetHomeOutputDirectory());
  85. }
  86. bool cmCacheManager::LoadCache(const char* path)
  87. {
  88. return this->LoadCache(path,true);
  89. }
  90. bool cmCacheManager::LoadCache(const char* path,
  91. bool internal)
  92. {
  93. std::set<std::string> emptySet;
  94. return this->LoadCache(path, internal, emptySet, emptySet);
  95. }
  96. bool cmCacheManager::ParseEntry(const char* entry,
  97. std::string& var,
  98. std::string& value,
  99. CacheEntryType& type)
  100. {
  101. // input line is: key:type=value
  102. cmRegularExpression reg("^([^:]*):([^=]*)=(.*[^\t ]|[\t ]*)[\t ]*$");
  103. // input line is: "key":type=value
  104. cmRegularExpression regQuoted("^\"([^\"]*)\":([^=]*)=(.*[^\t ]|[\t ]*)[\t ]*$");
  105. bool flag = false;
  106. if(regQuoted.find(entry))
  107. {
  108. var = regQuoted.match(1);
  109. type = cmCacheManager::StringToType(regQuoted.match(2).c_str());
  110. value = regQuoted.match(3);
  111. flag = true;
  112. }
  113. else if (reg.find(entry))
  114. {
  115. var = reg.match(1);
  116. type = cmCacheManager::StringToType(reg.match(2).c_str());
  117. value = reg.match(3);
  118. flag = true;
  119. }
  120. // if value is enclosed in single quotes ('foo') then remove them
  121. // it is used to enclose trailing space or tab
  122. if (flag &&
  123. value.size() >= 2 &&
  124. value[0] == '\'' &&
  125. value[value.size() - 1] == '\'')
  126. {
  127. value = value.substr(1,
  128. value.size() - 2);
  129. }
  130. return flag;
  131. }
  132. bool cmCacheManager::LoadCache(const char* path,
  133. bool internal,
  134. std::set<std::string>& excludes,
  135. std::set<std::string>& includes)
  136. {
  137. std::string cacheFile = path;
  138. cacheFile += "/CMakeCache.txt";
  139. // clear the old cache, if we are reading in internal values
  140. if ( internal )
  141. {
  142. m_Cache.clear();
  143. }
  144. std::ifstream fin(cacheFile.c_str());
  145. if(!fin)
  146. {
  147. return false;
  148. }
  149. const int bsize = 4096;
  150. char buffer[bsize];
  151. char *realbuffer;
  152. // input line is: key:type=value
  153. cmRegularExpression reg("^([^:]*):([^=]*)=(.*[^\t ]|[\t ]*)[\t ]*$");
  154. // input line is: "key":type=value
  155. cmRegularExpression regQuoted("^\"([^\"]*)\":([^=]*)=(.*[^\t ]|[\t ]*)[\t ]*$");
  156. std::string entryKey;
  157. while(fin)
  158. {
  159. // Format is key:type=value
  160. CacheEntry e;
  161. fin.getline(buffer, bsize);
  162. realbuffer = buffer;
  163. while(*realbuffer != '0' &&
  164. (*realbuffer == ' ' ||
  165. *realbuffer == '\t' ||
  166. *realbuffer == '\n'))
  167. {
  168. realbuffer++;
  169. }
  170. // skip blank lines and comment lines
  171. if(realbuffer[0] == '#' || realbuffer[0] == 0)
  172. {
  173. continue;
  174. }
  175. while(realbuffer[0] == '/' && realbuffer[1] == '/')
  176. {
  177. e.m_HelpString += &realbuffer[2];
  178. fin.getline(realbuffer, bsize);
  179. if(!fin)
  180. {
  181. continue;
  182. }
  183. }
  184. if(cmCacheManager::ParseEntry(realbuffer, entryKey, e.m_Value, e.m_Type))
  185. {
  186. if ( excludes.find(entryKey) == excludes.end() )
  187. {
  188. // Load internal values if internal is set.
  189. // If the entry is not internal to the cache being loaded
  190. // or if it is in the list of internal entries to be
  191. // imported, load it.
  192. if ( internal || (e.m_Type != INTERNAL) ||
  193. (includes.find(entryKey) != includes.end()) )
  194. {
  195. // If we are loading the cache from another project,
  196. // make all loaded entries internal so that it is
  197. // not visible in the gui
  198. if (!internal)
  199. {
  200. e.m_Type = INTERNAL;
  201. e.m_HelpString = "DO NOT EDIT, ";
  202. e.m_HelpString += entryKey;
  203. e.m_HelpString += " loaded from external file. "
  204. "To change this value edit this file: ";
  205. e.m_HelpString += path;
  206. e.m_HelpString += "/CMakeCache.txt" ;
  207. }
  208. m_Cache[entryKey] = e;
  209. }
  210. }
  211. }
  212. else
  213. {
  214. cmSystemTools::Error("Parse error in cache file ", cacheFile.c_str(),
  215. ". Offending entry: ", realbuffer);
  216. }
  217. }
  218. // if CMAKE version not found in the list file
  219. // add them as version 0.0
  220. if(!this->GetCacheValue("CMAKE_CACHE_MINOR_VERSION"))
  221. {
  222. this->AddCacheEntry("CMAKE_CACHE_MINOR_VERSION", "0",
  223. "Minor version of cmake used to create the "
  224. "current loaded cache", cmCacheManager::INTERNAL);
  225. this->AddCacheEntry("CMAKE_CACHE_MAJOR_VERSION", "0",
  226. "Major version of cmake used to create the "
  227. "current loaded cache", cmCacheManager::INTERNAL);
  228. }
  229. // check to make sure the cache directory has not
  230. // been moved
  231. if ( internal && this->GetCacheValue("CMAKE_CACHEFILE_DIR") )
  232. {
  233. std::string currentcwd = path;
  234. std::string oldcwd = this->GetCacheValue("CMAKE_CACHEFILE_DIR");
  235. cmSystemTools::ConvertToUnixSlashes(currentcwd);
  236. currentcwd += "/CMakeCache.txt";
  237. oldcwd += "/CMakeCache.txt";
  238. if(!cmSystemTools::SameFile(oldcwd.c_str(), currentcwd.c_str()))
  239. {
  240. std::string message =
  241. std::string("The current CMakeCache.txt directory ") +
  242. currentcwd + std::string(" is different than the directory ") +
  243. std::string(this->GetCacheValue("CMAKE_CACHEFILE_DIR")) +
  244. std::string(" where CMackeCache.txt was created. This may result "
  245. "in binaries being created in the wrong place. If you "
  246. "are not sure, reedit the CMakeCache.txt");
  247. cmSystemTools::Error(message.c_str());
  248. }
  249. }
  250. return true;
  251. }
  252. void cmCacheManager::DefineCache(cmMakefile *mf)
  253. {
  254. if (!mf)
  255. {
  256. return;
  257. }
  258. // add definition to the makefile
  259. for( std::map<cmStdString, CacheEntry>::const_iterator i = m_Cache.begin();
  260. i != m_Cache.end(); ++i)
  261. {
  262. const CacheEntry& ce = (*i).second;
  263. mf->AddDefinition((*i).first.c_str(), ce.m_Value.c_str());
  264. }
  265. }
  266. bool cmCacheManager::SaveCache(cmMakefile* mf)
  267. {
  268. return this->SaveCache(mf->GetHomeOutputDirectory());
  269. }
  270. bool cmCacheManager::SaveCache(const char* path)
  271. {
  272. std::string cacheFile = path;
  273. cacheFile += "/CMakeCache.txt";
  274. std::string tempFile = cacheFile;
  275. tempFile += ".tmp";
  276. std::ofstream fout(tempFile.c_str());
  277. if(!fout)
  278. {
  279. cmSystemTools::Error("Unable to open cache file for save. ",
  280. cacheFile.c_str());
  281. return false;
  282. }
  283. // before writting the cache, update the version numbers
  284. // to the
  285. char temp[1024];
  286. sprintf(temp, "%d", cmMakefile::GetMinorVersion());
  287. this->AddCacheEntry("CMAKE_CACHE_MINOR_VERSION", temp,
  288. "Minor version of cmake used to create the "
  289. "current loaded cache", cmCacheManager::INTERNAL);
  290. sprintf(temp, "%d", cmMakefile::GetMajorVersion());
  291. this->AddCacheEntry("CMAKE_CACHE_MAJOR_VERSION", temp,
  292. "Major version of cmake used to create the "
  293. "current loaded cache", cmCacheManager::INTERNAL);
  294. // Let us store the current working directory so that if somebody
  295. // Copies it, he will not be surprised
  296. std::string currentcwd = path;
  297. if ( currentcwd[0] >= 'A' && currentcwd[0] <= 'Z' &&
  298. currentcwd[1] == ':' )
  299. {
  300. currentcwd[0] = currentcwd[0] - 'A' + 'a';
  301. }
  302. cmSystemTools::ConvertToUnixSlashes(currentcwd);
  303. this->AddCacheEntry("CMAKE_CACHEFILE_DIR", currentcwd.c_str(),
  304. "This is the directory where this CMakeCahe.txt"
  305. " was created", cmCacheManager::INTERNAL);
  306. fout << "# This is the CMakeCache file.\n"
  307. << "# For build in directory: " << currentcwd << "\n"
  308. << "# You can edit this file to change values found and used by cmake.\n"
  309. << "# If you do not want to change any of the values, simply exit the editor.\n"
  310. << "# If you do want to change a value, simply edit, save, and exit the editor.\n"
  311. << "# The syntax for the file is as follows:\n"
  312. << "# KEY:TYPE=VALUE\n"
  313. << "# KEY is the name of a varible in the cache.\n"
  314. << "# TYPE is a hint to GUI's for the type of VALUE, DO NOT EDIT TYPE!.\n"
  315. << "# VALUE is the current value for the KEY.\n\n";
  316. fout << "########################\n";
  317. fout << "# EXTERNAL cache entries\n";
  318. fout << "########################\n";
  319. fout << "\n";
  320. for( std::map<cmStdString, CacheEntry>::const_iterator i = m_Cache.begin();
  321. i != m_Cache.end(); ++i)
  322. {
  323. const CacheEntry& ce = (*i).second;
  324. CacheEntryType t = ce.m_Type;
  325. if(t != INTERNAL)
  326. {
  327. // Format is key:type=value
  328. cmCacheManager::OutputHelpString(fout, ce.m_HelpString);
  329. std::string key;
  330. // support : in key name by double quoting
  331. if((*i).first.find(':') != std::string::npos ||
  332. (*i).first.find("//") == 0)
  333. {
  334. key = "\"";
  335. key += i->first;
  336. key += "\"";
  337. }
  338. else
  339. {
  340. key = i->first;
  341. }
  342. fout << key.c_str() << ":"
  343. << cmCacheManagerTypes[t] << "=";
  344. // if value has trailing space or tab, enclose it in single quotes
  345. if (ce.m_Value.size() &&
  346. (ce.m_Value[ce.m_Value.size() - 1] == ' ' ||
  347. ce.m_Value[ce.m_Value.size() - 1] == '\t'))
  348. {
  349. fout << '\'' << ce.m_Value << '\'';
  350. }
  351. else
  352. {
  353. fout << ce.m_Value;
  354. }
  355. fout << "\n\n";
  356. }
  357. }
  358. fout << "\n";
  359. fout << "########################\n";
  360. fout << "# INTERNAL cache entries\n";
  361. fout << "########################\n";
  362. fout << "\n";
  363. for( std::map<cmStdString, CacheEntry>::const_iterator i = m_Cache.begin();
  364. i != m_Cache.end(); ++i)
  365. {
  366. const CacheEntry& ce = (*i).second;
  367. CacheEntryType t = ce.m_Type;
  368. if(t == INTERNAL)
  369. {
  370. // Format is key:type=value
  371. cmCacheManager::OutputHelpString(fout, ce.m_HelpString);
  372. std::string key;
  373. // support : in key name by double quoting
  374. if((*i).first.find(':') != std::string::npos ||
  375. (*i).first.find("//") == 0)
  376. {
  377. key = "\"";
  378. key += i->first;
  379. key += "\"";
  380. }
  381. else
  382. {
  383. key = i->first;
  384. }
  385. fout << key.c_str() << ":"
  386. << cmCacheManagerTypes[t] << "=";
  387. // if value has trailing space or tab, enclose it in single quotes
  388. if (ce.m_Value.size() &&
  389. (ce.m_Value[ce.m_Value.size() - 1] == ' ' ||
  390. ce.m_Value[ce.m_Value.size() - 1] == '\t'))
  391. {
  392. fout << '\'' << ce.m_Value << '\'';
  393. }
  394. else
  395. {
  396. fout << ce.m_Value;
  397. }
  398. fout << "\n";
  399. }
  400. }
  401. fout << "\n";
  402. fout.close();
  403. cmSystemTools::CopyFileIfDifferent(tempFile.c_str(),
  404. cacheFile.c_str());
  405. cmSystemTools::RemoveFile(tempFile.c_str());
  406. return true;
  407. }
  408. void cmCacheManager::OutputHelpString(std::ofstream& fout,
  409. const std::string& helpString)
  410. {
  411. std::string::size_type end = helpString.size();
  412. if(end == 0)
  413. {
  414. return;
  415. }
  416. std::string oneLine;
  417. std::string::size_type pos = 0;
  418. std::string::size_type nextBreak = 60;
  419. bool done = false;
  420. while(!done)
  421. {
  422. if(nextBreak >= end)
  423. {
  424. nextBreak = end;
  425. done = true;
  426. }
  427. else
  428. {
  429. while(nextBreak < end && helpString[nextBreak] != ' ')
  430. {
  431. nextBreak++;
  432. }
  433. }
  434. oneLine = helpString.substr(pos, nextBreak - pos);
  435. fout << "//" << oneLine.c_str() << "\n";
  436. pos = nextBreak;
  437. nextBreak += 60;
  438. }
  439. }
  440. void cmCacheManager::RemoveCacheEntry(const char* key)
  441. {
  442. if(m_Cache.count(key))
  443. {
  444. m_Cache.erase(key);
  445. }
  446. else
  447. {
  448. std::cerr << "Failed to remove entry" << std::endl;
  449. }
  450. }
  451. cmCacheManager::CacheEntry *cmCacheManager::GetCacheEntry(const char* key)
  452. {
  453. if(m_Cache.count(key))
  454. {
  455. return &(m_Cache.find(key)->second);
  456. }
  457. return 0;
  458. }
  459. const char* cmCacheManager::GetCacheValue(const char* key) const
  460. {
  461. if(m_Cache.count(key))
  462. {
  463. return m_Cache.find(key)->second.m_Value.c_str();
  464. }
  465. return 0;
  466. }
  467. void cmCacheManager::PrintCache(std::ostream& out) const
  468. {
  469. out << "=================================================" << std::endl;
  470. out << "CMakeCache Contents:" << std::endl;
  471. for(std::map<cmStdString, CacheEntry>::const_iterator i = m_Cache.begin();
  472. i != m_Cache.end(); ++i)
  473. {
  474. if((*i).second.m_Type != INTERNAL)
  475. {
  476. out << (*i).first.c_str() << " = " << (*i).second.m_Value.c_str() << std::endl;
  477. }
  478. }
  479. out << "\n\n";
  480. out << "To change values in the CMakeCache, \nedit CMakeCache.txt in your output directory.\n";
  481. out << "=================================================" << std::endl;
  482. }
  483. void cmCacheManager::AddCacheEntry(const char* key,
  484. const char* value,
  485. const char* helpString,
  486. CacheEntryType type)
  487. {
  488. CacheEntry e;
  489. e.m_Value = value;
  490. e.m_Type = type;
  491. // make sure we only use unix style paths
  492. if(type == FILEPATH || type == PATH)
  493. {
  494. cmSystemTools::ConvertToUnixSlashes(e.m_Value);
  495. }
  496. e.m_HelpString = helpString;
  497. m_Cache[key] = e;
  498. }
  499. void cmCacheManager::AddCacheEntry(const char* key, bool v,
  500. const char* helpString)
  501. {
  502. if(v)
  503. {
  504. this->AddCacheEntry(key, "ON", helpString, cmCacheManager::BOOL);
  505. }
  506. else
  507. {
  508. this->AddCacheEntry(key, "OFF", helpString, cmCacheManager::BOOL);
  509. }
  510. }
  511. bool cmCacheManager::IsAdvanced(const char* key)
  512. {
  513. std::string advancedVar = key;
  514. advancedVar += "-ADVANCED";
  515. const char* value =
  516. cmCacheManager::GetInstance()->GetCacheValue(advancedVar.c_str());
  517. if(value)
  518. {
  519. return cmSystemTools::IsOn(value);
  520. }
  521. return false;
  522. }