blob: c371449c6b39f0da3491b417a0adbc2f1a673866 [file] [log] [blame]
Imre Kisa21712e2019-10-08 12:56:59 +02001#
2# Copyright (c) 2019-2021, Arm Limited. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6
7#[=======================================================================[.rst:
8FindLibClang
9------------
10
11CMake module for finding the LibClang library.
12
13Control flow
14^^^^^^^^^^^^
15
161. Running ``llvm-config`` if exists
172. Searching for library at common places
183. Searching in Windows registry if available
19
20
21Imported Targets
22^^^^^^^^^^^^^^^^
23
24This module provides the following imported targets, if found:
25
26``LibClang``
27 The Clang library
28
29
30Result Variables
31^^^^^^^^^^^^^^^^
32
33This will define the following variables:
34
35``LibClang_FOUND``
36 True if the system has the Clang library.
37``LibClang_LIBRARY_DIRS``
38 Libraries needed to link to Clang.
39
40#]=======================================================================]
41
42
43# 1. Use llvm-config
44find_program(_LLVM_CONFIG_COMMAND "llvm-config")
45
46if (_LLVM_CONFIG_COMMAND)
47 message(STATUS "Setting LibClang_LIBRARY_DIRS using ${_LLVM_CONFIG_COMMAND}")
48
49 execute_process(
50 COMMAND ${_LLVM_CONFIG_COMMAND} --libdir
51 OUTPUT_VARIABLE _LLVM_CONFIG_OUTPUT
52 )
53
54 # Stripping newline
55 string(STRIP ${_LLVM_CONFIG_OUTPUT} LibClang_LIBRARY_DIRS)
56endif()
57
58# 2. Try to find as library
59if (NOT LibClang_LIBRARY_DIRS)
60 message(STATUS "Setting LibClang_LIBRARY_DIRS based on common directories list")
61
62 set(LIBCLANG_COMMON_PATHS
63 /usr/lib/llvm-9/lib
64 /usr/lib/llvm-8/lib
65 /usr/lib/llvm-7/lib
66 /usr/lib/llvm-6.0/lib)
67
68 if (WIN32)
69 set(CMAKE_FIND_LIBRARY_PREFIXES "lib")
70 set(CMAKE_FIND_LIBRARY_SUFFIXES ".dll")
71
72 get_filename_component(LLVM_PATH_FROM_REGISTRY [HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\LLVM\\LLVM] ABSOLUTE)
73 list(APPEND LIBCLANG_COMMON_PATHS "${LLVM_PATH_FROM_REGISTRY}/bin")
74 endif()
75
76 find_library(_LIBCLANG_PATH
77 NAMES clang
78 HINTS ${LIBCLANG_COMMON_PATHS}
79 )
80
81 if (_LIBCLANG_PATH)
82 get_filename_component(LibClang_LIBRARY_DIRS ${_LIBCLANG_PATH} DIRECTORY)
83 endif()
84endif()
85
86include(FindPackageHandleStandardArgs)
87find_package_handle_standard_args(LibClang
88 "Please install llvm-config or set LibClang path manually"
89 LibClang_LIBRARY_DIRS
90)