1 # Copyright (c) 2004-present, Facebook, Inc.
4 # This source code is licensed under the MIT license found in the
5 # LICENSE file in the hphp/hsl/ subdirectory of this source tree.
7 # - Try to find libdwarf
8 # Once done this will define
10 # LIBDWARF_FOUND - system has libdwarf
11 # LIBDWARF_INCLUDE_DIRS - the libdwarf include directory
12 # LIBDWARF_LIBRARIES - Link these to use libdwarf
13 # LIBDWARF_DEFINITIONS - Compiler switches required for using libdwarf
16 # Locate libelf library at first
19 endif (NOT LIBELF_FOUND)
21 if (LIBDWARF_LIBRARIES AND LIBDWARF_INCLUDE_DIRS)
22 set (LibDwarf_FIND_QUIETLY TRUE)
23 endif (LIBDWARF_LIBRARIES AND LIBDWARF_INCLUDE_DIRS)
25 find_package(PkgConfig)
26 pkg_check_modules(PkgConfig_LibDwarf QUIET libdwarf)
28 find_path (DWARF_INCLUDE_DIR
32 ${PkgConfig_LibDwarf_INCLUDE_DIRS}
36 /usr/local/include/libdwarf
39 ENV CPATH) # PATH and INCLUDE will also work
41 if (DWARF_INCLUDE_DIR)
42 set (LIBDWARF_INCLUDE_DIRS ${DWARF_INCLUDE_DIR})
45 find_library (LIBDWARF_LIBRARIES
53 ${PkgConfig_LibDwarf_LIBRARY_DIRS}
54 ENV LIBRARY_PATH # PATH and LIB will also work
56 include (FindPackageHandleStandardArgs)
59 # handle the QUIETLY and REQUIRED arguments and set LIBDWARF_FOUND to TRUE
60 # if all listed variables are TRUE
61 FIND_PACKAGE_HANDLE_STANDARD_ARGS(LibDwarf DEFAULT_MSG
64 LIBDWARF_INCLUDE_DIRS)
66 if (LIBDWARF_LIBRARIES AND LIBDWARF_INCLUDE_DIRS)
67 set(CMAKE_REQUIRED_INCLUDES ${LIBDWARF_INCLUDE_DIRS})
68 set(CMAKE_REQUIRED_LIBRARIES ${LIBDWARF_LIBRARIES} ${LIBELF_LIBRARIES})
70 # libdwarf makes breaking changes occasionally and doesn't provide an easy
71 # way to test for them. The following checks should detect the changes and
72 # pass that information on accordingly.
73 INCLUDE(CheckCXXSourceCompiles)
74 INCLUDE(CheckFunctionExists)
76 MACRO(CHECK_LIBDWARF_INIT init params var)
77 # Check for the existence of this particular init function.
78 unset(INIT_EXISTS CACHE)
79 CHECK_FUNCTION_EXISTS(${init} INIT_EXISTS)
81 set(LIBDWARF_USE_INIT_C ${var})
83 # Check to see if we can use a const name.
86 if (NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
87 # -std=c++11 is already set in HPHPCompiler.cmake, don't
88 # add -std=c++0x on top of that or clang will give errors
89 set(CMAKE_REQUIRED_FLAGS "-std=c++0x")
92 CHECK_CXX_SOURCE_COMPILES("
95 int dwarfCallback(const char * a, int b, Dwarf_Unsigned c,
96 Dwarf_Unsigned d, Dwarf_Unsigned e, Dwarf_Unsigned f,
97 Dwarf_Unsigned * g, Dwarf_Ptr h, int * i) {}
98 int main() { ${init}(${params}); return 0; }" DW_CONST)
100 set(LIBDWARF_CONST_NAME 1)
102 set(LIBDWARF_CONST_NAME 0)
105 ENDMACRO(CHECK_LIBDWARF_INIT)
107 # Order is important, last one is used.
108 CHECK_LIBDWARF_INIT("dwarf_producer_init"
109 "0, dwarfCallback, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr" 0)
110 CHECK_LIBDWARF_INIT("dwarf_producer_init_c" "0, dwarfCallback, nullptr, nullptr, nullptr, nullptr" 1)
112 set(CMAKE_REQUIRED_INCLUDES)
113 set(CMAKE_REQUIRED_LIBRARIES)
116 if(LIBDWARF_CONST_NAME)
117 message(STATUS "libdwarf uses const char* type")
119 message(STATUS "libdwarf uses char* type")
121 if(LIBDWARF_USE_INIT_C)
122 message(STATUS "libdwarf has dwarf_producer_init_c")
124 message(STATUS "libdwarf does not have dwarf_producer_init_c, using dwarf_producer_init")
127 mark_as_advanced(LIBDW_INCLUDE_DIR DWARF_INCLUDE_DIR)
128 mark_as_advanced(LIBDWARF_INCLUDE_DIRS LIBDWARF_LIBRARIES)
129 mark_as_advanced(LIBDWARF_CONST_NAME LIBDWARF_USE_INIT_C)