Merge branch 'backport-NIOS2-CPU' into release
diff --git a/Modules/CMakeCCompiler.cmake.in b/Modules/CMakeCCompiler.cmake.in
index a1bfc70..c72e338 100644
--- a/Modules/CMakeCCompiler.cmake.in
+++ b/Modules/CMakeCCompiler.cmake.in
@@ -2,7 +2,6 @@
 set(CMAKE_C_COMPILER_ARG1 "@CMAKE_C_COMPILER_ARG1@")
 set(CMAKE_C_COMPILER_ID "@CMAKE_C_COMPILER_ID@")
 set(CMAKE_C_COMPILER_VERSION "@CMAKE_C_COMPILER_VERSION@")
-set(CMAKE_C_COMPILER_LINKS_STATICALLY "@CMAKE_C_COMPILER_LINKS_STATICALLY@")
 set(CMAKE_C_STANDARD_COMPUTED_DEFAULT "@CMAKE_C_STANDARD_COMPUTED_DEFAULT@")
 set(CMAKE_C_COMPILE_FEATURES "@CMAKE_C_COMPILE_FEATURES@")
 set(CMAKE_C90_COMPILE_FEATURES "@CMAKE_C90_COMPILE_FEATURES@")
diff --git a/Modules/CMakeCCompilerId.c.in b/Modules/CMakeCCompilerId.c.in
index b224007..63f8787 100644
--- a/Modules/CMakeCCompilerId.c.in
+++ b/Modules/CMakeCCompilerId.c.in
@@ -55,6 +55,7 @@
 #ifdef SIMULATE_VERSION_MAJOR
   require += info_simulate_version[argc];
 #endif
+  require += info_language_dialect_default[argc];
   (void)argv;
   return require;
 }
diff --git a/Modules/CMakeCInformation.cmake b/Modules/CMakeCInformation.cmake
index 0d102a1..d2417aa 100644
--- a/Modules/CMakeCInformation.cmake
+++ b/Modules/CMakeCInformation.cmake
@@ -75,10 +75,6 @@
   unset(CMAKE_C_ABI_FILES)
 endif()
 
-if(CMAKE_C_COMPILER_LINKS_STATICALLY)
-  set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS FALSE)
-endif()
-
 # This should be included before the _INIT variables are
 # used to initialize the cache.  Since the rule variables
 # have if blocks on them, users can still define them here.
diff --git a/Modules/CMakeCXXCompiler.cmake.in b/Modules/CMakeCXXCompiler.cmake.in
index 4218a6d..52e44f6 100644
--- a/Modules/CMakeCXXCompiler.cmake.in
+++ b/Modules/CMakeCXXCompiler.cmake.in
@@ -2,7 +2,6 @@
 set(CMAKE_CXX_COMPILER_ARG1 "@CMAKE_CXX_COMPILER_ARG1@")
 set(CMAKE_CXX_COMPILER_ID "@CMAKE_CXX_COMPILER_ID@")
 set(CMAKE_CXX_COMPILER_VERSION "@CMAKE_CXX_COMPILER_VERSION@")
-set(CMAKE_CXX_COMPILER_LINKS_STATICALLY "@CMAKE_CXX_COMPILER_LINKS_STATICALLY@")
 set(CMAKE_CXX_STANDARD_COMPUTED_DEFAULT "@CMAKE_CXX_STANDARD_COMPUTED_DEFAULT@")
 set(CMAKE_CXX_COMPILE_FEATURES "@CMAKE_CXX_COMPILE_FEATURES@")
 set(CMAKE_CXX98_COMPILE_FEATURES "@CMAKE_CXX98_COMPILE_FEATURES@")
diff --git a/Modules/CMakeCXXCompilerId.cpp.in b/Modules/CMakeCXXCompilerId.cpp.in
index d467507..61cd790 100644
--- a/Modules/CMakeCXXCompilerId.cpp.in
+++ b/Modules/CMakeCXXCompilerId.cpp.in
@@ -49,6 +49,7 @@
 #ifdef SIMULATE_VERSION_MAJOR
   require += info_simulate_version[argc];
 #endif
+  require += info_language_dialect_default[argc];
   (void)argv;
   return require;
 }
diff --git a/Modules/CMakeCXXInformation.cmake b/Modules/CMakeCXXInformation.cmake
index dad7969..091627b 100644
--- a/Modules/CMakeCXXInformation.cmake
+++ b/Modules/CMakeCXXInformation.cmake
@@ -74,10 +74,6 @@
   unset(CMAKE_CXX_ABI_FILES)
 endif()
 
-if(CMAKE_CXX_COMPILER_LINKS_STATICALLY)
-  set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS FALSE)
-endif()
-
 # This should be included before the _INIT variables are
 # used to initialize the cache.  Since the rule variables
 # have if blocks on them, users can still define them here.
diff --git a/Modules/CMakeDetermineCompilerId.cmake b/Modules/CMakeDetermineCompilerId.cmake
index 2e74100..81c2509 100644
--- a/Modules/CMakeDetermineCompilerId.cmake
+++ b/Modules/CMakeDetermineCompilerId.cmake
@@ -106,7 +106,6 @@
   set(MSVC_${lang}_ARCHITECTURE_ID "${MSVC_${lang}_ARCHITECTURE_ID}"
     PARENT_SCOPE)
   set(CMAKE_${lang}_CL_SHOWINCLUDES_PREFIX "${CMAKE_${lang}_CL_SHOWINCLUDES_PREFIX}" PARENT_SCOPE)
-  set(CMAKE_${lang}_COMPILER_LINKS_STATICALLY "${CMAKE_${lang}_COMPILER_LINKS_STATICALLY}" PARENT_SCOPE)
   set(CMAKE_${lang}_COMPILER_VERSION "${CMAKE_${lang}_COMPILER_VERSION}" PARENT_SCOPE)
   set(CMAKE_${lang}_SIMULATE_ID "${CMAKE_${lang}_SIMULATE_ID}" PARENT_SCOPE)
   set(CMAKE_${lang}_SIMULATE_VERSION "${CMAKE_${lang}_SIMULATE_VERSION}" PARENT_SCOPE)
@@ -533,13 +532,6 @@
       endif()
     endif()
 
-    if(UNIX)
-      execute_process(COMMAND file "${file}" OUTPUT_VARIABLE out ERROR_VARIABLE out)
-      if(out MATCHES "statically linked")
-        set(CMAKE_${lang}_COMPILER_LINKS_STATICALLY 1 PARENT_SCOPE)
-      endif()
-    endif()
-
     # Check if a valid compiler and platform were found.
     if(COMPILER_ID AND NOT COMPILER_ID_TWICE)
       set(CMAKE_${lang}_COMPILER_ID "${COMPILER_ID}")
diff --git a/Modules/CMakeFortranCompiler.cmake.in b/Modules/CMakeFortranCompiler.cmake.in
index 6b984e5..14fdd60 100644
--- a/Modules/CMakeFortranCompiler.cmake.in
+++ b/Modules/CMakeFortranCompiler.cmake.in
@@ -2,7 +2,6 @@
 set(CMAKE_Fortran_COMPILER_ARG1 "@CMAKE_Fortran_COMPILER_ARG1@")
 set(CMAKE_Fortran_COMPILER_ID "@CMAKE_Fortran_COMPILER_ID@")
 set(CMAKE_Fortran_COMPILER_VERSION "@CMAKE_Fortran_COMPILER_VERSION@")
-set(CMAKE_Fortran_COMPILER_LINKS_STATICALLY "@CMAKE_Fortran_COMPILER_LINKS_STATICALLY@")
 set(CMAKE_Fortran_PLATFORM_ID "@CMAKE_Fortran_PLATFORM_ID@")
 set(CMAKE_Fortran_SIMULATE_ID "@CMAKE_Fortran_SIMULATE_ID@")
 set(CMAKE_Fortran_SIMULATE_VERSION "@CMAKE_Fortran_SIMULATE_VERSION@")
diff --git a/Modules/CMakeFortranCompilerId.F.in b/Modules/CMakeFortranCompilerId.F.in
index 017af91..2f7f40e 100644
--- a/Modules/CMakeFortranCompilerId.F.in
+++ b/Modules/CMakeFortranCompilerId.F.in
@@ -47,6 +47,8 @@
 # define COMPILER_VERSION_PATCH HEX(__SUNPRO_F90    & 0xF)
 #elif defined(_CRAYFTN)
         PRINT *, 'INFO:compiler[Cray]'
+# define COMPILER_VERSION_MAJOR DEC(_RELEASE_MAJOR)
+# define COMPILER_VERSION_MINOR DEC(_RELEASE_MINOR)
 #elif defined(__G95__)
         PRINT *, 'INFO:compiler[G95]'
 # define COMPILER_VERSION_MAJOR DEC(__G95__)
diff --git a/Modules/CMakeFortranInformation.cmake b/Modules/CMakeFortranInformation.cmake
index aa48df7..79393d3 100644
--- a/Modules/CMakeFortranInformation.cmake
+++ b/Modules/CMakeFortranInformation.cmake
@@ -51,10 +51,6 @@
   unset(CMAKE_Fortran_ABI_FILES)
 endif()
 
-if(CMAKE_Fortran_COMPILER_LINKS_STATICALLY)
-  set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS FALSE)
-endif()
-
 # This should be included before the _INIT variables are
 # used to initialize the cache.  Since the rule variables
 # have if blocks on them, users can still define them here.
diff --git a/Modules/Compiler/AppleClang-C.cmake b/Modules/Compiler/AppleClang-C.cmake
index 5908c26..1cc72c0 100644
--- a/Modules/Compiler/AppleClang-C.cmake
+++ b/Modules/Compiler/AppleClang-C.cmake
@@ -18,6 +18,9 @@
       message(FATAL_ERROR "CMAKE_C_STANDARD_COMPUTED_DEFAULT should be set for ${CMAKE_C_COMPILER_ID} (${CMAKE_C_COMPILER}) version ${CMAKE_C_COMPILER_VERSION}")
     endif()
     set(CMAKE_C_STANDARD_DEFAULT ${CMAKE_C_STANDARD_COMPUTED_DEFAULT})
+  elseif(NOT DEFINED CMAKE_C_STANDARD_DEFAULT)
+    # Compiler id was forced so just guess the default standard level.
+    set(CMAKE_C_STANDARD_DEFAULT 99)
   endif()
 endif()
 
diff --git a/Modules/Compiler/AppleClang-CXX.cmake b/Modules/Compiler/AppleClang-CXX.cmake
index c4e342b..95bc79a 100644
--- a/Modules/Compiler/AppleClang-CXX.cmake
+++ b/Modules/Compiler/AppleClang-CXX.cmake
@@ -28,6 +28,9 @@
       message(FATAL_ERROR "CMAKE_CXX_STANDARD_COMPUTED_DEFAULT should be set for ${CMAKE_CXX_COMPILER_ID} (${CMAKE_CXX_COMPILER}) version ${CMAKE_CXX_COMPILER_VERSION}")
     endif()
     set(CMAKE_CXX_STANDARD_DEFAULT ${CMAKE_CXX_STANDARD_COMPUTED_DEFAULT})
+  elseif(NOT DEFINED CMAKE_CXX_STANDARD_DEFAULT)
+    # Compiler id was forced so just guess the default standard level.
+    set(CMAKE_CXX_STANDARD_DEFAULT 98)
   endif()
 endif()
 
diff --git a/Modules/Compiler/Clang-C.cmake b/Modules/Compiler/Clang-C.cmake
index a2e81c1..d8b7743 100644
--- a/Modules/Compiler/Clang-C.cmake
+++ b/Modules/Compiler/Clang-C.cmake
@@ -23,6 +23,13 @@
       message(FATAL_ERROR "CMAKE_C_STANDARD_COMPUTED_DEFAULT should be set for ${CMAKE_C_COMPILER_ID} (${CMAKE_C_COMPILER}) version ${CMAKE_C_COMPILER_VERSION}")
     endif()
     set(CMAKE_C_STANDARD_DEFAULT ${CMAKE_C_STANDARD_COMPUTED_DEFAULT})
+  elseif(NOT DEFINED CMAKE_C_STANDARD_DEFAULT)
+    # Compiler id was forced so just guess the default standard level.
+    if (NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.6)
+      set(CMAKE_C_STANDARD_DEFAULT 11)
+    else()
+      set(CMAKE_C_STANDARD_DEFAULT 99)
+    endif()
   endif()
 endif()
 
diff --git a/Modules/Compiler/Clang-CXX.cmake b/Modules/Compiler/Clang-CXX.cmake
index 055a8ee..6a0a5e2 100644
--- a/Modules/Compiler/Clang-CXX.cmake
+++ b/Modules/Compiler/Clang-CXX.cmake
@@ -37,6 +37,9 @@
       message(FATAL_ERROR "CMAKE_CXX_STANDARD_COMPUTED_DEFAULT should be set for ${CMAKE_CXX_COMPILER_ID} (${CMAKE_CXX_COMPILER}) version ${CMAKE_CXX_COMPILER_VERSION}")
     endif()
     set(CMAKE_CXX_STANDARD_DEFAULT ${CMAKE_CXX_STANDARD_COMPUTED_DEFAULT})
+  elseif(NOT DEFINED CMAKE_CXX_STANDARD_DEFAULT)
+    # Compiler id was forced so just guess the default standard level.
+    set(CMAKE_CXX_STANDARD_DEFAULT 98)
   endif()
 endif()
 
diff --git a/Modules/Compiler/GNU-C.cmake b/Modules/Compiler/GNU-C.cmake
index d979fb7..2c478da 100644
--- a/Modules/Compiler/GNU-C.cmake
+++ b/Modules/Compiler/GNU-C.cmake
@@ -28,6 +28,13 @@
       message(FATAL_ERROR "CMAKE_C_STANDARD_COMPUTED_DEFAULT should be set for ${CMAKE_C_COMPILER_ID} (${CMAKE_C_COMPILER}) version ${CMAKE_C_COMPILER_VERSION}")
     endif()
     set(CMAKE_C_STANDARD_DEFAULT ${CMAKE_C_STANDARD_COMPUTED_DEFAULT})
+  elseif(NOT DEFINED CMAKE_C_STANDARD_DEFAULT)
+    # Compiler id was forced so just guess the default standard level.
+    if (NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 5.0)
+      set(CMAKE_C_STANDARD_DEFAULT 11)
+    else()
+      set(CMAKE_C_STANDARD_DEFAULT 90)
+    endif()
   endif()
 endif()
 
diff --git a/Modules/Compiler/GNU-CXX.cmake b/Modules/Compiler/GNU-CXX.cmake
index a7e71c3..e1c555b 100644
--- a/Modules/Compiler/GNU-CXX.cmake
+++ b/Modules/Compiler/GNU-CXX.cmake
@@ -40,6 +40,9 @@
       message(FATAL_ERROR "CMAKE_CXX_STANDARD_COMPUTED_DEFAULT should be set for ${CMAKE_CXX_COMPILER_ID} (${CMAKE_CXX_COMPILER}) version ${CMAKE_CXX_COMPILER_VERSION}")
     endif()
     set(CMAKE_CXX_STANDARD_DEFAULT ${CMAKE_CXX_STANDARD_COMPUTED_DEFAULT})
+  elseif(NOT DEFINED CMAKE_CXX_STANDARD_DEFAULT)
+    # Compiler id was forced so just guess the default standard level.
+    set(CMAKE_CXX_STANDARD_DEFAULT 98)
   endif()
 endif()
 
diff --git a/Modules/Compiler/SunPro-CXX.cmake b/Modules/Compiler/SunPro-CXX.cmake
index 50d68ee..b4a5591 100644
--- a/Modules/Compiler/SunPro-CXX.cmake
+++ b/Modules/Compiler/SunPro-CXX.cmake
@@ -42,6 +42,9 @@
       message(FATAL_ERROR "CMAKE_CXX_STANDARD_COMPUTED_DEFAULT should be set for ${CMAKE_CXX_COMPILER_ID} (${CMAKE_CXX_COMPILER}) version ${CMAKE_CXX_COMPILER_VERSION}")
     endif()
     set(CMAKE_CXX_STANDARD_DEFAULT ${CMAKE_CXX_STANDARD_COMPUTED_DEFAULT})
+  elseif(NOT DEFINED CMAKE_CXX_STANDARD_DEFAULT)
+    # Compiler id was forced so just guess the default standard level.
+    set(CMAKE_CXX_STANDARD_DEFAULT 98)
   endif()
 endif()
 
diff --git a/Modules/FindGTest.cmake b/Modules/FindGTest.cmake
index fccf877..eb7abfd 100644
--- a/Modules/FindGTest.cmake
+++ b/Modules/FindGTest.cmake
@@ -124,11 +124,11 @@
           string(REGEX MATCH "${gtest_test_type_regex}" test_type ${hit})
 
           # Parameterized tests have a different signature for the filter
-          if(${test_type} STREQUAL "TEST_P")
+          if("x${test_type}" STREQUAL "xTEST_P")
             string(REGEX REPLACE ${gtest_case_name_regex}  "*/\\1.\\2/*" test_name ${hit})
-          elseif(${test_type} STREQUAL "TEST_F" OR ${test_type} STREQUAL "TEST")
+          elseif("x${test_type}" STREQUAL "xTEST_F" OR "x${test_type}" STREQUAL "xTEST")
             string(REGEX REPLACE ${gtest_case_name_regex} "\\1.\\2" test_name ${hit})
-          elseif(${test_type} STREQUAL "TYPED_TEST")
+          elseif("x${test_type}" STREQUAL "xTYPED_TEST")
             string(REGEX REPLACE ${gtest_case_name_regex} "\\1/*.\\2" test_name ${hit})
           else()
             message(WARNING "Could not parse GTest ${hit} for adding to CTest.")
diff --git a/Source/bindexplib.cxx b/Source/bindexplib.cxx
index dc4db63..b250842 100644
--- a/Source/bindexplib.cxx
+++ b/Source/bindexplib.cxx
@@ -425,7 +425,7 @@
       if(h->Sig1 == 0x0 && h->Sig2 == 0xffff) {
          DumpSymbols<cmANON_OBJECT_HEADER_BIGOBJ, cmIMAGE_SYMBOL_EX>
            symbolDumper((cmANON_OBJECT_HEADER_BIGOBJ*) lpFileBase, fout,
-                        (dosHeader->e_magic == IMAGE_FILE_MACHINE_AMD64));
+                        (h->Machine == IMAGE_FILE_MACHINE_AMD64));
          symbolDumper.DumpObjFile();
       } else {
          printf("unrecognized file format in '%s'\n", filename);
diff --git a/Source/cmELF.cxx b/Source/cmELF.cxx
index d062987..37dd328 100644
--- a/Source/cmELF.cxx
+++ b/Source/cmELF.cxx
@@ -567,8 +567,14 @@
     return true;
     }
 
-  // Allocate the dynamic section entries.
+  // If there are no entries we are done.
   ELF_Shdr const& sec = this->SectionHeaders[this->DynamicSectionIndex];
+  if(sec.sh_entsize == 0)
+    {
+    return false;
+    }
+
+  // Allocate the dynamic section entries.
   int n = static_cast<int>(sec.sh_size / sec.sh_entsize);
   this->DynamicSectionEntries.resize(n);
 
diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx
index 62598f4..194cd88 100644
--- a/Source/cmGeneratorTarget.cxx
+++ b/Source/cmGeneratorTarget.cxx
@@ -2747,6 +2747,7 @@
   const char* version = this->GetProperty("VERSION");
   const char* soversion = this->GetProperty("SOVERSION");
   if(!this->HasSOName(config) ||
+     this->Makefile->IsOn("CMAKE_PLATFORM_NO_VERSIONED_SONAME") ||
      this->Target->IsFrameworkOnApple())
     {
     // Versioning is supported only for shared libraries and modules,
diff --git a/Source/cmLinkedTree.h b/Source/cmLinkedTree.h
index 721a246..3b41459 100644
--- a/Source/cmLinkedTree.h
+++ b/Source/cmLinkedTree.h
@@ -24,7 +24,7 @@
   needs of the cmState.  For example, the Truncate() method is a specific
   requirement of the cmState.
 
-  An empty cmLinkedTree provides a Root() method, and an Extend() method,
+  An empty cmLinkedTree provides a Root() method, and an Push() method,
   each of which return iterators.  A Tree can be built up by extending
   from the root, and then extending from any other iterator.
 
@@ -142,16 +142,37 @@
     return iterator(const_cast<cmLinkedTree*>(this), 0);
   }
 
-  iterator Extend(iterator it)
+  iterator Push(iterator it)
   {
-    return Extend_impl(it, T());
+    return Push_impl(it, T());
   }
 
-  iterator Extend(iterator it, T t)
+  iterator Push(iterator it, T t)
   {
-    return Extend_impl(it, t);
+    return Push_impl(it, t);
   }
 
+  bool IsLast(iterator it)
+    {
+    return it.Position == this->Data.size();
+    }
+
+  iterator Pop(iterator it)
+    {
+    assert(!this->Data.empty());
+    assert(this->UpPositions.size() == this->Data.size());
+    bool const isLast = this->IsLast(it);
+    ++it;
+    // If this is the last entry then no other entry can refer
+    // to it so we can drop its storage.
+    if (isLast)
+      {
+      this->Data.pop_back();
+      this->UpPositions.pop_back();
+      }
+    return it;
+    }
+
   iterator Truncate()
   {
     assert(this->UpPositions.size() > 0);
@@ -179,7 +200,7 @@
     return &this->Data[pos];
   }
 
-  iterator Extend_impl(iterator it, T t)
+  iterator Push_impl(iterator it, T t)
   {
     assert(this->UpPositions.size() == this->Data.size());
     assert(it.Position <= this->UpPositions.size());
diff --git a/Source/cmListFileCache.cxx b/Source/cmListFileCache.cxx
index bff2986..1465f90 100644
--- a/Source/cmListFileCache.cxx
+++ b/Source/cmListFileCache.cxx
@@ -398,6 +398,21 @@
     }
 }
 
+cmListFileBacktrace::cmListFileBacktrace(cmState::Snapshot snapshot,
+                                         cmCommandContext const& cc)
+  : Context(cc)
+  , Snapshot(snapshot)
+{
+  if (this->Snapshot.IsValid())
+    {
+    this->Snapshot.Keep();
+    }
+}
+
+cmListFileBacktrace::~cmListFileBacktrace()
+{
+}
+
 void cmListFileBacktrace::PrintTitle(std::ostream& out) const
 {
   if (!this->Snapshot.IsValid())
diff --git a/Source/cmListFileCache.h b/Source/cmListFileCache.h
index 0afd7f5..17ee10f 100644
--- a/Source/cmListFileCache.h
+++ b/Source/cmListFileCache.h
@@ -90,10 +90,8 @@
 {
   public:
     cmListFileBacktrace(cmState::Snapshot snapshot = cmState::Snapshot(),
-                        cmCommandContext const& cc = cmCommandContext())
-      : Context(cc), Snapshot(snapshot)
-    {
-    }
+                        cmCommandContext const& cc = cmCommandContext());
+    ~cmListFileBacktrace();
 
     void PrintTitle(std::ostream& out) const;
     void PrintCallStack(std::ostream& out) const;
diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx
index 3230403..233e7fe 100644
--- a/Source/cmLocalGenerator.cxx
+++ b/Source/cmLocalGenerator.cxx
@@ -3003,7 +3003,7 @@
   // override user make variables.  If not the configuration will fall
   // back to the directory-level values set by the user.
   cmMakefile* mf = this->Makefile;
-  mf->PushScope();
+  cmMakefile::ScopePushPop varScope(mf);
   mf->AddDefinition("MACOSX_BUNDLE_EXECUTABLE_NAME", targetName.c_str());
   cmLGInfoProp(mf, target, "MACOSX_BUNDLE_INFO_STRING");
   cmLGInfoProp(mf, target, "MACOSX_BUNDLE_ICON_FILE");
@@ -3014,7 +3014,6 @@
   cmLGInfoProp(mf, target, "MACOSX_BUNDLE_BUNDLE_VERSION");
   cmLGInfoProp(mf, target, "MACOSX_BUNDLE_COPYRIGHT");
   mf->ConfigureFile(inFile.c_str(), fname, false, false, false);
-  mf->PopScope();
 }
 
 //----------------------------------------------------------------------------
@@ -3047,12 +3046,11 @@
   // override user make variables.  If not the configuration will fall
   // back to the directory-level values set by the user.
   cmMakefile* mf = this->Makefile;
-  mf->PushScope();
+  cmMakefile::ScopePushPop varScope(mf);
   mf->AddDefinition("MACOSX_FRAMEWORK_NAME", targetName.c_str());
   cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_ICON_FILE");
   cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_IDENTIFIER");
   cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_SHORT_VERSION_STRING");
   cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_BUNDLE_VERSION");
   mf->ConfigureFile(inFile.c_str(), fname, false, false, false);
-  mf->PopScope();
 }
diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx
index cb66a75..3c19f55 100644
--- a/Source/cmMakefile.cxx
+++ b/Source/cmMakefile.cxx
@@ -435,7 +435,7 @@
       this->EnforceCMP0011();
       }
     }
-  this->Makefile->PopPolicyBarrier(this->ReportError);
+  this->Makefile->PopSnapshot(this->ReportError);
 
   this->Makefile->PopFunctionBlockerBarrier(this->ReportError);
 }
@@ -549,7 +549,7 @@
 
   ~ListFileScope()
   {
-    this->Makefile->PopPolicyBarrier(this->ReportError);
+    this->Makefile->PopSnapshot(this->ReportError);
     this->Makefile->PopFunctionBlockerBarrier(this->ReportError);
   }
 
@@ -1551,7 +1551,7 @@
 {
   this->PopPolicy();
 
-  this->PopPolicyBarrier(reportError);
+  this->PopSnapshot(reportError);
 
   this->PopFunctionBlockerBarrier(reportError);
 
@@ -1582,7 +1582,7 @@
 void cmMakefile::PopMacroScope(bool reportError)
 {
   this->PopPolicy();
-  this->PopPolicyBarrier(reportError);
+  this->PopSnapshot(reportError);
 
   this->PopFunctionBlockerBarrier(reportError);
 }
@@ -1619,7 +1619,7 @@
   ~BuildsystemFileScope()
   {
     this->Makefile->PopFunctionBlockerBarrier(this->ReportError);
-    this->Makefile->PopPolicyBarrier(this->ReportError);
+    this->Makefile->PopSnapshot(this->ReportError);
 #if defined(CMAKE_BUILD_WITH_CMAKE)
     this->GG->GetFileLockPool().PopFileScope();
 #endif
@@ -4233,9 +4233,7 @@
 
   this->CheckForUnusedVariables();
 
-  this->StateSnapshot =
-      this->GetState()->Pop(this->StateSnapshot);
-  assert(this->StateSnapshot.IsValid());
+  this->PopSnapshot();
 }
 
 void cmMakefile::RaiseScope(const std::string& var, const char *varDef)
@@ -4601,20 +4599,15 @@
 }
 
 //----------------------------------------------------------------------------
-cmMakefile::PolicyPushPop::PolicyPushPop(cmMakefile* m, bool weak,
-                                         cmPolicies::PolicyMap const& pm):
-  Makefile(m), ReportError(true)
+cmMakefile::PolicyPushPop::PolicyPushPop(cmMakefile* m): Makefile(m)
 {
-  this->Makefile->StateSnapshot = this->Makefile->StateSnapshot.GetState()
-      ->CreatePolicyScopeSnapshot(this->Makefile->StateSnapshot);
-  this->Makefile->PushPolicy(weak, pm);
+  this->Makefile->PushPolicy();
 }
 
 //----------------------------------------------------------------------------
 cmMakefile::PolicyPushPop::~PolicyPushPop()
 {
   this->Makefile->PopPolicy();
-  this->Makefile->PopPolicyBarrier(this->ReportError);
 }
 
 //----------------------------------------------------------------------------
@@ -4634,8 +4627,11 @@
 }
 
 //----------------------------------------------------------------------------
-void cmMakefile::PopPolicyBarrier(bool reportError)
+void cmMakefile::PopSnapshot(bool reportError)
 {
+  // cmState::Snapshot manages nested policy scopes within it.
+  // Since the scope corresponding to the snapshot is closing,
+  // reject any still-open nested policy scopes with an error.
   while (!this->StateSnapshot.CanPopPolicyScope())
     {
     if(reportError)
diff --git a/Source/cmMakefile.h b/Source/cmMakefile.h
index 111f074..1edffdc 100644
--- a/Source/cmMakefile.h
+++ b/Source/cmMakefile.h
@@ -321,14 +321,10 @@
   class PolicyPushPop
   {
   public:
-    PolicyPushPop(cmMakefile* m,
-                  bool weak = false,
-                  cmPolicies::PolicyMap const& pm = cmPolicies::PolicyMap());
+    PolicyPushPop(cmMakefile* m);
     ~PolicyPushPop();
-    void Quiet() { this->ReportError = false; }
   private:
     cmMakefile* Makefile;
-    bool ReportError;
   };
   friend class PolicyPushPop;
 
@@ -904,7 +900,7 @@
   void PushPolicy(bool weak = false,
                   cmPolicies::PolicyMap const& pm = cmPolicies::PolicyMap());
   void PopPolicy();
-  void PopPolicyBarrier(bool reportError = true);
+  void PopSnapshot(bool reportError = true);
   friend class cmCMakePolicyCommand;
   class IncludeScope;
   friend class IncludeScope;
diff --git a/Source/cmOrderDirectories.cxx b/Source/cmOrderDirectories.cxx
index 35ee127..e3406a8 100644
--- a/Source/cmOrderDirectories.cxx
+++ b/Source/cmOrderDirectories.cxx
@@ -73,10 +73,8 @@
       {
       // Check if this directory conflicts with the entry.
       std::string const& dir = this->OD->OriginalDirectories[i];
-      if(dir != this->Directory &&
-         cmSystemTools::GetRealPath(dir) !=
-         cmSystemTools::GetRealPath(this->Directory) &&
-         this->FindConflict(dir))
+      if (!this->OD->IsSameDirectory(dir, this->Directory) &&
+          this->FindConflict(dir))
         {
         // The library will be found in this directory but this is not
         // the directory named for it.  Add an entry to make sure the
@@ -639,3 +637,23 @@
     ->IssueMessage(cmake::WARNING, e.str(),
                    this->Target->Target->GetBacktrace());
 }
+
+bool cmOrderDirectories::IsSameDirectory(std::string const& l,
+                                         std::string const& r)
+{
+  return this->GetRealPath(l) == this->GetRealPath(r);
+}
+
+std::string const& cmOrderDirectories::GetRealPath(std::string const& dir)
+{
+  std::map<std::string, std::string>::iterator i =
+    this->RealPaths.lower_bound(dir);
+  if (i == this->RealPaths.end() ||
+      this->RealPaths.key_comp()(dir, i->first))
+    {
+    typedef std::map<std::string, std::string>::value_type value_type;
+    i = this->RealPaths.insert(
+      i, value_type(dir, cmSystemTools::GetRealPath(dir)));
+    }
+  return i->second;
+}
diff --git a/Source/cmOrderDirectories.h b/Source/cmOrderDirectories.h
index 211c786..477216b 100644
--- a/Source/cmOrderDirectories.h
+++ b/Source/cmOrderDirectories.h
@@ -80,6 +80,12 @@
   struct ConflictList: public std::vector<ConflictPair> {};
   std::vector<ConflictList> ConflictGraph;
 
+  // Compare directories after resolving symlinks.
+  bool IsSameDirectory(std::string const& l, std::string const& r);
+
+  std::string const& GetRealPath(std::string const& dir);
+  std::map<std::string, std::string> RealPaths;
+
   friend class cmOrderDirectoriesConstraint;
   friend class cmOrderDirectoriesConstraintLibrary;
 };
diff --git a/Source/cmStandardIncludes.h b/Source/cmStandardIncludes.h
index 468a589..e212616 100644
--- a/Source/cmStandardIncludes.h
+++ b/Source/cmStandardIncludes.h
@@ -54,6 +54,8 @@
 
 #if defined( _MSC_VER )
 typedef unsigned short mode_t;
+#else
+# include <sys/types.h>
 #endif
 
 // use this class to shrink the size of symbols in .o files
diff --git a/Source/cmState.cxx b/Source/cmState.cxx
index ce9ff32..c1ead6c 100644
--- a/Source/cmState.cxx
+++ b/Source/cmState.cxx
@@ -27,6 +27,7 @@
   cmLinkedTree<cmState::PolicyStackEntry>::iterator PolicyRoot;
   cmLinkedTree<cmState::PolicyStackEntry>::iterator PolicyScope;
   cmState::SnapshotType SnapshotType;
+  bool Keep;
   cmLinkedTree<std::string>::iterator ExecutionListFile;
   cmLinkedTree<cmState::BuildsystemDirectoryStateType>::iterator
                                                           BuildSystemDirectory;
@@ -288,7 +289,7 @@
   assert(pos->Policies.IsValid());
   assert(pos->PolicyRoot.IsValid());
   this->VarTree.Clear();
-  pos->Vars = this->VarTree.Extend(this->VarTree.Root());
+  pos->Vars = this->VarTree.Push(this->VarTree.Root());
   pos->Parent = this->VarTree.Root();
   pos->Root = this->VarTree.Root();
 
@@ -751,14 +752,15 @@
 
 cmState::Snapshot cmState::CreateBaseSnapshot()
 {
-  PositionType pos = this->SnapshotData.Extend(this->SnapshotData.Root());
+  PositionType pos = this->SnapshotData.Push(this->SnapshotData.Root());
   pos->DirectoryParent = this->SnapshotData.Root();
   pos->ScopeParent = this->SnapshotData.Root();
   pos->SnapshotType = BaseType;
+  pos->Keep = true;
   pos->BuildSystemDirectory =
-      this->BuildsystemDirectory.Extend(this->BuildsystemDirectory.Root());
+      this->BuildsystemDirectory.Push(this->BuildsystemDirectory.Root());
   pos->ExecutionListFile =
-      this->ExecutionListFiles.Extend(this->ExecutionListFiles.Root());
+      this->ExecutionListFiles.Push(this->ExecutionListFiles.Root());
   pos->IncludeDirectoryPosition = 0;
   pos->CompileDefinitionsPosition = 0;
   pos->CompileOptionsPosition = 0;
@@ -768,7 +770,7 @@
   pos->PolicyScope = this->PolicyStack.Root();
   assert(pos->Policies.IsValid());
   assert(pos->PolicyRoot.IsValid());
-  pos->Vars = this->VarTree.Extend(this->VarTree.Root());
+  pos->Vars = this->VarTree.Push(this->VarTree.Root());
   assert(pos->Vars.IsValid());
   pos->Parent = this->VarTree.Root();
   pos->Root = this->VarTree.Root();
@@ -781,17 +783,18 @@
                                     long entryPointLine)
 {
   assert(originSnapshot.IsValid());
-  PositionType pos = this->SnapshotData.Extend(originSnapshot.Position);
+  PositionType pos = this->SnapshotData.Push(originSnapshot.Position);
   pos->EntryPointLine = entryPointLine;
   pos->EntryPointCommand = entryPointCommand;
   pos->DirectoryParent = originSnapshot.Position;
   pos->ScopeParent = originSnapshot.Position;
   pos->SnapshotType = BuildsystemDirectoryType;
+  pos->Keep = true;
   pos->BuildSystemDirectory =
-      this->BuildsystemDirectory.Extend(
+      this->BuildsystemDirectory.Push(
         originSnapshot.Position->BuildSystemDirectory);
   pos->ExecutionListFile =
-      this->ExecutionListFiles.Extend(
+      this->ExecutionListFiles.Push(
         originSnapshot.Position->ExecutionListFile);
   pos->BuildSystemDirectory->DirectoryEnd = pos;
   pos->Policies = originSnapshot.Position->Policies;
@@ -804,7 +807,7 @@
       originSnapshot.Position->Vars;
   pos->Parent = origin;
   pos->Root = origin;
-  pos->Vars = this->VarTree.Extend(origin);
+  pos->Vars = this->VarTree.Push(origin);
   cmState::Snapshot snapshot = cmState::Snapshot(this, pos);
   originSnapshot.Position->BuildSystemDirectory->Children.push_back(snapshot);
   return snapshot;
@@ -816,13 +819,14 @@
                                     long entryPointLine,
                                     std::string const& fileName)
 {
-  PositionType pos = this->SnapshotData.Extend(originSnapshot.Position,
-                                               *originSnapshot.Position);
+  PositionType pos = this->SnapshotData.Push(originSnapshot.Position,
+                                             *originSnapshot.Position);
   pos->ScopeParent = originSnapshot.Position;
   pos->EntryPointLine = entryPointLine;
   pos->EntryPointCommand = entryPointCommand;
   pos->SnapshotType = FunctionCallType;
-  pos->ExecutionListFile = this->ExecutionListFiles.Extend(
+  pos->Keep = false;
+  pos->ExecutionListFile = this->ExecutionListFiles.Push(
         originSnapshot.Position->ExecutionListFile, fileName);
   pos->BuildSystemDirectory->DirectoryEnd = pos;
   pos->PolicyScope = originSnapshot.Position->Policies;
@@ -830,7 +834,7 @@
   cmLinkedTree<cmDefinitions>::iterator origin =
       originSnapshot.Position->Vars;
   pos->Parent = origin;
-  pos->Vars = this->VarTree.Extend(origin);
+  pos->Vars = this->VarTree.Push(origin);
   return cmState::Snapshot(this, pos);
 }
 
@@ -841,12 +845,13 @@
                                     long entryPointLine,
                                     std::string const& fileName)
 {
-  PositionType pos = this->SnapshotData.Extend(originSnapshot.Position,
-                                               *originSnapshot.Position);
+  PositionType pos = this->SnapshotData.Push(originSnapshot.Position,
+                                             *originSnapshot.Position);
   pos->EntryPointLine = entryPointLine;
   pos->EntryPointCommand = entryPointCommand;
   pos->SnapshotType = MacroCallType;
-  pos->ExecutionListFile = this->ExecutionListFiles.Extend(
+  pos->Keep = false;
+  pos->ExecutionListFile = this->ExecutionListFiles.Push(
         originSnapshot.Position->ExecutionListFile, fileName);
   assert(originSnapshot.Position->Vars.IsValid());
   pos->BuildSystemDirectory->DirectoryEnd = pos;
@@ -860,12 +865,13 @@
                                  long entryPointLine,
                                  const std::string& fileName)
 {
-  PositionType pos = this->SnapshotData.Extend(originSnapshot.Position,
-                                               *originSnapshot.Position);
+  PositionType pos = this->SnapshotData.Push(originSnapshot.Position,
+                                             *originSnapshot.Position);
   pos->EntryPointLine = entryPointLine;
   pos->EntryPointCommand = entryPointCommand;
   pos->SnapshotType = CallStackType;
-  pos->ExecutionListFile = this->ExecutionListFiles.Extend(
+  pos->Keep = true;
+  pos->ExecutionListFile = this->ExecutionListFiles.Push(
         originSnapshot.Position->ExecutionListFile, fileName);
   assert(originSnapshot.Position->Vars.IsValid());
   pos->BuildSystemDirectory->DirectoryEnd = pos;
@@ -878,18 +884,20 @@
                                      std::string const& entryPointCommand,
                                      long entryPointLine)
 {
-  PositionType pos = this->SnapshotData.Extend(originSnapshot.Position,
-                                               *originSnapshot.Position);
+  PositionType pos = this->SnapshotData.Push(originSnapshot.Position,
+                                             *originSnapshot.Position);
   pos->ScopeParent = originSnapshot.Position;
   pos->EntryPointLine = entryPointLine;
   pos->EntryPointCommand = entryPointCommand;
   pos->SnapshotType = VariableScopeType;
+  pos->Keep = false;
+  pos->PolicyScope = originSnapshot.Position->Policies;
   assert(originSnapshot.Position->Vars.IsValid());
 
   cmLinkedTree<cmDefinitions>::iterator origin =
       originSnapshot.Position->Vars;
   pos->Parent = origin;
-  pos->Vars = this->VarTree.Extend(origin);
+  pos->Vars = this->VarTree.Push(origin);
   assert(pos->Vars.IsValid());
   return cmState::Snapshot(this, pos);
 }
@@ -900,12 +908,13 @@
                                       long entryPointLine,
                                       const std::string& fileName)
 {
-  PositionType pos = this->SnapshotData.Extend(originSnapshot.Position,
-                                               *originSnapshot.Position);
+  PositionType pos = this->SnapshotData.Push(originSnapshot.Position,
+                                             *originSnapshot.Position);
   pos->EntryPointLine = entryPointLine;
   pos->EntryPointCommand = entryPointCommand;
   pos->SnapshotType = InlineListFileType;
-  pos->ExecutionListFile = this->ExecutionListFiles.Extend(
+  pos->Keep = true;
+  pos->ExecutionListFile = this->ExecutionListFiles.Push(
         originSnapshot.Position->ExecutionListFile, fileName);
   pos->BuildSystemDirectory->DirectoryEnd = pos;
   pos->PolicyScope = originSnapshot.Position->Policies;
@@ -915,9 +924,10 @@
 cmState::Snapshot
 cmState::CreatePolicyScopeSnapshot(cmState::Snapshot originSnapshot)
 {
-  PositionType pos = this->SnapshotData.Extend(originSnapshot.Position,
-                                               *originSnapshot.Position);
+  PositionType pos = this->SnapshotData.Push(originSnapshot.Position,
+                                             *originSnapshot.Position);
   pos->SnapshotType = PolicyScopeType;
+  pos->Keep = false;
   pos->BuildSystemDirectory->DirectoryEnd = pos;
   pos->PolicyScope = originSnapshot.Position->Policies;
   return cmState::Snapshot(this, pos);
@@ -936,6 +946,21 @@
       prevPos->BuildSystemDirectory->CompileOptions.size();
   prevPos->BuildSystemDirectory->DirectoryEnd = prevPos;
 
+  if (!pos->Keep && this->SnapshotData.IsLast(pos))
+    {
+    if (pos->Vars != prevPos->Vars)
+      {
+      assert(this->VarTree.IsLast(pos->Vars));
+      this->VarTree.Pop(pos->Vars);
+      }
+    if (pos->ExecutionListFile != prevPos->ExecutionListFile)
+      {
+      assert(this->ExecutionListFiles.IsLast(pos->ExecutionListFile));
+      this->ExecutionListFiles.Pop(pos->ExecutionListFile);
+      }
+    this->SnapshotData.Pop(pos);
+    }
+
   return Snapshot(this, prevPos);
 }
 
@@ -998,6 +1023,11 @@
   this->ComputeRelativePathTopBinary();
 }
 
+void cmState::Snapshot::Keep()
+{
+  this->Position->Keep = true;
+}
+
 void cmState::Snapshot::SetListFile(const std::string& listfile)
 {
   *this->Position->ExecutionListFile = listfile;
@@ -1081,7 +1111,8 @@
 
   Snapshot snapshot;
   PositionType parentPos = this->Position;
-  while(parentPos->SnapshotType == cmState::PolicyScopeType)
+  while (parentPos->SnapshotType == cmState::PolicyScopeType ||
+         parentPos->SnapshotType == cmState::VariableScopeType)
     {
     ++parentPos;
     }
@@ -1092,7 +1123,8 @@
     }
 
   ++parentPos;
-  while(parentPos->SnapshotType == cmState::PolicyScopeType)
+  while (parentPos->SnapshotType == cmState::PolicyScopeType ||
+         parentPos->SnapshotType == cmState::VariableScopeType)
     {
     ++parentPos;
     }
@@ -1110,8 +1142,8 @@
 {
   PositionType pos = this->Position;
   pos->Policies =
-      this->State->PolicyStack.Extend(pos->Policies,
-                                      PolicyStackEntry(entry, weak));
+    this->State->PolicyStack.Push(pos->Policies,
+                                  PolicyStackEntry(entry, weak));
 }
 
 bool cmState::Snapshot::PopPolicy()
@@ -1121,7 +1153,7 @@
     {
     return false;
     }
-  ++pos->Policies;
+  pos->Policies = this->State->PolicyStack.Pop(pos->Policies);
   return true;
 }
 
diff --git a/Source/cmState.h b/Source/cmState.h
index 99e537c..a66603f 100644
--- a/Source/cmState.h
+++ b/Source/cmState.h
@@ -62,6 +62,7 @@
     std::vector<std::string> ClosureKeys() const;
     bool RaiseScope(std::string const& var, const char* varDef);
 
+    void Keep();
     void SetListFile(std::string const& listfile);
 
     std::string GetExecutionListFile() const;
diff --git a/Source/cmcmd.cxx b/Source/cmcmd.cxx
index f44c77d..a074444 100644
--- a/Source/cmcmd.cxx
+++ b/Source/cmcmd.cxx
@@ -1550,7 +1550,6 @@
     // pass it to the link command.
     this->ManifestFileRC = intDir + "/manifest.rc";
     this->ManifestFileRes = intDir + "/manifest.res";
-    this->LinkCommand.push_back(this->ManifestFileRes);
     }
   else if (this->UserManifests.empty())
     {
@@ -1658,6 +1657,9 @@
     return -1;
     }
 
+  // Tell the linker to use our manifest compiled into a resource.
+  this->LinkCommand.push_back(this->ManifestFileRes);
+
   // Run the link command (possibly generates intermediate manifest).
   if (!RunCommand("LINK Pass 1", this->LinkCommand, this->Verbose))
     {
diff --git a/Tests/MSManifest/Subdir/CMakeLists.txt b/Tests/MSManifest/Subdir/CMakeLists.txt
index a47cf00..11272bb 100644
--- a/Tests/MSManifest/Subdir/CMakeLists.txt
+++ b/Tests/MSManifest/Subdir/CMakeLists.txt
@@ -6,4 +6,6 @@
     COMMAND ${CMAKE_COMMAND} -Dexe=$<TARGET_FILE:MSManifest>
             -P ${CMAKE_CURRENT_SOURCE_DIR}/check.cmake
     )
+  add_executable(MSManifestNone main.c)
+  set_property(TARGET MSManifestNone PROPERTY LINK_FLAGS "/MANIFEST:NO")
 endif()
diff --git a/Tests/RunCMake/AutoExportDll/sub/sub.cxx b/Tests/RunCMake/AutoExportDll/sub/sub.cxx
index 9766b41..9a3145e 100644
--- a/Tests/RunCMake/AutoExportDll/sub/sub.cxx
+++ b/Tests/RunCMake/AutoExportDll/sub/sub.cxx
@@ -1,4 +1,6 @@
+#include <stdio.h>
 int sub()
 {
+  printf("");
   return 10;
 }
diff --git a/Tests/RunCMake/PolicyScope/NotClosed-result.txt b/Tests/RunCMake/PolicyScope/NotClosed-result.txt
new file mode 100644
index 0000000..d00491f
--- /dev/null
+++ b/Tests/RunCMake/PolicyScope/NotClosed-result.txt
@@ -0,0 +1 @@
+1
diff --git a/Tests/RunCMake/PolicyScope/NotClosed-stderr.txt b/Tests/RunCMake/PolicyScope/NotClosed-stderr.txt
new file mode 100644
index 0000000..293d161
--- /dev/null
+++ b/Tests/RunCMake/PolicyScope/NotClosed-stderr.txt
@@ -0,0 +1,4 @@
+^CMake Error at NotClosed.cmake:[0-9]+ \(include\):
+  cmake_policy PUSH without matching POP
+Call Stack \(most recent call first\):
+  CMakeLists.txt:[0-9]+ \(include\)$
diff --git a/Tests/RunCMake/PolicyScope/NotClosed.cmake b/Tests/RunCMake/PolicyScope/NotClosed.cmake
new file mode 100644
index 0000000..19b880e
--- /dev/null
+++ b/Tests/RunCMake/PolicyScope/NotClosed.cmake
@@ -0,0 +1 @@
+cmake_policy(PUSH)
diff --git a/Tests/RunCMake/PolicyScope/NotOpened-result.txt b/Tests/RunCMake/PolicyScope/NotOpened-result.txt
new file mode 100644
index 0000000..d00491f
--- /dev/null
+++ b/Tests/RunCMake/PolicyScope/NotOpened-result.txt
@@ -0,0 +1 @@
+1
diff --git a/Tests/RunCMake/PolicyScope/NotOpened-stderr.txt b/Tests/RunCMake/PolicyScope/NotOpened-stderr.txt
new file mode 100644
index 0000000..f27aac8
--- /dev/null
+++ b/Tests/RunCMake/PolicyScope/NotOpened-stderr.txt
@@ -0,0 +1,4 @@
+^CMake Error at NotOpened.cmake:[0-9]+ \(cmake_policy\):
+  cmake_policy POP without matching PUSH
+Call Stack \(most recent call first\):
+  CMakeLists.txt:[0-9]+ \(include\)$
diff --git a/Tests/RunCMake/PolicyScope/NotOpened.cmake b/Tests/RunCMake/PolicyScope/NotOpened.cmake
new file mode 100644
index 0000000..bb9f3a3
--- /dev/null
+++ b/Tests/RunCMake/PolicyScope/NotOpened.cmake
@@ -0,0 +1 @@
+cmake_policy(POP)
diff --git a/Tests/RunCMake/PolicyScope/RunCMakeTest.cmake b/Tests/RunCMake/PolicyScope/RunCMakeTest.cmake
index d6c021f..abd27f4 100644
--- a/Tests/RunCMake/PolicyScope/RunCMakeTest.cmake
+++ b/Tests/RunCMake/PolicyScope/RunCMakeTest.cmake
@@ -1,4 +1,6 @@
 include(RunCMake)
 
+run_cmake(NotClosed)
+run_cmake(NotOpened)
 run_cmake(parent-dir-generate-time)
 run_cmake(dir-in-macro-generate-time)
diff --git a/Tests/RunCMake/add_subdirectory/Function-stdout.txt b/Tests/RunCMake/add_subdirectory/Function-stdout.txt
new file mode 100644
index 0000000..16fa857
--- /dev/null
+++ b/Tests/RunCMake/add_subdirectory/Function-stdout.txt
@@ -0,0 +1,10 @@
+-- var='1' before my_add_subdirectory
+-- var='2' in my_add_subdirectory before add_subdirectory
+-- var='2' in subdirectory before set
+-- var='3' in subdirectory after set
+-- var_sub='' in subdirectory after set PARENT_SCOPE
+-- var='2' in my_add_subdirectory after add_subdirectory
+-- var_sub='sub' in my_add_subdirectory after add_subdirectory
+-- var='1' after my_add_subdirectory
+-- var_sub='' after my_add_subdirectory
+-- var='3' taken from subdirectory
diff --git a/Tests/RunCMake/add_subdirectory/Function.cmake b/Tests/RunCMake/add_subdirectory/Function.cmake
new file mode 100644
index 0000000..e804da9
--- /dev/null
+++ b/Tests/RunCMake/add_subdirectory/Function.cmake
@@ -0,0 +1,17 @@
+function(my_add_subdirectory dir)
+  set(var 2)
+  message(STATUS "var='${var}' in my_add_subdirectory before add_subdirectory")
+  add_subdirectory(${dir})
+  message(STATUS "var='${var}' in my_add_subdirectory after add_subdirectory")
+  message(STATUS "var_sub='${var_sub}' in my_add_subdirectory after add_subdirectory")
+endfunction()
+
+set(var 1)
+
+message(STATUS "var='${var}' before my_add_subdirectory")
+my_add_subdirectory(Function)
+message(STATUS "var='${var}' after my_add_subdirectory")
+message(STATUS "var_sub='${var_sub}' after my_add_subdirectory")
+
+get_directory_property(sub_var DIRECTORY Function DEFINITION var)
+message(STATUS "var='${sub_var}' taken from subdirectory")
diff --git a/Tests/RunCMake/add_subdirectory/Function/CMakeLists.txt b/Tests/RunCMake/add_subdirectory/Function/CMakeLists.txt
new file mode 100644
index 0000000..7b9e801
--- /dev/null
+++ b/Tests/RunCMake/add_subdirectory/Function/CMakeLists.txt
@@ -0,0 +1,5 @@
+message(STATUS "var='${var}' in subdirectory before set")
+set(var 3)
+message(STATUS "var='${var}' in subdirectory after set")
+set(var_sub sub PARENT_SCOPE)
+message(STATUS "var_sub='${var_sub}' in subdirectory after set PARENT_SCOPE")
diff --git a/Tests/RunCMake/add_subdirectory/RunCMakeTest.cmake b/Tests/RunCMake/add_subdirectory/RunCMakeTest.cmake
index a3ddec8..9d514e1 100644
--- a/Tests/RunCMake/add_subdirectory/RunCMakeTest.cmake
+++ b/Tests/RunCMake/add_subdirectory/RunCMakeTest.cmake
@@ -2,3 +2,4 @@
 
 run_cmake(DoesNotExist)
 run_cmake(Missing)
+run_cmake(Function)
diff --git a/Tests/RunCMake/find_package/PolicyPop-result.txt b/Tests/RunCMake/find_package/PolicyPop-result.txt
new file mode 100644
index 0000000..d00491f
--- /dev/null
+++ b/Tests/RunCMake/find_package/PolicyPop-result.txt
@@ -0,0 +1 @@
+1
diff --git a/Tests/RunCMake/find_package/PolicyPop-stderr.txt b/Tests/RunCMake/find_package/PolicyPop-stderr.txt
new file mode 100644
index 0000000..745d39a
--- /dev/null
+++ b/Tests/RunCMake/find_package/PolicyPop-stderr.txt
@@ -0,0 +1,5 @@
+^CMake Error at PolicyPop/PolicyPopConfigVersion.cmake:3 \(cmake_policy\):
+  cmake_policy POP without matching PUSH
+Call Stack \(most recent call first\):
+  PolicyPop.cmake:1 \(find_package\)
+  CMakeLists.txt:3 \(include\)$
diff --git a/Tests/RunCMake/find_package/PolicyPop.cmake b/Tests/RunCMake/find_package/PolicyPop.cmake
new file mode 100644
index 0000000..4866f24
--- /dev/null
+++ b/Tests/RunCMake/find_package/PolicyPop.cmake
@@ -0,0 +1 @@
+find_package(PolicyPop 1 CONFIG PATHS ${CMAKE_CURRENT_SOURCE_DIR}/PolicyPop NO_DEFAULT_PATH)
diff --git a/Tests/RunCMake/find_package/PolicyPop/PolicyPopConfig.cmake b/Tests/RunCMake/find_package/PolicyPop/PolicyPopConfig.cmake
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/Tests/RunCMake/find_package/PolicyPop/PolicyPopConfig.cmake
diff --git a/Tests/RunCMake/find_package/PolicyPop/PolicyPopConfigVersion.cmake b/Tests/RunCMake/find_package/PolicyPop/PolicyPopConfigVersion.cmake
new file mode 100644
index 0000000..483fb10
--- /dev/null
+++ b/Tests/RunCMake/find_package/PolicyPop/PolicyPopConfigVersion.cmake
@@ -0,0 +1,3 @@
+set(PACKAGE_VERSION 1)
+set(PACKAGE_VERSION_COMPATIBLE 1)
+cmake_policy(POP)
diff --git a/Tests/RunCMake/find_package/PolicyPush-result.txt b/Tests/RunCMake/find_package/PolicyPush-result.txt
new file mode 100644
index 0000000..d00491f
--- /dev/null
+++ b/Tests/RunCMake/find_package/PolicyPush-result.txt
@@ -0,0 +1 @@
+1
diff --git a/Tests/RunCMake/find_package/PolicyPush-stderr.txt b/Tests/RunCMake/find_package/PolicyPush-stderr.txt
new file mode 100644
index 0000000..1afcb16
--- /dev/null
+++ b/Tests/RunCMake/find_package/PolicyPush-stderr.txt
@@ -0,0 +1,5 @@
+^CMake Error at PolicyPush/PolicyPushConfigVersion.cmake:1 \(find_package\):
+  cmake_policy PUSH without matching POP
+Call Stack \(most recent call first\):
+  PolicyPush.cmake:1 \(find_package\)
+  CMakeLists.txt:3 \(include\)$
diff --git a/Tests/RunCMake/find_package/PolicyPush.cmake b/Tests/RunCMake/find_package/PolicyPush.cmake
new file mode 100644
index 0000000..30c3ce2
--- /dev/null
+++ b/Tests/RunCMake/find_package/PolicyPush.cmake
@@ -0,0 +1 @@
+find_package(PolicyPush 1 CONFIG PATHS ${CMAKE_CURRENT_SOURCE_DIR}/PolicyPush NO_DEFAULT_PATH)
diff --git a/Tests/RunCMake/find_package/PolicyPush/PolicyPushConfig.cmake b/Tests/RunCMake/find_package/PolicyPush/PolicyPushConfig.cmake
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/Tests/RunCMake/find_package/PolicyPush/PolicyPushConfig.cmake
diff --git a/Tests/RunCMake/find_package/PolicyPush/PolicyPushConfigVersion.cmake b/Tests/RunCMake/find_package/PolicyPush/PolicyPushConfigVersion.cmake
new file mode 100644
index 0000000..45a8dbd
--- /dev/null
+++ b/Tests/RunCMake/find_package/PolicyPush/PolicyPushConfigVersion.cmake
@@ -0,0 +1,3 @@
+set(PACKAGE_VERSION 1)
+set(PACKAGE_VERSION_COMPATIBLE 1)
+cmake_policy(PUSH)
diff --git a/Tests/RunCMake/find_package/RunCMakeTest.cmake b/Tests/RunCMake/find_package/RunCMakeTest.cmake
index 42705b7..81b2906 100644
--- a/Tests/RunCMake/find_package/RunCMakeTest.cmake
+++ b/Tests/RunCMake/find_package/RunCMakeTest.cmake
@@ -13,4 +13,6 @@
 run_cmake(MissingConfigRequired)
 run_cmake(MissingConfigVersion)
 run_cmake(MixedModeOptions)
+run_cmake(PolicyPush)
+run_cmake(PolicyPop)
 run_cmake(SetFoundFALSE)