Merge topic 'xcode-stdlib-flags'

8af334f5ba Xcode: Derive stdlib from CXX flags

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !2919
diff --git a/Modules/CMakeDetermineCompilerId.cmake b/Modules/CMakeDetermineCompilerId.cmake
index 2f84c8e..83ce392 100644
--- a/Modules/CMakeDetermineCompilerId.cmake
+++ b/Modules/CMakeDetermineCompilerId.cmake
@@ -360,6 +360,15 @@
     else()
       set(id_sdkroot "")
     endif()
+    set(id_clang_cxx_library "")
+    set(stdlib_regex "(^| )(-stdlib=)([^ ]+)( |$)")
+    string(REGEX MATCHALL "${stdlib_regex}" all_stdlib_matches "${CMAKE_CXX_FLAGS}")
+    if(all_stdlib_matches)
+      list(GET all_stdlib_matches "-1" last_stdlib_match)
+      if(last_stdlib_match MATCHES "${stdlib_regex}")
+        set(id_clang_cxx_library "CLANG_CXX_LIBRARY = \"${CMAKE_MATCH_3}\";")
+      endif()
+    endif()
     configure_file(${CMAKE_ROOT}/Modules/CompilerId/Xcode-3.pbxproj.in
       ${id_dir}/CompilerId${lang}.xcodeproj/project.pbxproj @ONLY)
     unset(_ENV_MACOSX_DEPLOYMENT_TARGET)
diff --git a/Modules/CompilerId/Xcode-3.pbxproj.in b/Modules/CompilerId/Xcode-3.pbxproj.in
index 813c074..672044e 100644
--- a/Modules/CompilerId/Xcode-3.pbxproj.in
+++ b/Modules/CompilerId/Xcode-3.pbxproj.in
@@ -86,6 +86,7 @@
 				SYMROOT = .;
 				@id_toolset@
 				@id_lang_version@
+				@id_clang_cxx_library@
 				@id_deployment_target@
 				@id_sdkroot@
 			};
diff --git a/Source/cmGlobalXCodeGenerator.cxx b/Source/cmGlobalXCodeGenerator.cxx
index 16f8a0e..51c001e 100644
--- a/Source/cmGlobalXCodeGenerator.cxx
+++ b/Source/cmGlobalXCodeGenerator.cxx
@@ -2254,6 +2254,22 @@
     debugStr = "NO";
   }
 
+  // extract C++ stdlib
+  for (auto const& language : languages) {
+    if (language != "CXX") {
+      continue;
+    }
+    std::string& flags = cflags[language];
+
+    auto stdlib =
+      this->ExtractFlagRegex("(^| )(-stdlib=[^ ]+)( |$)", 2, flags);
+    if (stdlib.size() > 8) {
+      const auto cxxLibrary = stdlib.substr(8);
+      buildSettings->AddAttribute("CLANG_CXX_LIBRARY",
+                                  this->CreateString(cxxLibrary));
+    }
+  }
+
   buildSettings->AddAttribute("COMBINE_HIDPI_IMAGES",
                               this->CreateString("YES"));
   buildSettings->AddAttribute("GCC_GENERATE_DEBUGGING_SYMBOLS",