summaryrefslogtreecommitdiff
path: root/cmake/Findelfutils.cmake
blob: f5b3f0330f7640d0701b6c7b775815ff4afcb5df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#.rst:
# Findelfutils
# -----------------
#
# Try to locate the elfutils development installation
#
# On Debian-like systems, you can install libdw-dev.
#
# Otherwise, the ``ELFUTILS_INSTALL_DIR`` (CMake or Environment) variable
# can be used to pinpoint the elfutils binary installation
#
# If found, this will define the following variables:
#
# ``elfutils_FOUND``
#     True if the elfutils header and library files have been found
#
# ``elfutils::dw``
# ``elfutils::elf``
#     Imported targets containing the includes and libraries need to build
#     against
#

if (TARGET elfutils::dw AND TARGET elfutils::elf)
  set(elfutils_FOUND TRUE)
  return()
endif()

find_path(ELFUTILS_INCLUDE_DIR
  NAMES libdwfl.h elfutils/libdwfl.h
  PATH_SUFFIXES include
  HINTS
    "${ELFUTILS_INSTALL_DIR}" ENV ELFUTILS_INSTALL_DIR "${CMAKE_PREFIX_PATH}"
)

foreach(lib dw elf eu_compat)
  find_library(ELFUTILS_LIB_${lib}
    NAMES ${lib}
    PATH_SUFFIXES lib
    HINTS
      "${ELFUTILS_INSTALL_DIR}" ENV ELFUTILS_INSTALL_DIR "${CMAKE_PREFIX_PATH}"
  )
endforeach()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(elfutils
  DEFAULT_MSG
    ELFUTILS_INCLUDE_DIR ELFUTILS_LIB_dw ELFUTILS_LIB_elf
)

if(elfutils_FOUND)
  foreach(lib dw elf)
    if (NOT TARGET elfutils::${lib})
      add_library(elfutils::${lib} UNKNOWN IMPORTED)
      set_target_properties(elfutils::${lib}
        PROPERTIES
          IMPORTED_LOCATION "${ELFUTILS_LIB_${lib}}"
          INTERFACE_INCLUDE_DIRECTORIES
            "${ELFUTILS_INCLUDE_DIR};${ELFUTILS_INCLUDE_DIR}/elfutils"
      )
      if (ELFUTILS_LIB_eu_compat)
        target_link_libraries(elfutils::${lib} INTERFACE ${ELFUTILS_LIB_eu_compat})
      endif()
    endif()
  endforeach()
else()
  message(STATUS "                        (set ELFUTILS_INSTALL_DIR, or install libdw-dev on a Debian-like system)")
endif()

mark_as_advanced(ELFUTILS_INCLUDE_DIR ELFUTILS_LIB_elf ELFUTILS_LIB_dw)

include(FeatureSummary)
set_package_properties(elfutils PROPERTIES
  URL "https://sourceware.org/elfutils/"
  DESCRIPTION "a collection of utilities and libraries to read, create and modify ELF binary files")