xref: /aosp_15_r20/external/stg/cmake/FindLibElf.cmake (revision 9e3b08ae94a55201065475453d799e8b1378bea6)
1# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2#
3# Copyright 2023 Google LLC
4#
5# Licensed under the Apache License v2.0 with LLVM Exceptions (the "License");
6# you may not use this file except in compliance with the License.  You may
7# obtain a copy of the License at
8#
9# https://llvm.org/LICENSE.txt
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations under
15# the License.
16#
17# Author: Aleksei Vetrov
18
19#[=======================================================================[.rst:
20FindLibElf
21----------
22
23Finds the ELF processing library (libelf).
24
25Imported Targets
26^^^^^^^^^^^^^^^^
27
28This module provides the following imported targets, if found:
29
30``LibElf::LibElf``
31  The LibElf library
32
33Result Variables
34^^^^^^^^^^^^^^^^
35
36This will define the following variables:
37
38``LibElf_FOUND``
39  True if the system has the LibElf library.
40``LibElf_VERSION``
41  The version of the LibElf library which was found.
42``LibElf_INCLUDE_DIRS``
43  Include directories needed to use LibElf.
44``LibElf_LIBRARIES``
45  Libraries needed to link to LibElf.
46``LibElf_DEFINITIONS``
47  the compiler switches required for using LibElf
48
49Cache Variables
50^^^^^^^^^^^^^^^
51
52The following cache variables may also be set:
53
54``LibElf_INCLUDE_DIR``
55  The directory containing ``libelf.h``.
56``LibElf_LIBRARY``
57  The path to the ``libelf.so``.
58
59#]=======================================================================]
60
61find_package(PkgConfig)
62pkg_check_modules(PC_LibElf QUIET libelf)
63
64find_library(
65  LibElf_LIBRARY
66  NAMES elf
67  HINTS ${PC_LibElf_LIBDIR} ${PC_LibElf_LIBRARY_DIRS})
68# Try the value from user if the library is not found.
69if(DEFINED LibElf_LIBRARIES AND NOT DEFINED LibElf_LIBRARY)
70  set(LibElf_LIBRARY ${LibElf_LIBRARIES})
71endif()
72mark_as_advanced(LibElf_LIBRARY)
73
74find_path(
75  LibElf_INCLUDE_DIR
76  NAMES libelf.h
77  HINTS ${PC_LibElf_INCLUDEDIR} ${PC_LibElf_INCLUDE_DIRS})
78# Try the value from user if the library is not found.
79if(DEFINED LibElf_INCLUDE_DIRS AND NOT DEFINED LibElf_INCLUDE_DIR)
80  set(LibElf_INCLUDE_DIR ${LibElf_INCLUDE_DIRS})
81endif()
82mark_as_advanced(LibElf_INCLUDE_DIR)
83
84set(LibElf_VERSION ${PC_LibElf_VERSION})
85
86include(FindPackageHandleStandardArgs)
87find_package_handle_standard_args(
88  LibElf
89  REQUIRED_VARS LibElf_LIBRARY LibElf_INCLUDE_DIR
90  VERSION_VAR LibElf_VERSION)
91
92if(LibElf_FOUND)
93  set(LibElf_LIBRARIES ${LibElf_LIBRARY})
94  set(LibElf_INCLUDE_DIRS ${LibElf_INCLUDE_DIR})
95  set(LibElf_DEFINITIONS ${PC_LibElf_CFLAGS_OTHER})
96endif()
97
98if(LibElf_FOUND AND NOT TARGET LibElf::LibElf)
99  add_library(LibElf::LibElf UNKNOWN IMPORTED)
100  set_target_properties(
101    LibElf::LibElf
102    PROPERTIES IMPORTED_LOCATION "${LibElf_LIBRARY}"
103               INTERFACE_COMPILE_OPTIONS "${PC_LibElf_CFLAGS_OTHER}"
104               INTERFACE_INCLUDE_DIRECTORIES "${LibElf_INCLUDE_DIR}")
105endif()
106