Merge branch 'vs-depends-dedup' into release-3.14

Merge-request: !3388
diff --git a/Help/release/3.14.rst b/Help/release/3.14.rst
index 8a251bd..e3a7a62 100644
--- a/Help/release/3.14.rst
+++ b/Help/release/3.14.rst
@@ -412,3 +412,11 @@
   incorrectly propagate usage requirements of those dependencies to
   dependents that link the static library.  This has been fixed.
   The bug also existed in 3.13.0 through 3.13.4 and is fixed in 3.13.5.
+
+3.14.5
+------
+
+* Entries of the ``CPATH`` environment variable are no longer excluded
+  from explicit use via :command:`include_directories` and
+  :command:`target_include_directories` as they were in CMake 3.14.0
+  through 3.14.4.
diff --git a/Modules/FindBoost.cmake b/Modules/FindBoost.cmake
index 552c2fd..6a59dff 100644
--- a/Modules/FindBoost.cmake
+++ b/Modules/FindBoost.cmake
@@ -1075,6 +1075,16 @@
     # Compiler feature for `context` same as for `fiber`.
     set(_Boost_CONTEXT_COMPILER_FEATURES ${_Boost_FIBER_COMPILER_FEATURES})
   endif()
+
+  # Boost Contract library available in >= 1.67
+  if(NOT Boost_VERSION_STRING VERSION_LESS 1.67.0)
+    # From `libs/contract/build/boost_contract_build.jam`
+    set(_Boost_CONTRACT_COMPILER_FEATURES
+        cxx_lambdas
+        cxx_variadic_templates
+    )
+  endif()
+
   string(TOUPPER ${component} uppercomponent)
   set(${_ret} ${_Boost_${uppercomponent}_COMPILER_FEATURES} PARENT_SCOPE)
 endfunction()
diff --git a/Modules/FindThreads.cmake b/Modules/FindThreads.cmake
index 919392a..36b55c2 100644
--- a/Modules/FindThreads.cmake
+++ b/Modules/FindThreads.cmake
@@ -32,9 +32,6 @@
 The compiler flag can only be used with the imported
 target. Use of both the imported target as well as this switch is highly
 recommended for new code.
-
-This module is not needed for C++11 and later if threading is done using
-``std::thread`` from the standard library.
 #]=======================================================================]
 
 include (CheckLibraryExists)
diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx
index 7e15234..f6962d1 100644
--- a/Source/cmLocalGenerator.cxx
+++ b/Source/cmLocalGenerator.cxx
@@ -88,6 +88,19 @@
 
   this->ComputeObjectMaxPath();
 
+  // Canonicalize entries of the CPATH environment variable the same
+  // way detection of CMAKE_<LANG>_IMPLICIT_INCLUDE_DIRECTORIES does.
+  {
+    std::vector<std::string> cpath;
+    cmSystemTools::GetPath(cpath, "CPATH");
+    for (std::string& cp : cpath) {
+      if (cmSystemTools::FileIsFullPath(cp)) {
+        cp = cmSystemTools::CollapseFullPath(cp);
+        this->EnvCPATH.emplace(std::move(cp));
+      }
+    }
+  }
+
   std::vector<std::string> enabledLanguages =
     this->GetState()->GetEnabledLanguages();
 
@@ -988,9 +1001,18 @@
   }
 
   // Checks if this is not an excluded (implicit) include directory.
-  auto notExcluded = [&implicitSet, &implicitExclude](std::string const& dir) {
-    return ((implicitSet.find(dir) == implicitSet.end()) &&
-            (implicitExclude.find(dir) == implicitExclude.end()));
+  auto notExcluded = [this, &implicitSet, &implicitExclude,
+                      &lang](std::string const& dir) {
+    return (
+      // Do not exclude directories that are not in an excluded set.
+      ((implicitSet.find(dir) == implicitSet.end()) &&
+       (implicitExclude.find(dir) == implicitExclude.end()))
+      // Do not exclude entries of the CPATH environment variable even though
+      // they are implicitly searched by the compiler.  They are meant to be
+      // user-specified directories that can be re-ordered or converted to
+      // -isystem without breaking real compiler builtin headers.
+      || ((lang == "C" || lang == "CXX") &&
+          (this->EnvCPATH.find(dir) != this->EnvCPATH.end())));
   };
 
   // Get the target-specific include directories.
diff --git a/Source/cmLocalGenerator.h b/Source/cmLocalGenerator.h
index f9839f6..9521ec5 100644
--- a/Source/cmLocalGenerator.h
+++ b/Source/cmLocalGenerator.h
@@ -429,6 +429,8 @@
   std::string::size_type ObjectPathMax;
   std::set<std::string> ObjectMaxPathViolations;
 
+  std::set<std::string> EnvCPATH;
+
   typedef std::unordered_map<std::string, cmGeneratorTarget*>
     GeneratorTargetMap;
   GeneratorTargetMap GeneratorTargetSearchIndex;
diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt
index 431a492..588c8e0 100644
--- a/Tests/CMakeLists.txt
+++ b/Tests/CMakeLists.txt
@@ -3620,6 +3620,24 @@
     --test-command IncludeDirectories)
   list(APPEND TEST_BUILD_DIRS "${CMake_BINARY_DIR}/Tests/IncludeDirectories")
 
+  if(CMAKE_GENERATOR MATCHES "^((Unix|MSYS) Makefiles|Ninja)$" AND
+     ((CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.4)
+      OR (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND NOT "x${CMAKE_CXX_SIMULATE_ID}" STREQUAL "xMSVC")
+      OR (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")))
+    add_test(IncludeDirectoriesCPATH ${CMAKE_CTEST_COMMAND}
+      --build-and-test
+      "${CMake_SOURCE_DIR}/Tests/IncludeDirectoriesCPATH"
+      "${CMake_BINARY_DIR}/Tests/IncludeDirectoriesCPATH"
+      --build-two-config
+      ${build_generator_args}
+      --build-project IncludeDirectoriesCPATH
+      --build-options ${build_options})
+    list(APPEND TEST_BUILD_DIRS "${CMake_BINARY_DIR}/Tests/IncludeDirectoriesCPATH")
+    set_tests_properties(IncludeDirectoriesCPATH
+      PROPERTIES
+        ENVIRONMENT "CPATH=${CMAKE_CURRENT_SOURCE_DIR}/IncludeDirectoriesCPATH/viacpath")
+  endif()
+
   add_test(InterfaceLinkLibraries ${CMAKE_CTEST_COMMAND}
     --build-and-test
     "${CMake_SOURCE_DIR}/Tests/InterfaceLinkLibraries"
diff --git a/Tests/IncludeDirectoriesCPATH/CMakeLists.txt b/Tests/IncludeDirectoriesCPATH/CMakeLists.txt
new file mode 100644
index 0000000..31cbc36
--- /dev/null
+++ b/Tests/IncludeDirectoriesCPATH/CMakeLists.txt
@@ -0,0 +1,22 @@
+cmake_minimum_required (VERSION 3.14)
+project(IncludeDirectoriesCPATH CXX)
+message(STATUS "ENV{CPATH}: '$ENV{CPATH}'")
+message(STATUS "CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES: '${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES}'")
+
+include(CheckCXXCompilerFlag)
+check_cxx_compiler_flag(-Wunused-variable run_sys_includes_test)
+if(run_sys_includes_test)
+  # The Bullseye wrapper appears to break the -isystem effect.
+  execute_process(COMMAND ${CMAKE_CXX_COMPILER} --version OUTPUT_VARIABLE out ERROR_VARIABLE out)
+  if("x${out}" MATCHES "Bullseye")
+    set(run_sys_includes_test 0)
+  endif()
+endif()
+if (NOT run_sys_includes_test)
+  return()
+endif()
+
+add_library(consumer consumer.cpp)
+add_library(consumer_system consumer.cpp)
+target_compile_options(consumer_system PRIVATE -Werror=unused-variable)
+target_include_directories(consumer_system SYSTEM PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/viacpath")
diff --git a/Tests/IncludeDirectoriesCPATH/consumer.cpp b/Tests/IncludeDirectoriesCPATH/consumer.cpp
new file mode 100644
index 0000000..59608da
--- /dev/null
+++ b/Tests/IncludeDirectoriesCPATH/consumer.cpp
@@ -0,0 +1,6 @@
+#include "systemlib.h"
+
+int consumer()
+{
+  return systemlib();
+}
diff --git a/Tests/IncludeDirectoriesCPATH/viacpath/systemlib.h b/Tests/IncludeDirectoriesCPATH/viacpath/systemlib.h
new file mode 100644
index 0000000..1aaafa9
--- /dev/null
+++ b/Tests/IncludeDirectoriesCPATH/viacpath/systemlib.h
@@ -0,0 +1,15 @@
+#ifndef SYSTEMLIB_H
+#define SYSTEMLIB_H
+
+int systemlib()
+{
+  return 0;
+}
+
+int unusedFunc()
+{
+  int unused;
+  return systemlib();
+}
+
+#endif
diff --git a/Utilities/cmlibarchive/libarchive/archive_write_add_filter_b64encode.c b/Utilities/cmlibarchive/libarchive/archive_write_add_filter_b64encode.c
index 85eb087..b46b19a 100644
--- a/Utilities/cmlibarchive/libarchive/archive_write_add_filter_b64encode.c
+++ b/Utilities/cmlibarchive/libarchive/archive_write_add_filter_b64encode.c
@@ -60,7 +60,7 @@
     const void *, size_t);
 static int archive_filter_b64encode_close(struct archive_write_filter *);
 static int archive_filter_b64encode_free(struct archive_write_filter *);
-static void b64_encode(struct archive_string *, const unsigned char *, size_t);
+static void la_b64_encode(struct archive_string *, const unsigned char *, size_t);
 static int64_t atol8(const char *, size_t);
 
 static const char base64[] = {
@@ -180,7 +180,7 @@
 }
 
 static void
-b64_encode(struct archive_string *as, const unsigned char *p, size_t len)
+la_b64_encode(struct archive_string *as, const unsigned char *p, size_t len)
 {
 	int c;
 
@@ -234,12 +234,12 @@
 		}
 		if (state->hold_len < LBYTES)
 			return (ret);
-		b64_encode(&state->encoded_buff, state->hold, LBYTES);
+		la_b64_encode(&state->encoded_buff, state->hold, LBYTES);
 		state->hold_len = 0;
 	}
 
 	for (; length >= LBYTES; length -= LBYTES, p += LBYTES)
-		b64_encode(&state->encoded_buff, p, LBYTES);
+		la_b64_encode(&state->encoded_buff, p, LBYTES);
 
 	/* Save remaining bytes. */
 	if (length > 0) {
@@ -270,7 +270,7 @@
 
 	/* Flush remaining bytes. */
 	if (state->hold_len != 0)
-		b64_encode(&state->encoded_buff, state->hold, state->hold_len);
+		la_b64_encode(&state->encoded_buff, state->hold, state->hold_len);
 	archive_string_sprintf(&state->encoded_buff, "====\n");
 	/* Write the last block */
 	archive_write_set_bytes_in_last_block(f->archive, 1);