cmake_minimum_required(VERSION 3.16)
project("atomorph")

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

option(WINDOWS "Whether the build target is Windows or not." OFF)
option(SHARED "Whether we should build a shared library, instead of a static one." OFF)
option(DEMO "Whether or not the demo should be built." OFF)
option(OPENCV "Whether or not opencv should be targeted." OFF)
option(STATICC "Whether or not dependencies should be statically linked." ON)

if (NOT WINDOWS)
    set(CMAKE_POSITION_INDEPENDENT_CODE ON)
else()
    set(CMAKE_POSITION_INDEPENDENT_CODE OFF)
endif()

set(LIBTYPE STATIC)
if (SHARED)
    set(LIBTYPE SHARED)
endif (SHARED)

if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
    add_link_options(-fuse-ld=lld)
endif ()

file(GLOB_RECURSE SRC_FILES "src/*.cpp")
add_library(atomorph ${LIBTYPE} ${SRC_FILES})

if (WINDOWS)
    MESSAGE(WARNING, "Using Windows Build.")
    # Add a definition for the compiler, so we can use it in C++ as well.
    ADD_DEFINITIONS(-D WINDOWS=1)
    # -m64: Build a 64 bit library
    add_compile_options(-m64)
    if (SHARED)
        set_target_properties(atomorph PROPERTIES SUFFIX ".dll")
    endif()
endif (WINDOWS)

if (STATICC)
    message("Linking dependencies statically.")
    set (CMAKE_SHARED_LINKER_FLAGS "-Wl,--as-needed")
    set(LINKS -lm -lpthread -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -Wl,-Bdynamic ${LINKS})
endif(STATICC)

if (OPENCV)
    include(CMakeLists.txt.in)
    include_opencv()
    find_package( OpenCV REQUIRED )
    include_directories( ${OpenCV_INCLUDE_DIRS} )

    add_definitions(-D ATOMORPH_OPENCV)
    if (STATICC)
        set(LINKS ${LINKS} -Wl,-Bstatic opencv_core opencv_flann -Wl,-Bdynamic)
    else()
        set(LINKS ${LINKS} ${OpenCV_LIBS})
    endif()
endif()

target_link_libraries(atomorph ${LINKS})

if (DEMO)
    file(GLOB_RECURSE DEMO_SRC_FILES "demo/*.cpp")
    add_executable(atomorph_demo ${DEMO_SRC_FILES})
    target_link_libraries(atomorph_demo atomorph -lpthread)
endif()