android_ant_projects.cmake 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. find_host_program(ANDROID_EXECUTABLE
  2. NAMES android.bat android
  3. PATHS "${ANDROID_SDK_TOOLS}"
  4. DOC "Android 'android' tool location"
  5. )
  6. if(NOT ANDROID_EXECUTABLE)
  7. message(FATAL_ERROR "Android SDK Tools: Can't find 'android' tool")
  8. elseif(NOT ANDROID_SDK_DETECT_QUIET)
  9. message(STATUS "Android SDK Tools: Found 'android' tool: ${ANDROID_EXECUTABLE}")
  10. endif()
  11. set(ANDROID_PROJECT_PROPERTIES_FILE project.properties)
  12. set(ANDROID_ANT_PROPERTIES_FILE ant.properties)
  13. set(ANDROID_MANIFEST_FILE AndroidManifest.xml)
  14. set(ANDROID_LIB_PROJECT_FILES build.xml local.properties proguard-project.txt ${ANDROID_PROJECT_PROPERTIES_FILE})
  15. set(ANDROID_PROJECT_FILES ${ANDROID_LIB_PROJECT_FILES})
  16. execute_process(COMMAND ${ANDROID_EXECUTABLE} list target -c
  17. RESULT_VARIABLE ANDROID_PROCESS
  18. OUTPUT_VARIABLE ANDROID_SDK_TARGETS
  19. ERROR_VARIABLE ANDROID_PROCESS_ERRORS
  20. OUTPUT_STRIP_TRAILING_WHITESPACE
  21. )
  22. if(NOT ANDROID_PROCESS EQUAL 0)
  23. set(ANDROID_EXECUTABLE "ANDROID_EXECUTABLE-NOTFOUND" CACHE INTERNAL)
  24. message(FATAL_ERROR "Android: Failed to get list of installed Android targets.")
  25. endif()
  26. string(REGEX MATCHALL "[^\n]+" ANDROID_SDK_TARGETS "${ANDROID_SDK_TARGETS}")
  27. # clear ANDROID_SDK_TARGET if no target is provided by user
  28. if(NOT ANDROID_SDK_TARGET)
  29. set(ANDROID_SDK_TARGET "" CACHE STRING "Android SDK target for the OpenCV Java API and samples")
  30. endif()
  31. if(ANDROID_SDK_TARGETS)
  32. message(STATUS "Android SDK Tools: Available targets: ${ANDROID_SDK_TARGETS}")
  33. set_property(CACHE ANDROID_SDK_TARGET PROPERTY STRINGS ${ANDROID_SDK_TARGETS} )
  34. else()
  35. message(FATAL_ERROR "Android: List of installed Android targets is empty")
  36. endif()
  37. # finds minimal installed SDK target compatible with provided names or API levels
  38. # usage:
  39. # get_compatible_android_api_level(VARIABLE [level1] [level2] ...)
  40. macro(android_get_compatible_target VAR)
  41. set(${VAR} "${VAR}-NOTFOUND")
  42. if(ANDROID_SDK_TARGETS)
  43. list(GET ANDROID_SDK_TARGETS 0 __lvl)
  44. string(REGEX MATCH "[0-9]+$" __lvl "${__lvl}")
  45. #find minimal level matching to all provided levels
  46. foreach(lvl ${ARGN})
  47. string(REGEX MATCH "[0-9]+$" __level "${lvl}")
  48. if(__level GREATER __lvl)
  49. set(__lvl ${__level})
  50. endif()
  51. endforeach()
  52. #search for compatible levels
  53. foreach(lvl ${ANDROID_SDK_TARGETS})
  54. string(REGEX MATCH "[0-9]+$" __level "${lvl}")
  55. if(__level EQUAL __lvl)
  56. #look for exact match
  57. foreach(usrlvl ${ARGN})
  58. if("${usrlvl}" STREQUAL "${lvl}")
  59. set(${VAR} "${lvl}")
  60. break()
  61. endif()
  62. endforeach()
  63. if("${${VAR}}" STREQUAL "${lvl}")
  64. break() #exact match was found
  65. elseif(NOT ${VAR})
  66. set(${VAR} "${lvl}")
  67. endif()
  68. elseif(__level GREATER __lvl)
  69. if(NOT ${VAR})
  70. set(${VAR} "${lvl}")
  71. endif()
  72. break()
  73. endif()
  74. endforeach()
  75. unset(__lvl)
  76. unset(__level)
  77. endif()
  78. endmacro()
  79. unset(__android_project_chain CACHE)
  80. # add_android_project(target_name ${path} NATIVE_DEPS opencv_core LIBRARY_DEPS ${OpenCV_BINARY_DIR} SDK_TARGET 11)
  81. macro(add_android_project target path)
  82. # parse arguments
  83. set(android_proj_arglist NATIVE_DEPS LIBRARY_DEPS SDK_TARGET IGNORE_JAVA IGNORE_MANIFEST COPY_LIBS)
  84. set(__varname "android_proj_")
  85. foreach(v ${android_proj_arglist})
  86. set(${__varname}${v} "")
  87. endforeach()
  88. foreach(arg ${ARGN})
  89. set(__var "${__varname}")
  90. foreach(v ${android_proj_arglist})
  91. if("${v}" STREQUAL "${arg}")
  92. set(__varname "android_proj_${v}")
  93. break()
  94. endif()
  95. endforeach()
  96. if(__var STREQUAL __varname)
  97. list(APPEND ${__var} "${arg}")
  98. endif()
  99. endforeach()
  100. # get compatible SDK target
  101. android_get_compatible_target(android_proj_sdk_target ${ANDROID_NATIVE_API_LEVEL} ${android_proj_SDK_TARGET})
  102. if(NOT android_proj_sdk_target)
  103. message(WARNING "Can not find any SDK target compatible with: ${ANDROID_NATIVE_API_LEVEL} ${android_proj_SDK_TARGET}
  104. The project ${target} will not be build")
  105. endif()
  106. # check native dependencies
  107. if(android_proj_IGNORE_JAVA)
  108. ocv_check_dependencies(${android_proj_NATIVE_DEPS})
  109. else()
  110. ocv_check_dependencies(${android_proj_NATIVE_DEPS} opencv_java)
  111. endif()
  112. if(EXISTS "${path}/jni/Android.mk" )
  113. # find if native_app_glue is used
  114. file(STRINGS "${path}/jni/Android.mk" NATIVE_APP_GLUE REGEX ".*(call import-module,android/native_app_glue)" )
  115. if(NATIVE_APP_GLUE)
  116. if(ANDROID_NATIVE_API_LEVEL LESS 9 OR NOT EXISTS "${ANDROID_NDK}/sources/android/native_app_glue")
  117. set(OCV_DEPENDENCIES_FOUND FALSE)
  118. endif()
  119. endif()
  120. endif()
  121. 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}")
  122. project(${target})
  123. set(android_proj_bin_dir "${CMAKE_CURRENT_BINARY_DIR}/.build")
  124. # project sources
  125. set(__src_glob "${path}/res/*" "${path}/src/*")
  126. if(NOT android_proj_IGNORE_MANIFEST)
  127. list(APPEND __src_glob "${path}/${ANDROID_MANIFEST_FILE}")
  128. endif()
  129. ocv_copyfiles_append_dir(SRC_COPY "${path}" "${android_proj_bin_dir}" ${__src_glob})
  130. ocv_copyfiles_add_forced_target(${target}_copy_src SRC_COPY "Copy project sources: ${target}")
  131. set(android_proj_file_deps ${target}_copy_src "${OPENCV_DEPHELPER}/${target}_copy_src")
  132. set(android_proj_lib_deps_commands "")
  133. set(android_proj_target_files ${ANDROID_PROJECT_FILES})
  134. ocv_list_add_prefix(android_proj_target_files "${android_proj_bin_dir}/")
  135. # process Android library dependencies
  136. foreach(dep ${android_proj_LIBRARY_DEPS})
  137. file(RELATIVE_PATH __dep "${android_proj_bin_dir}" "${dep}")
  138. list(APPEND android_proj_lib_deps_commands
  139. COMMAND ${ANDROID_EXECUTABLE} --silent update project --path "${android_proj_bin_dir}" --library "${__dep}")
  140. endforeach()
  141. # fix Android project
  142. add_custom_command(
  143. OUTPUT ${android_proj_target_files}
  144. COMMAND ${CMAKE_COMMAND} -E remove ${android_proj_target_files}
  145. "${android_proj_bin_dir}/bin/${ANDROID_MANIFEST_FILE}" # avoid warning about sub-projects
  146. COMMAND ${ANDROID_EXECUTABLE} --silent update project --path "${android_proj_bin_dir}" --target "${android_proj_sdk_target}" --name "${target}"
  147. ${android_proj_lib_deps_commands}
  148. WORKING_DIRECTORY "${android_proj_bin_dir}"
  149. DEPENDS ${android_proj_file_deps}
  150. COMMENT "Updating Android project at ${path}. SDK target: ${android_proj_sdk_target}"
  151. )
  152. list(APPEND android_proj_file_deps ${android_proj_target_files})
  153. # build native part
  154. file(GLOB_RECURSE android_proj_jni_files "${path}/jni/*.c" "${path}/jni/*.h" "${path}/jni/*.cpp" "${path}/jni/*.hpp")
  155. if(android_proj_jni_files AND EXISTS ${path}/jni/Android.mk AND NOT DEFINED JNI_LIB_NAME)
  156. # find local module name in Android.mk file to build native lib
  157. file(STRINGS "${path}/jni/Android.mk" JNI_LIB_NAME REGEX "LOCAL_MODULE[ ]*:=[ ]*.*" )
  158. string(REGEX REPLACE "LOCAL_MODULE[ ]*:=[ ]*([a-zA-Z_][a-zA-Z_0-9]*)[ ]*" "\\1" JNI_LIB_NAME "${JNI_LIB_NAME}")
  159. if(JNI_LIB_NAME)
  160. if(NATIVE_APP_GLUE)
  161. include_directories(${ANDROID_NDK}/sources/android/native_app_glue)
  162. list(APPEND android_proj_jni_files ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c)
  163. ocv_warnings_disable(CMAKE_C_FLAGS -Wstrict-prototypes -Wunused-parameter -Wmissing-prototypes)
  164. set(android_proj_NATIVE_DEPS ${android_proj_NATIVE_DEPS} android)
  165. endif()
  166. add_library(${JNI_LIB_NAME} SHARED ${android_proj_jni_files})
  167. ocv_target_include_modules_recurse(${JNI_LIB_NAME} ${android_proj_NATIVE_DEPS})
  168. ocv_target_include_directories(${JNI_LIB_NAME} "${path}/jni")
  169. ocv_target_link_libraries(${JNI_LIB_NAME} PRIVATE ${OPENCV_LINKER_LIBS} ${android_proj_NATIVE_DEPS})
  170. set_target_properties(${JNI_LIB_NAME} PROPERTIES
  171. OUTPUT_NAME "${JNI_LIB_NAME}"
  172. LIBRARY_OUTPUT_DIRECTORY "${android_proj_bin_dir}/libs/${ANDROID_NDK_ABI_NAME}"
  173. )
  174. if(NOT BUILD_WITH_DEBUG_INFO AND NOT CMAKE_BUILD_TYPE MATCHES "Debug")
  175. add_custom_command(TARGET ${JNI_LIB_NAME} POST_BUILD COMMAND ${CMAKE_STRIP} --strip-unneeded "$<TARGET_FILE:${JNI_LIB_NAME}>")
  176. endif()
  177. endif()
  178. endif()
  179. # build java part
  180. if(android_proj_IGNORE_JAVA)
  181. set(android_proj_extra_deps "")
  182. else()
  183. list(APPEND android_proj_extra_deps opencv_java_android)
  184. endif()
  185. set(_native_deps "")
  186. if(NOT android_proj_IGNORE_JAVA)
  187. set(_native_deps opencv_java)
  188. endif()
  189. if(android_proj_native_deps)
  190. list(APPEND _native_deps ${android_proj_native_deps})
  191. endif()
  192. if(_native_deps)
  193. list(APPEND android_proj_extra_deps ${_native_deps})
  194. endif()
  195. if((android_proj_COPY_LIBS OR ANDROID_EXAMPLES_WITH_LIBS) AND _native_deps)
  196. message(STATUS "Android project with native libs: " ${target} " (" ${_native_deps} ")")
  197. ocv_copyfiles_append_dir(NATIVE_COPY "${OpenCV_BINARY_DIR}/lib" "${android_proj_bin_dir}/libs" "${OpenCV_BINARY_DIR}/lib/*.so")
  198. ocv_copyfiles_add_target(${target}_copy_libs NATIVE_COPY "Copy native libs for project: ${target}" ${_native_deps})
  199. list(APPEND android_proj_extra_deps ${target}_copy_libs)
  200. endif()
  201. add_custom_command(
  202. OUTPUT "${android_proj_bin_dir}/bin/${target}-debug.apk"
  203. COMMAND ${ANT_EXECUTABLE} -q -noinput -k debug -Djava.target=1.6 -Djava.source=1.6
  204. COMMAND ${CMAKE_COMMAND} -E touch "${android_proj_bin_dir}/bin/${target}-debug.apk" # needed because ant does not update the timestamp of updated apk
  205. WORKING_DIRECTORY "${android_proj_bin_dir}"
  206. DEPENDS ${android_proj_extra_deps} ${android_proj_file_deps} ${JNI_LIB_NAME}
  207. COMMENT "Generating ${target}-debug.apk"
  208. )
  209. unset(JNI_LIB_NAME)
  210. add_custom_target(${target} ALL SOURCES "${android_proj_bin_dir}/bin/${target}-debug.apk" )
  211. # There is some strange problem with concurrent Android .APK builds:
  212. # <android-sdk>/tools/ant/build.xml:781: Problem reading <build_dir>/bin/classes.jar'
  213. if(__android_project_chain)
  214. add_dependencies(${target} ${__android_project_chain})
  215. endif()
  216. set(__android_project_chain ${target} CACHE INTERNAL "auxiliary variable used for Android progects chaining")
  217. # put the final .apk to the OpenCV's bin folder
  218. add_custom_command(TARGET ${target} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different "${android_proj_bin_dir}/bin/${target}-debug.apk" "${OpenCV_BINARY_DIR}/bin/${target}.apk")
  219. if(INSTALL_ANDROID_EXAMPLES AND "${target}" MATCHES "^example-")
  220. get_filename_component(sample_dir "${path}" NAME)
  221. # apk
  222. install(FILES "${OpenCV_BINARY_DIR}/bin/${target}.apk" DESTINATION "samples" COMPONENT samples)
  223. # clear "external" project files (need to remove files generated by 'android' tool)
  224. set(external_target_files ${ANDROID_PROJECT_FILES})
  225. ocv_list_add_prefix(external_target_files "\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/samples/${sample_dir}/")
  226. install(CODE "
  227. MESSAGE(STATUS \"Cleaning generated project files from Android sample install directory: ${sample_dir}\")
  228. FOREACH(f ${ANDROID_PROJECT_FILES})
  229. FILE(REMOVE \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/samples/${sample_dir}/\${f}\")
  230. ENDFOREACH()
  231. " COMPONENT samples)
  232. #java part
  233. install(DIRECTORY "${android_proj_bin_dir}/res" DESTINATION "samples/${sample_dir}" COMPONENT samples)
  234. install(DIRECTORY "${android_proj_bin_dir}/src" DESTINATION "samples/${sample_dir}" COMPONENT samples)
  235. install(FILES "${android_proj_bin_dir}/${ANDROID_MANIFEST_FILE}" DESTINATION "samples/${sample_dir}" COMPONENT samples)
  236. #jni part + eclipse files
  237. file(GLOB_RECURSE jni_files RELATIVE "${path}" "${path}/jni/*" "${path}/.cproject")
  238. ocv_list_filterout(jni_files "\\\\.svn")
  239. foreach(f ${jni_files} ".classpath" ".project" ".settings/org.eclipse.jdt.core.prefs")
  240. get_filename_component(install_subdir "${f}" PATH)
  241. install(FILES "${path}/${f}" DESTINATION "samples/${sample_dir}/${install_subdir}" COMPONENT samples)
  242. endforeach()
  243. #update proj
  244. if(android_proj_lib_deps_commands)
  245. set(inst_lib_opt " --library ../../sdk/java")
  246. endif()
  247. install(CODE "
  248. MESSAGE(STATUS \"Fixing Android library reference for sample: ${sample_dir}\")
  249. EXECUTE_PROCESS(
  250. COMMAND ${ANDROID_EXECUTABLE} --silent update project --path . --target \"${android_proj_sdk_target}\" --name \"${target}\" ${inst_lib_opt}
  251. WORKING_DIRECTORY \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/samples/${sample_dir}\"
  252. )
  253. " COMPONENT samples)
  254. # empty 'gen'
  255. install(CODE "FILE(MAKE_DIRECTORY \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/samples/${sample_dir}/gen\")" COMPONENT samples)
  256. endif()
  257. endif()
  258. endmacro()