Home | History | Annotate | Download | only in cmake
      1 if(EXISTS "${ANDROID_EXECUTABLE}")
      2   set(ANDROID_SDK_DETECT_QUIET TRUE)
      3 endif()
      4 
      5 file(TO_CMAKE_PATH "$ENV{ProgramFiles}" ProgramFiles_ENV_PATH)
      6 file(TO_CMAKE_PATH "$ENV{HOME}" HOME_ENV_PATH)
      7 
      8 if(CMAKE_HOST_WIN32)
      9   set(ANDROID_SDK_OS windows)
     10 elseif(CMAKE_HOST_APPLE)
     11   set(ANDROID_SDK_OS macosx)
     12 else()
     13   set(ANDROID_SDK_OS linux)
     14 endif()
     15 
     16 #find android SDK: search in ANDROID_SDK first
     17 find_host_program(ANDROID_EXECUTABLE
     18   NAMES android.bat android
     19   PATH_SUFFIXES tools
     20   PATHS
     21     ENV ANDROID_SDK
     22   DOC "Android SDK location"
     23   NO_DEFAULT_PATH
     24   )
     25 
     26 # Now search default paths
     27 find_host_program(ANDROID_EXECUTABLE
     28   NAMES android.bat android
     29   PATH_SUFFIXES android-sdk-${ANDROID_SDK_OS}/tools
     30                 android-sdk-${ANDROID_SDK_OS}_x86/tools
     31                 android-sdk-${ANDROID_SDK_OS}_86/tools
     32                 android-sdk/tools
     33   PATHS /opt
     34         "${HOME_ENV_PATH}/NVPACK"
     35         "$ENV{SystemDrive}/NVPACK"
     36         "${ProgramFiles_ENV_PATH}/Android"
     37   DOC "Android SDK location"
     38   )
     39 
     40 if(ANDROID_EXECUTABLE)
     41   if(NOT ANDROID_SDK_DETECT_QUIET)
     42     message(STATUS "Found android tool: ${ANDROID_EXECUTABLE}")
     43   endif()
     44 
     45   get_filename_component(ANDROID_SDK_TOOLS_PATH "${ANDROID_EXECUTABLE}" PATH)
     46 
     47   #read source.properties
     48   if(EXISTS "${ANDROID_SDK_TOOLS_PATH}/source.properties")
     49     file(STRINGS "${ANDROID_SDK_TOOLS_PATH}/source.properties" ANDROID_SDK_TOOLS_SOURCE_PROPERTIES_LINES REGEX "^[ ]*[^#].*$")
     50     foreach(line ${ANDROID_SDK_TOOLS_SOURCE_PROPERTIES_LINES})
     51       string(REPLACE "\\:" ":" line ${line})
     52       string(REPLACE "=" ";" line ${line})
     53       list(GET line 0 line_name)
     54       list(GET line 1 line_value)
     55       string(REPLACE "." "_" line_name ${line_name})
     56       SET(ANDROID_TOOLS_${line_name} "${line_value}" CACHE INTERNAL "from ${ANDROID_SDK_TOOLS_PATH}/source.properties")
     57       MARK_AS_ADVANCED(ANDROID_TOOLS_${line_name})
     58     endforeach()
     59   endif()
     60 
     61   #fix missing revision (SDK tools before r9 don't set revision number correctly)
     62   if(NOT ANDROID_TOOLS_Pkg_Revision)
     63     SET(ANDROID_TOOLS_Pkg_Revision "Unknown" CACHE INTERNAL "")
     64     MARK_AS_ADVANCED(ANDROID_TOOLS_Pkg_Revision)
     65   endif()
     66 
     67   #fix missing description
     68   if(NOT ANDROID_TOOLS_Pkg_Desc)
     69     SET(ANDROID_TOOLS_Pkg_Desc "Android SDK Tools, revision ${ANDROID_TOOLS_Pkg_Revision}." CACHE INTERNAL "")
     70     MARK_AS_ADVANCED(ANDROID_TOOLS_Pkg_Desc)
     71   endif()
     72 
     73   #warn about outdated SDK
     74   if(NOT ANDROID_TOOLS_Pkg_Revision GREATER 13)
     75     SET(ANDROID_TOOLS_Pkg_Desc "${ANDROID_TOOLS_Pkg_Desc} It is recommended to update your SDK tools to revision 14 or newer." CACHE INTERNAL "")
     76   endif()
     77 
     78   if(ANDROID_TOOLS_Pkg_Revision GREATER 13)
     79     SET(ANDROID_PROJECT_PROPERTIES_FILE project.properties)
     80     SET(ANDROID_ANT_PROPERTIES_FILE ant.properties)
     81   else()
     82     SET(ANDROID_PROJECT_PROPERTIES_FILE default.properties)
     83     SET(ANDROID_ANT_PROPERTIES_FILE build.properties)
     84   endif()
     85 
     86   set(ANDROID_MANIFEST_FILE AndroidManifest.xml)
     87   set(ANDROID_LIB_PROJECT_FILES build.xml local.properties proguard-project.txt ${ANDROID_PROJECT_PROPERTIES_FILE})
     88   set(ANDROID_PROJECT_FILES ${ANDROID_LIB_PROJECT_FILES})
     89 
     90   #get installed targets
     91   if(ANDROID_TOOLS_Pkg_Revision GREATER 11)
     92     execute_process(COMMAND ${ANDROID_EXECUTABLE} list target -c
     93       RESULT_VARIABLE ANDROID_PROCESS
     94       OUTPUT_VARIABLE ANDROID_SDK_TARGETS
     95       ERROR_VARIABLE ANDROID_PROCESS_ERRORS
     96       OUTPUT_STRIP_TRAILING_WHITESPACE
     97       )
     98     string(REGEX MATCHALL "[^\n]+" ANDROID_SDK_TARGETS "${ANDROID_SDK_TARGETS}")
     99   else()
    100     #old SDKs (r11 and older) don't provide compact list
    101     execute_process(COMMAND ${ANDROID_EXECUTABLE} list target
    102       RESULT_VARIABLE ANDROID_PROCESS
    103       OUTPUT_VARIABLE ANDROID_SDK_TARGETS_FULL
    104       ERROR_VARIABLE ANDROID_PROCESS_ERRORS
    105       OUTPUT_STRIP_TRAILING_WHITESPACE
    106       )
    107     string(REGEX MATCHALL "(^|\n)id: [0-9]+ or \"([^\n]+[0-9+])\"(\n|$)" ANDROID_SDK_TARGETS_FULL "${ANDROID_SDK_TARGETS_FULL}")
    108 
    109     SET(ANDROID_SDK_TARGETS "")
    110     if(ANDROID_PROCESS EQUAL 0)
    111       foreach(line ${ANDROID_SDK_TARGETS_FULL})
    112         string(REGEX REPLACE "(^|\n)id: [0-9]+ or \"([^\n]+[0-9+])\"(\n|$)" "\\2" line "${line}")
    113         list(APPEND ANDROID_SDK_TARGETS "${line}")
    114       endforeach()
    115     endif()
    116   endif()
    117 
    118   if(NOT ANDROID_PROCESS EQUAL 0)
    119     message(ERROR "Failed to get list of installed Android targets.")
    120     set(ANDROID_EXECUTABLE "ANDROID_EXECUTABLE-NOTFOUND")
    121   endif()
    122 
    123   # clear ANDROID_SDK_TARGET if no target is provided by user
    124   if(NOT ANDROID_SDK_TARGET)
    125     set(ANDROID_SDK_TARGET "" CACHE STRING "Android SDK target for the OpenCV Java API and samples")
    126   endif()
    127   if(ANDROID_SDK_TARGETS)
    128     set_property( CACHE ANDROID_SDK_TARGET PROPERTY STRINGS ${ANDROID_SDK_TARGETS} )
    129   endif()
    130 endif(ANDROID_EXECUTABLE)
    131 
    132 # finds minimal installed SDK target compatible with provided names or API levels
    133 # usage:
    134 #   get_compatible_android_api_level(VARIABLE [level1] [level2] ...)
    135 macro(android_get_compatible_target VAR)
    136   set(${VAR} "${VAR}-NOTFOUND")
    137   if(ANDROID_SDK_TARGETS)
    138     list(GET ANDROID_SDK_TARGETS 0 __lvl)
    139     string(REGEX MATCH "[0-9]+$" __lvl "${__lvl}")
    140 
    141     #find minimal level mathing to all provided levels
    142     foreach(lvl ${ARGN})
    143       string(REGEX MATCH "[0-9]+$" __level "${lvl}")
    144       if(__level GREATER __lvl)
    145         set(__lvl ${__level})
    146       endif()
    147     endforeach()
    148 
    149     #search for compatible levels
    150     foreach(lvl ${ANDROID_SDK_TARGETS})
    151       string(REGEX MATCH "[0-9]+$" __level "${lvl}")
    152       if(__level EQUAL __lvl)
    153         #look for exact match
    154         foreach(usrlvl ${ARGN})
    155           if("${usrlvl}" STREQUAL "${lvl}")
    156             set(${VAR} "${lvl}")
    157             break()
    158           endif()
    159         endforeach()
    160         if("${${VAR}}" STREQUAL "${lvl}")
    161           break() #exact match was found
    162         elseif(NOT ${VAR})
    163           set(${VAR} "${lvl}")
    164         endif()
    165       elseif(__level GREATER __lvl)
    166         if(NOT ${VAR})
    167           set(${VAR} "${lvl}")
    168         endif()
    169         break()
    170       endif()
    171     endforeach()
    172 
    173     unset(__lvl)
    174     unset(__level)
    175   endif()
    176 endmacro()
    177 
    178 unset(__android_project_chain CACHE)
    179 
    180 # add_android_project(target_name ${path} NATIVE_DEPS opencv_core LIBRARY_DEPS ${OpenCV_BINARY_DIR} SDK_TARGET 11)
    181 macro(add_android_project target path)
    182   # parse arguments
    183   set(android_proj_arglist NATIVE_DEPS LIBRARY_DEPS SDK_TARGET IGNORE_JAVA IGNORE_MANIFEST COPY_LIBS)
    184   set(__varname "android_proj_")
    185   foreach(v ${android_proj_arglist})
    186     set(${__varname}${v} "")
    187   endforeach()
    188   foreach(arg ${ARGN})
    189     set(__var "${__varname}")
    190     foreach(v ${android_proj_arglist})
    191       if("${v}" STREQUAL "${arg}")
    192         set(__varname "android_proj_${v}")
    193         break()
    194       endif()
    195     endforeach()
    196     if(__var STREQUAL __varname)
    197       list(APPEND ${__var} "${arg}")
    198     endif()
    199   endforeach()
    200 
    201   # get compatible SDK target
    202   android_get_compatible_target(android_proj_sdk_target ${ANDROID_NATIVE_API_LEVEL} ${android_proj_SDK_TARGET})
    203 
    204   if(NOT android_proj_sdk_target)
    205     message(WARNING "Can not find any SDK target compatible with: ${ANDROID_NATIVE_API_LEVEL} ${android_proj_SDK_TARGET}
    206                      The project ${target} will not be build")
    207   endif()
    208 
    209   # check native dependencies
    210   if(android_proj_IGNORE_JAVA)
    211     ocv_check_dependencies(${android_proj_NATIVE_DEPS})
    212   else()
    213     ocv_check_dependencies(${android_proj_NATIVE_DEPS} opencv_java)
    214   endif()
    215 
    216   if(EXISTS "${path}/jni/Android.mk" )
    217     # find if native_app_glue is used
    218     file(STRINGS "${path}/jni/Android.mk" NATIVE_APP_GLUE REGEX ".*(call import-module,android/native_app_glue)" )
    219     if(NATIVE_APP_GLUE)
    220       if(ANDROID_NATIVE_API_LEVEL LESS 9 OR NOT EXISTS "${ANDROID_NDK}/sources/android/native_app_glue")
    221         set(OCV_DEPENDENCIES_FOUND FALSE)
    222       endif()
    223     endif()
    224   endif()
    225 
    226   if(OCV_DEPENDENCIES_FOUND AND android_proj_sdk_target AND ANDROID_EXECUTABLE AND ANT_EXECUTABLE AND ANDROID_TOOLS_Pkg_Revision GREATER 13 AND EXISTS "${path}/${ANDROID_MANIFEST_FILE}")
    227 
    228     project(${target})
    229     set(android_proj_bin_dir "${CMAKE_CURRENT_BINARY_DIR}/.build")
    230 
    231     # get project sources
    232     file(GLOB_RECURSE android_proj_files RELATIVE "${path}" "${path}/res/*" "${path}/src/*")
    233 
    234     if(NOT android_proj_IGNORE_MANIFEST)
    235       list(APPEND android_proj_files ${ANDROID_MANIFEST_FILE})
    236     endif()
    237 
    238     # copy sources out from the build tree
    239     set(android_proj_file_deps "")
    240     foreach(f ${android_proj_files})
    241       add_custom_command(
    242         OUTPUT "${android_proj_bin_dir}/${f}"
    243         COMMAND ${CMAKE_COMMAND} -E copy "${path}/${f}" "${android_proj_bin_dir}/${f}"
    244         MAIN_DEPENDENCY "${path}/${f}"
    245         COMMENT "Copying ${f}")
    246       list(APPEND android_proj_file_deps "${path}/${f}" "${android_proj_bin_dir}/${f}")
    247     endforeach()
    248 
    249     set(android_proj_lib_deps_commands "")
    250     set(android_proj_target_files ${ANDROID_PROJECT_FILES})
    251     ocv_list_add_prefix(android_proj_target_files "${android_proj_bin_dir}/")
    252 
    253     # process Android library dependencies
    254     foreach(dep ${android_proj_LIBRARY_DEPS})
    255       file(RELATIVE_PATH __dep "${android_proj_bin_dir}" "${dep}")
    256       list(APPEND android_proj_lib_deps_commands
    257         COMMAND ${ANDROID_EXECUTABLE} --silent update project --path "${android_proj_bin_dir}" --library "${__dep}")
    258     endforeach()
    259 
    260     # fix Android project
    261     add_custom_command(
    262         OUTPUT ${android_proj_target_files}
    263         COMMAND ${CMAKE_COMMAND} -E remove ${android_proj_target_files}
    264         COMMAND ${ANDROID_EXECUTABLE} --silent update project --path "${android_proj_bin_dir}" --target "${android_proj_sdk_target}" --name "${target}"
    265         ${android_proj_lib_deps_commands}
    266         MAIN_DEPENDENCY "${android_proj_bin_dir}/${ANDROID_MANIFEST_FILE}"
    267         DEPENDS "${path}/${ANDROID_MANIFEST_FILE}"
    268         COMMENT "Updating Android project at ${path}. SDK target: ${android_proj_sdk_target}"
    269         )
    270 
    271     list(APPEND android_proj_file_deps ${android_proj_target_files})
    272 
    273     # build native part
    274     file(GLOB_RECURSE android_proj_jni_files "${path}/jni/*.c" "${path}/jni/*.h" "${path}/jni/*.cpp" "${path}/jni/*.hpp")
    275     ocv_list_filterout(android_proj_jni_files "\\\\.svn")
    276 
    277     if(android_proj_jni_files AND EXISTS ${path}/jni/Android.mk AND NOT DEFINED JNI_LIB_NAME)
    278       # find local module name in Android.mk file to build native lib
    279       file(STRINGS "${path}/jni/Android.mk" JNI_LIB_NAME REGEX "LOCAL_MODULE[ ]*:=[ ]*.*" )
    280       string(REGEX REPLACE "LOCAL_MODULE[ ]*:=[ ]*([a-zA-Z_][a-zA-Z_0-9]*)[ ]*" "\\1" JNI_LIB_NAME "${JNI_LIB_NAME}")
    281 
    282       if(JNI_LIB_NAME)
    283         if(NATIVE_APP_GLUE)
    284           include_directories(${ANDROID_NDK}/sources/android/native_app_glue)
    285           list(APPEND android_proj_jni_files ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c)
    286           ocv_warnings_disable(CMAKE_C_FLAGS -Wstrict-prototypes -Wunused-parameter -Wmissing-prototypes)
    287           set(android_proj_NATIVE_DEPS ${android_proj_NATIVE_DEPS} android)
    288         endif()
    289 
    290         add_library(${JNI_LIB_NAME} MODULE ${android_proj_jni_files})
    291         ocv_target_include_modules_recurse(${JNI_LIB_NAME} ${android_proj_NATIVE_DEPS})
    292         ocv_target_include_directories(${JNI_LIB_NAME} "${path}/jni")
    293         ocv_target_link_libraries(${JNI_LIB_NAME} ${OPENCV_LINKER_LIBS} ${android_proj_NATIVE_DEPS})
    294 
    295         set_target_properties(${JNI_LIB_NAME} PROPERTIES
    296             OUTPUT_NAME "${JNI_LIB_NAME}"
    297             LIBRARY_OUTPUT_DIRECTORY "${android_proj_bin_dir}/libs/${ANDROID_NDK_ABI_NAME}"
    298             )
    299 
    300         get_target_property(android_proj_jni_location "${JNI_LIB_NAME}" LOCATION)
    301         if (NOT (CMAKE_BUILD_TYPE MATCHES "debug"))
    302             add_custom_command(TARGET ${JNI_LIB_NAME} POST_BUILD COMMAND ${CMAKE_STRIP} --strip-unneeded "${android_proj_jni_location}")
    303         endif()
    304       endif()
    305     endif()
    306 
    307     # build java part
    308     if(android_proj_IGNORE_JAVA)
    309       add_custom_command(
    310          OUTPUT "${android_proj_bin_dir}/bin/${target}-debug.apk"
    311          COMMAND ${ANT_EXECUTABLE} -q -noinput -k debug
    312          COMMAND ${CMAKE_COMMAND} -E touch "${android_proj_bin_dir}/bin/${target}-debug.apk" # needed because ant does not update the timestamp of updated apk
    313          WORKING_DIRECTORY "${android_proj_bin_dir}"
    314          MAIN_DEPENDENCY "${android_proj_bin_dir}/${ANDROID_MANIFEST_FILE}"
    315          DEPENDS ${android_proj_file_deps} ${JNI_LIB_NAME})
    316     else()
    317       add_custom_command(
    318          OUTPUT "${android_proj_bin_dir}/bin/${target}-debug.apk"
    319          COMMAND ${ANT_EXECUTABLE} -q -noinput -k debug
    320          COMMAND ${CMAKE_COMMAND} -E touch "${android_proj_bin_dir}/bin/${target}-debug.apk" # needed because ant does not update the timestamp of updated apk
    321          WORKING_DIRECTORY "${android_proj_bin_dir}"
    322          MAIN_DEPENDENCY "${android_proj_bin_dir}/${ANDROID_MANIFEST_FILE}"
    323          DEPENDS "${OpenCV_BINARY_DIR}/bin/classes.jar.dephelper" opencv_java # as we are part of OpenCV we can just force this dependency
    324          DEPENDS ${android_proj_file_deps} ${JNI_LIB_NAME})
    325     endif()
    326 
    327     unset(JNI_LIB_NAME)
    328 
    329     add_custom_target(${target} ALL SOURCES "${android_proj_bin_dir}/bin/${target}-debug.apk" )
    330     if(NOT android_proj_IGNORE_JAVA)
    331       add_dependencies(${target} opencv_java)
    332     endif()
    333     if(android_proj_native_deps)
    334       add_dependencies(${target} ${android_proj_native_deps})
    335     endif()
    336 
    337     if (android_proj_COPY_LIBS OR ANDROID_EXAMPLES_WITH_LIBS)
    338       message(STATUS "Android project with libs: " ${target})
    339       add_custom_target(
    340         ${target}_copy_libs
    341         COMMAND ${CMAKE_COMMAND} -DSRC_DIR=${OpenCV_BINARY_DIR}/lib -DDST_DIR=${android_proj_bin_dir}/libs -P ${OpenCV_SOURCE_DIR}/cmake/copyAndroidLibs.cmake
    342         WORKING_DIRECTORY ${OpenCV_BINARY_DIR}/lib
    343       )
    344       add_dependencies(${target} ${target}_copy_libs)
    345       if (ANDROID_EXAMPLES_WITH_LIBS)
    346         add_dependencies(${target}_copy_libs "${OpenCV_BINARY_DIR}/bin/classes.jar.dephelper" opencv_java)
    347       endif()
    348     endif()
    349 
    350     if(__android_project_chain)
    351       add_dependencies(${target} ${__android_project_chain})
    352     endif()
    353     set(__android_project_chain ${target} CACHE INTERNAL "auxiliary variable used for Android progects chaining")
    354 
    355     # put the final .apk to the OpenCV's bin folder
    356     add_custom_command(TARGET ${target} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy "${android_proj_bin_dir}/bin/${target}-debug.apk" "${OpenCV_BINARY_DIR}/bin/${target}.apk")
    357     if(INSTALL_ANDROID_EXAMPLES AND "${target}" MATCHES "^example-")
    358       #apk
    359       install(FILES "${OpenCV_BINARY_DIR}/bin/${target}.apk" DESTINATION "samples" COMPONENT samples)
    360       get_filename_component(sample_dir "${path}" NAME)
    361       #java part
    362       list(REMOVE_ITEM android_proj_files ${ANDROID_MANIFEST_FILE})
    363       foreach(f ${android_proj_files} ${ANDROID_MANIFEST_FILE})
    364         get_filename_component(install_subdir "${f}" PATH)
    365         install(FILES "${android_proj_bin_dir}/${f}" DESTINATION "samples/${sample_dir}/${install_subdir}" COMPONENT samples)
    366       endforeach()
    367       #jni part + eclipse files
    368       file(GLOB_RECURSE jni_files RELATIVE "${path}" "${path}/jni/*" "${path}/.cproject")
    369       ocv_list_filterout(jni_files "\\\\.svn")
    370       foreach(f ${jni_files} ".classpath" ".project" ".settings/org.eclipse.jdt.core.prefs")
    371         get_filename_component(install_subdir "${f}" PATH)
    372         install(FILES "${path}/${f}" DESTINATION "samples/${sample_dir}/${install_subdir}" COMPONENT samples)
    373       endforeach()
    374       #update proj
    375       if(android_proj_lib_deps_commands)
    376         set(inst_lib_opt " --library ../../sdk/java")
    377       endif()
    378       install(CODE "EXECUTE_PROCESS(COMMAND ${ANDROID_EXECUTABLE} --silent update project --path . --target \"${android_proj_sdk_target}\" --name \"${target}\" ${inst_lib_opt}
    379                                     WORKING_DIRECTORY \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/samples/${sample_dir}\"
    380                                    )"  COMPONENT samples)
    381       #empty 'gen'
    382       install(CODE "MAKE_DIRECTORY(\"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/samples/${sample_dir}/gen\")" COMPONENT samples)
    383     endif()
    384   endif()
    385 endmacro()
    386