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


##### set project version ########################

include( TDEVersion )
cmake_minimum_required( VERSION ${TDE_CMAKE_MINIMUM_VERSION} )
tde_set_project_version( )


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

project( libr )
set( PACKAGE libr )
set( PACKAGE_VERSION 0.7.0 )


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

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


##### include our 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 )
option( WITH_GTK              "Build support for GTK"        ON  )

# NOTE: WITH_GTK build gtk bindings to ease libr in gtk applications.
#       It uses weak linkage, so gtk will be required during build
#       only and not strictly necessary during runtime.

#### 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 )
    check_c_source_compiles(
        "#include <bfd.h>
         int main() { bfd *ihandle, *ohandle; asection *iscn, *oscn;
           bfd_copy_private_section_data(ihandle, iscn, ohandle, oscn, NULL);
         }"
        HAVE_BFD_2_45)
    tde_restore( CMAKE_REQUIRED_LIBRARIES )
    tde_restore( CMAKE_REQUIRED_DEFINITIONS )
    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 )

if( WITH_GTK )
    pkg_search_module( GTK gtk+-2.0 )
    if( NOT GTK_FOUND )
        tde_message_fatal( "gtk2 is required but was not found on your system" )
    endif( )
endif( WITH_GTK )

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 )
