diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx
index 66357f57c41872bf8b89fc6b6d7e617d04f9d620..1de73797388fda4086526405941fdd4488b9f3b2 100644
--- a/Source/cmLocalGenerator.cxx
+++ b/Source/cmLocalGenerator.cxx
@@ -73,11 +73,6 @@ std::string cmLocalGenerator::ConvertToRelativeOutputPath(const char* p)
     m_CurrentOutputDirectory = m_Makefile->GetCurrentOutputDirectory();
     m_HomeOutputDirectory =  m_Makefile->GetHomeOutputDirectory();
     m_HomeDirectory = m_Makefile->GetHomeDirectory();
-#if defined(_WIN32) || defined(__APPLE__)
-    m_CurrentOutputDirectory = cmSystemTools::LowerCase(m_CurrentOutputDirectory);
-    m_HomeOutputDirectory = cmSystemTools::LowerCase(m_HomeOutputDirectory);
-    m_HomeDirectory = cmSystemTools::LowerCase(m_HomeDirectory);
-#endif
     if(m_RelativePathToSourceDir.size() == 0)
       {
       m_RelativePathToSourceDir = cmSystemTools::RelativePath(
@@ -102,16 +97,53 @@ std::string cmLocalGenerator::ConvertToRelativeOutputPath(const char* p)
 
   // Do the work of converting to a relative path 
   std::string pathIn = p;
+  std::string ret = pathIn;
+  if(m_CurrentOutputDirectory.size() <= ret.size())
+    {
+    std::string sub = ret.substr(0, m_CurrentOutputDirectory.size());
+    if(
 #if defined(_WIN32) || defined(__APPLE__)
-  pathIn = cmSystemTools::LowerCase(pathIn);
+      cmSystemTools::LowerCase(sub) ==
+      cmSystemTools::LowerCase(m_CurrentOutputDirectory)
+#else
+      sub == m_CurrentOutputDirectory
 #endif
-
-  std::string ret = pathIn;
-  cmSystemTools::ReplaceString(ret, m_CurrentOutputDirectory.c_str(), "");
-  cmSystemTools::ReplaceString(ret, m_HomeDirectory.c_str(),
-                               m_RelativePathToSourceDir.c_str());
-  cmSystemTools::ReplaceString(ret, m_HomeOutputDirectory.c_str(),
-                               m_RelativePathToBinaryDir.c_str());
+      )
+      {
+      ret = ret.substr(m_CurrentOutputDirectory.size(), ret.npos);
+      }
+    }
+  if(m_HomeDirectory.size() <= ret.size())
+    {
+    std::string sub = ret.substr(0, m_HomeDirectory.size());
+    if(
+#if defined(_WIN32) || defined(__APPLE__)
+      cmSystemTools::LowerCase(sub) ==
+      cmSystemTools::LowerCase(m_HomeDirectory)
+#else
+      sub == m_HomeDirectory
+#endif
+      )
+      {
+      ret = m_RelativePathToSourceDir + ret.substr(m_HomeDirectory.size(), ret.npos);
+      }
+    }
+  if(m_HomeOutputDirectory.size() <= ret.size())
+    {
+    std::string sub = ret.substr(0, m_HomeOutputDirectory.size());
+    if(
+#if defined(_WIN32) || defined(__APPLE__)
+      cmSystemTools::LowerCase(sub) ==
+      cmSystemTools::LowerCase(m_HomeOutputDirectory)
+#else
+      sub == m_HomeOutputDirectory
+#endif
+      )
+      {
+      ret = m_RelativePathToBinaryDir + ret.substr(m_HomeOutputDirectory.size(), ret.npos);
+      }
+    }
+  
   std::string relpath = m_RelativePathToBinaryDir;
   if(relpath.size())
     {
@@ -121,7 +153,14 @@ std::string cmLocalGenerator::ConvertToRelativeOutputPath(const char* p)
     {
     relpath = ".";
     }
-  if(ret == m_HomeOutputDirectoryNoSlash)
+  if(
+#if defined(_WIN32) || defined(__APPLE__)
+    cmSystemTools::LowerCase(ret) ==
+    cmSystemTools::LowerCase(m_HomeOutputDirectoryNoSlash)
+#else
+    ret == m_HomeOutputDirectoryNoSlash
+#endif
+    )
     {
     ret = relpath;
     }
diff --git a/Source/cmTryCompileCommand.cxx b/Source/cmTryCompileCommand.cxx
index 6bea62d00e2c56c4074e32a01a03be29b76d1aaa..3719c6ffb274782dafa4a4afb78d62798244b276 100644
--- a/Source/cmTryCompileCommand.cxx
+++ b/Source/cmTryCompileCommand.cxx
@@ -15,9 +15,9 @@
 
 =========================================================================*/
 #include "cmTryCompileCommand.h"
+#include "cmake.h"
 #include "cmCacheManager.h"
 #include "cmListFileCache.h"
-
 #include <cmsys/Directory.hxx>
 
 int cmTryCompileCommand::CoreTryCompileCode(
@@ -236,9 +236,11 @@ int cmTryCompileCommand::CoreTryCompileCode(
   if (srcFileSignature && clean)
     {    
     cmListFileCache::GetInstance()->FlushCache(outFileName.c_str());
-    cmTryCompileCommand::CleanupFiles(binaryDirectory);
+    if(!mf->GetCMakeInstance()->GetDebugTryCompile())
+      {
+      cmTryCompileCommand::CleanupFiles(binaryDirectory);
+      }
     }
-  
   return res;
 }
 
@@ -266,6 +268,7 @@ void cmTryCompileCommand::CleanupFiles(const char* binDir)
     {
     return;
     }
+  
   std::string bdir = binDir;
   if(bdir.find("CMakeTmp") == std::string::npos)
     {
diff --git a/Source/cmake.cxx b/Source/cmake.cxx
index bba8a382bf634e69ded3f7043c75b82edb0f2798..19172f9b5ce76f380234502a0e9739649f258d4a 100644
--- a/Source/cmake.cxx
+++ b/Source/cmake.cxx
@@ -64,6 +64,7 @@ void cmNeedBackwardsCompatibility(const std::string& variable,
 
 cmake::cmake()
 {
+  m_DebugTryCompile = false;
 #ifdef __APPLE__
   struct rlimit rlp;
   if(!getrlimit(RLIMIT_STACK, &rlp))
@@ -320,6 +321,11 @@ void cmake::SetArgs(const std::vector<std::string>& args)
       {
       // skip for now
       }
+    else if(arg.find("--debug-trycompile",0) == 0)
+      {
+      std::cout << "debug trycompile on\n";
+      this->DebugTryCompileOn();
+      }
     else if(arg.find("-G",0) == 0)
       {
       std::string value = arg.substr(2);
diff --git a/Source/cmake.h b/Source/cmake.h
index eca769448aed3ac880afc8811243fa8b2b3b80eb..d818070439ddc3c809c906abebfceb36c62e2090 100644
--- a/Source/cmake.h
+++ b/Source/cmake.h
@@ -1,3 +1,5 @@
+#ifndef cmake_h
+#define cmake_h
 /*=========================================================================
 
   Program:   CMake - Cross-Platform Makefile Generator
@@ -252,6 +254,10 @@ class cmake
   void SetScriptMode(bool mode) { m_ScriptMode = mode; }
   bool GetScriptMode() { return m_ScriptMode; }
   
+  ///! Debug the try compile stuff by not delelting the files
+  bool GetDebugTryCompile(){return m_DebugTryCompile;}
+  void DebugTryCompileOn(){m_DebugTryCompile = true;}
+  
 protected:
   typedef cmGlobalGenerator* (*CreateGeneratorFunctionType)();
   typedef std::map<cmStdString, CreateGeneratorFunctionType> RegisteredGeneratorsMap;
@@ -294,6 +300,7 @@ private:
   std::string m_CMakeCommand;
   const char* m_CXXEnvironment;
   const char* m_CCEnvironment;
+  bool m_DebugTryCompile;
 };
 
 #define CMAKE_STANDARD_OPTIONS_TABLE \
@@ -323,3 +330,4 @@ private:
    "included in each directory of a source tree with the name CMakeLists.txt.  " \
    "Users build a project by using CMake to generate a build system " \
    "for a native tool on their platform.", 0}
+#endif