cmake_minimum_required( VERSION 3.1.0 FATAL_ERROR )

project( "restbed" VERSION 4.7.0 LANGUAGES CXX )
message( "                                                              " )
message( "##############################################################" )
message( "#                                                            #" )
message( "#  Copyright 2013-2020, Corvusoft Ltd, All Rights Reserved.  #" )
message( "#                                                            #" )
message( "#  Restbed is dual-licensed requiring commerical entities    #" )
message( "#  to acquire a Corvusoft Permissive License, Contact        #" )
message( "#  sales@corvusoft.co.uk for details.                        #" )
message( "#                                                            #" )
message( "##############################################################" )
message( "                                                              " )

#
# Build Options
#
option( BUILD_TESTS "Build unit tests." OFF )
option( BUILD_SSL "Build secure socket layer support." OFF )
option( BUILD_IPC "Build unix domain socket layer support." OFF )

#
# Configuration
#
set( CMAKE_CXX_STANDARD 14 )
set( INCLUDE_DIR "${PROJECT_SOURCE_DIR}/source" )
set( SOURCE_DIR  "${PROJECT_SOURCE_DIR}/source/corvusoft/${PROJECT_NAME}" )

if ( NOT DEFINED CMAKE_INSTALL_LIBDIR )
    set( CMAKE_INSTALL_LIBDIR "library")
endif ( )

if( ${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC )
    set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /D_WIN32_WINNT=0x0601 /W4 /wd4068 /wd4702 /wd4996 /wd4265" )
endif ( )

if( NOT WIN32 )
    set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Weffc++ -pedantic -Wno-unknown-pragmas -Wno-deprecated-declarations -Wno-non-virtual-dtor" )
endif ( )

if ( UNIX AND NOT APPLE )
    set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread" )
endif ( )

if ( APPLE )
    set( CMAKE_MACOSX_RPATH ON )
endif ( )

set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake" )

find_package( asio REQUIRED )

if ( BUILD_SSL )
    add_definitions( "-DBUILD_SSL" )
    find_package( openssl REQUIRED )
endif ( )

include_directories( ${INCLUDE_DIR} SYSTEM ${asio_INCLUDE} ${ssl_INCLUDE} )

if ( BUILD_IPC )
    add_definitions( "-DBUILD_IPC" )
endif ( )

if ( WIN32 )
	add_definitions( -DWIN_DLL_EXPORT )
	set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4251" )
endif ( )

#
# Build
#
file( GLOB_RECURSE MANIFEST "${SOURCE_DIR}/*.cpp" "dependency/asio/asio/src/asio.cpp" )

set( STATIC_LIBRARY_NAME "${PROJECT_NAME}-static" )
add_library( ${STATIC_LIBRARY_NAME} STATIC ${MANIFEST} )
set_property( TARGET ${STATIC_LIBRARY_NAME} PROPERTY CXX_STANDARD 14 )
set_property( TARGET ${STATIC_LIBRARY_NAME} PROPERTY CXX_STANDARD_REQUIRED ON )
set_target_properties( ${STATIC_LIBRARY_NAME} PROPERTIES OUTPUT_NAME ${PROJECT_NAME} )

target_compile_definitions(${STATIC_LIBRARY_NAME} PUBLIC ASIO_SEPARATE_COMPILATION)
target_include_directories(${STATIC_LIBRARY_NAME} PUBLIC source)

if ( BUILD_SSL )
    target_link_libraries( ${STATIC_LIBRARY_NAME} LINK_PRIVATE ${ssl_LIBRARY_STATIC} ${crypto_LIBRARY_STATIC} ${CMAKE_DL_LIBS} )
else ( )
    target_link_libraries( ${STATIC_LIBRARY_NAME} ${CMAKE_DL_LIBS} )
endif ( )

if ( BUILD_TESTS )
    find_package( catch REQUIRED )

    enable_testing( )
    add_subdirectory( "${PROJECT_SOURCE_DIR}/test/unit" )
    add_subdirectory( "${PROJECT_SOURCE_DIR}/test/feature" )
    add_subdirectory( "${PROJECT_SOURCE_DIR}/test/regression" )
    add_subdirectory( "${PROJECT_SOURCE_DIR}/test/integration" )
endif ( )
