Prechádzať zdrojové kódy

ENH: Added cmMakefile::NeedCacheCompatibility method and support for it in cmCacheManager. This will allow commands to modify their behavior when running with a cache loaded from an earlier CMake version.

Brad King 18 rokov pred
rodič
commit
7213408287

+ 38 - 3
Source/cmCacheManager.cxx

@@ -320,10 +320,27 @@ bool cmCacheManager::LoadCache(const char* path,
                            ". Offending entry: ", realbuffer);
       }
     }
-  // if CMAKE version not found in the list file
-  // add them as version 0.0
-  if(!this->GetCacheValue("CMAKE_CACHE_MINOR_VERSION"))
+  this->CacheMajorVersion = 0;
+  this->CacheMinorVersion = 0;
+  if(const char* cmajor = this->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION"))
     {
+    unsigned int v=0;
+    if(sscanf(cmajor, "%u", &v) == 1)
+      {
+      this->CacheMajorVersion = v;
+      }
+    if(const char* cminor = this->GetCacheValue("CMAKE_CACHE_MINOR_VERSION"))
+      {
+      if(sscanf(cminor, "%u", &v) == 1)
+        {
+        this->CacheMinorVersion = v;
+        }
+      }
+    }
+  else
+    {
+    // CMake version not found in the list file.
+    // Set as version 0.0
     this->AddCacheEntry("CMAKE_CACHE_MINOR_VERSION", "0",
                         "Minor version of cmake used to create the "
                         "current loaded cache", cmCacheManager::INTERNAL);
@@ -950,3 +967,21 @@ bool cmCacheManager::CacheIterator::PropertyExists(const char* property) const
     }
   return true;
 }
+
+//----------------------------------------------------------------------------
+bool cmCacheManager::NeedCacheCompatibility(int major, int minor)
+{
+  // Compatibility is not needed if the cache version is zero because
+  // the cache was created or modified by the user.
+  if(this->CacheMajorVersion == 0)
+    {
+    return false;
+    }
+
+  // Compatibility is needed if the cache version is equal to or lower
+  // than the given version.
+  unsigned int actual_compat =
+    CMake_VERSION_ENCODE(this->CacheMajorVersion, this->CacheMinorVersion, 0);
+  return (actual_compat &&
+          actual_compat <= CMake_VERSION_ENCODE(major, minor, 0));
+}

+ 9 - 1
Source/cmCacheManager.h

@@ -142,6 +142,11 @@ public:
   ///! Get a value from the cache given a key
   const char* GetCacheValue(const char* key) const;
 
+  /** Get the version of CMake that wrote the cache.  */
+  unsigned int GetCacheMajorVersion() { return this->CacheMajorVersion; }
+  unsigned int GetCacheMinorVersion() { return this->CacheMinorVersion; }
+  bool NeedCacheCompatibility(int major, int minor);
+
 protected:
   ///! Add an entry into the cache
   void AddCacheEntry(const char* key, const char* value, 
@@ -154,7 +159,10 @@ protected:
   CacheEntry *GetCacheEntry(const char *key);
   ///! Clean out the CMakeFiles directory if no CMakeCache.txt
   void CleanCMakeFiles(const char* path);
-  
+
+  // Cache version info
+  unsigned int CacheMajorVersion;
+  unsigned int CacheMinorVersion;
 private:
   typedef  std::map<cmStdString, CacheEntry> CacheEntryMap;
   static void OutputHelpString(std::ofstream& fout, 

+ 6 - 20
Source/cmMakefile.cxx

@@ -145,32 +145,18 @@ void cmMakefile::Initialize()
 
 unsigned int cmMakefile::GetCacheMajorVersion()
 {
-  if(const char* vstr =
-     this->GetCacheManager()->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION"))
-    {
-    unsigned int v=0;
-    if(sscanf(vstr, "%u", &v) == 1)
-      {
-      return v;
-      }
-    }
-  return 0;
+  return this->GetCacheManager()->GetCacheMajorVersion();
 }
 
 unsigned int cmMakefile::GetCacheMinorVersion()
 {
-  if(const char* vstr =
-     this->GetCacheManager()->GetCacheValue("CMAKE_CACHE_MINOR_VERSION"))
-    {
-    unsigned int v=0;
-    if(sscanf(vstr, "%u", &v) == 1)
-      {
-      return v;
-      }
-    }
-  return 0;
+  return this->GetCacheManager()->GetCacheMinorVersion();
 }
 
+bool cmMakefile::NeedCacheCompatibility(int major, int minor)
+{
+  return this->GetCacheManager()->NeedCacheCompatibility(major, minor);
+}
 
 cmMakefile::~cmMakefile()
 {

+ 4 - 0
Source/cmMakefile.h

@@ -58,6 +58,10 @@ public:
    */
   unsigned int GetCacheMajorVersion();
   unsigned int GetCacheMinorVersion();
+
+  /** Return whether compatibility features needed for a version of
+      the cache or lower should be enabled.  */
+  bool NeedCacheCompatibility(int major, int minor);
   
   /**
    * Construct an empty makefile.