OpenCVDetectAndroidSDK.cmake 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. if(EXISTS "${ANDROID_EXECUTABLE}")
  2. set(ANDROID_SDK_DETECT_QUIET TRUE)
  3. endif()
  4. # fixup for https://github.com/android-ndk/ndk/issues/596
  5. if(DEFINED ANDROID_NDK_REVISION AND ANDROID_NDK_REVISION MATCHES "(1[56])([0-9]+)\\.([^\n]+)\n")
  6. set(ANDROID_NDK_REVISION "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}")
  7. set(ANDROID_NDK_REVISION "${ANDROID_NDK_REVISION}" CACHE INTERNAL "Android NDK revision")
  8. endif()
  9. # fixup -g option: https://github.com/opencv/opencv/issues/8460#issuecomment-434249750
  10. if(INSTALL_CREATE_DISTRIB
  11. AND (NOT BUILD_WITH_DEBUG_INFO AND NOT CMAKE_BUILD_TYPE MATCHES "Debug")
  12. AND NOT OPENCV_SKIP_ANDROID_G_OPTION_FIX
  13. )
  14. if(" ${CMAKE_CXX_FLAGS} " MATCHES " -g ")
  15. message(STATUS "Android: fixup -g compiler option from Android toolchain")
  16. endif()
  17. string(REPLACE " -g " " " CMAKE_CXX_FLAGS " ${CMAKE_CXX_FLAGS} ")
  18. string(REPLACE " -g " " " CMAKE_C_FLAGS " ${CMAKE_C_FLAGS} ")
  19. string(REPLACE " -g " " " CMAKE_ASM_FLAGS " ${CMAKE_ASM_FLAGS} ")
  20. if(NOT " ${CMAKE_CXX_FLAGS_DEBUG}" MATCHES " -g")
  21. set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
  22. endif()
  23. if(NOT " ${CMAKE_C_FLAGS_DEBUG}" MATCHES " -g")
  24. set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g")
  25. endif()
  26. endif()
  27. # https://developer.android.com/studio/command-line/variables.html
  28. ocv_check_environment_variables(ANDROID_SDK_ROOT ANDROID_HOME ANDROID_SDK)
  29. set(__msg_BUILD_ANDROID_PROJECTS "Use BUILD_ANDROID_PROJECTS=OFF to prepare Android project files without building them")
  30. macro(ocv_detect_android_sdk)
  31. if(NOT DEFINED ANDROID_SDK)
  32. if(DEFINED ANDROID_SDK AND EXISTS "${ANDROID_SDK}")
  33. set(ANDROID_SDK "${ANDROID_SDK}" CACHE INTERNAL "Android SDK path")
  34. elseif(DEFINED ANDROID_HOME AND EXISTS "${ANDROID_HOME}")
  35. set(ANDROID_SDK "${ANDROID_HOME}" CACHE INTERNAL "Android SDK path")
  36. elseif(DEFINED ANDROID_SDK_ROOT AND EXISTS "${ANDROID_SDK_ROOT}")
  37. set(ANDROID_SDK "${ANDROID_SDK_ROOT}" CACHE INTERNAL "Android SDK path")
  38. endif()
  39. if(DEFINED ANDROID_SDK)
  40. message(STATUS "Android SDK: using location: ${ANDROID_SDK}")
  41. endif()
  42. endif()
  43. if(NOT DEFINED ANDROID_SDK)
  44. message(FATAL_ERROR "Android SDK: specify path to Android SDK via ANDROID_SDK_ROOT / ANDROID_HOME / ANDROID_SDK variables")
  45. endif()
  46. if(NOT EXISTS "${ANDROID_SDK}")
  47. message(FATAL_ERROR "Android SDK: specified path doesn't exist: ${ANDROID_SDK}")
  48. endif()
  49. endmacro()
  50. macro(ocv_detect_android_sdk_tools)
  51. # https://developer.android.com/studio/releases/sdk-tools.html
  52. if(NOT DEFINED ANDROID_SDK_TOOLS)
  53. if(DEFINED ANDROID_SDK AND EXISTS "${ANDROID_SDK}/tools")
  54. set(ANDROID_SDK_TOOLS "${ANDROID_SDK}/tools" CACHE INTERNAL "Android SDK Tools path")
  55. endif()
  56. endif()
  57. if(NOT DEFINED ANDROID_SDK_TOOLS)
  58. message(FATAL_ERROR "Android SDK Tools: can't automatically find Android SDK Tools. Specify path via ANDROID_SDK_TOOLS variable")
  59. endif()
  60. if(NOT EXISTS "${ANDROID_SDK_TOOLS}")
  61. message(FATAL_ERROR "Android SDK Tools: specified path doesn't exist: ${ANDROID_SDK_TOOLS}")
  62. endif()
  63. if(NOT DEFINED ANDROID_SDK_TOOLS_VERSION)
  64. ocv_parse_properties_file("${ANDROID_SDK_TOOLS}/source.properties"
  65. ANDROID_TOOLS CACHE Pkg_Revision
  66. MSG_PREFIX "Android SDK Tools: "
  67. )
  68. if(NOT DEFINED ANDROID_TOOLS_Pkg_Revision)
  69. message(FATAL_ERROR "Android SDK Tools: Can't determine package version: ANDROID_SDK_TOOLS=${ANDROID_SDK_TOOLS}\n"
  70. "Check specified parameters or force version via 'ANDROID_SDK_TOOLS_VERSION' variable.\n"
  71. "${__msg_BUILD_ANDROID_PROJECTS}")
  72. elseif(NOT ANDROID_SDK_DETECT_QUIET)
  73. set(__info "")
  74. if(DEFINED ANDROID_TOOLS_Pkg_Desc)
  75. set(__info " (description: '${ANDROID_TOOLS_Pkg_Desc}')")
  76. endif()
  77. message(STATUS "Android SDK Tools: ver. ${ANDROID_TOOLS_Pkg_Revision}${__info}")
  78. endif()
  79. set(ANDROID_SDK_TOOLS_VERSION "${ANDROID_TOOLS_Pkg_Revision}" CACHE INTERNAL "Android SDK Tools version")
  80. endif()
  81. if(NOT DEFINED ANDROID_TOOLS_Pkg_Revision)
  82. set(ANDROID_TOOLS_Pkg_Revision "${ANDROID_SDK_TOOLS_VERSION}" CACHE INTERNAL "Android SDK Tools version (deprecated)")
  83. endif()
  84. set(ANDROID_SDK_TOOLS_PATH "${ANDROID_SDK_TOOLS}" CACHE INTERNAL "Android SDK Tools path (deprecated)")
  85. endmacro() # ocv_detect_android_sdk_tools
  86. macro(ocv_detect_android_sdk_build_tools)
  87. # https://developer.android.com/studio/releases/build-tools.html
  88. if(NOT DEFINED ANDROID_SDK_BUILD_TOOLS_VERSION)
  89. if(NOT DEFINED ANDROID_SDK_BUILD_TOOLS)
  90. set(__search_dir ${ANDROID_SDK}/build-tools)
  91. if(NOT EXISTS "${__search_dir}")
  92. message(FATAL_ERROR "Android SDK Build Tools: directory doesn't exist: ${__search_dir} "
  93. "${__msg_BUILD_ANDROID_PROJECTS}")
  94. endif()
  95. if(NOT DEFINED ANDROID_SDK_BUILD_TOOLS_SUBDIR)
  96. file(GLOB __found RELATIVE "${__search_dir}" ${__search_dir}/*)
  97. set(__dirlist "")
  98. set(__selected 0)
  99. set(__versions "")
  100. foreach(d ${__found})
  101. if(IS_DIRECTORY "${__search_dir}/${d}")
  102. list(APPEND __dirlist ${d})
  103. if(d MATCHES "[0-9]+(\\.[0-9]+)*")
  104. list(APPEND __versions "${d}")
  105. endif()
  106. if(__selected VERSION_LESS d)
  107. set(__selected "${d}")
  108. endif()
  109. endif()
  110. endforeach()
  111. if(__selected VERSION_GREATER 0)
  112. set(ANDROID_SDK_BUILD_TOOLS_SUBDIR "${__selected}")
  113. elseif(__dirlist)
  114. set(__versions "")
  115. foreach(d ${__dirlist})
  116. if(EXISTS "${__search_dir}/${d}/source.properties")
  117. ocv_clear_vars(ANDROID_BUILD_TOOLS_Pkg_Revision)
  118. ocv_parse_properties_file("${__search_dir}/${d}/source.properties"
  119. ANDROID_BUILD_TOOLS
  120. MSG_PREFIX "Android SDK Tools: "
  121. )
  122. if(DEFINED ANDROID_BUILD_TOOLS_Pkg_Revision)
  123. list(APPEND __versions "${ANDROID_BUILD_TOOLS_Pkg_Revision}")
  124. if(__selected VERSION_LESS ANDROID_BUILD_TOOLS_Pkg_Revision)
  125. set(ANDROID_SDK_BUILD_TOOLS_SUBDIR "${d}")
  126. set(__selected "${ANDROID_BUILD_TOOLS_Pkg_Revision}")
  127. endif()
  128. endif()
  129. endif()
  130. endforeach()
  131. endif()
  132. if(DEFINED ANDROID_SDK_BUILD_TOOLS_SUBDIR)
  133. set(ANDROID_SDK_BUILD_TOOLS_VERSION "${__selected}" CACHE STRING "Android SDK Build Tools version")
  134. set_property(CACHE ANDROID_SDK_BUILD_TOOLS_VERSION PROPERTY STRINGS ${__versions})
  135. set(ANDROID_SDK_BUILD_TOOLS "${__search_dir}/${d}" CACHE INTERNAL "Android SDK Build Tools path")
  136. message(STATUS "Android SDK Build Tools: ver. ${ANDROID_SDK_BUILD_TOOLS_VERSION} (subdir ${ANDROID_SDK_BUILD_TOOLS_SUBDIR} from ${__dirlist})")
  137. else()
  138. message(FATAL_ERROR "Android SDK Build Tools: autodetection failed. "
  139. "Specify ANDROID_SDK_BUILD_TOOLS_VERSION / ANDROID_SDK_BUILD_TOOLS_SUBDIR / ANDROID_SDK_BUILD_TOOLS variable to bypass autodetection.\n"
  140. "${__msg_BUILD_ANDROID_PROJECTS}")
  141. endif()
  142. endif()
  143. else()
  144. ocv_parse_properties_file("${ANDROID_SDK_BUILD_TOOLS}/source.properties"
  145. ANDROID_BUILD_TOOLS
  146. MSG_PREFIX "Android SDK Tools: "
  147. )
  148. if(NOT DEFINED ANDROID_BUILD_TOOLS_Pkg_Revision)
  149. message(FATAL_ERROR "Android SDK Build Tools: Can't detect version: ANDROID_SDK_BUILD_TOOLS=${ANDROID_SDK_BUILD_TOOLS}\n"
  150. "Specify ANDROID_SDK_BUILD_TOOLS_VERSION variable to bypass autodetection.\n"
  151. "${__msg_BUILD_ANDROID_PROJECTS}")
  152. else()
  153. set(ANDROID_SDK_BUILD_TOOLS_VERSION "${ANDROID_BUILD_TOOLS_Pkg_Revision}" CACHE INTERNAL "Android SDK Build Tools version")
  154. message(STATUS "Android SDK Build Tools: ver. ${ANDROID_SDK_BUILD_TOOLS_VERSION} (ANDROID_SDK_BUILD_TOOLS=${ANDROID_SDK_BUILD_TOOLS})")
  155. endif()
  156. endif() # ANDROID_SDK_BUILD_TOOLS
  157. endif() # ANDROID_SDK_BUILD_TOOLS_VERSION
  158. endmacro() # ocv_detect_android_sdk_build_tools
  159. if(BUILD_ANDROID_PROJECTS)
  160. ocv_detect_android_sdk()
  161. ocv_detect_android_sdk_build_tools()
  162. if(NOT ANDROID_SDK_BUILD_TOOLS_VERSION VERSION_LESS 26.0.2)
  163. # https://developer.android.com/studio/releases/gradle-plugin.html
  164. message(STATUS "Android SDK Build Tools: Gradle 3.0.0+ builds support is available")
  165. ocv_update(ANDROID_PROJECTS_SUPPORT_GRADLE ON)
  166. else()
  167. ocv_detect_android_sdk_tools()
  168. if(ANDROID_SDK_TOOLS_VERSION VERSION_LESS 14)
  169. message(FATAL_ERROR "Android SDK Tools: OpenCV requires Android SDK Tools revision 14 or newer.\n"
  170. "${__msg_BUILD_ANDROID_PROJECTS}")
  171. endif()
  172. if(NOT ANDROID_SDK_TOOLS_VERSION VERSION_LESS 25.3.0)
  173. message(STATUS "Android SDK Tools: Ant (Eclipse) builds are NOT supported by Android SDK")
  174. ocv_update(ANDROID_PROJECTS_SUPPORT_ANT OFF)
  175. else()
  176. include(${CMAKE_CURRENT_LIST_DIR}/../OpenCVDetectApacheAnt.cmake)
  177. if(ANT_EXECUTABLE AND NOT ANT_VERSION VERSION_LESS 1.7)
  178. message(STATUS "Android SDK Tools: Ant (Eclipse) builds are supported")
  179. ocv_update(ANDROID_PROJECTS_SUPPORT_ANT ON)
  180. endif()
  181. endif()
  182. endif()
  183. if(NOT DEFINED ANDROID_PROJECTS_BUILD_TYPE)
  184. if(ANDROID_PROJECTS_SUPPORT_ANT)
  185. ocv_update(ANDROID_PROJECTS_BUILD_TYPE "ANT")
  186. elseif(ANDROID_PROJECTS_SUPPORT_GRADLE)
  187. ocv_update(ANDROID_PROJECTS_BUILD_TYPE "GRADLE")
  188. else()
  189. message(FATAL_ERROR "Android SDK: Can't build Android projects as requested by BUILD_ANDROID_PROJECTS=ON variable.\n"
  190. "${__msg_BUILD_ANDROID_PROJECTS}")
  191. endif()
  192. endif()
  193. if(ANDROID_PROJECTS_BUILD_TYPE STREQUAL "ANT")
  194. message(STATUS "Android SDK Tools: Prepare Android projects for using Ant build scripts (deprecated)")
  195. elseif(ANDROID_PROJECTS_BUILD_TYPE STREQUAL "GRADLE")
  196. message(STATUS "Android SDK Tools: Prepare Android projects for using Gradle 3.0.0+ build scripts")
  197. endif()
  198. else()
  199. message("Android: Projects builds are DISABLED")
  200. macro(add_android_project)
  201. endmacro()
  202. endif() # BUILD_ANDROID_PROJECTS
  203. if(ANDROID_PROJECTS_BUILD_TYPE STREQUAL "ANT")
  204. include(${CMAKE_CURRENT_LIST_DIR}/android_ant_projects.cmake)
  205. elseif(ANDROID_PROJECTS_BUILD_TYPE STREQUAL "GRADLE")
  206. include(${CMAKE_CURRENT_LIST_DIR}/android_gradle_projects.cmake)
  207. elseif(BUILD_ANDROID_PROJECTS)
  208. message(FATAL_ERROR "Internal error")
  209. else()
  210. # TODO
  211. #include(${CMAKE_CURRENT_LIST_DIR}/android_disabled_projects.cmake)
  212. endif()