project(ceccclient)
cmake_minimum_required(VERSION 3.12.0)

set(ceccclient_NAME ceccclient)
set(ceccclient_DESCRIPTION "libCEC test client")
set(ceccclient_VERSION_MAJOR ${LIBCEC_VERSION_MAJOR})
set(ceccclient_VERSION_MINOR ${LIBCEC_VERSION_MINOR})
set(ceccclient_VERSION_PATCH ${LIBCEC_VERSION_PATCH})

enable_language(C)
include(CheckCSourceCompiles)
include(CheckLibraryExists)
include(CheckIncludeFiles)
include(../../cmake/WindowsDebug.cmake)

find_package(Threads REQUIRED)

set(ceccclient_SOURCES cecc-client.c)

add_executable(cecc-client ${ceccclient_SOURCES})
set_target_properties(cecc-client PROPERTIES VERSION ${LIBCEC_VERSION_MAJOR}.${LIBCEC_VERSION_MINOR}.${LIBCEC_VERSION_PATCH})
target_link_libraries(cecc-client ${CMAKE_THREAD_LIBS_INIT})

if (NOT WIN32)
  # check for dlopen
  check_library_exists(dl dlopen "" HAVE_DLOPEN)
  if (HAVE_DLOPEN)
    target_link_libraries(cecc-client dl)
  endif()
else()
  add_definitions(-DTARGET_WINDOWS -DNOMINMAX -D_CRT_SECURE_NO_WARNINGS -D_WINSOCKAPI_)
  check_symbol_exists(_AMD64_ Windows.h WIN64)
  check_symbol_exists(_ARM64_ Windows.h ARM64)
  if (WIN64 OR ARM64)
    string(REPLACE "/arch:SSE2" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
  else()
    add_definitions(-D_USE_32BIT_TIME_T)
  endif()
endif()

include_directories(${PROJECT_SOURCE_DIR}
                    ${PROJECT_SOURCE_DIR}/../libcec
                    ${PROJECT_SOURCE_DIR}/../../include)

# write env.h
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/env.h.in ${CMAKE_CURRENT_SOURCE_DIR}/env.h)

# bin/ on every platform, as for cec-client
install(TARGETS     cecc-client
        DESTINATION ${CMAKE_INSTALL_BINDIR})
