include(CMakePushCheckState)
include(CheckSymbolExists)

cmake_push_check_state(RESET)
find_library(EXECINFO_LIBRARY NAMES execinfo)
if(EXECINFO_LIBRARY)
  set(CMAKE_REQUIRED_LIBRARIES "${EXECINFO_LIBRARY}")
  list(APPEND PLATFORM_LIBRARIES "${EXECINFO_LIBRARY}")
endif(EXECINFO_LIBRARY)
check_symbol_exists(backtrace execinfo.h HAVE_BACKTRACE)
cmake_pop_check_state()

cmake_push_check_state(RESET)
set(CMAKE_REQUIRED_DEFINITIONS "-D_GNU_SOURCE")
find_library(DL_LIBRARY NAMES dl)
if(DL_LIBRARY)
  set(CMAKE_REQUIRED_LIBRARIES "${DL_LIBRARY}")
  list(APPEND PLATFORM_LIBRARIES "${DL_LIBRARY}")
endif(DL_LIBRARY)
check_symbol_exists(dladdr dlfcn.h HAVE_DLADDR)
cmake_pop_check_state()

if(HAVE_BACKTRACE)
  add_definitions(-DHAVE_BACKTRACE=1)
endif()

if(HAVE_DLADDR)
  add_definitions(-DHAVE_DLADDR=1)
endif()

if(HAVE_BACKTRACE OR HAVE_DLADDR)
  target_compile_options(project_options INTERFACE -ggdb3)
  target_link_libraries(project_options INTERFACE -rdynamic)
  add_definitions(-D_GNU_SOURCE=1)
endif()
