cmake_minimum_required(VERSION 3.6 FATAL_ERROR)
if(POLICY CMP0048)
   cmake_policy(SET CMP0048 NEW)
endif()

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
include(PreventInSourceBuilds)

if(NOT DEFINED BUILD_SHARED_LIBS)
    option(BUILD_SHARED_LIBS "Build as shared library" ON)
endif()
if(NOT DEFINED LIB_MAN)
    option(LIB_MAN "Build libcerf man pages" ON)
endif()
if(NOT DEFINED LIB_RUN)
    option(LIB_RUN "Build executables for command-line computation" ON)
endif()
if(NOT DEFINED LIB_INSTALL)
    option(LIB_INSTALL "Install libcerf" ON)
endif()
if(NOT DEFINED CERF_CPP)
    option(CERF_CPP "Build libcerf as native C++" OFF)
endif()
option(PEDANTIC "Compile with pedantic warnings" ON)

if(CERF_CPP)
    project(cerfcpp CXX)
    message("Build C++ library libcerfcpp")
    # add_compile_definitions(CERF_AS_CPP) # requires CMake 3.13, therefore more explicitly:
    if(MSVC)
        add_compile_options(/DCERF_AS_CPP)
    else()
        add_compile_options(-DCERF_AS_CPP)
    endif()
    set(CMAKE_CXX_STANDARD 14)
else()
    message("Build C library libcerf")
    project(cerf C)
    set(CMAKE_C_STANDARD 99)
endif()

set(CERF_SOVERSION                 1) # API version
set(CERF_VERSION ${CERF_SOVERSION}.13) # minor version

if(MSVC)
    if (NOT CERF_CPP)
        message(FATAL_ERROR "Under MSVC, only CERF_CPP=ON is supported")
    endif()
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS}")
    set(MS_NOWARN "/wd4018 /wd4068 /wd4101 /wd4244 /wd4267 /wd4305 /wd4715 /wd4996")
    set(MS_D "-D_CRT_SECURE_NO_WARNINGS -D_SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING")
    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} ${MS_NOWARN} ${MS_D}")
    set(CTEST_CONFIGURATION_TYPE "${JOB_BUILD_CONFIGURATION}")
    # set(DEFAULT_BUILD_SHARED_LIBS OFF)
    # set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin/$<CONFIG>)
    if(BUILD_SHARED_LIBS)
        set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
    endif()
else()
    add_compile_options(-O2 -Wno-sign-compare -fno-omit-frame-pointer)
    if(PEDANTIC)
        add_compile_options(-pedantic -Wall)
    endif()
    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} ${DEB_FLAGS} -O0 -g")
    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -O3 -w")
endif()

include(CTest)

if(LIB_INSTALL)
    set(destination ${CMAKE_INSTALL_PREFIX})
    configure_file("libcerf.pc.in" "libcerf.pc" @ONLY)
    install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libcerf.pc"
        DESTINATION "${destination}/lib/pkgconfig/")
endif()

add_subdirectory(lib)
add_subdirectory(test)
if(LIB_RUN)
    add_subdirectory(run)
endif()
if(LIB_MAN)
    add_subdirectory(man)
endif()
