2012-03-26 03:57:58 +08:00
|
|
|
if(EXISTS "${ANDROID_EXECUTABLE}")
|
|
|
|
set(ANDROID_SDK_DETECT_QUIET TRUE)
|
|
|
|
endif()
|
|
|
|
|
2012-02-03 19:26:49 +08:00
|
|
|
file(TO_CMAKE_PATH "$ENV{ProgramFiles}" ProgramFiles_ENV_PATH)
|
2012-03-16 17:16:31 +08:00
|
|
|
file(TO_CMAKE_PATH "$ENV{HOME}" HOME_ENV_PATH)
|
2012-02-03 19:26:49 +08:00
|
|
|
|
2012-03-26 03:57:58 +08:00
|
|
|
if(CMAKE_HOST_WIN32)
|
|
|
|
set(ANDROID_SDK_OS windows)
|
|
|
|
elseif(CMAKE_HOST_APPLE)
|
2012-05-20 16:26:20 +08:00
|
|
|
set(ANDROID_SDK_OS macosx)
|
2012-03-26 03:57:58 +08:00
|
|
|
else()
|
|
|
|
set(ANDROID_SDK_OS linux)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
#find android SDK: search in ANDROID_SDK first
|
|
|
|
find_host_program(ANDROID_EXECUTABLE
|
|
|
|
NAMES android.bat android
|
|
|
|
PATH_SUFFIXES tools
|
|
|
|
PATHS
|
|
|
|
ENV ANDROID_SDK
|
|
|
|
DOC "Android SDK location"
|
|
|
|
NO_DEFAULT_PATH
|
|
|
|
)
|
|
|
|
|
|
|
|
# Now search default paths
|
2012-02-03 19:26:49 +08:00
|
|
|
find_host_program(ANDROID_EXECUTABLE
|
|
|
|
NAMES android.bat android
|
2012-03-26 03:57:58 +08:00
|
|
|
PATH_SUFFIXES android-sdk-${ANDROID_SDK_OS}/tools
|
|
|
|
android-sdk-${ANDROID_SDK_OS}_x86/tools
|
|
|
|
android-sdk-${ANDROID_SDK_OS}_86/tools
|
|
|
|
android-sdk/tools
|
|
|
|
PATHS /opt
|
|
|
|
"${HOME_ENV_PATH}/NVPACK"
|
|
|
|
"$ENV{SystemDrive}/NVPACK"
|
|
|
|
"${ProgramFiles_ENV_PATH}/Android"
|
|
|
|
DOC "Android SDK location"
|
2012-02-03 19:26:49 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
if(ANDROID_EXECUTABLE)
|
2012-03-26 03:57:58 +08:00
|
|
|
if(NOT ANDROID_SDK_DETECT_QUIET)
|
2012-04-30 22:33:52 +08:00
|
|
|
message(STATUS "Found android tool: ${ANDROID_EXECUTABLE}")
|
2012-03-26 03:57:58 +08:00
|
|
|
endif()
|
|
|
|
|
2012-02-03 19:26:49 +08:00
|
|
|
get_filename_component(ANDROID_SDK_TOOLS_PATH "${ANDROID_EXECUTABLE}" PATH)
|
|
|
|
|
|
|
|
#read source.properties
|
|
|
|
if(EXISTS "${ANDROID_SDK_TOOLS_PATH}/source.properties")
|
|
|
|
file(STRINGS "${ANDROID_SDK_TOOLS_PATH}/source.properties" ANDROID_SDK_TOOLS_SOURCE_PROPERTIES_LINES REGEX "^[ ]*[^#].*$")
|
|
|
|
foreach(line ${ANDROID_SDK_TOOLS_SOURCE_PROPERTIES_LINES})
|
|
|
|
string(REPLACE "\\:" ":" line ${line})
|
|
|
|
string(REPLACE "=" ";" line ${line})
|
|
|
|
list(GET line 0 line_name)
|
|
|
|
list(GET line 1 line_value)
|
|
|
|
string(REPLACE "." "_" line_name ${line_name})
|
2012-03-16 17:16:31 +08:00
|
|
|
SET(ANDROID_TOOLS_${line_name} "${line_value}" CACHE INTERNAL "from ${ANDROID_SDK_TOOLS_PATH}/source.properties")
|
2012-02-03 19:26:49 +08:00
|
|
|
MARK_AS_ADVANCED(ANDROID_TOOLS_${line_name})
|
|
|
|
endforeach()
|
|
|
|
endif()
|
|
|
|
|
2012-03-26 03:57:58 +08:00
|
|
|
#fix missing revision (SDK tools before r9 don't set revision number correctly)
|
2012-02-03 19:26:49 +08:00
|
|
|
if(NOT ANDROID_TOOLS_Pkg_Revision)
|
2012-03-16 17:16:31 +08:00
|
|
|
SET(ANDROID_TOOLS_Pkg_Revision "Unknown" CACHE INTERNAL "")
|
2012-02-03 19:26:49 +08:00
|
|
|
MARK_AS_ADVANCED(ANDROID_TOOLS_Pkg_Revision)
|
|
|
|
endif()
|
|
|
|
|
2012-03-26 03:57:58 +08:00
|
|
|
#fix missing description
|
2012-02-03 19:26:49 +08:00
|
|
|
if(NOT ANDROID_TOOLS_Pkg_Desc)
|
2012-03-16 17:16:31 +08:00
|
|
|
SET(ANDROID_TOOLS_Pkg_Desc "Android SDK Tools, revision ${ANDROID_TOOLS_Pkg_Revision}." CACHE INTERNAL "")
|
2012-02-03 19:26:49 +08:00
|
|
|
MARK_AS_ADVANCED(ANDROID_TOOLS_Pkg_Desc)
|
|
|
|
endif()
|
|
|
|
|
2012-03-26 03:57:58 +08:00
|
|
|
#warn about outdated SDK
|
|
|
|
if(NOT ANDROID_TOOLS_Pkg_Revision GREATER 13)
|
|
|
|
SET(ANDROID_TOOLS_Pkg_Desc "${ANDROID_TOOLS_Pkg_Desc} It is recommended to update your SDK tools to revision 14 or newer." CACHE INTERNAL "")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
if(ANDROID_TOOLS_Pkg_Revision GREATER 13)
|
|
|
|
SET(ANDROID_PROJECT_PROPERTIES_FILE project.properties)
|
|
|
|
SET(ANDROID_ANT_PROPERTIES_FILE ant.properties)
|
|
|
|
else()
|
|
|
|
SET(ANDROID_PROJECT_PROPERTIES_FILE default.properties)
|
|
|
|
SET(ANDROID_ANT_PROPERTIES_FILE build.properties)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
set(ANDROID_MANIFEST_FILE AndroidManifest.xml)
|
|
|
|
set(ANDROID_LIB_PROJECT_FILES build.xml local.properties proguard-project.txt ${ANDROID_PROJECT_PROPERTIES_FILE})
|
2012-03-28 00:05:52 +08:00
|
|
|
set(ANDROID_PROJECT_FILES ${ANDROID_LIB_PROJECT_FILES})
|
2012-03-26 03:57:58 +08:00
|
|
|
|
2012-02-03 19:26:49 +08:00
|
|
|
#get installed targets
|
2012-03-26 03:57:58 +08:00
|
|
|
if(ANDROID_TOOLS_Pkg_Revision GREATER 11)
|
|
|
|
execute_process(COMMAND ${ANDROID_EXECUTABLE} list target -c
|
|
|
|
RESULT_VARIABLE ANDROID_PROCESS
|
|
|
|
OUTPUT_VARIABLE ANDROID_SDK_TARGETS
|
|
|
|
ERROR_VARIABLE ANDROID_PROCESS_ERRORS
|
|
|
|
OUTPUT_STRIP_TRAILING_WHITESPACE
|
|
|
|
)
|
|
|
|
string(REGEX MATCHALL "[^\n]+" ANDROID_SDK_TARGETS "${ANDROID_SDK_TARGETS}")
|
|
|
|
else()
|
|
|
|
#old SDKs (r11 and older) don't provide compact list
|
|
|
|
execute_process(COMMAND ${ANDROID_EXECUTABLE} list target
|
|
|
|
RESULT_VARIABLE ANDROID_PROCESS
|
|
|
|
OUTPUT_VARIABLE ANDROID_SDK_TARGETS_FULL
|
|
|
|
ERROR_VARIABLE ANDROID_PROCESS_ERRORS
|
|
|
|
OUTPUT_STRIP_TRAILING_WHITESPACE
|
|
|
|
)
|
|
|
|
string(REGEX MATCHALL "(^|\n)id: [0-9]+ or \"([^\n]+[0-9+])\"(\n|$)" ANDROID_SDK_TARGETS_FULL "${ANDROID_SDK_TARGETS_FULL}")
|
|
|
|
|
|
|
|
SET(ANDROID_SDK_TARGETS "")
|
|
|
|
if(ANDROID_PROCESS EQUAL 0)
|
|
|
|
foreach(line ${ANDROID_SDK_TARGETS_FULL})
|
|
|
|
string(REGEX REPLACE "(^|\n)id: [0-9]+ or \"([^\n]+[0-9+])\"(\n|$)" "\\2" line "${line}")
|
|
|
|
list(APPEND ANDROID_SDK_TARGETS "${line}")
|
|
|
|
endforeach()
|
|
|
|
endif()
|
|
|
|
endif()
|
|
|
|
|
|
|
|
if(NOT ANDROID_PROCESS EQUAL 0)
|
|
|
|
message(ERROR "Failed to get list of installed Android targets.")
|
|
|
|
set(ANDROID_EXECUTABLE "ANDROID_EXECUTABLE-NOTFOUND")
|
2012-02-03 19:26:49 +08:00
|
|
|
endif()
|
|
|
|
|
2012-03-28 00:05:52 +08:00
|
|
|
# clear ANDROID_SDK_TARGET if no target is provided by user
|
2012-02-03 19:26:49 +08:00
|
|
|
if(NOT ANDROID_SDK_TARGET)
|
2012-03-28 00:05:52 +08:00
|
|
|
set(ANDROID_SDK_TARGET "" CACHE STRING "Android SDK target for the OpenCV Java API and samples")
|
|
|
|
endif()
|
|
|
|
if(ANDROID_SDK_TARGETS AND CMAKE_VERSION VERSION_GREATER "2.8")
|
|
|
|
set_property( CACHE ANDROID_SDK_TARGET PROPERTY STRINGS ${ANDROID_SDK_TARGETS} )
|
|
|
|
endif()
|
2012-02-03 19:26:49 +08:00
|
|
|
endif(ANDROID_EXECUTABLE)
|
2012-03-26 03:57:58 +08:00
|
|
|
|
|
|
|
# finds minimal installed SDK target compatible with provided names or API levels
|
|
|
|
# usage:
|
|
|
|
# get_compatible_android_api_level(VARIABLE [level1] [level2] ...)
|
|
|
|
macro(android_get_compatible_target VAR)
|
|
|
|
set(${VAR} "${VAR}-NOTFOUND")
|
|
|
|
if(ANDROID_SDK_TARGETS)
|
2012-04-14 05:50:59 +08:00
|
|
|
list(GET ANDROID_SDK_TARGETS 0 __lvl)
|
2012-03-26 03:57:58 +08:00
|
|
|
string(REGEX MATCH "[0-9]+$" __lvl "${__lvl}")
|
|
|
|
|
|
|
|
#find minimal level mathing to all provided levels
|
|
|
|
foreach(lvl ${ARGN})
|
|
|
|
string(REGEX MATCH "[0-9]+$" __level "${lvl}")
|
|
|
|
if(__level GREATER __lvl)
|
|
|
|
set(__lvl ${__level})
|
|
|
|
endif()
|
|
|
|
endforeach()
|
|
|
|
|
|
|
|
#search for compatible levels
|
|
|
|
foreach(lvl ${ANDROID_SDK_TARGETS})
|
|
|
|
string(REGEX MATCH "[0-9]+$" __level "${lvl}")
|
|
|
|
if(__level EQUAL __lvl)
|
|
|
|
#look for exact match
|
|
|
|
foreach(usrlvl ${ARGN})
|
|
|
|
if("${usrlvl}" STREQUAL "${lvl}")
|
|
|
|
set(${VAR} "${lvl}")
|
|
|
|
break()
|
|
|
|
endif()
|
|
|
|
endforeach()
|
|
|
|
if("${${VAR}}" STREQUAL "${lvl}")
|
|
|
|
break() #exact match was found
|
|
|
|
elseif(NOT ${VAR})
|
|
|
|
set(${VAR} "${lvl}")
|
|
|
|
endif()
|
|
|
|
elseif(__level GREATER __lvl)
|
|
|
|
if(NOT ${VAR})
|
|
|
|
set(${VAR} "${lvl}")
|
|
|
|
endif()
|
|
|
|
break()
|
|
|
|
endif()
|
|
|
|
endforeach()
|
|
|
|
|
|
|
|
unset(__lvl)
|
|
|
|
unset(__level)
|
|
|
|
endif()
|
|
|
|
endmacro()
|
2012-03-31 20:26:03 +08:00
|
|
|
|
|
|
|
#add_android_project(target_name ${path} NATIVE_DEPS opencv_core LIBRARY_DEPS ${OpenCV_BINARY_DIR} SDK_TARGET 11)
|
|
|
|
macro(add_android_project target path)
|
|
|
|
# parse arguments
|
2012-06-21 22:50:05 +08:00
|
|
|
set(android_proj_arglist NATIVE_DEPS LIBRARY_DEPS SDK_TARGET IGNORE_JAVA)
|
2012-03-31 20:26:03 +08:00
|
|
|
set(__varname "android_proj_")
|
|
|
|
foreach(v ${android_proj_arglist})
|
|
|
|
set(${__varname}${v} "")
|
|
|
|
endforeach()
|
|
|
|
foreach(arg ${ARGN})
|
|
|
|
set(__var "${__varname}")
|
|
|
|
foreach(v ${android_proj_arglist})
|
|
|
|
if("${v}" STREQUAL "${arg}")
|
|
|
|
set(__varname "android_proj_${v}")
|
|
|
|
break()
|
|
|
|
endif()
|
|
|
|
endforeach()
|
|
|
|
if(__var STREQUAL __varname)
|
|
|
|
list(APPEND ${__var} "${arg}")
|
|
|
|
endif()
|
|
|
|
endforeach()
|
|
|
|
|
|
|
|
# get compatible SDK target
|
|
|
|
android_get_compatible_target(android_proj_sdk_target ${ANDROID_NATIVE_API_LEVEL} ${android_proj_SDK_TARGET})
|
|
|
|
|
|
|
|
if(NOT android_proj_sdk_target)
|
|
|
|
message(WARNING "Can not find any SDK target compatible with: ${ANDROID_NATIVE_API_LEVEL} ${android_proj_SDK_TARGET}
|
|
|
|
The project ${target} will not be build")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
# check native dependencies
|
2012-06-21 22:50:05 +08:00
|
|
|
if(android_proj_IGNORE_JAVA)
|
|
|
|
ocv_check_dependencies(${android_proj_NATIVE_DEPS})
|
|
|
|
else()
|
|
|
|
ocv_check_dependencies(${android_proj_NATIVE_DEPS} opencv_java)
|
|
|
|
endif()
|
2012-03-31 20:26:03 +08:00
|
|
|
|
|
|
|
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}")
|
|
|
|
|
|
|
|
project(${target})
|
|
|
|
set(android_proj_bin_dir "${CMAKE_CURRENT_BINARY_DIR}/.build")
|
2012-06-26 16:48:45 +08:00
|
|
|
|
2012-03-31 20:26:03 +08:00
|
|
|
# get project sources
|
|
|
|
file(GLOB_RECURSE android_proj_files RELATIVE "${path}" "${path}/res/*" "${path}/src/*")
|
|
|
|
ocv_list_filterout(android_proj_files ".svn")
|
|
|
|
|
|
|
|
# copy sources out from the build tree
|
|
|
|
set(android_proj_file_deps "")
|
|
|
|
foreach(f ${android_proj_files} ${ANDROID_MANIFEST_FILE})
|
|
|
|
add_custom_command(
|
|
|
|
OUTPUT "${android_proj_bin_dir}/${f}"
|
|
|
|
COMMAND ${CMAKE_COMMAND} -E copy "${path}/${f}" "${android_proj_bin_dir}/${f}"
|
|
|
|
MAIN_DEPENDENCY "${path}/${f}"
|
|
|
|
COMMENT "Copying ${f}")
|
|
|
|
list(APPEND android_proj_file_deps "${path}/${f}" "${android_proj_bin_dir}/${f}")
|
|
|
|
endforeach()
|
|
|
|
|
|
|
|
set(android_proj_lib_deps_commands "")
|
|
|
|
set(android_proj_target_files ${ANDROID_PROJECT_FILES})
|
|
|
|
ocv_list_add_prefix(android_proj_target_files "${android_proj_bin_dir}/")
|
|
|
|
|
|
|
|
# process Android library dependencies
|
|
|
|
foreach(dep ${android_proj_LIBRARY_DEPS})
|
|
|
|
file(RELATIVE_PATH __dep "${android_proj_bin_dir}" "${dep}")
|
|
|
|
list(APPEND android_proj_lib_deps_commands
|
|
|
|
COMMAND ${ANDROID_EXECUTABLE} --silent update project --path "${android_proj_bin_dir}" --library "${__dep}")
|
|
|
|
endforeach()
|
|
|
|
|
|
|
|
# fix Android project
|
|
|
|
add_custom_command(
|
|
|
|
OUTPUT ${android_proj_target_files}
|
|
|
|
COMMAND ${CMAKE_COMMAND} -E remove ${android_proj_target_files}
|
|
|
|
COMMAND ${ANDROID_EXECUTABLE} --silent update project --path "${android_proj_bin_dir}" --target "${android_proj_sdk_target}" --name "${target}"
|
|
|
|
${android_proj_lib_deps_commands}
|
|
|
|
MAIN_DEPENDENCY "${android_proj_bin_dir}/${ANDROID_MANIFEST_FILE}"
|
|
|
|
DEPENDS "${path}/${ANDROID_MANIFEST_FILE}"
|
|
|
|
COMMENT "Updating Android project at ${path}. SDK target: ${android_proj_sdk_target}"
|
|
|
|
)
|
|
|
|
|
|
|
|
list(APPEND android_proj_file_deps ${android_proj_target_files})
|
|
|
|
|
|
|
|
# build native part
|
|
|
|
file(GLOB_RECURSE android_proj_jni_files "${path}/jni/*.c" "${path}/jni/*.h" "${path}/jni/*.cpp" "${path}/jni/*.hpp")
|
|
|
|
ocv_list_filterout(android_proj_jni_files ".svn")
|
|
|
|
|
2012-06-21 22:50:05 +08:00
|
|
|
if(android_proj_jni_files AND EXISTS ${path}/jni/Android.mk AND NOT DEFINED JNI_LIB_NAME)
|
2012-03-31 20:26:03 +08:00
|
|
|
file(STRINGS "${path}/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}")
|
|
|
|
|
|
|
|
if(JNI_LIB_NAME)
|
|
|
|
ocv_include_modules_recurse(${android_proj_NATIVE_DEPS})
|
|
|
|
ocv_include_directories("${path}/jni")
|
|
|
|
|
|
|
|
add_library(${JNI_LIB_NAME} MODULE ${android_proj_jni_files})
|
|
|
|
target_link_libraries(${JNI_LIB_NAME} ${OPENCV_LINKER_LIBS} ${android_proj_NATIVE_DEPS})
|
|
|
|
|
|
|
|
set_target_properties(${JNI_LIB_NAME} PROPERTIES
|
|
|
|
OUTPUT_NAME "${JNI_LIB_NAME}"
|
|
|
|
LIBRARY_OUTPUT_DIRECTORY "${android_proj_bin_dir}/libs/${ANDROID_NDK_ABI_NAME}"
|
|
|
|
)
|
|
|
|
|
|
|
|
get_target_property(android_proj_jni_location "${JNI_LIB_NAME}" LOCATION)
|
|
|
|
add_custom_command(TARGET ${JNI_LIB_NAME} POST_BUILD COMMAND ${CMAKE_STRIP} --strip-unneeded "${android_proj_jni_location}")
|
|
|
|
endif()
|
|
|
|
endif()
|
|
|
|
|
|
|
|
# build java part
|
2012-06-21 22:50:05 +08:00
|
|
|
if(android_proj_IGNORE_JAVA)
|
|
|
|
add_custom_command(
|
|
|
|
OUTPUT "${android_proj_bin_dir}/bin/${target}-debug.apk"
|
|
|
|
COMMAND ${ANT_EXECUTABLE} -q -noinput -k debug
|
|
|
|
COMMAND ${CMAKE_COMMAND} -E touch "${android_proj_bin_dir}/bin/${target}-debug.apk" # needed because ant does not update the timestamp of updated apk
|
|
|
|
WORKING_DIRECTORY "${android_proj_bin_dir}"
|
|
|
|
MAIN_DEPENDENCY "${android_proj_bin_dir}/${ANDROID_MANIFEST_FILE}"
|
|
|
|
DEPENDS ${android_proj_file_deps} ${JNI_LIB_NAME})
|
|
|
|
else()
|
2012-03-31 20:26:03 +08:00
|
|
|
add_custom_command(
|
|
|
|
OUTPUT "${android_proj_bin_dir}/bin/${target}-debug.apk"
|
|
|
|
COMMAND ${ANT_EXECUTABLE} -q -noinput -k debug
|
|
|
|
COMMAND ${CMAKE_COMMAND} -E touch "${android_proj_bin_dir}/bin/${target}-debug.apk" # needed because ant does not update the timestamp of updated apk
|
|
|
|
WORKING_DIRECTORY "${android_proj_bin_dir}"
|
|
|
|
MAIN_DEPENDENCY "${android_proj_bin_dir}/${ANDROID_MANIFEST_FILE}"
|
|
|
|
DEPENDS "${OpenCV_BINARY_DIR}/bin/classes.jar" opencv_java # as we are part of OpenCV we can just force this dependency
|
|
|
|
DEPENDS ${android_proj_file_deps} ${JNI_LIB_NAME})
|
2012-06-21 22:50:05 +08:00
|
|
|
endif()
|
2012-03-31 20:26:03 +08:00
|
|
|
|
2012-06-26 16:48:45 +08:00
|
|
|
unset(JNI_LIB_NAME)
|
|
|
|
|
2012-03-31 20:26:03 +08:00
|
|
|
add_custom_target(${target} ALL SOURCES "${android_proj_bin_dir}/bin/${target}-debug.apk" )
|
2012-06-21 22:50:05 +08:00
|
|
|
if(NOT android_proj_IGNORE_JAVA)
|
|
|
|
add_dependencies(${target} opencv_java)
|
|
|
|
endif()
|
|
|
|
if(android_proj_native_deps)
|
|
|
|
add_dependencies(${target} ${android_proj_native_deps})
|
|
|
|
endif()
|
2012-03-31 20:26:03 +08:00
|
|
|
|
|
|
|
# put the final .apk to the OpenCV's bin folder
|
|
|
|
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")
|
2012-04-30 22:33:52 +08:00
|
|
|
if(INSTALL_ANDROID_EXAMPLES AND "${target}" MATCHES "^example-")
|
2012-03-31 20:26:03 +08:00
|
|
|
install(FILES "${OpenCV_BINARY_DIR}/bin/${target}.apk" DESTINATION "bin" COMPONENT main)
|
|
|
|
endif()
|
|
|
|
endif()
|
|
|
|
endmacro()
|