STYLE: Fix line-too-long
[cmake.git] / Source / cmCacheManager.cxx
blob55e37a90ad02161145c248ea1d0543db6b92d738
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmCacheManager.cxx,v $
5 Language: C++
6 Date: $Date: 2008-08-25 14:31:28 $
7 Version: $Revision: 1.101 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
18 #include "cmCacheManager.h"
19 #include "cmSystemTools.h"
20 #include "cmCacheManager.h"
21 #include "cmMakefile.h"
22 #include "cmake.h"
23 #include "cmVersion.h"
25 #include <cmsys/Directory.hxx>
26 #include <cmsys/Glob.hxx>
28 #include <cmsys/RegularExpression.hxx>
30 #if defined(_WIN32) || defined(__CYGWIN__)
31 # include <windows.h>
32 #endif // _WIN32
34 const char* cmCacheManagerTypes[] =
35 { "BOOL",
36 "PATH",
37 "FILEPATH",
38 "STRING",
39 "INTERNAL",
40 "STATIC",
41 "UNINITIALIZED",
45 cmCacheManager::cmCacheManager()
47 this->CacheMajorVersion = 0;
48 this->CacheMinorVersion = 0;
51 const char* cmCacheManager::TypeToString(cmCacheManager::CacheEntryType type)
53 if ( type > 6 )
55 return cmCacheManagerTypes[6];
57 return cmCacheManagerTypes[type];
60 cmCacheManager::CacheEntryType cmCacheManager::StringToType(const char* s)
62 int i = 0;
63 while(cmCacheManagerTypes[i])
65 if(strcmp(s, cmCacheManagerTypes[i]) == 0)
67 return static_cast<CacheEntryType>(i);
69 ++i;
71 return STRING;
74 bool cmCacheManager::LoadCache(cmMakefile* mf)
76 return this->LoadCache(mf->GetHomeOutputDirectory());
80 bool cmCacheManager::LoadCache(const char* path)
82 return this->LoadCache(path,true);
85 bool cmCacheManager::LoadCache(const char* path,
86 bool internal)
88 std::set<cmStdString> emptySet;
89 return this->LoadCache(path, internal, emptySet, emptySet);
92 bool cmCacheManager::ParseEntry(const char* entry,
93 std::string& var,
94 std::string& value)
96 // input line is: key:type=value
97 static cmsys::RegularExpression reg(
98 "^([^:]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
99 // input line is: "key":type=value
100 static cmsys::RegularExpression regQuoted(
101 "^\"([^\"]*)\"=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
102 bool flag = false;
103 if(regQuoted.find(entry))
105 var = regQuoted.match(1);
106 value = regQuoted.match(2);
107 flag = true;
109 else if (reg.find(entry))
111 var = reg.match(1);
112 value = reg.match(2);
113 flag = true;
116 // if value is enclosed in single quotes ('foo') then remove them
117 // it is used to enclose trailing space or tab
118 if (flag &&
119 value.size() >= 2 &&
120 value[0] == '\'' &&
121 value[value.size() - 1] == '\'')
123 value = value.substr(1,
124 value.size() - 2);
127 return flag;
130 bool cmCacheManager::ParseEntry(const char* entry,
131 std::string& var,
132 std::string& value,
133 CacheEntryType& type)
135 // input line is: key:type=value
136 static cmsys::RegularExpression reg(
137 "^([^:]*):([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
138 // input line is: "key":type=value
139 static cmsys::RegularExpression regQuoted(
140 "^\"([^\"]*)\":([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
141 bool flag = false;
142 if(regQuoted.find(entry))
144 var = regQuoted.match(1);
145 type = cmCacheManager::StringToType(regQuoted.match(2).c_str());
146 value = regQuoted.match(3);
147 flag = true;
149 else if (reg.find(entry))
151 var = reg.match(1);
152 type = cmCacheManager::StringToType(reg.match(2).c_str());
153 value = reg.match(3);
154 flag = true;
157 // if value is enclosed in single quotes ('foo') then remove them
158 // it is used to enclose trailing space or tab
159 if (flag &&
160 value.size() >= 2 &&
161 value[0] == '\'' &&
162 value[value.size() - 1] == '\'')
164 value = value.substr(1,
165 value.size() - 2);
168 return flag;
171 void cmCacheManager::CleanCMakeFiles(const char* path)
173 std::string glob = path;
174 glob += cmake::GetCMakeFilesDirectory();
175 glob += "/*.cmake";
176 cmsys::Glob globIt;
177 globIt.FindFiles(glob);
178 std::vector<std::string> files = globIt.GetFiles();
179 for(std::vector<std::string>::iterator i = files.begin();
180 i != files.end(); ++i)
182 cmSystemTools::RemoveFile(i->c_str());
186 bool cmCacheManager::LoadCache(const char* path,
187 bool internal,
188 std::set<cmStdString>& excludes,
189 std::set<cmStdString>& includes)
191 std::string cacheFile = path;
192 cacheFile += "/CMakeCache.txt";
193 // clear the old cache, if we are reading in internal values
194 if ( internal )
196 this->Cache.clear();
198 if(!cmSystemTools::FileExists(cacheFile.c_str()))
200 this->CleanCMakeFiles(path);
201 return false;
204 std::ifstream fin(cacheFile.c_str());
205 if(!fin)
207 return false;
209 const char *realbuffer;
210 std::string buffer;
211 std::string entryKey;
212 while(fin)
214 // Format is key:type=value
215 CacheEntry e;
216 cmSystemTools::GetLineFromStream(fin, buffer);
217 realbuffer = buffer.c_str();
218 while(*realbuffer != '0' &&
219 (*realbuffer == ' ' ||
220 *realbuffer == '\t' ||
221 *realbuffer == '\r' ||
222 *realbuffer == '\n'))
224 realbuffer++;
226 // skip blank lines and comment lines
227 if(realbuffer[0] == '#' || realbuffer[0] == 0)
229 continue;
231 while(realbuffer[0] == '/' && realbuffer[1] == '/')
233 if ((realbuffer[2] == '\\') && (realbuffer[3]=='n'))
235 e.Properties["HELPSTRING"] += "\n";
236 e.Properties["HELPSTRING"] += &realbuffer[4];
238 else
240 e.Properties["HELPSTRING"] += &realbuffer[2];
242 cmSystemTools::GetLineFromStream(fin, buffer);
243 realbuffer = buffer.c_str();
244 if(!fin)
246 continue;
249 if(cmCacheManager::ParseEntry(realbuffer, entryKey, e.Value, e.Type))
251 if ( excludes.find(entryKey) == excludes.end() )
253 // Load internal values if internal is set.
254 // If the entry is not internal to the cache being loaded
255 // or if it is in the list of internal entries to be
256 // imported, load it.
257 if ( internal || (e.Type != INTERNAL) ||
258 (includes.find(entryKey) != includes.end()) )
260 // If we are loading the cache from another project,
261 // make all loaded entries internal so that it is
262 // not visible in the gui
263 if (!internal)
265 e.Type = INTERNAL;
266 e.Properties["HELPSTRING"] = "DO NOT EDIT, ";
267 e.Properties["HELPSTRING"] += entryKey;
268 e.Properties["HELPSTRING"] += " loaded from external file. "
269 "To change this value edit this file: ";
270 e.Properties["HELPSTRING"] += path;
271 e.Properties["HELPSTRING"] += "/CMakeCache.txt" ;
273 if ( e.Type == cmCacheManager::INTERNAL &&
274 (entryKey.size() > strlen("-ADVANCED")) &&
275 strcmp(entryKey.c_str() + (entryKey.size() -
276 strlen("-ADVANCED")), "-ADVANCED") == 0 )
278 std::string value = e.Value;
279 std::string akey =
280 entryKey.substr(0, (entryKey.size() - strlen("-ADVANCED")));
281 cmCacheManager::CacheIterator it =
282 this->GetCacheIterator(akey.c_str());
283 if ( it.IsAtEnd() )
285 e.Type = cmCacheManager::UNINITIALIZED;
286 this->Cache[akey] = e;
288 if (!it.Find(akey.c_str()))
290 cmSystemTools::Error("Internal CMake error when reading cache");
292 it.SetProperty("ADVANCED", value.c_str());
294 else if ( e.Type == cmCacheManager::INTERNAL &&
295 (entryKey.size() > strlen("-MODIFIED")) &&
296 strcmp(entryKey.c_str() + (entryKey.size() -
297 strlen("-MODIFIED")), "-MODIFIED") == 0 )
299 std::string value = e.Value;
300 std::string akey =
301 entryKey.substr(0, (entryKey.size() - strlen("-MODIFIED")));
302 cmCacheManager::CacheIterator it =
303 this->GetCacheIterator(akey.c_str());
304 if ( it.IsAtEnd() )
306 e.Type = cmCacheManager::UNINITIALIZED;
307 this->Cache[akey] = e;
309 if (!it.Find(akey.c_str()))
311 cmSystemTools::Error("Internal CMake error when reading cache");
313 it.SetProperty("MODIFIED", value.c_str());
315 else
317 e.Initialized = true;
318 this->Cache[entryKey] = e;
323 else
325 cmSystemTools::Error("Parse error in cache file ", cacheFile.c_str(),
326 ". Offending entry: ", realbuffer);
329 this->CacheMajorVersion = 0;
330 this->CacheMinorVersion = 0;
331 if(const char* cmajor = this->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION"))
333 unsigned int v=0;
334 if(sscanf(cmajor, "%u", &v) == 1)
336 this->CacheMajorVersion = v;
338 if(const char* cminor = this->GetCacheValue("CMAKE_CACHE_MINOR_VERSION"))
340 if(sscanf(cminor, "%u", &v) == 1)
342 this->CacheMinorVersion = v;
346 else
348 // CMake version not found in the list file.
349 // Set as version 0.0
350 this->AddCacheEntry("CMAKE_CACHE_MINOR_VERSION", "0",
351 "Minor version of cmake used to create the "
352 "current loaded cache", cmCacheManager::INTERNAL);
353 this->AddCacheEntry("CMAKE_CACHE_MAJOR_VERSION", "0",
354 "Major version of cmake used to create the "
355 "current loaded cache", cmCacheManager::INTERNAL);
358 // check to make sure the cache directory has not
359 // been moved
360 if ( internal && this->GetCacheValue("CMAKE_CACHEFILE_DIR") )
362 std::string currentcwd = path;
363 std::string oldcwd = this->GetCacheValue("CMAKE_CACHEFILE_DIR");
364 cmSystemTools::ConvertToUnixSlashes(currentcwd);
365 currentcwd += "/CMakeCache.txt";
366 oldcwd += "/CMakeCache.txt";
367 if(!cmSystemTools::SameFile(oldcwd.c_str(), currentcwd.c_str()))
369 std::string message =
370 std::string("The current CMakeCache.txt directory ") +
371 currentcwd + std::string(" is different than the directory ") +
372 std::string(this->GetCacheValue("CMAKE_CACHEFILE_DIR")) +
373 std::string(" where CMackeCache.txt was created. This may result "
374 "in binaries being created in the wrong place. If you "
375 "are not sure, reedit the CMakeCache.txt");
376 cmSystemTools::Error(message.c_str());
379 return true;
382 bool cmCacheManager::SaveCache(cmMakefile* mf)
384 return this->SaveCache(mf->GetHomeOutputDirectory());
388 bool cmCacheManager::SaveCache(const char* path)
390 std::string cacheFile = path;
391 cacheFile += "/CMakeCache.txt";
392 std::string tempFile = cacheFile;
393 tempFile += ".tmp";
394 std::ofstream fout(tempFile.c_str());
395 if(!fout)
397 cmSystemTools::Error("Unable to open cache file for save. ",
398 cacheFile.c_str());
399 cmSystemTools::ReportLastSystemError("");
400 return false;
402 // before writing the cache, update the version numbers
403 // to the
404 char temp[1024];
405 sprintf(temp, "%d", cmVersion::GetMinorVersion());
406 this->AddCacheEntry("CMAKE_CACHE_MINOR_VERSION", temp,
407 "Minor version of cmake used to create the "
408 "current loaded cache", cmCacheManager::INTERNAL);
409 sprintf(temp, "%d", cmVersion::GetMajorVersion());
410 this->AddCacheEntry("CMAKE_CACHE_MAJOR_VERSION", temp,
411 "Major version of cmake used to create the "
412 "current loaded cache", cmCacheManager::INTERNAL);
414 this->AddCacheEntry("CMAKE_CACHE_RELEASE_VERSION",
415 cmVersion::GetReleaseVersion().c_str(),
416 "Major version of cmake used to create the "
417 "current loaded cache", cmCacheManager::INTERNAL);
419 // Let us store the current working directory so that if somebody
420 // Copies it, he will not be surprised
421 std::string currentcwd = path;
422 if ( currentcwd[0] >= 'A' && currentcwd[0] <= 'Z' &&
423 currentcwd[1] == ':' )
425 currentcwd[0] = currentcwd[0] - 'A' + 'a';
427 cmSystemTools::ConvertToUnixSlashes(currentcwd);
428 this->AddCacheEntry("CMAKE_CACHEFILE_DIR", currentcwd.c_str(),
429 "This is the directory where this CMakeCahe.txt"
430 " was created", cmCacheManager::INTERNAL);
432 fout << "# This is the CMakeCache file.\n"
433 << "# For build in directory: " << currentcwd << "\n";
434 cmCacheManager::CacheEntry* cmakeCacheEntry
435 = this->GetCacheEntry("CMAKE_COMMAND");
436 if ( cmakeCacheEntry )
438 fout << "# It was generated by CMake: " <<
439 cmakeCacheEntry->Value << std::endl;
442 fout << "# You can edit this file to change values found and used by cmake."
443 << std::endl
444 << "# If you do not want to change any of the values, simply exit the "
445 "editor." << std::endl
446 << "# If you do want to change a value, simply edit, save, and exit "
447 "the editor." << std::endl
448 << "# The syntax for the file is as follows:\n"
449 << "# KEY:TYPE=VALUE\n"
450 << "# KEY is the name of a variable in the cache.\n"
451 << "# TYPE is a hint to GUI's for the type of VALUE, DO NOT EDIT "
452 "TYPE!." << std::endl
453 << "# VALUE is the current value for the KEY.\n\n";
455 fout << "########################\n";
456 fout << "# EXTERNAL cache entries\n";
457 fout << "########################\n";
458 fout << "\n";
460 for( std::map<cmStdString, CacheEntry>::const_iterator i =
461 this->Cache.begin(); i != this->Cache.end(); ++i)
463 const CacheEntry& ce = (*i).second;
464 CacheEntryType t = ce.Type;
465 if(!ce.Initialized)
468 // This should be added in, but is not for now.
469 cmSystemTools::Error("Cache entry \"", (*i).first.c_str(),
470 "\" is uninitialized");
473 else if(t != INTERNAL)
475 // Format is key:type=value
476 std::map<cmStdString,cmStdString>::const_iterator it =
477 ce.Properties.find("HELPSTRING");
478 if ( it == ce.Properties.end() )
480 cmCacheManager::OutputHelpString(fout, "Missing description");
482 else
484 cmCacheManager::OutputHelpString(fout, it->second);
486 std::string key;
487 // support : in key name by double quoting
488 if((*i).first.find(':') != std::string::npos ||
489 (*i).first.find("//") == 0)
491 key = "\"";
492 key += i->first;
493 key += "\"";
495 else
497 key = i->first;
499 fout << key.c_str() << ":"
500 << cmCacheManagerTypes[t] << "=";
501 // if value has trailing space or tab, enclose it in single quotes
502 if (ce.Value.size() &&
503 (ce.Value[ce.Value.size() - 1] == ' ' ||
504 ce.Value[ce.Value.size() - 1] == '\t'))
506 fout << '\'' << ce.Value << '\'';
508 else
510 fout << ce.Value;
512 fout << "\n\n";
516 fout << "\n";
517 fout << "########################\n";
518 fout << "# INTERNAL cache entries\n";
519 fout << "########################\n";
520 fout << "\n";
522 for( cmCacheManager::CacheIterator i = this->NewIterator();
523 !i.IsAtEnd(); i.Next())
525 if ( !i.Initialized() )
527 continue;
530 CacheEntryType t = i.GetType();
531 bool advanced = i.PropertyExists("ADVANCED");
532 if ( advanced )
534 // Format is key:type=value
535 std::string key;
536 std::string rkey = i.GetName();
537 std::string helpstring;
538 // If this is advanced variable, we have to do some magic for
539 // backward compatibility
540 helpstring = "Advanced flag for variable: ";
541 helpstring += i.GetName();
542 rkey += "-ADVANCED";
543 cmCacheManager::OutputHelpString(fout, helpstring.c_str());
544 // support : in key name by double quoting
545 if(rkey.find(':') != std::string::npos ||
546 rkey.find("//") == 0)
548 key = "\"";
549 key += rkey;
550 key += "\"";
552 else
554 key = rkey;
556 fout << key.c_str() << ":INTERNAL="
557 << (i.GetPropertyAsBool("ADVANCED") ? "1" : "0") << "\n";
559 bool modified = i.PropertyExists("MODIFIED");
560 if ( modified )
562 // Format is key:type=value
563 std::string key;
564 std::string rkey = i.GetName();
565 std::string helpstring;
566 // If this is advanced variable, we have to do some magic for
567 // backward compatibility
568 helpstring = "Modified flag for variable: ";
569 helpstring += i.GetName();
570 rkey += "-MODIFIED";
571 cmCacheManager::OutputHelpString(fout, helpstring.c_str());
572 // support : in key name by double quoting
573 if(rkey.find(':') != std::string::npos ||
574 rkey.find("//") == 0)
576 key = "\"";
577 key += rkey;
578 key += "\"";
580 else
582 key = rkey;
584 fout << key.c_str() << ":INTERNAL="
585 << (i.GetPropertyAsBool("MODIFIED") ? "1" : "0") << "\n";
587 if(t == cmCacheManager::INTERNAL)
589 // Format is key:type=value
590 std::string key;
591 std::string rkey = i.GetName();
592 std::string helpstring;
593 const char* hs = i.GetProperty("HELPSTRING");
594 if ( hs )
596 helpstring = i.GetProperty("HELPSTRING");
598 else
600 helpstring = "";
602 cmCacheManager::OutputHelpString(fout, helpstring.c_str());
603 // support : in key name by double quoting
604 if(rkey.find(':') != std::string::npos ||
605 rkey.find("//") == 0)
607 key = "\"";
608 key += rkey;
609 key += "\"";
611 else
613 key = rkey;
615 fout << key.c_str() << ":"
616 << cmCacheManagerTypes[t] << "=";
617 // if value has trailing space or tab, enclose it in single quotes
618 std::string value = i.GetValue();
619 if (value.size() &&
620 (value[value.size() - 1] == ' ' ||
621 value[value.size() - 1] == '\t'))
623 fout << '\'' << value << '\'';
625 else
627 fout << value;
629 fout << "\n";
632 fout << "\n";
633 fout.close();
634 cmSystemTools::CopyFileIfDifferent(tempFile.c_str(),
635 cacheFile.c_str());
636 cmSystemTools::RemoveFile(tempFile.c_str());
637 std::string checkCacheFile = path;
638 checkCacheFile += cmake::GetCMakeFilesDirectory();
639 cmSystemTools::MakeDirectory(checkCacheFile.c_str());
640 checkCacheFile += "/cmake.check_cache";
641 std::ofstream checkCache(checkCacheFile.c_str());
642 if(!checkCache)
644 cmSystemTools::Error("Unable to open check cache file for write. ",
645 checkCacheFile.c_str());
646 return false;
648 checkCache << "# This file is generated by cmake for dependency checking "
649 "of the CMakeCache.txt file\n";
650 return true;
653 bool cmCacheManager::DeleteCache(const char* path)
655 std::string cacheFile = path;
656 cmSystemTools::ConvertToUnixSlashes(cacheFile);
657 std::string cmakeFiles = cacheFile;
658 cacheFile += "/CMakeCache.txt";
659 cmSystemTools::RemoveFile(cacheFile.c_str());
660 // now remove the files in the CMakeFiles directory
661 // this cleans up language cache files
662 cmsys::Directory dir;
663 cmakeFiles += cmake::GetCMakeFilesDirectory();
664 dir.Load(cmakeFiles.c_str());
665 for (unsigned long fileNum = 0;
666 fileNum < dir.GetNumberOfFiles();
667 ++fileNum)
669 if(!cmSystemTools::
670 FileIsDirectory(dir.GetFile(fileNum)))
672 std::string fullPath = cmakeFiles;
673 fullPath += "/";
674 fullPath += dir.GetFile(fileNum);
675 cmSystemTools::RemoveFile(fullPath.c_str());
678 return true;
681 void cmCacheManager::OutputHelpString(std::ofstream& fout,
682 const std::string& helpString)
684 std::string::size_type end = helpString.size();
685 if(end == 0)
687 return;
689 std::string oneLine;
690 std::string::size_type pos = 0;
691 for (std::string::size_type i=0; i<=end; i++)
693 if ((i==end)
694 || (helpString[i]=='\n')
695 || ((i-pos >= 60) && (helpString[i]==' ')))
697 fout << "//";
698 if (helpString[pos] == '\n')
700 pos++;
701 fout << "\\n";
703 oneLine = helpString.substr(pos, i - pos);
704 fout << oneLine.c_str() << "\n";
705 pos = i;
710 void cmCacheManager::RemoveCacheEntry(const char* key)
712 CacheEntryMap::iterator i = this->Cache.find(key);
713 if(i != this->Cache.end())
715 this->Cache.erase(i);
720 cmCacheManager::CacheEntry *cmCacheManager::GetCacheEntry(const char* key)
722 CacheEntryMap::iterator i = this->Cache.find(key);
723 if(i != this->Cache.end())
725 return &i->second;
727 return 0;
730 cmCacheManager::CacheIterator cmCacheManager::GetCacheIterator(
731 const char *key)
733 return CacheIterator(*this, key);
736 const char* cmCacheManager::GetCacheValue(const char* key) const
738 CacheEntryMap::const_iterator i = this->Cache.find(key);
739 if(i != this->Cache.end() &&
740 i->second.Initialized)
742 return i->second.Value.c_str();
744 return 0;
748 void cmCacheManager::PrintCache(std::ostream& out) const
750 out << "=================================================" << std::endl;
751 out << "CMakeCache Contents:" << std::endl;
752 for(std::map<cmStdString, CacheEntry>::const_iterator i =
753 this->Cache.begin(); i != this->Cache.end(); ++i)
755 if((*i).second.Type != INTERNAL)
757 out << (*i).first.c_str() << " = " << (*i).second.Value.c_str()
758 << std::endl;
761 out << "\n\n";
762 out << "To change values in the CMakeCache, "
763 << std::endl << "edit CMakeCache.txt in your output directory.\n";
764 out << "=================================================" << std::endl;
768 void cmCacheManager::AddCacheEntry(const char* key,
769 const char* value,
770 const char* helpString,
771 CacheEntryType type)
773 CacheEntry& e = this->Cache[key];
774 if ( value )
776 e.Value = value;
777 e.Initialized = true;
779 else
781 e.Value = "";
783 e.Type = type;
784 // make sure we only use unix style paths
785 if(type == FILEPATH || type == PATH)
787 cmSystemTools::ConvertToUnixSlashes(e.Value);
789 if ( helpString )
791 e.Properties["HELPSTRING"] = helpString;
793 else
795 e.Properties["HELPSTRING"] =
796 "(This variable does not exist and should not be used)";
798 this->Cache[key] = e;
801 void cmCacheManager::AddCacheEntry(const char* key, bool v,
802 const char* helpString)
804 if(v)
806 this->AddCacheEntry(key, "ON", helpString, cmCacheManager::BOOL);
808 else
810 this->AddCacheEntry(key, "OFF", helpString, cmCacheManager::BOOL);
814 bool cmCacheManager::CacheIterator::IsAtEnd() const
816 return this->Position == this->Container.Cache.end();
819 void cmCacheManager::CacheIterator::Begin()
821 this->Position = this->Container.Cache.begin();
824 bool cmCacheManager::CacheIterator::Find(const char* key)
826 this->Position = this->Container.Cache.find(key);
827 return !this->IsAtEnd();
830 void cmCacheManager::CacheIterator::Next()
832 if (!this->IsAtEnd())
834 ++this->Position;
838 void cmCacheManager::CacheIterator::SetValue(const char* value)
840 if (this->IsAtEnd())
842 return;
844 CacheEntry* entry = &this->GetEntry();
845 if ( value )
847 entry->Value = value;
848 entry->Initialized = true;
850 else
852 entry->Value = "";
856 const char* cmCacheManager::CacheIterator::GetProperty(
857 const char* property) const
859 // make sure it is not at the end
860 if (this->IsAtEnd())
862 return 0;
865 if ( !strcmp(property, "TYPE") || !strcmp(property, "VALUE") )
867 cmSystemTools::Error("Property \"", property,
868 "\" cannot be accessed through the GetProperty()");
869 return 0;
871 const CacheEntry* ent = &this->GetEntry();
872 std::map<cmStdString,cmStdString>::const_iterator it =
873 ent->Properties.find(property);
874 if ( it == ent->Properties.end() )
876 return 0;
878 return it->second.c_str();
881 void cmCacheManager::CacheIterator::SetProperty(const char* p, const char* v)
883 // make sure it is not at the end
884 if (this->IsAtEnd())
886 return;
889 if ( !strcmp(p, "TYPE") || !strcmp(p, "VALUE") )
891 cmSystemTools::Error("Property \"", p,
892 "\" cannot be accessed through the SetProperty()");
893 return;
895 CacheEntry* ent = &this->GetEntry();
896 ent->Properties[p] = v;
900 bool cmCacheManager::CacheIterator::GetValueAsBool() const
902 return cmSystemTools::IsOn(this->GetEntry().Value.c_str());
905 bool cmCacheManager::CacheIterator::GetPropertyAsBool(
906 const char* property) const
908 // make sure it is not at the end
909 if (this->IsAtEnd())
911 return false;
914 if ( !strcmp(property, "TYPE") || !strcmp(property, "VALUE") )
916 cmSystemTools::Error("Property \"", property,
917 "\" cannot be accessed through the GetPropertyAsBool()");
918 return false;
920 const CacheEntry* ent = &this->GetEntry();
921 std::map<cmStdString,cmStdString>::const_iterator it =
922 ent->Properties.find(property);
923 if ( it == ent->Properties.end() )
925 return false;
927 return cmSystemTools::IsOn(it->second.c_str());
931 void cmCacheManager::CacheIterator::SetProperty(const char* p, bool v)
933 // make sure it is not at the end
934 if (this->IsAtEnd())
936 return;
939 if ( !strcmp(p, "TYPE") || !strcmp(p, "VALUE") )
941 cmSystemTools::Error("Property \"", p,
942 "\" cannot be accessed through the SetProperty()");
943 return;
945 CacheEntry* ent = &this->GetEntry();
946 ent->Properties[p] = v ? "ON" : "OFF";
949 bool cmCacheManager::CacheIterator::PropertyExists(const char* property) const
951 // make sure it is not at the end
952 if (this->IsAtEnd())
954 return false;
957 if ( !strcmp(property, "TYPE") || !strcmp(property, "VALUE") )
959 cmSystemTools::Error("Property \"", property,
960 "\" cannot be accessed through the PropertyExists()");
961 return false;
963 const CacheEntry* ent = &this->GetEntry();
964 std::map<cmStdString,cmStdString>::const_iterator it =
965 ent->Properties.find(property);
966 if ( it == ent->Properties.end() )
968 return false;
970 return true;
973 //----------------------------------------------------------------------------
974 bool cmCacheManager::NeedCacheCompatibility(int major, int minor)
976 // Compatibility is not needed if the cache version is zero because
977 // the cache was created or modified by the user.
978 if(this->CacheMajorVersion == 0)
980 return false;
983 // Compatibility is needed if the cache version is equal to or lower
984 // than the given version.
985 unsigned int actual_compat =
986 CMake_VERSION_ENCODE(this->CacheMajorVersion, this->CacheMinorVersion, 0);
987 return (actual_compat &&
988 actual_compat <= CMake_VERSION_ENCODE(major, minor, 0));