cmCacheManager.cxx 16 KB

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