cmake: link dynamically to libclang-cpp if found and ENABLE_LLVM_SHARED is set

ENABLE_LLVM_SHARED allows to dynamically link against libLLVM, but
libclang is still unconditionally linked statically.
Search for libclang-cpp.so, and if it is found and ENABLE_LLVM_SHARED
is set dynamically link against it.
Also expand the libstdc++ static linking check to include this new
condition.
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 35b5ee6..58fa70c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -79,6 +79,7 @@
 find_library(libclangSema NAMES clangSema clang-cpp HINTS ${CLANG_SEARCH})
 find_library(libclangSerialization NAMES clangSerialization clang-cpp HINTS ${CLANG_SEARCH})
 find_library(libclangASTMatchers NAMES clangASTMatchers clang-cpp HINTS ${CLANG_SEARCH})
+find_library(libclang-shared libclang-cpp.so HINTS ${CLANG_SEARCH})
 if(libclangBasic STREQUAL "libclangBasic-NOTFOUND")
   message(FATAL_ERROR "Unable to find clang libraries")
 endif()
diff --git a/cmake/clang_libs.cmake b/cmake/clang_libs.cmake
index c33b635..3f1523b 100644
--- a/cmake/clang_libs.cmake
+++ b/cmake/clang_libs.cmake
@@ -26,6 +26,9 @@
 llvm_expand_dependencies(llvm_libs ${_llvm_libs})
 endif()
 
+if(ENABLE_LLVM_SHARED AND NOT libclang-shared STREQUAL "libclang-shared-NOTFOUND")
+set(clang_libs ${libclang-shared})
+else()
 # order is important
 set(clang_libs
   ${libclangFrontend}
@@ -46,6 +49,7 @@
   ${libclangAST}
   ${libclangLex}
   ${libclangBasic})
+endif()
 
 # prune unused llvm static library stuff when linking into the new .so
 set(_exclude_flags)
diff --git a/cmake/static_libstdc++.cmake b/cmake/static_libstdc++.cmake
index 3c8ac17..787ed9a 100644
--- a/cmake/static_libstdc++.cmake
+++ b/cmake/static_libstdc++.cmake
@@ -1,7 +1,7 @@
 # only turn on static-libstdc++ if also linking statically against clang
 string(REGEX MATCH ".*[.]a$" LIBCLANG_ISSTATIC "${libclangBasic}")
 # if gcc 4.9 or higher is used, static libstdc++ is a good option
-if (CMAKE_COMPILER_IS_GNUCC AND LIBCLANG_ISSTATIC)
+if (CMAKE_COMPILER_IS_GNUCC AND LIBCLANG_ISSTATIC AND (NOT ENABLE_LLVM_SHARED OR libclang-shared STREQUAL "libclang-shared-NOTFOUND"))
   execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION)
   if (GCC_VERSION VERSION_GREATER 4.9 OR GCC_VERSION VERSION_EQUAL 4.9)
     execute_process(COMMAND ${CMAKE_C_COMPILER} -print-libgcc-file-name OUTPUT_VARIABLE GCC_LIB)