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

    # Test most examples with legacy C++ (some are overridden below)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++98")

else()
    # Visual studio settings
    set(CMAKE_DEBUG_POSTFIX "-debug")
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})

add_executable(imu_data_channel_example
    imu_data_channel_example.cpp
)

target_link_libraries(imu_data_channel_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)
    target_link_libraries(imu_data_channel_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)
    target_link_libraries(imu_data_channel_example ws2_32)
endif()

# PCL example
if(PCL_FOUND)
    add_executable(pcl_example
        pcl_example.cpp
    )
    # Latest PCL needs C++14, so we will not build this specific one with C++98
    string(REPLACE "-std=c++98" "-std=c++14" COMPILE_FLAGS_CXX14 "${CMAKE_CXX_FLAGS}")
    set_target_properties(pcl_example PROPERTIES COMPILE_FLAGS "${COMPILE_FLAGS_CXX14}")
    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()

