From fd33bf93a5e9d86ee249cc3fde22a221dfe861e8 Mon Sep 17 00:00:00 2001
From: Bill Hoffman <bill.hoffman@kitware.com>
Date: Thu, 13 Dec 2007 15:54:29 -0500
Subject: ENH: fix for bug 6102, allow users to change the compiler

---
 Source/cmGlobalUnixMakefileGenerator3.cxx | 22 +++++++++-
 Source/cmMakefileTargetGenerator.cxx      | 12 +++++
 Source/cmake.cxx                          | 73 ++++++++++++++++++++++++++++++-
 Source/cmake.h                            |  2 +
 4 files changed, 106 insertions(+), 3 deletions(-)

diff --git a/Source/cmGlobalUnixMakefileGenerator3.cxx b/Source/cmGlobalUnixMakefileGenerator3.cxx
index 0eefef3..f3a5dde 100644
--- a/Source/cmGlobalUnixMakefileGenerator3.cxx
+++ b/Source/cmGlobalUnixMakefileGenerator3.cxx
@@ -63,7 +63,7 @@ void cmGlobalUnixMakefileGenerator3
                            " not set, after EnableLanguage");
       continue;
       }
-    const char* name = mf->GetRequiredDefinition(langComp.c_str());
+    const char* name = mf->GetRequiredDefinition(langComp.c_str()); 
     if(!cmSystemTools::FileIsFullPath(name))
       {
       path = cmSystemTools::FindProgram(name);
@@ -87,6 +87,26 @@ void cmGlobalUnixMakefileGenerator3
       }
     std::string doc = lang;
     doc += " compiler.";
+    const char* cname = this->GetCMakeInstance()->
+      GetCacheManager()->GetCacheValue(langComp.c_str());
+    std::string changeVars;
+    if(cname && (path != cname))
+      {
+      const char* cvars = 
+        this->GetCMakeInstance()->GetProperty(
+          "__CMAKE_DELETE_CACHE_CHANGE_VARS_");
+      if(cvars)
+        {
+        changeVars += cvars;
+        changeVars += ";";
+        }
+      changeVars += langComp;
+      changeVars += ";";
+      changeVars += cname;
+      this->GetCMakeInstance()->SetProperty(
+        "__CMAKE_DELETE_CACHE_CHANGE_VARS_",
+        changeVars.c_str());
+      }
     mf->AddCacheDefinition(langComp.c_str(), path.c_str(),
                            doc.c_str(), cmCacheManager::FILEPATH);
     }
diff --git a/Source/cmMakefileTargetGenerator.cxx b/Source/cmMakefileTargetGenerator.cxx
index 70075b1..d3da5bb 100644
--- a/Source/cmMakefileTargetGenerator.cxx
+++ b/Source/cmMakefileTargetGenerator.cxx
@@ -236,6 +236,18 @@ void cmMakefileTargetGenerator::WriteTargetLanguageFlags()
   // write language flags for target
   std::set<cmStdString> languages;
   this->Target->GetLanguages(languages);
+    // put the compiler in the rules.make file so that if it changes
+  // things rebuild
+  for(std::set<cmStdString>::const_iterator l = languages.begin();
+      l != languages.end(); ++l)
+    {
+    cmStdString compiler = "CMAKE_";
+    compiler += *l;
+    compiler += "_COMPILER";
+    *this->FlagFileStream << "# compile " << l->c_str() << " with " << 
+      this->Makefile->GetSafeDefinition(compiler.c_str()) << "\n";
+    }
+
   for(std::set<cmStdString>::const_iterator l = languages.begin();
       l != languages.end(); ++l)
     {
diff --git a/Source/cmake.cxx b/Source/cmake.cxx
index 4eb92ad..6b89319 100644
--- a/Source/cmake.cxx
+++ b/Source/cmake.cxx
@@ -224,7 +224,7 @@ void cmake::CleanupCommandsAndMacros()
   for(RegisteredCommandsMap::iterator j = this->Commands.begin();
       j != this->Commands.end(); ++j)
     {
-    if ( !j->second->IsA("cmMacroHelperCommand") && 
+    if ( !j->second->IsA("cmMacroHelpperCommand") && 
          !j->second->IsA("cmFunctionHelperCommand"))
       {
       commands.push_back(j->second);
@@ -1793,9 +1793,79 @@ int cmake::DoPreConfigureChecks()
     }
   return 1;
 }
+struct SaveCacheEntry
+{
+  std::string key;
+  std::string value;
+  std::string help;
+  cmCacheManager::CacheEntryType type;
+};
+
+int cmake::HandleDeleteCacheVariables(const char* var)
+{
+  std::vector<std::string> argsSplit;
+  cmSystemTools::ExpandListArgument(std::string(var), argsSplit);
+  // erase the property to avoid infinite recursion
+  this->SetProperty("__CMAKE_DELETE_CACHE_CHANGE_VARS_", "");
+
+  cmCacheManager::CacheIterator ci = this->CacheManager->NewIterator();
+  std::vector<SaveCacheEntry> saved;
+  cmOStringStream warning;
+  warning 
+    << "You have changed variables that require your cache to be deleted.\n"
+    << "Configure will be re-run and you may have to reset some variables.\n"
+    << "The following variables have changed:\n";
+  for(std::vector<std::string>::iterator i = argsSplit.begin();
+      i != argsSplit.end(); ++i)
+    { 
+    SaveCacheEntry save;
+    save.key = *i;
+    warning << *i << "= ";
+    i++;
+    save.value = *i;
+    warning << *i << "\n";
+    if(ci.Find(save.key.c_str()))
+      {
+      save.type = ci.GetType();
+      save.help = ci.GetProperty("HELPSTRING");
+      }
+    saved.push_back(save);
+    }
+  
+  // remove the cache
+  this->CacheManager->DeleteCache(this->GetStartOutputDirectory());
+  // load the empty cache
+  this->LoadCache();
+  // restore the changed compilers
+  for(std::vector<SaveCacheEntry>::iterator i = saved.begin();
+      i != saved.end(); ++i)
+    {
+    this->AddCacheEntry(i->key.c_str(), i->value.c_str(),
+                        i->help.c_str(), i->type);
+    }
+  cmSystemTools::Message(warning.str().c_str());
+  // avoid reconfigure if there were errors
+  if(!cmSystemTools::GetErrorOccuredFlag())
+    {
+    // re-run configure
+    return this->Configure();
+    }
+}
 
 int cmake::Configure()
 {
+  int ret = this->ActualConfigure();
+  const char* delCacheVars = this->GetProperty("__CMAKE_DELETE_CACHE_CHANGE_VARS_");
+  if(delCacheVars && delCacheVars[0] != 0)
+    {
+    return this->HandleDeleteCacheVariables(delCacheVars);
+    }
+  return ret;
+
+}
+
+int cmake::ActualConfigure()
+{
   // Construct right now our path conversion table before it's too late:
   this->UpdateConversionPathTable();
   this->CleanupCommandsAndMacros();
@@ -1955,7 +2025,6 @@ int cmake::Configure()
 
   // actually do the configure
   this->GlobalGenerator->Configure();
-  
   // Before saving the cache
   // if the project did not define one of the entries below, add them now
   // so users can edit the values in the cache:
diff --git a/Source/cmake.h b/Source/cmake.h
index feb154d..be03bdf 100644
--- a/Source/cmake.h
+++ b/Source/cmake.h
@@ -142,6 +142,7 @@ class cmake
    * files for the tree. It will not produce any actual Makefiles, or
    * workspaces. Generate does that.  */
   int Configure();
+  int ActualConfigure();
 
   /**
    * Configure the cmMakefiles. This routine will create a GlobalGenerator if
@@ -328,6 +329,7 @@ class cmake
   static void DefineProperties(cmake *cm);
 
 protected:
+  int HandleDeleteCacheVariables(const char* var);
   cmPropertyMap Properties;
   std::set<std::pair<cmStdString,cmProperty::ScopeType> > AccessedProperties;
 
-- 
cgit v0.12