# ----------------------------------------------------------------------------
# CMake file for java support
# ----------------------------------------------------------------------------
if ( NOT ANDROID OR NOT PYTHON_EXECUTABLE OR ANDROID_NATIVE_API_LEVEL LESS 8 )
ocv_module_disable ( java )
endif ( )
set ( the_description "The java bindings" )
ocv_add_module ( java BINDINGS opencv_core opencv_imgproc OPTIONAL opencv_objdetect opencv_features2d opencv_video opencv_highgui opencv_ml opencv_calib3d opencv_photo opencv_nonfree )
ocv_module_include_directories ( "${CMAKE_CURRENT_SOURCE_DIR}/generator/src/cpp" )
# get list of modules to wrap
string ( REPLACE "opencv_" "" OPENCV_JAVA_MODULES "${OPENCV_MODULE_${the_module}_REQ_DEPS};${OPENCV_MODULE_${the_module}_OPT_DEPS}" )
foreach ( module ${ OPENCV_JAVA_MODULES } )
if ( NOT HAVE_opencv_ ${ module } )
list ( REMOVE_ITEM OPENCV_JAVA_MODULES ${ module } )
endif ( )
endforeach ( )
set ( GEN_JAVA "${CMAKE_CURRENT_SOURCE_DIR}/generator/gen_java.py" )
set ( HDR_PARSER "${CMAKE_CURRENT_SOURCE_DIR}/../python/src2/hdr_parser.py" )
set ( GEN_JAVADOC "${CMAKE_CURRENT_SOURCE_DIR}/generator/gen_javadoc.py" )
set ( RST_PARSER "${CMAKE_CURRENT_SOURCE_DIR}/generator/rst_parser.py" )
# add dependencies to cmake (we should rerun cmake if any of these scripts is modified)
configure_file ( "${GEN_JAVA}" "${OpenCV_BINARY_DIR}/junk/gen_java.junk" COPYONLY )
configure_file ( "${HDR_PARSER}" "${OpenCV_BINARY_DIR}/junk/hdr_parser.junk" COPYONLY )
set ( java_hdr_deps "" )
set ( generated_cpp_sources "" )
set ( generated_java_sources "" )
# setup raw java and cpp files generation (without javadoc and at temporary location)
foreach ( module ${ OPENCV_JAVA_MODULES } )
# get list of module headers
if ( EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/generator/config/${module}.filelist" )
file ( STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/generator/config/${module}.filelist" module_headers )
ocv_list_add_prefix ( module_headers "${OPENCV_MODULE_opencv_${module}_LOCATION}/" )
else ( )
set ( module_headers "${OPENCV_MODULE_opencv_${module}_HEADERS}" )
endif ( )
# C headers must go first
set ( module_headers_cpp ${ module_headers } )
ocv_list_filterout ( module_headers_cpp "\\\\.h$" )
if ( module_headers_cpp )
list ( REMOVE_ITEM module_headers ${ module_headers_cpp } )
list ( APPEND module_headers ${ module_headers_cpp } )
endif ( )
unset ( module_headers_cpp )
# add dependencies to cmake (we should rerun cmake if any of these headers is modified)
foreach ( header ${ module_headers } )
get_filename_component ( header_name "${header}" NAME_WE )
configure_file ( "${header}" "${OpenCV_BINARY_DIR}/junk/${header_name}.junk" COPYONLY )
endforeach ( )
# first run (to get list of generated files)
if ( module_headers )
file ( REMOVE_RECURSE "${CMAKE_CURRENT_BINARY_DIR}/gen_java_out/" )
file ( MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/gen_java_out" )
execute_process ( COMMAND ${ PYTHON_EXECUTABLE } "${GEN_JAVA}" "${HDR_PARSER}" ${ module } ${ module_headers }
W O R K I N G _ D I R E C T O R Y " $ { C M A K E _ C U R R E N T _ B I N A R Y _ D I R } / g e n _ j a v a _ o u t "
O U T P U T _ Q U I E T E R R O R _ Q U I E T )
file ( GLOB_RECURSE ${ module } _generated_java_sources RELATIVE "${CMAKE_CURRENT_BINARY_DIR}/gen_java_out/" "${CMAKE_CURRENT_BINARY_DIR}/gen_java_out/*.java" )
ocv_list_add_prefix ( ${ module } _generated_java_sources "${CMAKE_CURRENT_BINARY_DIR}/" )
# second run (at build time)
add_custom_command ( OUTPUT ${ ${module } _generated_java_sources} "${CMAKE_CURRENT_BINARY_DIR}/${module}.cpp"
C O M M A N D $ { P Y T H O N _ E X E C U T A B L E } " $ { G E N _ J A V A } " " $ { H D R _ P A R S E R } " $ { m o d u l e } $ { m o d u l e _ h e a d e r s }
W O R K I N G _ D I R E C T O R Y $ { C M A K E _ C U R R E N T _ B I N A R Y _ D I R }
D E P E N D S " $ { G E N _ J A V A } " " $ { H D R _ P A R S E R } " $ { m o d u l e _ h e a d e r s } )
list ( APPEND java_hdr_deps ${ module_headers } )
list ( APPEND generated_cpp_sources "${CMAKE_CURRENT_BINARY_DIR}/${module}.cpp" )
list ( APPEND generated_java_sources ${ ${module } _generated_java_sources} )
endif ( )
endforeach ( )
# get handwritten files used for wrappers generation
file ( GLOB handwrittren_h_sources "${CMAKE_CURRENT_SOURCE_DIR}/generator/src/cpp/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/generator/src/cpp/*.hpp" )
file ( GLOB handwrittren_cpp_sources "${CMAKE_CURRENT_SOURCE_DIR}/generator/src/cpp/*.cpp" )
file ( GLOB handwrittren_java_sources "${CMAKE_CURRENT_SOURCE_DIR}/generator/src/java/*.java" )
file ( GLOB handwrittren_aidl_sources "${CMAKE_CURRENT_SOURCE_DIR}/generator/src/java/*.aidl" )
# remove handwritten java files for disabled modules
foreach ( jfile ${ handwrittren_java_sources } )
string ( REGEX REPLACE "^.*/([^+]+)\\+.*\\.java$" "\\1" jmodname "${jfile}" )
if ( DEFINED HAVE_opencv_ ${ jmodname } AND NOT HAVE_opencv_ ${ jmodname } )
list ( REMOVE_ITEM handwrittren_java_sources "${jfile}" )
endif ( )
endforeach ( )
# remove VideoCapture wrapper if highgui is disabled
if ( NOT HAVE_opencv_highgui )
list ( REMOVE_ITEM handwrittren_cpp_sources "${CMAKE_CURRENT_SOURCE_DIR}/generator/src/cpp/VideoCapture.cpp" )
endif ( )
# create list of javadoc documented files
unset ( documented_java_files )
foreach ( java_file ${ handwrittren_java_sources } ${ generated_java_sources } )
get_filename_component ( java_file_name "${java_file}" NAME_WE )
list ( APPEND documented_java_files "${CMAKE_CURRENT_BINARY_DIR}/${java_file_name}-jdoc.java" )
endforeach ( )
# generate javadoc files
file ( GLOB_RECURSE refman_rst_headers "${CMAKE_CURRENT_SOURCE_DIR}/../*.rst" )
set ( java_documented_headers_deps ${ handwrittren_java_sources } ${ generated_java_sources } ${ java_hdr_deps } ${ refman_rst_headers }
" $ { G E N _ J A V A D O C } " " $ { R S T _ P A R S E R } " " $ { G E N _ J A V A } " " $ { H D R _ P A R S E R } " )
#TODO: pass list of modules
add_custom_command (
O U T P U T $ { d o c u m e n t e d _ j a v a _ f i l e s }
C O M M A N D $ { P Y T H O N _ E X E C U T A B L E } " $ { G E N _ J A V A D O C } " " $ { C M A K E _ C U R R E N T _ S O U R C E _ D I R } / g e n e r a t o r / s r c / j a v a " " $ { C M A K E _ C U R R E N T _ B I N A R Y _ D I R } " 2 > " $ { C M A K E _ C U R R E N T _ B I N A R Y _ D I R } / g e t _ j a v a d o c _ e r r o r s . l o g "
W O R K I N G _ D I R E C T O R Y $ { C M A K E _ C U R R E N T _ B I N A R Y _ D I R }
D E P E N D S $ { j a v a _ d o c u m e n t e d _ h e a d e r s _ d e p s }
)
# copy generated java files to the final location
set ( JAVA_OUTPUT_DIR "src/org/opencv" )
set ( JAVA_INSTALL_ROOT "sdk/java" )
set ( JNI_INSTALL_ROOT "sdk/native" )
# copy each documented header to the final destination
set ( java_files "" )
foreach ( java_file ${ documented_java_files } ${ handwrittren_aidl_sources } )
get_filename_component ( java_file_name "${java_file}" NAME )
string ( REPLACE "-jdoc.java" ".java" java_file_name "${java_file_name}" )
string ( REPLACE "+" "/" java_file_name "${java_file_name}" )
add_custom_command (
O U T P U T " $ { O p e n C V _ B I N A R Y _ D I R } / $ { J A V A _ O U T P U T _ D I R } / $ { j a v a _ f i l e _ n a m e } "
C O M M A N D $ { C M A K E _ C O M M A N D } - E c o p y " $ { j a v a _ f i l e } " " $ { O p e n C V _ B I N A R Y _ D I R } / $ { J A V A _ O U T P U T _ D I R } / $ { j a v a _ f i l e _ n a m e } "
M A I N _ D E P E N D E N C Y " $ { j a v a _ f i l e } "
D E P E N D S $ { j a v a _ d o c u m e n t e d _ h e a d e r s _ d e p s }
C O M M E N T " G e n e r a t i n g $ { J A V A _ O U T P U T _ D I R } / $ { j a v a _ f i l e _ n a m e } "
)
list ( APPEND java_files "${OpenCV_BINARY_DIR}/${JAVA_OUTPUT_DIR}/${java_file_name}" )
if ( ANDROID )
get_filename_component ( install_subdir "${java_file_name}" PATH )
install ( FILES "${OpenCV_BINARY_DIR}/${JAVA_OUTPUT_DIR}/${java_file_name}" DESTINATION ${ JAVA_INSTALL_ROOT } / ${ JAVA_OUTPUT_DIR } / ${ install_subdir } COMPONENT main )
endif ( )
endforeach ( )
# custom target for java API
set ( api_target ${ the_module } _api )
add_custom_target ( ${ api_target } DEPENDS ${ java_files } ${ documented_java_files } ${ java_documented_headers_deps } )
# add opencv_java library
add_library ( ${ the_module } SHARED ${ handwrittren_h_sources } ${ handwrittren_cpp_sources } ${ generated_cpp_sources } )
if ( BUILD_FAT_JAVA_LIB )
set ( __deps ${ OPENCV_MODULE_${the_module } _DEPS} ${ OPENCV_MODULES_BUILD } )
list ( REMOVE_ITEM __deps ${ the_module } opencv_ts )
ocv_list_unique ( __deps )
set ( __extradeps ${ __deps } )
ocv_list_filterout ( __extradeps "^opencv_" )
if ( __extradeps )
list ( REMOVE_ITEM __deps ${ __extradeps } )
endif ( )
target_link_libraries ( ${ the_module } -Wl,-whole-archive ${ __deps } -Wl,-no-whole-archive ${ __extradeps } ${ OPENCV_LINKER_LIBS } )
else ( )
target_link_libraries ( ${ the_module } ${ OPENCV_MODULE_${the_module } _DEPS} ${ OPENCV_LINKER_LIBS } )
endif ( )
add_dependencies ( ${ the_module } ${ api_target } )
# Additional target properties
set_target_properties ( ${ the_module } PROPERTIES
O U T P U T _ N A M E " $ { t h e _ m o d u l e } "
A R C H I V E _ O U T P U T _ D I R E C T O R Y $ { L I B R A R Y _ O U T P U T _ P A T H }
R U N T I M E _ O U T P U T _ D I R E C T O R Y $ { E X E C U T A B L E _ O U T P U T _ P A T H }
I N S T A L L _ N A M E _ D I R $ { O P E N C V _ L I B _ I N S T A L L _ P A T H }
L I N K _ I N T E R F A C E _ L I B R A R I E S " "
)
install ( TARGETS ${ the_module } LIBRARY DESTINATION ${ OPENCV_LIB_INSTALL_PATH } COMPONENT main )
if ( ANDROID )
target_link_libraries ( ${ the_module } jnigraphics ) # for Mat <=> Bitmap converters
# force strip library after the build command
# because samples and tests will make a copy of the library before install
get_target_property ( __opencv_java_location ${ the_module } LOCATION )
add_custom_command ( TARGET ${ the_module } POST_BUILD COMMAND ${ CMAKE_STRIP } --strip-unneeded "${__opencv_java_location}" )
set ( lib_proj_files "" )
# manifest, jni, Eclipse project
file ( GLOB_RECURSE android_lib_project_files RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/android_lib/" "${CMAKE_CURRENT_SOURCE_DIR}/android_lib/*" )
list ( REMOVE_ITEM android_lib_project_files "${ANDROID_MANIFEST_FILE}" )
foreach ( f ${ android_lib_project_files } )
if ( NOT f MATCHES "\\.svn" )
add_custom_command (
O U T P U T " $ { O p e n C V _ B I N A R Y _ D I R } / $ { f } "
C O M M A N D $ { C M A K E _ C O M M A N D } - E c o p y " $ { C M A K E _ C U R R E N T _ S O U R C E _ D I R } / a n d r o i d _ l i b / $ { f } " " $ { O p e n C V _ B I N A R Y _ D I R } / $ { f } "
M A I N _ D E P E N D E N C Y " $ { C M A K E _ C U R R E N T _ S O U R C E _ D I R } / a n d r o i d _ l i b / $ { f } "
C O M M E N T " G e n e r a t i n g $ { f } "
)
list ( APPEND lib_proj_files "${OpenCV_BINARY_DIR}/${f}" )
if ( NOT f MATCHES "jni/.+" )
get_filename_component ( install_subdir "${f}" PATH )
install ( FILES "${OpenCV_BINARY_DIR}/${f}" DESTINATION "${JAVA_INSTALL_ROOT}/${install_subdir}" COMPONENT main )
endif ( )
endif ( )
endforeach ( )
# library project jni sources
foreach ( jni_file ${ handwrittren_cpp_sources } ${ handwrittren_h_sources } ${ generated_cpp_sources } )
get_filename_component ( jni_file_name "${jni_file}" NAME )
add_custom_command (
O U T P U T " $ { O p e n C V _ B I N A R Y _ D I R } / j n i / $ { j n i _ f i l e _ n a m e } "
C O M M A N D $ { C M A K E _ C O M M A N D } - E c o p y " $ { j n i _ f i l e } " " $ { O p e n C V _ B I N A R Y _ D I R } / j n i / $ { j n i _ f i l e _ n a m e } "
D E P E N D S " $ { j n i _ f i l e } " $ { j a v a _ h d r _ d e p s }
C O M M E N T " G e n e r a t i n g j n i / $ { j n i _ f i l e _ n a m e } "
)
list ( APPEND lib_proj_files "${OpenCV_BINARY_DIR}/jni/${jni_file_name}" )
endforeach ( )
# create Android library project in build folder
if ( ANDROID_EXECUTABLE )
set ( lib_target ${ the_module } _android_library )
set ( lib_target_files ${ ANDROID_LIB_PROJECT_FILES } )
ocv_list_add_prefix ( lib_target_files "${OpenCV_BINARY_DIR}/" )
android_get_compatible_target ( lib_target_sdk_target ${ ANDROID_NATIVE_API_LEVEL } ${ ANDROID_SDK_TARGET } )
add_custom_command (
O U T P U T $ { l i b _ t a r g e t _ f i l e s } " $ { O p e n C V _ B I N A R Y _ D I R } / $ { A N D R O I D _ M A N I F E S T _ F I L E } "
C O M M A N D $ { C M A K E _ C O M M A N D } - E r e m o v e $ { l i b _ t a r g e t _ f i l e s }
C O M M A N D $ { C M A K E _ C O M M A N D } - E c o p y " $ { C M A K E _ C U R R E N T _ S O U R C E _ D I R } / a n d r o i d _ l i b / $ { A N D R O I D _ M A N I F E S T _ F I L E } " " $ { O p e n C V _ B I N A R Y _ D I R } / $ { A N D R O I D _ M A N I F E S T _ F I L E } "
C O M M A N D $ { A N D R O I D _ E X E C U T A B L E } - - s i l e n t c r e a t e l i b - p r o j e c t - - p a t h \ " $ { O p e n C V _ B I N A R Y _ D I R } \ " - - t a r g e t \ " $ { l i b _ t a r g e t _ s d k _ t a r g e t } \ " - - n a m e O p e n C V - - p a c k a g e o r g . o p e n c v 2 > \ " $ { C M A K E _ C U R R E N T _ B I N A R Y _ D I R } / c r e a t e _ l i b _ p r o j e c t . l o g \ "
C O M M A N D $ { C M A K E _ C O M M A N D } - E c o p y " $ { C M A K E _ C U R R E N T _ S O U R C E _ D I R } / a n d r o i d _ l i b / $ { A N D R O I D _ M A N I F E S T _ F I L E } " " $ { O p e n C V _ B I N A R Y _ D I R } / $ { A N D R O I D _ M A N I F E S T _ F I L E } "
M A I N _ D E P E N D E N C Y " $ { C M A K E _ C U R R E N T _ S O U R C E _ D I R } / a n d r o i d _ l i b / $ { A N D R O I D _ M A N I F E S T _ F I L E } "
D E P E N D S $ { l i b _ p r o j _ f i l e s }
C O M M E N T " G e n e r a t i n g O p e n C V A n d r o i d l i b r a r y p r o j e c t . S D K t a r g e t : $ { l i b _ t a r g e t _ s d k _ t a r g e t } "
)
install ( FILES "${OpenCV_BINARY_DIR}/${ANDROID_PROJECT_PROPERTIES_FILE}" DESTINATION ${ JAVA_INSTALL_ROOT } COMPONENT main )
install ( FILES "${OpenCV_BINARY_DIR}/${ANDROID_MANIFEST_FILE}" DESTINATION ${ JAVA_INSTALL_ROOT } COMPONENT main )
# creating empty 'gen' and 'res' folders
install ( CODE "MAKE_DIRECTORY(\" \$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/sdk/java/gen\")" COMPONENT main )
install ( CODE "MAKE_DIRECTORY(\" \$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/sdk/java/res\")" COMPONENT main )
if ( ANT_EXECUTABLE AND ANDROID_TOOLS_Pkg_Revision GREATER 13 )
# build the library project
# normally we should do this after a native part, but for a library project we can build the java part first
add_custom_command (
O U T P U T " $ { O p e n C V _ B I N A R Y _ D I R } / b i n / c l a s s e s . j a r " " $ { O p e n C V _ B I N A R Y _ D I R } / b i n / . c l a s s e s . j a r . d e p h e l p e r "
C O M M A N D $ { A N T _ E X E C U T A B L E } - q - n o i n p u t - k d e b u g
C O M M A N D $ { C M A K E _ C O M M A N D } - E t o u c h " $ { O p e n C V _ B I N A R Y _ D I R } / b i n / . c l a s s e s . j a r . d e p h e l p e r " # can not rely on classes.jar because different versions of SDK update timestamp at different times
W O R K I N G _ D I R E C T O R Y " $ { O p e n C V _ B I N A R Y _ D I R } "
D E P E N D S $ { l i b _ p r o j _ f i l e s } $ { l i b _ t a r g e t _ f i l e s } $ { j a v a _ f i l e s }
C O M M E N T " B u i l d i n g O p e n C V A n d r o i d l i b r a r y p r o j e c t "
)
#install(FILES "${OpenCV_BINARY_DIR}/bin/classes.jar" "${OpenCV_BINARY_DIR}/bin/jarlist.cache" "${OpenCV_BINARY_DIR}/bin/build.prop" DESTINATION bin COMPONENT main)
#install(DIRECTORY "${OpenCV_BINARY_DIR}/bin/res" "${OpenCV_BINARY_DIR}/bin/classes" DESTINATION bin COMPONENT main)
list ( APPEND lib_target_files "${OpenCV_BINARY_DIR}/bin/classes.jar" )
endif ( )
add_custom_target ( ${ lib_target } SOURCES ${ lib_proj_files } ${ lib_target_files } "${OpenCV_BINARY_DIR}/${ANDROID_MANIFEST_FILE}" )
add_dependencies ( ${ lib_target } ${ api_target } )
add_dependencies ( ${ the_module } ${ lib_target } )
endif ( )
endif ( ANDROID )
#android test project
if ( ANDROID AND BUILD_TESTS )
add_subdirectory ( android_test )
endif ( )