From 9b2d7e19db082cd90db33ed78e15e9c184a04703 Mon Sep 17 00:00:00 2001 From: Andrey Kamaev Date: Fri, 23 Mar 2012 17:03:05 +0000 Subject: [PATCH] Added "fat" mode for Java wrapper --- CMakeLists.txt | 1 + cmake/OpenCVAndroidProject.cmake | 30 +++++----- cmake/OpenCVGenAndroidMK.cmake | 13 +++++ cmake/templates/OpenCV.mk.in | 57 ++++++++++++++----- modules/java/CMakeLists.txt | 18 +++++- .../android/tutorial-3-native/jni/Android.mk | 3 + .../opencv/samples/tutorial3/Sample3View.java | 4 ++ .../android/tutorial-4-mixed/jni/Android.mk | 1 + .../samples/tutorial4/SampleViewBase.java | 4 ++ 9 files changed, 101 insertions(+), 30 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9108d70b4..cf1d8f490 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -147,6 +147,7 @@ OCV_OPTION(BUILD_PERF_TESTS "Build performance tests" OCV_OPTION(BUILD_TESTS "Build accuracy & regression tests" ON IF (NOT IOS) ) OCV_OPTION(BUILD_WITH_DEBUG_INFO "Include debug info into debug libs (not MSCV only)" ON ) OCV_OPTION(BUILD_WITH_STATIC_CRT "Enables use of staticaly linked CRT for staticaly linked OpenCV" ON IF MSVC ) +OCV_OPTION(BUILD_FAT_JAVA_LIB "Create fat java wrapper containing the whole OpenCV library" ON IF ANDROID AND NOT BUILD_SHARED_LIBS AND CMAKE_COMPILER_IS_GNUCXX ) # 3rd party libs OCV_OPTION(BUILD_ZLIB "Build zlib from source" WIN32 OR IOS ) diff --git a/cmake/OpenCVAndroidProject.cmake b/cmake/OpenCVAndroidProject.cmake index 59c547db9..e055ecb71 100644 --- a/cmake/OpenCVAndroidProject.cmake +++ b/cmake/OpenCVAndroidProject.cmake @@ -1,8 +1,12 @@ # creates target "${_target}_android_project" for building standard Android project macro(add_android_project _target _path) - SET (android_dependencies opencv_contrib opencv_legacy opencv_objdetect opencv_calib3d opencv_features2d opencv_video opencv_highgui opencv_ml opencv_imgproc opencv_flann opencv_core) + SET(android_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) + if(BUILD_FAT_JAVA_LIB) + SET(android_dependencies opencv_java) + else() LIST(APPEND android_dependencies opencv_androidcamera) + endif() endif() if (ANDROID AND CAN_BUILD_ANDROID_PROJECTS) @@ -49,32 +53,32 @@ macro(add_android_project _target _path) list(APPEND ${_target}_project_files "${build_path}/${f}") endforeach() - # process default.properties - file(STRINGS "${_path}/default.properties" default_properties REGEX "^android\\.library\\.reference\\.1=.+$") - if (default_properties) + # process project.properties + file(STRINGS "${_path}/project.properties" project_properties REGEX "^android\\.library\\.reference\\.1=.+$") + if (project_properties) # has opencv dependency file(RELATIVE_PATH OPENCV_REFERENCE_PATH "${build_path}" "${CMAKE_BINARY_DIR}") add_custom_command( - OUTPUT "${build_path}/default.properties" + OUTPUT "${build_path}/project.properties" OUTPUT "${build_path}/build.xml" OUTPUT "${build_path}/local.properties" OUTPUT "${build_path}/proguard.cfg" - COMMAND ${CMAKE_COMMAND} -E echo "" > "default.properties" + COMMAND ${CMAKE_COMMAND} -E echo "" > "project.properties" COMMAND ${ANDROID_EXECUTABLE} update project --name "${_target}" --target "${ANDROID_SDK_TARGET}" --library "${OPENCV_REFERENCE_PATH}" --path . WORKING_DIRECTORY ${build_path} DEPENDS ${${_target}_project_files} - DEPENDS "${CMAKE_BINARY_DIR}/default.properties" + DEPENDS "${CMAKE_BINARY_DIR}/project.properties" DEPENDS "${CMAKE_BINARY_DIR}/AndroidManifest.xml" COMMENT "Updating android project - ${_target}" ) else() # has no opencv dependency add_custom_command( - OUTPUT "${build_path}/default.properties" + OUTPUT "${build_path}/project.properties" OUTPUT "${build_path}/build.xml" OUTPUT "${build_path}/local.properties" OUTPUT "${build_path}/proguard.cfg" - COMMAND ${CMAKE_COMMAND} -E echo "" > "default.properties" + COMMAND ${CMAKE_COMMAND} -E echo "" > "project.properties" COMMAND ${ANDROID_EXECUTABLE} update project --name "${_target}" --target "${ANDROID_SDK_TARGET}" --path . WORKING_DIRECTORY ${build_path} DEPENDS ${${_target}_project_files} @@ -83,11 +87,11 @@ macro(add_android_project _target _path) endif() if("${build_path}" STREQUAL "${_path}") - #in case of in-source build default.properties file is not generated (it is just overwritten :) - SET_SOURCE_FILES_PROPERTIES("${build_path}/default.properties" PROPERTIES GENERATED FALSE) + #in case of in-source build project.properties file is not generated (it is just overwritten :) + SET_SOURCE_FILES_PROPERTIES("${build_path}/project.properties" PROPERTIES GENERATED FALSE) endif() - list(APPEND ${_target}_project_files "${build_path}/default.properties" "${build_path}/build.xml" "${build_path}/local.properties" "${build_path}/proguard.cfg") + list(APPEND ${_target}_project_files "${build_path}/project.properties" "${build_path}/build.xml" "${build_path}/local.properties" "${build_path}/proguard.cfg") # build native part of android project if(jni_files) @@ -112,7 +116,7 @@ macro(add_android_project _target _path) ADD_CUSTOM_COMMAND( TARGET ${JNI_LIB_NAME} POST_BUILD - COMMAND ${CMAKE_STRIP} "${build_path}/libs/${ANDROID_NDK_ABI_NAME}/*.so" + COMMAND ${CMAKE_STRIP} --strip-unneeded "${build_path}/libs/${ANDROID_NDK_ABI_NAME}/*.so" ) else() SET(JNI_LIB_NAME) diff --git a/cmake/OpenCVGenAndroidMK.cmake b/cmake/OpenCVGenAndroidMK.cmake index 61efd420a..0056d86c0 100644 --- a/cmake/OpenCVGenAndroidMK.cmake +++ b/cmake/OpenCVGenAndroidMK.cmake @@ -12,6 +12,13 @@ if(ANDROID) set(OPENCV_LIBTYPE_CONFIGMAKE "STATIC") endif() + if(BUILD_FAT_JAVA_LIB) + set(OPENCV_LIBTYPE_CONFIGMAKE "SHARED") + set(OPENCV_STATIC_LIBTYPE_CONFIGMAKE "STATIC") + else() + set(OPENCV_STATIC_LIBTYPE_CONFIGMAKE ${OPENCV_LIBTYPE_CONFIGMAKE}) + endif() + # setup lists of camera libs foreach(abi ARMEABI ARMEABI_V7A X86) ANDROID_GET_ABI_RAWNAME(${abi} ndkabi) @@ -64,6 +71,12 @@ if(ANDROID) endforeach() string(REPLACE "opencv_" "" OPENCV_MODULES_CONFIGMAKE "${OPENCV_MODULES_CONFIGMAKE}") + if(BUILD_FAT_JAVA_LIB) + set(OPENCV_LIBS_CONFIGMAKE java) + else() + set(OPENCV_LIBS_CONFIGMAKE "${OPENCV_MODULES_CONFIGMAKE}") + endif() + # ------------------------------------------------------------------------------------------- # Part 1/2: ${BIN_DIR}/OpenCV.mk -> For use *without* "make install" # ------------------------------------------------------------------------------------------- diff --git a/cmake/templates/OpenCV.mk.in b/cmake/templates/OpenCV.mk.in index ae5087b3c..1120f206e 100644 --- a/cmake/templates/OpenCV.mk.in +++ b/cmake/templates/OpenCV.mk.in @@ -5,14 +5,31 @@ USER_LOCAL_PATH:=$(LOCAL_PATH) LOCAL_PATH:=$(subst ?,,$(firstword ?$(subst \, ,$(subst /, ,$(call my-dir))))) OPENCV_THIS_DIR:=$(patsubst $(LOCAL_PATH)\\%,%,$(patsubst $(LOCAL_PATH)/%,%,$(call my-dir))) +OPENCV_MK_DIR:=$(dir $(lastword $(MAKEFILE_LIST))) OPENCV_LIBS_DIR:=@OPENCV_LIBS_DIR_CONFIGCMAKE@ OPENCV_BASEDIR:=@OPENCV_BASE_INCLUDE_DIR_CONFIGCMAKE@ OPENCV_LOCAL_C_INCLUDES:=@OPENCV_INCLUDE_DIRS_CONFIGCMAKE@ - -OPENCV_LIB_TYPE:=@OPENCV_LIBTYPE_CONFIGMAKE@ OPENCV_MODULES:=@OPENCV_MODULES_CONFIGMAKE@ -OPENCV_3RDPARTY_COMPONENTS:=@OPENCV_3RDPARTY_COMPONENTS_CONFIGMAKE@ -OPENCV_EXTRA_COMPONENTS:=@OPENCV_EXTRA_COMPONENTS_CONFIGMAKE@ + +ifeq ($(OPENCV_LIB_TYPE),) + OPENCV_LIB_TYPE:=@OPENCV_LIBTYPE_CONFIGMAKE@ +endif + +ifeq ($(OPENCV_LIB_TYPE),SHARED) + OPENCV_LIBS:=@OPENCV_LIBS_CONFIGMAKE@ + OPENCV_LIB_TYPE:=@OPENCV_LIBTYPE_CONFIGMAKE@ +else + OPENCV_LIBS:=$(OPENCV_MODULES) + OPENCV_LIB_TYPE:=@OPENCV_STATIC_LIBTYPE_CONFIGMAKE@ +endif + +ifeq ($(OPENCV_LIB_TYPE),SHARED) + OPENCV_3RDPARTY_COMPONENTS:= + OPENCV_EXTRA_COMPONENTS:= +else + OPENCV_3RDPARTY_COMPONENTS:=@OPENCV_3RDPARTY_COMPONENTS_CONFIGMAKE@ + OPENCV_EXTRA_COMPONENTS:=@OPENCV_EXTRA_COMPONENTS_CONFIGMAKE@ +endif ifeq (${OPENCV_CAMERA_MODULES},off) OPENCV_CAMERA_MODULES:= @@ -32,13 +49,14 @@ ifeq ($(OPENCV_LIB_TYPE),SHARED) OPENCV_LIB_SUFFIX:=so else OPENCV_LIB_SUFFIX:=a + OPENCV_INSTALL_MODULES:=on endif define add_opencv_module include $(CLEAR_VARS) LOCAL_MODULE:=opencv_$1 LOCAL_SRC_FILES:=$(OPENCV_LIBS_DIR)/libs/$(TARGET_ARCH_ABI)/libopencv_$1.$(OPENCV_LIB_SUFFIX) - include $(PREBUILT_@OPENCV_LIBTYPE_CONFIGMAKE@_LIBRARY) + include $(PREBUILT_$(OPENCV_LIB_TYPE)_LIBRARY) endef define add_opencv_3rdparty_component @@ -55,7 +73,9 @@ define add_opencv_camera_module include $(PREBUILT_SHARED_LIBRARY) endef -$(foreach module,$(OPENCV_MODULES),$(eval $(call add_opencv_module,$(module)))) +ifeq ($(OPENCV_INSTALL_MODULES),on) +$(foreach module,$(OPENCV_LIBS),$(eval $(call add_opencv_module,$(module)))) +endif $(foreach module,$(OPENCV_3RDPARTY_COMPONENTS),$(eval $(call add_opencv_3rdparty_component,$(module)))) $(foreach module,$(OPENCV_CAMERA_MODULES),$(eval $(call add_opencv_camera_module,$(module)))) @@ -63,18 +83,25 @@ ifneq ($(OPENCV_BASEDIR),) OPENCV_LOCAL_C_INCLUDES += $(foreach mod, $(OPENCV_MODULES), $(OPENCV_BASEDIR)/modules/$(mod)/include) endif -ifeq ($(OPENCV_LIB_TYPE),STATIC) - OPENCV_LOCAL_LIBRARIES += $(foreach mod, $(OPENCV_MODULES), opencv_$(mod)) +ifeq ($(OPENCV_LOCAL_CFLAGS),) + OPENCV_LOCAL_CFLAGS := -fPIC -DANDROID -fsigned-char endif -OPENCV_LOCAL_LIBRARIES += $(OPENCV_EXTRA_COMPONENTS) -OPENCV_LOCAL_CFLAGS := -fPIC -DANDROID -fsigned-char - include $(CLEAR_VARS) -LOCAL_C_INCLUDES += $(OPENCV_LOCAL_C_INCLUDES) -LOCAL_STATIC_LIBRARIES += $(OPENCV_LOCAL_LIBRARIES) -LOCAL_CFLAGS += $(OPENCV_LOCAL_CFLAGS) -LOCAL_LDLIBS += $(foreach lib,$(OPENCV_EXTRA_COMPONENTS), -l$(lib)) +LOCAL_C_INCLUDES += $(OPENCV_LOCAL_C_INCLUDES) +LOCAL_CFLAGS += $(OPENCV_LOCAL_CFLAGS) + +ifeq ($(OPENCV_INSTALL_MODULES),on) + LOCAL_$(OPENCV_LIB_TYPE)_LIBRARIES += $(foreach mod, $(OPENCV_LIBS), opencv_$(mod)) +else + LOCAL_LDLIBS += -L$(call host-path,$(LOCAL_PATH)/$(OPENCV_LIBS_DIR)/libs/$(TARGET_ARCH_ABI)) $(foreach lib, $(OPENCV_LIBS), -lopencv_$(lib)) +endif + +ifeq ($(OPENCV_LIB_TYPE),STATIC) + LOCAL_STATIC_LIBRARIES += $(OPENCV_3RDPARTY_COMPONENTS) +endif + +LOCAL_LDLIBS += $(foreach lib,$(OPENCV_EXTRA_COMPONENTS), -l$(lib)) #restore the LOCAL_PATH LOCAL_PATH:=$(USER_LOCAL_PATH) diff --git a/modules/java/CMakeLists.txt b/modules/java/CMakeLists.txt index a6b92310b..1054095a4 100644 --- a/modules/java/CMakeLists.txt +++ b/modules/java/CMakeLists.txt @@ -160,7 +160,20 @@ add_custom_target(${api_target} DEPENDS ${java_files}) # add opencv_java library add_library(${target} SHARED ${handwrittren_h_sources} ${handwrittren_cpp_sources} ${generated_cpp_sources}) -target_link_libraries(${target} ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_LINKER_LIBS}) +if(BUILD_FAT_JAVA_LIB) + set(__deps ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULES_BUILD}) + list(REMOVE_ITEM __deps ${the_module}) + ocv_list_unique(__deps) + set(__extradeps ${__deps}) + ocv_list_filterout(__extradeps "^opencv_") + if(__extradeps) + list(REMOVE_ITEM __deps ${__extradeps}) + endif() + + target_link_libraries(${target} -Wl,-whole-archive ${__deps} -Wl,-no-whole-archive ${__extradeps} ${OPENCV_LINKER_LIBS}) +else() + target_link_libraries(${target} ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_LINKER_LIBS}) +endif() add_dependencies(${target} ${api_target}) # Additional target properties @@ -169,6 +182,7 @@ set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH} RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH} INSTALL_NAME_DIR ${OPENCV_LIB_INSTALL_PATH} + LINK_INTERFACE_LIBRARIES "" ) install(TARGETS ${target} LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT main) @@ -181,7 +195,7 @@ if(ANDROID) add_custom_command( TARGET ${target} POST_BUILD - COMMAND ${CMAKE_STRIP} "${LIBRARY_OUTPUT_PATH}/lib${target}.so" + COMMAND ${CMAKE_STRIP} --strip-unneeded "${LIBRARY_OUTPUT_PATH}/lib${target}.so" ) set(lib_proj_files "") diff --git a/samples/android/tutorial-3-native/jni/Android.mk b/samples/android/tutorial-3-native/jni/Android.mk index a9e302b47..7429d65da 100644 --- a/samples/android/tutorial-3-native/jni/Android.mk +++ b/samples/android/tutorial-3-native/jni/Android.mk @@ -2,6 +2,9 @@ LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) +OPENCV_LIB_TYPE:=STATIC +OPENCV_INSTALL_MODULES:=on + include ../includeOpenCV.mk ifeq ("$(wildcard $(OPENCV_MK_PATH))","") #try to load OpenCV.mk from default install location diff --git a/samples/android/tutorial-3-native/src/org/opencv/samples/tutorial3/Sample3View.java b/samples/android/tutorial-3-native/src/org/opencv/samples/tutorial3/Sample3View.java index 8076bd5ca..f940f9851 100644 --- a/samples/android/tutorial-3-native/src/org/opencv/samples/tutorial3/Sample3View.java +++ b/samples/android/tutorial-3-native/src/org/opencv/samples/tutorial3/Sample3View.java @@ -24,6 +24,10 @@ class Sample3View extends SampleViewBase { public native void FindFeatures(int width, int height, byte yuv[], int[] rgba); static { + try { + System.loadLibrary("opencv_java"); + } catch(Exception e) { + } System.loadLibrary("native_sample"); } } diff --git a/samples/android/tutorial-4-mixed/jni/Android.mk b/samples/android/tutorial-4-mixed/jni/Android.mk index cb230ac36..a20c24693 100644 --- a/samples/android/tutorial-4-mixed/jni/Android.mk +++ b/samples/android/tutorial-4-mixed/jni/Android.mk @@ -3,6 +3,7 @@ LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) OPENCV_CAMERA_MODULES:=off + include ../includeOpenCV.mk ifeq ("$(wildcard $(OPENCV_MK_PATH))","") #try to load OpenCV.mk from default install location diff --git a/samples/android/tutorial-4-mixed/src/org/opencv/samples/tutorial4/SampleViewBase.java b/samples/android/tutorial-4-mixed/src/org/opencv/samples/tutorial4/SampleViewBase.java index 634ef253f..d464ba967 100644 --- a/samples/android/tutorial-4-mixed/src/org/opencv/samples/tutorial4/SampleViewBase.java +++ b/samples/android/tutorial-4-mixed/src/org/opencv/samples/tutorial4/SampleViewBase.java @@ -128,4 +128,8 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde } } } + + static { + System.loadLibrary("opencv_java"); + } } \ No newline at end of file