From ad1a1db73c33e35fac0dd2be75b6fb588ef9fe40 Mon Sep 17 00:00:00 2001
From: marha <marha@users.sourceforge.net>
Date: Tue, 10 Nov 2009 17:07:33 +0000
Subject: Avoid unnecessary re-compilations when environment variables are
 changed in makefiles.

---
 tools/mhmake/src/mhmake.cpp           |   1 +
 tools/mhmake/src/mhmakefileparser.cpp | 100 ++++++++++++++++++++--------------
 tools/mhmake/src/mhmakefileparser.h   |   1 +
 tools/mhmake/src/util.cpp             |   9 ---
 tools/mhmake/src/util.h               |   2 +-
 5 files changed, 61 insertions(+), 52 deletions(-)

diff --git a/tools/mhmake/src/mhmake.cpp b/tools/mhmake/src/mhmake.cpp
index 9f2699f91..69f69f241 100644
--- a/tools/mhmake/src/mhmake.cpp
+++ b/tools/mhmake/src/mhmake.cpp
@@ -106,6 +106,7 @@ int __CDECL main(int argc, char* argv[])
     {
       (*LoadMakIt)->m_pParser->BuildIncludedMakefiles();
       (*LoadMakIt)->m_pParser->ParseBuildedIncludeFiles();
+      (*LoadMakIt)->m_pParser->CheckEnv();
       LoadMakIt++;
     }
 
diff --git a/tools/mhmake/src/mhmakefileparser.cpp b/tools/mhmake/src/mhmakefileparser.cpp
index e3434e312..0ba67cc4e 100644
--- a/tools/mhmake/src/mhmakefileparser.cpp
+++ b/tools/mhmake/src/mhmakefileparser.cpp
@@ -936,6 +936,26 @@ void mhmakefileparser::RestoreEnv() const
   m_spCurEnv=NULL;
 }
 
+
+///////////////////////////////////////////////////////////////////////////////
+//
+//Checks if the variables retreived from the environment or command-line have been
+//changed. Do this at late as possible because they can also be changed in theLexer
+//makefiles.
+//
+void mhmakefileparser::CheckEnv(void)
+{
+  if (CompareEnv())
+  {
+    #ifdef _DEBUG
+    if (!g_GenProjectTree)
+      cout << "Rebuilding everything of "<< m_MakeDir->GetQuotedFullFileName() <<" because environment and/or command-line variables have been changed.\n";
+    #endif
+    SetRebuildAll();
+  }
+
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 //
 //Create a Md5 string from m_GlobalCommandLineVars and USED_ENVVARS
@@ -961,13 +981,13 @@ uint32 mhmakefileparser::CreateEnvMd5_32() const
   string Md5;
   string EnvVars=ExpandVar(USED_ENVVARS);
   const char *pTmp=EnvVars.c_str();
-  map<string,string> Variables;
 
   RestoreEnv();  // Restore the environment before creating the md5, so that we have the original environment when the makefile was parsed.
 
-  /* First create a list of variables to put in the md5 string. This is needed because a variable could be in the
-   * environment and passed on the command-line. This is especially important when switching environments were a certain
-   * variable sometimes is passed with the environment and sometimes on the command line */
+  // Now create the md5 string
+  md5_starts( &ctx );
+
+  DBGOUT(cout << "MD5 of " << m_MakeDir->GetQuotedFullFileName() << endl);
 
   while (*pTmp)
   {
@@ -975,48 +995,13 @@ uint32 mhmakefileparser::CreateEnvMd5_32() const
     pTmp=NextItem(pTmp,Var,";");
     if (!SkipVar(Var))
     {
-      string Val=GetFromEnv(Var,false);
-      transform(Var.begin(),Var.end(),Var.begin(),(int(__CDECL *)(int))toupper);
+      string Val=ExpandVar(Var);
       transform(Val.begin(),Val.end(),Val.begin(),(int(__CDECL *)(int))toupper);
       DBGOUT(cout << GetMakeDir()->GetQuotedFullFileName() << " -> Setting GetFromEnv var " << Var << " to " << Val << endl);
-      Variables[Var]=Val;
-    }
-  }
-  map<string,string>::const_iterator ItEnd=loadedmakefile::sm_Statics.m_GlobalCommandLineVars.end();
-  map<string,string>::const_iterator It=loadedmakefile::sm_Statics.m_GlobalCommandLineVars.begin();
-  while (It!=ItEnd)
-  {
-    if (!SkipVar(It->first))
-    {
-      string Var=It->first;
-      transform(Var.begin(),Var.end(),Var.begin(),(int(__CDECL *)(int))toupper);
-      string Val=It->second;
-      transform(Val.begin(),Val.end(),Val.begin(),(int(__CDECL *)(int))toupper);
-      DBGOUT(cout << GetMakeDir()->GetQuotedFullFileName() << " -> Setting Commandline var " << Var << " to " << Val << endl);
-      Variables[Var]=Val;
-    }
-    It++;
-  }
-
-  // Now create the md5 string
-  md5_starts( &ctx );
-
-  DBGOUT(cout << "MD5 of " << m_MakeDir->GetQuotedFullFileName() << endl);
-
-  map<string,string>::const_iterator VarIt=Variables.begin();
-  map<string,string>::const_iterator VarItEnd=Variables.end();
-  while (VarIt!=VarItEnd)
-  {
-    md5_update( &ctx, (uint8 *) VarIt->first.c_str(), (unsigned long)VarIt->first.size());
-
-    if (!VarIt->second.empty())
-    {
+      md5_update( &ctx, (uint8 *) Var.c_str(), (unsigned long)Var.size());
       md5_update( &ctx, (uint8 *) "=", 1);
-      md5_update( &ctx, (uint8 *) VarIt->second.c_str(), (unsigned long)VarIt->second.size());
-      DBGOUT(cout << VarIt->first << "=" << VarIt->second << endl);
+      md5_update( &ctx, (uint8 *) Val.c_str(), (unsigned long)Val.size());
     }
-    DBGOUT(else cout << VarIt->first << endl;)
-    VarIt++;
   }
 
   return md5_finish32( &ctx);
@@ -1082,8 +1067,38 @@ string mhmakefileparser::GetFromEnv(const string &Var, bool Cache) const
 
 void mhmakefileparser::CreateUSED_ENVVARS()
 {
+
+  set<string> Variables;
+
   set<string>::const_iterator It=m_UsedEnvVars.begin();
   set<string>::const_iterator ItEnd=m_UsedEnvVars.end();
+
+  while (It!=ItEnd)
+  {
+    string Var=*It;
+    if (!SkipVar(Var))
+    {
+      transform(Var.begin(),Var.end(),Var.begin(),(int(__CDECL *)(int))toupper);
+      Variables.insert(Var);
+    }
+    It++;
+  }
+
+  map<string,string>::const_iterator CLItEnd=loadedmakefile::sm_Statics.m_GlobalCommandLineVars.end();
+  map<string,string>::const_iterator CLIt=loadedmakefile::sm_Statics.m_GlobalCommandLineVars.begin();
+  while (CLIt!=CLItEnd)
+  {
+    string Var=CLIt->first;
+    if (!SkipVar(Var))
+    {
+      transform(Var.begin(),Var.end(),Var.begin(),(int(__CDECL *)(int))toupper);
+      Variables.insert(Var);
+    }
+    CLIt++;
+  }
+
+  It=Variables.begin();
+  ItEnd=Variables.end();
   string Val;
   while (It!=ItEnd)
   {
@@ -1091,6 +1106,7 @@ void mhmakefileparser::CreateUSED_ENVVARS()
     Val+=";";
     It++;
   }
+
   m_Variables[USED_ENVVARS]=Val;
 }
 
diff --git a/tools/mhmake/src/mhmakefileparser.h b/tools/mhmake/src/mhmakefileparser.h
index e5c435cec..f95d68c01 100644
--- a/tools/mhmake/src/mhmakefileparser.h
+++ b/tools/mhmake/src/mhmakefileparser.h
@@ -129,6 +129,7 @@ public:
 
   void SaveEnv();
   void RestoreEnv() const;
+  void CheckEnv(void);
 
   void SetRebuildAll()
   {
diff --git a/tools/mhmake/src/util.cpp b/tools/mhmake/src/util.cpp
index 86b6ffcd1..95f8b68c9 100644
--- a/tools/mhmake/src/util.cpp
+++ b/tools/mhmake/src/util.cpp
@@ -616,15 +616,6 @@ void loadedmakefile::LoadMakefile()
   }
   curdir::ChangeCurDir(CurDir);
 
-  if (m_pParser->CompareEnv())
-  {
-    #ifdef _DEBUG
-    if (!g_GenProjectTree)
-      cout << "Rebuilding everything of "<< m_Makefile->GetQuotedFullFileName() <<" because environment and/or command-line variables have been changed.\n";
-    #endif
-    m_pParser->SetRebuildAll();
-  }
-
 }
 
 #ifdef _DEBUG
diff --git a/tools/mhmake/src/util.h b/tools/mhmake/src/util.h
index 578f4a3a1..4bb17ef07 100644
--- a/tools/mhmake/src/util.h
+++ b/tools/mhmake/src/util.h
@@ -50,7 +50,7 @@
 #define PLATFORM     "linux"
 #endif
 
-#define MHMAKEVER    "1.4.4"
+#define MHMAKEVER    "1.4.5"
 
 #define MAKEDEPFILE  ".makefile.dep"
 
-- 
cgit v1.2.3