CMakeLists.txt 5.0 KB
Newer Older
1 2 3 4 5
# ----------------------------------------------------------------------------
#  CMake file for Android samples. See root CMakeLists.txt
#
# ----------------------------------------------------------------------------

6
if (BUILD_ANDROID_EXAMPLES)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
    project(android_samples)

    include_directories(
        "${CMAKE_SOURCE_DIR}/modules/core/include"
        "${CMAKE_SOURCE_DIR}/modules/flann/include"
        "${CMAKE_SOURCE_DIR}/modules/imgproc/include"
        "${CMAKE_SOURCE_DIR}/modules/video/include"
        "${CMAKE_SOURCE_DIR}/modules/highgui/include"
        "${CMAKE_SOURCE_DIR}/modules/ml/include"
        "${CMAKE_SOURCE_DIR}/modules/calib3d/include"
        "${CMAKE_SOURCE_DIR}/modules/features2d/include"
        "${CMAKE_SOURCE_DIR}/modules/objdetect/include"
        "${CMAKE_SOURCE_DIR}/modules/legacy/include"
        "${CMAKE_SOURCE_DIR}/modules/contrib/include"
    )

    SET (sample_dependencies opencv_contrib opencv_legacy opencv_objdetect opencv_calib3d opencv_features2d opencv_video opencv_highgui opencv_ml opencv_imgproc opencv_flann opencv_core)
    if(NOT BUILD_SHARED_LIBS)
        LIST(APPEND sample_dependencies opencv_androidcamera)
    endif()

    SET(additional_clean_files "")

    macro(ADD_ANDROID_SAMPLE sample_name)
        #message(STATUS "Build android sample: '${sample_name}'")
32 33
        #SET(sample_dir "${CMAKE_CURRENT_SOURCE_DIR}/${sample}")
        SET(sample_dir "${CMAKE_CURRENT_BINARY_DIR}/${sample}")
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
        
        add_custom_target(${sample} ALL)
        add_dependencies(${sample} opencv_java opencv_java_api)

        set_target_properties(${sample} PROPERTIES
            PROJECT_LABEL "(ANDROID EXAMPLE) ${sample}")

        file(RELATIVE_PATH OPENCV_REFERENCE_PATH "${sample_dir}" "${CMAKE_BINARY_DIR}")

        if(NOT ("${sample}" STREQUAL "0-base" OR "${sample}" STREQUAL "2-native"))
            SET(opencv_reference --library \"${OPENCV_REFERENCE_PATH}\")
        else()
            SET(opencv_reference)
        endif()

        add_custom_command(
            TARGET ${sample}
            WORKING_DIRECTORY ${sample_dir}
            COMMAND ${CMAKE_COMMAND} -E remove -f "${sample_dir}/default.properties"
            COMMAND ${CMAKE_COMMAND} -E touch "${sample_dir}/default.properties"
54
            COMMAND ${ANDROID_EXECUTABLE} update project --name "${sample}" --target "${ANDROID_SDK_TARGET}" ${opencv_reference} --path .
55
            COMMAND ${ANT_EXECUTABLE} debug
56
            COMMAND ${CMAKE_COMMAND} -E copy "${sample_dir}/bin/${sample}-debug.apk" "${CMAKE_BINARY_DIR}/bin/${sample}.apk"
57 58
        )

59
        LIST(APPEND additional_clean_files "${CMAKE_BINARY_DIR}/bin/${sample}.apk" "${sample_dir}/build.xml" "${sample_dir}/local.properties" "${sample_dir}/proguard.cfg")
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
        
        if(IS_DIRECTORY "${sample_dir}/jni")
            INCLUDE_DIRECTORIES("${sample_dir}/jni")
            FILE( GLOB srcs "${sample_dir}/jni/*.cpp" )

            FILE(STRINGS "${sample_dir}/jni/Android.mk" JNI_LIB_NAME REGEX "LOCAL_MODULE[ ]*:=[ ]*.*" )
            string(REGEX REPLACE "LOCAL_MODULE[ ]*:=[ ]*([a-zA-Z_][a-zA-Z_0-9]*)[ ]*" "\\1" JNI_LIB_NAME "${JNI_LIB_NAME}")

            ADD_LIBRARY( ${JNI_LIB_NAME} MODULE ${srcs} )

            ADD_DEPENDENCIES(${JNI_LIB_NAME} ${sample_dependencies})
            TARGET_LINK_LIBRARIES(${JNI_LIB_NAME} ${OPENCV_LINKER_LIBS} ${sample_dependencies})

            set_target_properties(${JNI_LIB_NAME} PROPERTIES
                OUTPUT_NAME "${JNI_LIB_NAME}"
                LIBRARY_OUTPUT_DIRECTORY "${sample_dir}/libs/${ARMEABI_NDK_NAME}"
            )

            ADD_CUSTOM_COMMAND(
                TARGET ${JNI_LIB_NAME}
                POST_BUILD
                COMMAND ${CMAKE_STRIP} "${sample_dir}/libs/${ARMEABI_NDK_NAME}/*.so"
                )

            add_dependencies(${sample} ${JNI_LIB_NAME})
        endif()
86 87 88 89

        if(INSTALL_ANDROID_EXAMPLES)
            install(FILES "${CMAKE_BINARY_DIR}/bin/${sample}.apk" DESTINATION bin COMPONENT main)
        endif()
90 91 92
    endmacro()

    file(GLOB android_samples RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *)
93
    list(REMOVE_ITEM android_samples hello-android)
94
    list(SORT android_samples)
95
    
96 97 98 99 100
    file(COPY ${android_samples} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}"
        PATTERN ".svn" EXCLUDE
        PATTERN "gen" EXCLUDE
        PATTERN "bin" EXCLUDE
        )
101 102 103 104 105 106 107

    foreach(sample ${android_samples})
        if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${sample}/AndroidManifest.xml)
            ADD_ANDROID_SAMPLE(${sample})
        endif()
    endforeach()

108
    #hello-android sample
109
    ADD_EXECUTABLE(hello-android hello-android/main.cpp)
110 111 112
    ADD_DEPENDENCIES(hello-android ${sample_dependencies})
    TARGET_LINK_LIBRARIES(hello-android ${OPENCV_LINKER_LIBS} ${sample_dependencies})
    set_target_properties(hello-android PROPERTIES OUTPUT_NAME hello-android RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}")
113 114 115
    if(INSTALL_ANDROID_EXAMPLES)
        install(TARGETS hello-android DESTINATION bin COMPONENT main)
    endif()
116

117 118
    set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${additional_clean_files}")
endif()