OPTION(BUILD_SHARED_LIBS "Build shared libraries" ON)

CMAKE_MINIMUM_REQUIRED(VERSION 2.6 FATAL_ERROR)
CMAKE_POLICY(VERSION 2.6)
ENABLE_TESTING()

SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
FIND_PACKAGE(ZMQ REQUIRED)
FIND_PACKAGE(ZSTD 1.0 COMPONENTS static)

IF(MSVC)
   ADD_DEFINITIONS(-DZMQ_STATIC)
ELSE(MSVC)
   SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0")
   SET(CMAKE_CXX_FLAGS "-Wall -fPIC -O2")
ENDIF(MSVC)

ADD_SUBDIRECTORY(test)
#ADD_SUBDIRECTORY(thirdparty/bwapilib)

SET(CMAKE_CXX_STANDARD 14)
SET(CMAKE_CXX_STANDARD_REQUIRED ON)

IF(ZSTD_FOUND)
   ADD_DEFINITIONS(-DWITH_ZSTD)
ENDIF()

FILE(GLOB headers include/torchcraft/*.h)
FILE(GLOB cppsrc replayer/*.cpp client/*.cpp)
ADD_LIBRARY(torchcraft ${cppsrc})
TARGET_INCLUDE_DIRECTORIES(torchcraft
   PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/include
    ${CMAKE_CURRENT_SOURCE_DIR}/BWEnv/fbs
    ${CMAKE_CURRENT_SOURCE_DIR}/replayer
    ${CMAKE_CURRENT_SOURCE_DIR}/client
   PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/include/torchcraft
    ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/bwapilib
    "${ZSTD_INCLUDE_DIR}"
    "${ZMQ_INCLUDE_DIR}"
 )

IF(MSVC)
   TARGET_LINK_LIBRARIES(torchcraft shlwapi ws2_32 wsock32 Iphlpapi)
ELSEIF(APPLE)
   SET_TARGET_PROPERTIES(torchcraft PROPERTIES
       LINK_FLAGS "-undefined dynamic_lookup")
ENDIF(MSVC)

SET_TARGET_PROPERTIES(torchcraft PROPERTIES
    VERSION   2.0.0
    SOVERSION 2.0.0
    PUBLIC_HEADER "${headers}")
TARGET_LINK_LIBRARIES(torchcraft ${ZMQ_LIBRARIES} BWAPILIB)
IF(ZSTD_FOUND)
    # Use the static library since we're using "advanced/experimental" features
    TARGET_LINK_LIBRARIES(torchcraft ${ZSTD_LIBRARY})
ENDIF()

IF(NOT MSVC)
   INSTALL(TARGETS torchcraft
      LIBRARY DESTINATION lib
      ARCHIVE DESTINATION lib
      PUBLIC_HEADER DESTINATION include)
ENDIF(NOT MSVC)
