#
#  libcontra - LIBrary for CONnection TRAcking
#
#  (C) 2017-2023 Thorsten Alteholz <libcontra@alteholz.eu>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; version 2 of the License
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program (see the file COPYING included with this
#  distribution); if not, write to the Free Software Foundation, Inc.,
#  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
include(CheckIncludeFile)
include(GNUInstallDirs)

SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC -Wl,--no-undefined")
ADD_LIBRARY(contra SHARED libcontra.c libcontra_connect.c libcontra_log.c libcontra_socket.c libcontra_close.c libcontra_listen.c libcontra_poll.c libcontra_bind.c libcontra_accept.c libcontra_send.c libcontra_recv.c libcontra_setsockopt.c libcontra_getsockopt.c libcontra_gethostbyname.c libcontra_gethostbyaddr.c libcontra_freeaddrinfo.c libcontra_getaddrinfo.c)
ADD_LIBRARY(contra-only-connect SHARED libcontra-only-connect.c libcontra_connect.c libcontra_log.c)

CHECK_INCLUDE_FILE("sys/types.h" HAVE_SYS_TYPES_H)
CHECK_INCLUDE_FILE("stdbool.h" HAVE_STDBOOL_H) 
CHECK_INCLUDE_FILE("dlfcn.h" HAVE_DLFCN_H)
CHECK_INCLUDE_FILE("arpa/inet.h" HAVE_ARPA_INET_H)
CHECK_INCLUDE_FILE("sys/socket.h" HAVE_SYS_SOCKET_H)
CHECK_INCLUDE_FILE("netdb.h" HAVE_NETDB_H)
CHECK_INCLUDE_FILE("errno.h" HAVE_ERRNO_H)
CHECK_INCLUDE_FILE("stdlib.h" HAVE_STDLIB_H)
CHECK_INCLUDE_FILE("stdio.h" HAVE_STDIO_H)
CHECK_INCLUDE_FILE("string.h" HAVE_STRING_H)
CHECK_INCLUDE_FILE("stdarg.h" HAVE_STDARG_H)
CHECK_INCLUDE_FILE("pthread.h" HAVE_PTHREAD_H)


TARGET_LINK_LIBRARIES(contra -ldl)
TARGET_INCLUDE_DIRECTORIES(contra PUBLIC .)
TARGET_LINK_LIBRARIES(contra-only-connect -ldl)
TARGET_INCLUDE_DIRECTORIES(contra-only-connect PUBLIC .)

# INSTALL(TARGETS contra DESTINATION lib)
# INSTALL(TARGETS contra-only-connect DESTINATION lib)
#INSTALL(TARGETS contra DESTINATION ${CMAKE_INSTALL_LIBDIR})
#INSTALL(TARGETS contra-only-connect DESTINATION ${CMAKE_INSTALL_LIBDIR})

set_target_properties (contra PROPERTIES VERSION 0.2.2 SOVERSION 1)
set_target_properties (contra-only-connect PROPERTIES VERSION 0.2.2 SOVERSION 1)

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "/usr/lib")
INSTALL(TARGETS contra DESTINATION ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
INSTALL(TARGETS contra-only-connect DESTINATION ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
