#################################################
#
#  (C) 2014 Slávek Banko
#  slavek (DOT) banko (AT) axis.cz
#
#  Improvements and feedback are welcome
#
#  This file is released under GPL >= 2
#
#################################################

cmake_minimum_required( VERSION 3.1 )


##### general package setup #####################

project( libr )
set( PACKAGE libr )
set( VERSION R14.0.12 )
set( PACKAGE_VERSION 0.6.0 )


##### include essential cmake modules ###########

include( FindPkgConfig )
include( CheckIncludeFile )
include( CheckFunctionExists )
include( CheckLibraryExists )
include( CheckSymbolExists )


##### include our cmake modules #################

set( CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules" )
include( TDEMacros )


##### setup install paths #######################

include( TDESetupPaths )
tde_setup_paths( )


##### optional stuff ############################

option( WITH_BACKEND_LIBBFD "Build with libbfd backend" "ON" )
option( WITH_BACKEND_LIBELF "Build with libelf backend" "OFF" )
option( WITH_BACKEND_READONLY "Build with read-only backend" "OFF" )


#### configure checks ###########################

if( WITH_BACKEND_LIBBFD )
    tde_save_and_set( CMAKE_REQUIRED_DEFINITIONS "-DPACKAGE" "-DPACKAGE_VERSION" )
    check_include_file( bfd.h HAVE_BFD_H )
    if( NOT HAVE_BFD_H )
        tde_message_fatal( "Could not find libbfd header file (bfd.h)!\nThis file is usually included in the package binutils-dev." )
    endif( NOT HAVE_BFD_H )
    tde_save_and_set( CMAKE_REQUIRED_LIBRARIES "bfd" )
    check_symbol_exists( bfd_asymbol_section bfd.h HAVE_BFD_2_34 )
    tde_restore( CMAKE_REQUIRED_DEFINITIONS )
    tde_restore( CMAKE_REQUIRED_LIBRARIES )
    set( BACKEND_LIBRARIES "-lbfd" )
    set( LIBR_BACKEND "bfd" )
    set( BACKEND_NAME "libbfd" )

    check_include_file( libiberty.h HAVE_LIBIBERTY_H )
    if( HAVE_LIBIBERTY_H )
        list( APPEND BACKEND_LIBRARIES "-liberty" )
    endif( HAVE_LIBIBERTY_H )

elseif( WITH_BACKEND_LIBELF )
    # Is libelf 0.8.2 safe enough? testing is currently on 0.8.6
    pkg_search_module( BACKEND libelf>=0.8.2 )
    if( NOT BACKEND_FOUND )
        tde_message_fatal( "libelf >= 0.8.2 are required, but not found on your system" )
    endif( NOT BACKEND_FOUND )
    set( LIBR_BACKEND "elf" )
    set( BACKEND_NAME "libelf" )

elseif( WITH_BACKEND_READONLY )
    set( BACKEND_NAME "readonly" )
    set( LIBR_BACKEND "ro" )

else( )
    tde_message_fatal( "Backend is not selected, cannot build." )
endif( )


find_package( ZLIB )
if( NOT ZLIB_FOUND )
    tde_message_fatal( "zlib are required, but not found on your system" )
endif( NOT ZLIB_FOUND )

pkg_search_module( LIBGLADE libglade-2.0>=2.0.0 )
if( NOT LIBGLADE_FOUND )
    tde_message_fatal( "libglade-2.0 are required, but not found on your system" )
endif( NOT LIBGLADE_FOUND )

set( DL_LIBRARIES dl )
check_library_exists( ${DL_LIBRARIES} dlopen /lib HAVE_LIBDL )
if( NOT HAVE_LIBDL )
  unset( DL_LIBRARIES )
  check_function_exists( dlopen HAVE_DLOPEN )
  if( HAVE_DLOPEN )
    set( HAVE_LIBDL 1 )
  endif( HAVE_DLOPEN )
endif( NOT HAVE_LIBDL )

set( EXTRA_LIBRARIES "-lm -lpthread" )
if( UNIX )
  set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden" )
endif( UNIX )

message( STATUS "Ready to build with backend ${BACKEND_NAME}" )


#### pkg-config #################################

set( prefix      ${CMAKE_INSTALL_PREFIX} )
set( exec_prefix ${EXEC_INSTALL_PREFIX} )
set( libdir      ${LIB_INSTALL_DIR} )
set( includedir  ${INCLUDE_INSTALL_DIR}/libr )

configure_file( libr.pc.in libr.pc @ONLY )
install( FILES ${CMAKE_CURRENT_BINARY_DIR}/libr.pc
         DESTINATION ${PKGCONFIG_INSTALL_DIR} )


##### build #####################################

tde_auto_add_subdirectories()


##### write configure files #####################

configure_file( src/config.h.cmake config.h @ONLY )
configure_file( doc/libr.cfg.cmake doc/libr.cfg @ONLY )
