cmake_minimum_required(VERSION 3.12.0)
project(p8-platform)

enable_language(CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED YES)
set(CMAKE_CXX_EXTENSIONS NO)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR})

find_package(Threads REQUIRED)
include(GNUInstallDirs)
include(CheckAtomic.cmake)

set(p8-platform_NAME p8-platform)
set(p8-platform_DESCRIPTION "Pulse-Eight platform support library")
set(p8-platform_VERSION_MAJOR 2)
set(p8-platform_VERSION_MINOR 2)
set(p8-platform_VERSION_PATCH 0)

set(CMAKE_POSITION_INDEPENDENT_CODE on)

if(WIN32)
  set(PLAT_SOURCES src/windows/dlfcn-win32.cpp
                   src/windows/os-threads.cpp)
endif()

set(p8-platform_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})

if(NOT ${CORE_SYSTEM_NAME} STREQUAL "")
  if(${CORE_SYSTEM_NAME} STREQUAL "osx" OR ${CORE_SYSTEM_NAME} STREQUAL "ios")
    list(APPEND p8-platform_LIBRARIES "-framework CoreVideo")
  endif()
endif()

set(SOURCES src/util/StringUtils.cpp)

add_library(p8-platform ${SOURCES} ${PLAT_SOURCES})
target_link_libraries(p8-platform ${p8-platform_LIBRARIES})
set_target_properties(p8-platform
    PROPERTIES
    VERSION ${p8-platform_VERSION_MAJOR}.${p8-platform_VERSION_MINOR}.${p8-platform_VERSION_PATCH}
    SOVERSION ${p8-platform_VERSION_MAJOR})

if(WIN32)
  include(CheckSymbolExists)
  check_symbol_exists(_AMD64_ Windows.h WIN64)
  check_symbol_exists(_ARM64_ Windows.h ARM64)

  if (MSVC)
    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /D_HAS_ITERATOR_DEBUGGING=1 /D_SECURE_SCL=1")
    add_compile_definitions($<$<CONFIG:Debug>:_ITERATOR_DEBUG_LEVEL=2>)
    add_compile_definitions($<$<CONFIG:Debug>:_HAS_ITERATOR_DEBUGGING=1>)
    # generate pdb in release mode too
    set_target_properties(p8-platform
        PROPERTIES
        COMPILE_PDB_NAME_DEBUG p8-platform${CMAKE_DEBUG_POSTFIX}
        COMPILE_PDB_NAME_RELEASE p8-platform
        COMPILE_PDB_NAME_MINSIZEREL p8-platform
        COMPILE_PDB_NAME_RELWITHDEBINFO p8-platform)

    if (WIN64 OR ARM64)
      # default setting that got removed in recent vs versions, generates a warning if set
      string(REPLACE "/arch:SSE2" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
    endif()

  endif(MSVC)

  if (NOT WIN64 AND NOT ARM64)
    add_definitions(-D_USE_32BIT_TIME_T)
  endif()
endif(WIN32)

install(TARGETS p8-platform DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(FILES src/os.h DESTINATION include/p8-platform)
if(WIN32)
  install(FILES src/windows/dlfcn-win32.h
                src/windows/os-socket.h
                src/windows/os-threads.h
                src/windows/os-types.h
          DESTINATION include/p8-platform/windows)
else(WIN32)
  install(FILES src/posix/os-socket.h
                src/posix/os-threads.h
                src/posix/os-types.h
          DESTINATION include/p8-platform/posix)
endif(WIN32)
install(FILES src/sockets/cdevsocket.h
              src/sockets/socket.h
              src/sockets/tcp.h
        DESTINATION include/p8-platform/sockets)
install(FILES src/threads/atomics.h
              src/threads/mutex.h
              src/threads/threads.h
        DESTINATION include/p8-platform/threads)
install(FILES src/util/atomic.h
              src/util/buffer.h
              src/util/StringUtils.h
              src/util/StdString.h
              src/util/timeutils.h
              src/util/util.h
        DESTINATION include/p8-platform/util)

if(MSVC)
  # install generated pdb
  install(FILES $<TARGET_FILE_DIR:p8-platform>/p8-platform.pdb
          DESTINATION "${CMAKE_INSTALL_LIBDIR}")
endif(MSVC)

if(NOT WIN32)
  configure_file(p8-platform.pc.in p8-platform.pc @ONLY)
  install(FILES ${CMAKE_CURRENT_BINARY_DIR}/p8-platform.pc
          DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
endif(NOT WIN32)

# config mode
# how to walk back to the prefix from the installed config file. worked out here
# because the depth varies with the libdir: lib, lib64, lib/x86_64-linux-gnu
file (RELATIVE_PATH p8-platform_CONFIG_TO_PREFIX
      "${CMAKE_INSTALL_FULL_LIBDIR}/p8-platform" "${CMAKE_INSTALL_PREFIX}")

# export a static build as STATIC rather than UNKNOWN, because cmake only adds
# the c++ runtime for an imported library that says it's static. a shared build
# stays UNKNOWN: it doesn't need the runtime spelled out, and SHARED would want
# an IMPORTED_IMPLIB next to the dll on windows
get_target_property (p8-platform_TARGET_TYPE p8-platform TYPE)
if (p8-platform_TARGET_TYPE STREQUAL "STATIC_LIBRARY")
  set (p8-platform_LIBRARY_TYPE "STATIC")
else ()
  set (p8-platform_LIBRARY_TYPE "UNKNOWN")
endif ()
configure_file (p8-platform-config.cmake.in
                p8-platform-config.cmake @ONLY)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/p8-platform-config.cmake
        DESTINATION ${CMAKE_INSTALL_LIBDIR}/p8-platform)

