blob: 00b0e658f54c3f969301e37d893b987ed9dd23cc [file] [log] [blame]
Imre Kisa21712e2019-10-08 12:56:59 +02001#
Gabor Tothdd09f0b2023-06-23 14:53:40 +02002# Copyright (c) 2019-2023, Arm Limited. All rights reserved.
Imre Kisa21712e2019-10-08 12:56:59 +02003#
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
Gabor Tothdd09f0b2023-06-23 14:53:40 +020063 /usr/lib/llvm-14/lib
Balint Dobszay12d171d2021-05-14 14:24:25 +020064 /usr/lib/llvm-10/lib
Imre Kisa21712e2019-10-08 12:56:59 +020065 /usr/lib/llvm-9/lib
66 /usr/lib/llvm-8/lib
67 /usr/lib/llvm-7/lib
68 /usr/lib/llvm-6.0/lib)
69
70 if (WIN32)
71 set(CMAKE_FIND_LIBRARY_PREFIXES "lib")
72 set(CMAKE_FIND_LIBRARY_SUFFIXES ".dll")
73
74 get_filename_component(LLVM_PATH_FROM_REGISTRY [HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\LLVM\\LLVM] ABSOLUTE)
75 list(APPEND LIBCLANG_COMMON_PATHS "${LLVM_PATH_FROM_REGISTRY}/bin")
76 endif()
77
78 find_library(_LIBCLANG_PATH
79 NAMES clang
80 HINTS ${LIBCLANG_COMMON_PATHS}
81 )
82
83 if (_LIBCLANG_PATH)
84 get_filename_component(LibClang_LIBRARY_DIRS ${_LIBCLANG_PATH} DIRECTORY)
85 endif()
86endif()
87
88include(FindPackageHandleStandardArgs)
89find_package_handle_standard_args(LibClang
90 "Please install llvm-config or set LibClang path manually"
91 LibClang_LIBRARY_DIRS
92)