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