include_directories(${PROJECT_SOURCE_DIR})

if(NOT WIN32 OR MINGW)
    include(CheckCXXCompilerFlag)

    # Some useful flags
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -Wall")
    CHECK_CXX_COMPILER_FLAG("-march=native" NATIVE_ARCH_SUPPORT)
    if(NATIVE_ARCH_SUPPORT AND NOT DISABLE_NATIVE)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native")
    endif()
    
    # No c++11 requirement for the examples, as opposed to the library
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++98")

else()
    # Visual studio settings
    foreach(flag
            CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_RELWITHDEBINFO
            CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_DEBUG_INIT
            CMAKE_CXX_FLAGS_RELEASE  CMAKE_CXX_FLAGS_RELWITHDEBINFO
            CMAKE_CXX_FLAGS_DEBUG  CMAKE_CXX_FLAGS_DEBUG_INIT)
        string(REPLACE "/MD"  "/MT" "${flag}" "${${flag}}")
        set("${flag}" "${${flag}} /EHsc /arch:SSE4.1 /D__SSE__ /D__SSE2__ /D__SSE3__ /D__SSE4_1__")
    endforeach()
endif()


# All examples except PCL and OpenCV
add_executable(imagetransfer_example
    imagetransfer_example.cpp
)

target_link_libraries(imagetransfer_example visiontransfer${LIB_SUFFIX})

add_executable(asynctransfer_example
    asynctransfer_example.cpp
)

target_link_libraries(asynctransfer_example visiontransfer${LIB_SUFFIX})

add_executable(server_example
    server_example.cpp
)

target_link_libraries(server_example visiontransfer${LIB_SUFFIX})

add_executable(parameter_example
    parameter_example.cpp
)

target_link_libraries(parameter_example visiontransfer${LIB_SUFFIX})

add_executable(parameter_enumeration_example
    parameter_enumeration_example.cpp
)

target_link_libraries(parameter_enumeration_example visiontransfer${LIB_SUFFIX})


if(UNIX OR MINGW)
    target_link_libraries(asynctransfer_example pthread)
    target_link_libraries(imagetransfer_example pthread)
    target_link_libraries(server_example pthread)
    target_link_libraries(parameter_example pthread)
    target_link_libraries(parameter_enumeration_example pthread)
endif()

if(WIN32)
    target_link_libraries(imagetransfer_example ws2_32)
    target_link_libraries(asynctransfer_example ws2_32)
    target_link_libraries(server_example ws2_32)
    target_link_libraries(parameter_example ws2_32)
    target_link_libraries(parameter_enumeration_example ws2_32)
endif()

# PCL example
if(PCL_FOUND)
    add_executable(pcl_example
        pcl_example.cpp
    )

    target_link_libraries(pcl_example visiontransfer${LIB_SUFFIX} ${PCL_COMMON_LIBRARIES} ${PCL_IO_LIBRARIES} ${PCL_FILTERS_LIBRARIES})

    if(UNIX OR MINGW)
        target_link_libraries(pcl_example pthread)
    endif()

    if(WIN32)
        target_link_libraries(pcl_example ws2_32)
    endif()
endif()

# OpenCV example
if(OpenCV_FOUND)
    add_executable(opencv_example
        opencv_example.cpp
    )
    # OpenCV >=4.0 needs C++11, so we will not build this specific one with C++98
    string(REPLACE "-std=c++98" "-std=c++11" COMPILE_FLAGS_CXX11 "${CMAKE_CXX_FLAGS}")
    set_target_properties(opencv_example PROPERTIES COMPILE_FLAGS "${COMPILE_FLAGS_CXX11}")
    target_link_libraries(opencv_example visiontransfer${LIB_SUFFIX} ${OpenCV_LIBS})

    if(UNIX OR MINGW)
        target_link_libraries(opencv_example pthread)
    endif()

    if(WIN32)
        target_link_libraries(opencv_example ws2_32)
    endif()
endif()

