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
+)
diff --git a/cmake/UnitTest.cmake b/cmake/UnitTest.cmake
index d2255df..207b25c 100644
--- a/cmake/UnitTest.cmake
+++ b/cmake/UnitTest.cmake
@@ -11,6 +11,7 @@
 #     CPICKER_COMMAND: command of the c-picker
 #     CPICKER_CACHE_PATH: root directory of the c-picker generate files
 #     UNIT_TEST_COMMON_SOURCES: common source files for every test build
+#     CLANG_LIBRARY_PATH: libclang directory for c-picker
 #   Modules
 #     CTest module should be included in the root CMakeLists.txt before calling this function
 
@@ -43,14 +44,18 @@
 
 			# Fetching referenced source files as the dependencies of the generated file
 			execute_process(
-				COMMAND ${CPICKER_COMMAND} --config ${TEST_SOURCE} --root ${TF_A_PATH} --print-dependencies
+				COMMAND
+					${CMAKE_COMMAND} -E env CLANG_LIBRARY_PATH=${CLANG_LIBRARY_PATH}
+					${CPICKER_COMMAND} --config ${TEST_SOURCE} --root ${TF_A_PATH} --print-dependencies
 				OUTPUT_VARIABLE CPICKER_DEPENDENCIES
 			)
 
 			# Adding custom command for invoking c-picker
 			add_custom_command(
 				OUTPUT ${CPICKER_OUTPUT}
-				COMMAND ${CPICKER_COMMAND} --config ${TEST_SOURCE} --root ${TF_A_PATH} > ${CPICKER_OUTPUT}
+				COMMAND
+					${CMAKE_COMMAND} -E env CLANG_LIBRARY_PATH=${CLANG_LIBRARY_PATH}
+					${CPICKER_COMMAND} --config ${TEST_SOURCE} --root ${TF_A_PATH} > ${CPICKER_OUTPUT}
 				DEPENDS ${TEST_SOURCE} ${CPICKER_DEPENDENCIES}
 				COMMENT "Generating c-picker output ${CPICKER_OUTPUT}"
 			)