Add $INSTALL/lib/elfutils subdirectory to build paths (#680)
[dyninst.git] / cmake / Modules / FindLibElf.cmake
1 #========================================================================================
2 # FindLibElf.cmake
3 #
4 # Find libelf include dirs and libraries
5 #
6 #               ----------------------------------------
7 #
8 # Use this module by invoking find_package with the form::
9 #
10 #  find_package(LibElf
11 #    [version] [EXACT]      # Minimum or EXACT version e.g. 0.173
12 #    [REQUIRED]             # Fail with error if libelf is not found
13 #  )
14 #
15 # This module reads hints about search locations from variables::
16 #
17 #       LibElf_ROOT_DIR         - Base directory the of libelf installation
18 #       LibElf_INCLUDEDIR       - Hint directory that contains the libelf headers files
19 #       LibElf_LIBRARYDIR       - Hint directory that contains the libelf library files
20 #
21 # and saves search results persistently in CMake cache entries::
22 #
23 #       LibElf_FOUND                    - True if headers and requested libraries were found
24 #       LibElf_INCLUDE_DIRS     - libelf include directories
25 #       LibElf_LIBRARY_DIRS             - Link directories for libelf libraries
26 #       LibElf_LIBRARIES                - libelf library files
27 #
28 #
29 # Based on the version by Bernhard Walle <bernhard.walle@gmx.de> Copyright (c) 2008
30 #
31 #========================================================================================
32
33 include(DyninstSystemPaths)
34
35 # Non-standard subdirectories to search
36 set(_path_suffixes libelf libelfls elfutils)
37
38 find_path(LibElf_INCLUDE_DIR
39           NAMES libelf.h
40           HINTS ${LibElf_ROOT_DIR}/include ${LibElf_ROOT_DIR} ${LibElf_INCLUDEDIR}
41           PATHS ${DYNINST_SYSTEM_INCLUDE_PATHS}
42           PATH_SUFFIXES ${_path_suffixes}
43           DOC "libelf include directories")
44
45 find_library(LibElf_LIBRARIES
46              NAMES libelf.so.1 libelf.so
47              HINTS ${LibElf_ROOT_DIR}/lib ${LibElf_ROOT_DIR} ${LibElf_LIBRARYDIR}
48              PATHS ${DYNINST_SYSTEM_LIBRARY_PATHS}
49              PATH_SUFFIXES ${_path_suffixes})
50
51 # Find the library with the highest version
52 set(_max_ver 0.0)
53 set(_max_ver_lib)
54 foreach(l ${LibElf_LIBRARIES})
55   get_filename_component(_elf_realpath ${LibElf_LIBRARIES} REALPATH)
56   string(REGEX MATCH
57                "libelf\\-(.+)\\.so\\.*$"
58                res
59                ${_elf_realpath})
60
61   # The library version number is stored in CMAKE_MATCH_1
62   set(_cur_ver ${CMAKE_MATCH_1})
63
64   if(${_cur_ver} VERSION_GREATER ${_max_ver})
65     set(_max_ver ${_cur_ver})
66     set(_max_ver_lib ${l})
67   endif()
68 endforeach()
69
70 # Set the exported variables to the best match
71 set(LibElf_LIBRARIES ${_max_ver_lib})
72 set(LibElf_VERSION ${_max_ver})
73
74 include(FindPackageHandleStandardArgs)
75 find_package_handle_standard_args(LibElf
76                                   FOUND_VAR
77                                   LibElf_FOUND
78                                   REQUIRED_VARS
79                                   LibElf_LIBRARIES
80                                   LibElf_INCLUDE_DIR
81                                   VERSION_VAR
82                                   LibElf_VERSION)
83
84 # Export cache variables
85 if(LibElf_FOUND)
86   set(LibElf_INCLUDE_DIRS ${LibElf_INCLUDE_DIR})
87   set(LibElf_LIBRARIES ${LibElf_LIBRARIES})
88
89   # Because we only report the library with the largest version, we are
90   # guaranteed there is only one file in LibElf_LIBRARIES
91   get_filename_component(_elf_dir ${LibElf_LIBRARIES} DIRECTORY)
92   set(LibElf_LIBRARY_DIRS ${_elf_dir} "${_elf_dir}/elfutils")
93 endif()