cmake_minimum_required(VERSION 3.18)

set( FLOW_VERSION_MAJOR 8 )
set( FLOW_VERSION_MINOR 0 )
set( FLOW_VERSION_BUILD 3 )
set( FLOW_VERSION_REVISION 1 )

set( FLOW_VERSION ${FLOW_VERSION_MAJOR}.${FLOW_VERSION_MINOR}.${FLOW_VERSION_BUILD}.${FLOW_VERSION_REVISION} )

project( FlowEngineFree
            VERSION ${FLOW_VERSION}
            DESCRIPTION "Flow Engine SDK Free" )

option( BUILD_EXAMPLES "Build the Examples" ON )

if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
    message("Please switch to x64 build.")
    return()
endif()

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

IF ( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" )
	find_package( ImageMagick 7.0.0 REQUIRED )
ENDIF()

SET( REDIST_DIR ${PROJECT_SOURCE_DIR}/redist )

add_definitions( -DFLE_FREE )

if ( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" )

    set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-result -m64 -msse4.2 -fPIC -fopenmp" )
    set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-result -std=c++17 -m64 -msse4.2 -fPIC -fopenmp" )
    list( APPEND CUDA_NVCC_FLAGS "-Xcudafe --diag_suppress=174 -Wno-deprecated-gpu-targets -Wno-deprecated-declarations -std=c++17 --compiler-options -fPIC" )

    add_definitions(
        -D__LINUX__
        -DUSE_STD_MUTEX
    )

    set( FLOWENGINE_LIB_NAME "libFlowEngineFree.so" )
    set( FLOWENGINE_ADDITIONAL_LIBS "stdc++fs" )

elseif( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" )

    if ( MSVC_VERSION GREATER_EQUAL 1920 ) # VS2019 and newer
        set( CMAKE_CXX_STANDARD 17 )
        set( CMAKE_CXX_STANDARD_REQUIRED ON )
    endif()

    foreach( OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES} )
        string( TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG )
        set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${REDIST_DIR} )
        set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${REDIST_DIR} )
        set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${REDIST_DIR} )
    endforeach( OUTPUTCONFIG CMAKE_CONFIGURATION_TYPES )

    set( FLOWENGINE_LIB_NAME "FlowEngineFree.lib" )
    set( FLOWENGINE_BIN_NAME "FlowEngineFree.dll" )

endif()

ADD_DEFINITIONS(
    -DMAGICKCORE_QUANTUM_DEPTH=16
    -DMAGICKCORE_HDRI_ENABLE=1
)

set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${REDIST_DIR} )
set( CMAKE_EXECUTABLE_OUTPUT_PATH  ${REDIST_DIR} )

# -- FlowEngine library

add_library( FlowEngineFree STATIC IMPORTED )

target_include_directories( FlowEngineFree INTERFACE ${CMAKE_SOURCE_DIR}/include )

set_target_properties( FlowEngineFree PROPERTIES
     IMPORTED_LOCATION "${CMAKE_SOURCE_DIR}/lib/${FLOWENGINE_LIB_NAME}" )


if ( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" )
	set( CMAKE_INSTALL_PREFIX "C:/Program Files/FlowEngine" CACHE PATH "Installation prefix" FORCE )
endif()

install(
    FILES ${CMAKE_SOURCE_DIR}/lib/${FLOWENGINE_LIB_NAME}
    DESTINATION lib )

if ( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" )
    install(
        FILES ${CMAKE_SOURCE_DIR}/redist/${FLOWENGINE_BIN_NAME}
        DESTINATION bin )
endif()

file( GLOB_RECURSE FLOWENGINE_HEADERS ${CMAKE_SOURCE_DIR}/include/FlowEngine/*.h )

install(
    FILES ${FLOWENGINE_HEADERS}
    DESTINATION include/FlowEngine )

# -- Examples

if( BUILD_EXAMPLES )

    function( add_example ExampleName ExampleSources )
        add_executable( ${ExampleName} ${ExampleSources} )
        target_link_libraries( ${ExampleName} PRIVATE FlowEngineFree ${FLOWENGINE_ADDITIONAL_LIBS} )
        if( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" )
            add_custom_command(
                TARGET ${ExampleName} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                        ${CMAKE_CURRENT_SOURCE_DIR}/bin/${FLOWENGINE_BIN_NAME}
                        ${CMAKE_CURRENT_SOURCE_DIR}/redist/${FLOWENGINE_BIN_NAME})
        endif()
    endfunction( add_example )

    add_example( ExampleBasic          "examples/ExampleBasic.cpp" )
    add_example( ExampleStreaming      "examples/ExampleStreaming.cpp" )
    add_example( ExampleMarkers        "examples/ExampleMarkers.cpp" )

    # Advanced
    add_executable( ExampleAdvanced "examples/ExampleAdvanced.cpp" "examples/FbxHelper.cpp" )
    target_link_libraries( ExampleAdvanced PRIVATE FlowEngineFree ${FLOWENGINE_ADDITIONAL_LIBS} )
    if( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" )
        add_custom_command(
            TARGET ExampleAdvanced POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy
                    ${CMAKE_CURRENT_SOURCE_DIR}/bin/${FLOWENGINE_BIN_NAME}
                    ${CMAKE_CURRENT_SOURCE_DIR}/redist/${FLOWENGINE_BIN_NAME})
    endif()

endif()
