Add and use FindLibClang module for c-picker setup
c-picker uses libclang to parse the code. If compatible libclang is not
available in the system's default library directory c-picker needs to
have CLANG_LIBRARY_PATH environment variable pointing to the suitable
directory. The new module tries to find this directory and the
environment variable is now set during the c-picker call.
Change-Id: I09327b2d09620884ee72ff831ead546a0d10b900
Signed-off-by: Imre Kis <imre.kis@arm.com>
diff --git a/cmake/FindLibClang.cmake b/cmake/FindLibClang.cmake
new file mode 100644
index 0000000..c0eefc5
--- /dev/null
+++ b/cmake/FindLibClang.cmake
@@ -0,0 +1,81 @@
+#
+# Copyright (c) 2019, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+
+#[=======================================================================[.rst:
+FindLibClang
+-------
+
+Finds the LibClang library.
+
+Imported Targets
+^^^^^^^^^^^^^^^^
+
+This module provides the following imported targets, if found:
+
+``LibClang``
+ The Clang library
+
+Result Variables
+^^^^^^^^^^^^^^^^
+
+This will define the following variables:
+
+``LibClang_FOUND``
+ True if the system has the Clang library.
+``LibClang_LIBRARY_DIRS``
+ Libraries needed to link to Clang.
+
+#]=======================================================================]
+
+
+# 1. Use llvm-config
+find_program(_LLVM_CONFIG_COMMAND "llvm-config")
+
+if (_LLVM_CONFIG_COMMAND)
+ message(STATUS "Setting LibClang_LIBRARY_DIRS using ${_LLVM_CONFIG_COMMAND}")
+
+ execute_process(
+ COMMAND ${_LLVM_CONFIG_COMMAND} --libdir
+ OUTPUT_VARIABLE _LLVM_CONFIG_OUTPUT
+ )
+
+ # Stripping newline
+ string(STRIP ${_LLVM_CONFIG_OUTPUT} LibClang_LIBRARY_DIRS)
+endif()
+
+# 2. Try to find as library
+if (NOT LibClang_LIBRARY_DIRS)
+ message(STATUS "Setting LibClang_LIBRARY_DIRS based on common directories list")
+
+ set(LIBCLANG_COMMON_PATHS
+ /usr/lib/llvm-9/lib
+ /usr/lib/llvm-8/lib
+ /usr/lib/llvm-7/lib
+ /usr/lib/llvm-6.0/lib)
+
+ if (WIN32)
+ set(CMAKE_FIND_LIBRARY_PREFIXES "lib")
+ set(CMAKE_FIND_LIBRARY_SUFFIXES ".dll")
+
+ get_filename_component(LLVM_PATH_FROM_REGISTRY [HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\LLVM\\LLVM] ABSOLUTE)
+ list(APPEND LIBCLANG_COMMON_PATHS "${LLVM_PATH_FROM_REGISTRY}/bin")
+ endif()
+
+ find_library(_LIBCLANG_PATH
+ NAMES clang
+ HINTS ${LIBCLANG_COMMON_PATHS}
+ )
+
+ if (_LIBCLANG_PATH)
+ get_filename_component(LibClang_LIBRARY_DIRS ${_LIBCLANG_PATH} DIRECTORY)
+ endif()
+endif()
+
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(LibClang
+ "Please install llvm-config or set LibClang path manually"
+ LibClang_LIBRARY_DIRS
+)