set(CMAKE_VERSION 3.6.2)
cmake_minimum_required(VERSION ${CMAKE_VERSION})
set(CMAKE_CXX_STANDARD 11)

project(bytesflow-opencv-media)

SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/output/bin)
SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/output/lib)

set(-DCMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Wall -s -Wdeprecated-declarations")
set(-DCMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wall -s -Wdeprecated-declarations")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
add_definitions("-Wall -g")

add_subdirectory(src/opencv)
add_subdirectory(jni)
add_subdirectory(src/utils)
add_subdirectory(src/ffmpeg)


############################################# 加入 json 支持############################################
set(JSON_HEADER
        src/json/include)
FILE(GLOB JSON_ROOT_CPP
        src/json/libs/lib_json/*.cpp)
include_directories(${JSON_HEADER})


############################################# 加入jni支持############################################
message(FIND JAVA_HOME---> $ENV{JAVA_HOME})
set(JAVA_HOME $ENV{JAVA_HOME})
message(JAVA_HOME ${JAVA_HOME})
if (NOT DEFINED ENV{JAVA_HOME})
    message("not defined environment variable:JAVA_HOME")
    set(JAVA_HOME /Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home)
    set(JNI_PATH ${JAVA_HOME}/include)
endif ()

############################################# 加入 ffmpeg src 支持############################################

IF (CMAKE_SYSTEM_NAME MATCHES "Linux")
    set(JNI_MD_PATH ${JAVA_HOME}/include/linux)
    set(JNI_PATH ${JAVA_HOME}/include)
    include_directories(${JNI_PATH})

    set(OPENCV_HOME /usr/local)
    set(OPENCV_INCLUDE ${OPENCV_HOME}/include/opencv4/)
    set(OPENCV_LIBS ${OPENCV_HOME}/lib64)

    set(FFMPEG_PREFIX /usr)
    set(FFMPEG_INCLUDE_DIRS ${FFMPEG_PREFIX}/include/ffmpeg/)
    set(FFMPEG_LIBRARIES ${FFMPEG_PREFIX}/lib64/)
    MESSAGE(FFMPEG_PREFIX ${FFMPEG_PREFIX})
    MESSAGE(FFMPEG_INCLUDE_DIRS ${FFMPEG_INCLUDE_DIRS})
    MESSAGE(FFMPEG_LIBS ${FFMPEG_LIBRARIES})


ELSEIF (CMAKE_SYSTEM_NAME MATCHES "Darwin")
    set(JNI_PATH ${JAVA_HOME}/include)
    set(JNI_MD_PATH ${JAVA_HOME}/include/darwin)
    include_directories(${JNI_PATH})
    set(OPENCV_HOME /usr/local/Cellar/opencv/4.5.4_4)
    set(OPENCV_INCLUDE ${OPENCV_HOME}/include/opencv4)
    set(OPENCV_LIBS ${OPENCV_HOME}/lib)


    set(FFMPEG_PREFIX /usr/local/Cellar/ffmpeg@4/4.4.1)
    set(FFMPEG_INCLUDE_DIRS ${FFMPEG_PREFIX}/include/)
    set(FFMPEG_LIBS ${FFMPEG_PREFIX}/lib/)
    MESSAGE(FFMPEG_PREFIX ${FFMPEG_PREFIX})
    MESSAGE(FFMPEG_INCLUDE_DIRS ${FFMPEG_INCLUDE_DIRS})
    MESSAGE(FFMPEG_LIBRARIES ${FFMPEG_LIBRARIES})


ELSE ()
    MESSAGE(STATUS "other platform: ${CMAKE_SYSTEM_NAME}")
ENDIF (CMAKE_SYSTEM_NAME MATCHES "Linux")

message(OPENCV_INCLUDE>${OPENCV_INCLUDE})
message(OPENCV_LIBS>${OPENCV_LIBS})
message(JAVA_HOME>${JAVA_HOME})
message(FFMPEG_INCLUDE_DIRS>${FFMPEG_INCLUDE_DIRS})

include_directories(${OPENCV_INCLUDE})
include_directories(${JNI_MD_PATH})
include_directories(${FFMPEG_INCLUDE_DIRS})

include_directories(src/third_party/include)
link_directories(src/third_party/linux_x64)

link_directories(${OPENCV_LIBS})
link_directories(${FFMPEG_LIBS})

add_library(piaoquan_java_opencv SHARED
        ${OpenCV_SRC}
        ${UTILS_SRC}
        ${JSON_ROOT_CPP}
        ${FFMPEG_SRC}
        ${JAVA_JNI_SRC})

#add_library(video_similarity_detection SHARED
#        ${UTILS_SRC}
#        ${JSON_ROOT_CPP}
#        ${Project_SRC})

set(FFmpeg_LIBS
        -lavcodec
        -lavdevice
        -lavfilter
        -lavformat
        -lavutil
        -lswscale
        -lswresample)
set(OpenCV_LIBS
        -lopencv_imgproc
        -lopencv_imgcodecs
        -lopencv_core
        -lopencv_highgui
        #        -lopencv_xphoto
        #        -lopencv_xobjdetect
        #        -lopencv_ximgproc
        #        -lopencv_xfeatures2d
        #        -lopencv_wechat_qrcode
        #        -lopencv_viz
        #        -lopencv_videostab
        #        -lopencv_videoio
        #        -lopencv_video
        #        -lopencv_tracking
        #        -lopencv_text
        #        -lopencv_surface_matching
        #        -lopencv_superres
        #        -lopencv_structured_light
        #        -lopencv_stitching
        #        -lopencv_stereo
        #        -lopencv_shape
        #        -lopencv_sfm
        #        -lopencv_saliency
        #        -lopencv_rgbd
        #        -lopencv_reg
        #        -lopencv_rapid
        #        -lopencv_quality
        #        -lopencv_plot
        #        -lopencv_photo
        #        -lopencv_phase_unwrapping
        #        -lopencv_optflow
        #        -lopencv_objdetect
        #        -lopencv_ml
        #        -lopencv_mcc
        #        -lopencv_line_descriptor
        #        -lopencv_intensity_transform
        #        -lopencv_img_hash
        #        -lopencv_hfs
        #        -lopencv_gapi
        #        -lopencv_fuzzy
        #        -lopencv_freetype
        #        -lopencv_flann
        #        -lopencv_features2d
        #        -lopencv_face
        #        -lopencv_dpm
        #        -lopencv_dnn_superres
        #        -lopencv_dnn_objdetect
        #        -lopencv_dnn
        #        -lopencv_datasets
        #        -lopencv_ccalib
        #        -lopencv_calib3d
        #        -lopencv_bioinspired
        #        -lopencv_bgsegm
        #        -lopencv_barcode
        #        -lopencv_aruco
        #        -lopencv_alphamat
        )

# 条件判断
if (CMAKE_BUILD_TYPE AND (CMAKE_BUILD_TYPE STREQUAL "Debug"))
    set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wall -O0")
    message("Debug mode:${CMAKE_C_FLAGS_DEBUG}")
    add_executable(ImageBlurDetection_debug main.cpp
            ${OpenCV_SRC}
            ${UTILS_SRC}
            ${JSON_ROOT_CPP}
            ${FFMPEG_SRC}
            ${JAVA_JNI_SRC}
            )
    target_link_libraries(ImageBlurDetection_debug -lpthread
            ${OpenCV_LIBS}
            ${FFmpeg_LIBS}
            )
    add_executable(test_md5 md5_main.cpp
            src/utils/md5.h
            src/utils/md5.c
            )
    add_executable(video_similarity_comparison src/video_similarity_comparison.cpp
            ${OpenCV_SRC}
            ${JSON_ROOT_CPP}
            ${FFMPEG_SRC}
            ${UTILS_SRC}
            )
    target_link_libraries(video_similarity_comparison -lpthread
            ${OpenCV_LIBS}
            ${FFmpeg_LIBS}
            )
elseif (CMAKE_BUILD_TYPE AND (CMAKE_BUILD_TYPE STREQUAL "Release"))
    set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Wall -O3")
    message("Release mode:${CMAKE_C_FLAGS_RELEASE}")
else ()
    message("else:${CMAKE_BUILD_TYPE}")
    message("else:${CMAKE_C_FLAGS_RELEASE}")
endif ()

if (UNIX)
    include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/third_party/breakpad/ret/include/breakpad)
    link_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/third_party/breakpad/ret/libs)
    target_link_libraries(piaoquan_java_opencv -lpthread
            ${FFmpeg_LIBS}
            ${OpenCV_LIBS}
            libbreakpad.a
            libbreakpad_client.a
            )
else ()
    target_link_libraries(piaoquan_java_opencv -lpthread
            ${FFmpeg_LIBS}
            ${OpenCV_LIBS}
            )
endif ()





