cmake_minimum_required(VERSION 3.6 FATAL_ERROR)

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()

project(cerf
    VERSION 2.0
    DESCRIPTION "a self-contained numeric library that provides an efficient and accurate implementation of complex error functions, along with Dawson, Faddeeva, and Voigt functions."
    LANGUAGES C)
set(CMAKE_C_STANDARD 11)

if(WIN32)
    if(MSVC)
        message(FATAL_ERROR "Visual Studio is not C99 compliant.
In particular, it provides no correct implementation of <complex.h>.
Therefore, we do not support Visual Studio.
To compile under and for Windows, use Mingw-w64.
(VS support in libcerf <= 1.13 was using C++.
 To simplify things, libcerf is now pure C).")
    endif()
    set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
    set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
endif()

option(PEDANTIC "Compile with pedantic warnings" ON)
option(WERROR "Treat warnings as errors" OFF)
add_compile_options(-O2 -Wno-sign-compare -fno-omit-frame-pointer)
if(PEDANTIC)
    add_compile_options(-pedantic -Wall)
endif()
if(WERROR)
    add_compile_options(-Werror)
endif()
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} ${DEB_FLAGS} -O0 -g")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -O3 -w")

include(CTest)

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

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