1 # ----------------------------------------------------------------------------
2 # CMake file for java support
3 # ----------------------------------------------------------------------------
4 if(IOS OR NOT PYTHON_EXECUTABLE OR NOT ANT_EXECUTABLE OR NOT (JNI_FOUND OR (ANDROID AND ANDROID_NATIVE_API_LEVEL GREATER 7)))
5 ocv_module_disable(java)
8 set(the_description "The java bindings")
9 ocv_add_module(java BINDINGS opencv_core opencv_imgproc OPTIONAL opencv_objdetect opencv_features2d opencv_video opencv_imgcodecs opencv_videoio opencv_ml opencv_calib3d opencv_photo opencv_nonfree opencv_contrib)
10 ocv_module_include_directories("${CMAKE_CURRENT_SOURCE_DIR}/generator/src/cpp")
13 include_directories(${JNI_INCLUDE_DIRS})
17 set(JAVA_INSTALL_ROOT "sdk/java")
18 set(JNI_INSTALL_ROOT "sdk/native")
20 # get list of modules to wrap
21 string(REPLACE "opencv_" "" OPENCV_JAVA_MODULES "${OPENCV_MODULE_${the_module}_REQ_DEPS};${OPENCV_MODULE_${the_module}_OPT_DEPS}")
22 foreach(module ${OPENCV_JAVA_MODULES})
23 if(NOT HAVE_opencv_${module})
24 list(REMOVE_ITEM OPENCV_JAVA_MODULES ${module})
28 ######################################################################################################################################
31 set(scripts_gen_java "${CMAKE_CURRENT_SOURCE_DIR}/generator/gen_java.py")
32 set(scripts_hdr_parser "${CMAKE_CURRENT_SOURCE_DIR}/../python/src2/hdr_parser.py")
33 set(scripts_gen_javadoc "${CMAKE_CURRENT_SOURCE_DIR}/generator/gen_javadoc.py")
34 set(scripts_rst_parser "${CMAKE_CURRENT_SOURCE_DIR}/generator/rst_parser.py")
36 # handwritten C/C++ and Java sources
37 file(GLOB handwrittren_h_sources "${CMAKE_CURRENT_SOURCE_DIR}/generator/src/cpp/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/generator/src/cpp/*.hpp")
38 file(GLOB handwrittren_cpp_sources "${CMAKE_CURRENT_SOURCE_DIR}/generator/src/cpp/*.cpp")
39 file(GLOB handwrittren_java_sources "${CMAKE_CURRENT_SOURCE_DIR}/generator/src/java/*.java")
40 file(GLOB handwrittren_aidl_sources "${CMAKE_CURRENT_SOURCE_DIR}/generator/src/java/*.aidl")
42 ocv_list_filterout(handwrittren_java_sources "/(engine|android)\\\\+")
43 ocv_list_filterout(handwrittren_aidl_sources "/(engine|android)\\\\+")
45 file(GLOB_RECURSE handwrittren_lib_project_files_rel RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/android_lib/" "${CMAKE_CURRENT_SOURCE_DIR}/android_lib/*")
46 list(REMOVE_ITEM handwrittren_lib_project_files_rel "${ANDROID_MANIFEST_FILE}")
49 # headers of OpenCV modules
50 set(opencv_public_headers "")
51 foreach(module ${OPENCV_JAVA_MODULES})
52 # get list of module headers
53 if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/generator/config/${module}.filelist")
54 file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/generator/config/${module}.filelist" module_headers)
55 ocv_list_add_prefix(module_headers "${OPENCV_MODULE_opencv_${module}_LOCATION}/")
57 set(module_headers "${OPENCV_MODULE_opencv_${module}_HEADERS}")
60 # C headers must go first
61 set(module_headers_cpp ${module_headers})
62 ocv_list_filterout(module_headers_cpp "\\\\.h$")
63 if(module_headers_cpp)
64 list(REMOVE_ITEM module_headers ${module_headers_cpp})
65 list(APPEND module_headers ${module_headers_cpp})
67 unset(module_headers_cpp)
69 set(opencv_public_headers_${module} ${module_headers})
70 list(APPEND opencv_public_headers ${module_headers})
72 list(REMOVE_ITEM OPENCV_JAVA_MODULES ${module})
76 # rst documentation used for javadoc generation
77 set(javadoc_rst_sources "")
78 foreach(module ${OPENCV_JAVA_MODULES})
79 file(GLOB_RECURSE refman_rst_headers "${OPENCV_MODULE_opencv_${module}_LOCATION}/*.rst")
80 list(APPEND javadoc_rst_sources ${refman_rst_headers})
84 set(generated_cpp_sources "")
85 foreach(module ${OPENCV_JAVA_MODULES})
86 list(APPEND generated_cpp_sources "${CMAKE_CURRENT_BINARY_DIR}/${module}.cpp")
89 # IMPORTANT: add dependencies to cmake (we should rerun cmake if any of these files is modified)
90 configure_file("${scripts_gen_java}" "${OpenCV_BINARY_DIR}/junk/gen_java.junk" COPYONLY)
91 configure_file("${scripts_hdr_parser}" "${OpenCV_BINARY_DIR}/junk/hdr_parser.junk" COPYONLY)
92 foreach(header ${opencv_public_headers})
93 get_filename_component(header_name "${header}" NAME)
94 configure_file("${header}" "${OpenCV_BINARY_DIR}/junk/${header_name}.junk" COPYONLY)
97 # generated java files
98 set(generated_java_sources "")
99 foreach(module ${OPENCV_JAVA_MODULES})
100 # first run of gen_java.py (to get list of generated files)
101 file(REMOVE_RECURSE "${CMAKE_CURRENT_BINARY_DIR}/gen_java_out/")
102 file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/gen_java_out")
103 execute_process(COMMAND ${PYTHON_EXECUTABLE} "${scripts_gen_java}" "${scripts_hdr_parser}" ${module} ${opencv_public_headers_${module}}
104 WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/gen_java_out"
105 OUTPUT_QUIET ERROR_QUIET)
106 unset(generated_java_sources_${module})
107 file(GLOB_RECURSE generated_java_sources_${module} RELATIVE "${CMAKE_CURRENT_BINARY_DIR}/gen_java_out/" "${CMAKE_CURRENT_BINARY_DIR}/gen_java_out/*.java")
108 ocv_list_add_prefix(generated_java_sources_${module} "${CMAKE_CURRENT_BINARY_DIR}/")
110 list(APPEND generated_java_sources ${generated_java_sources_${module}})
113 # generated java files with javadoc
114 set(documented_java_files "")
115 foreach(java_file ${generated_java_sources} ${handwrittren_java_sources})
116 get_filename_component(java_file_name "${java_file}" NAME_WE)
117 list(APPEND documented_java_files "${CMAKE_CURRENT_BINARY_DIR}/${java_file_name}-jdoc.java")
120 ######################################################################################################################################
122 # step 1: generate .cpp/.java from OpenCV headers
123 set(step1_depends "${scripts_gen_java}" "${scripts_hdr_parser}" ${opencv_public_headers})
124 foreach(module ${OPENCV_JAVA_MODULES})
125 # second run of gen_java.py (at build time)
126 add_custom_command(OUTPUT ${generated_java_sources_${module}} "${CMAKE_CURRENT_BINARY_DIR}/${module}.cpp"
127 COMMAND ${PYTHON_EXECUTABLE} "${scripts_gen_java}" "${scripts_hdr_parser}" ${module} ${opencv_public_headers_${module}}
128 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
129 DEPENDS "${scripts_gen_java}" "${scripts_hdr_parser}" ${opencv_public_headers_${module}}
133 # step 2: generate javadoc comments
134 set(step2_depends ${step1_depends} ${scripts_gen_javadoc} ${scripts_rst_parser} ${javadoc_rst_sources} ${generated_java_sources} ${handwrittren_java_sources})
135 string(REPLACE ";" "," OPENCV_JAVA_MODULES_STR "${OPENCV_JAVA_MODULES}")
136 add_custom_command(OUTPUT ${documented_java_files}
137 COMMAND ${PYTHON_EXECUTABLE} "${scripts_gen_javadoc}" --modules ${OPENCV_JAVA_MODULES_STR} "${CMAKE_CURRENT_SOURCE_DIR}/generator/src/java" "${CMAKE_CURRENT_BINARY_DIR}" 2> "${CMAKE_CURRENT_BINARY_DIR}/get_javadoc_errors.log"
138 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
139 DEPENDS ${step2_depends}
143 # step 3: copy files to destination
144 set(step3_input_files ${documented_java_files} ${handwrittren_aidl_sources})
146 foreach(java_file ${step3_input_files})
147 get_filename_component(java_file_name "${java_file}" NAME)
148 string(REPLACE "-jdoc.java" ".java" java_file_name "${java_file_name}")
149 string(REPLACE "+" "/" java_file_name "${java_file_name}")
150 set(output_name "${OpenCV_BINARY_DIR}/src/org/opencv/${java_file_name}")
152 add_custom_command(OUTPUT "${output_name}"
153 COMMAND ${CMAKE_COMMAND} -E copy "${java_file}" "${output_name}"
154 MAIN_DEPENDENCY "${java_file}"
155 DEPENDS ${step2_depends}
156 COMMENT "Generating src/org/opencv/${java_file_name}"
158 list(APPEND copied_files "${output_name}")
161 get_filename_component(install_subdir "${java_file_name}" PATH)
162 install(FILES "${output_name}" DESTINATION "${JAVA_INSTALL_ROOT}/src/org/opencv/${install_subdir}" COMPONENT java)
167 set(android_copied_files "")
168 set(android_step3_input_files "")
169 foreach(file ${handwrittren_lib_project_files_rel})
170 configure_file("${CMAKE_CURRENT_SOURCE_DIR}/android_lib/${file}" "${OpenCV_BINARY_DIR}/${file}" @ONLY)
171 list(APPEND android_copied_files "${OpenCV_BINARY_DIR}/${file}")
172 list(APPEND android_step3_input_files "${CMAKE_CURRENT_SOURCE_DIR}/android_lib/${file}")
174 if(NOT file MATCHES "jni/.+")
175 get_filename_component(install_subdir "${file}" PATH)
176 install(FILES "${OpenCV_BINARY_DIR}/${file}" DESTINATION "${JAVA_INSTALL_ROOT}/${install_subdir}" COMPONENT java)
180 # library project jni sources (nothing really depends on them so we will not add them to step3_input_files)
181 foreach(jni_file ${handwrittren_cpp_sources} ${handwrittren_h_sources} ${generated_cpp_sources})
182 get_filename_component(jni_file_name "${jni_file}" NAME)
183 add_custom_command(OUTPUT "${OpenCV_BINARY_DIR}/jni/${jni_file_name}"
184 COMMAND ${CMAKE_COMMAND} -E copy "${jni_file}" "${OpenCV_BINARY_DIR}/jni/${jni_file_name}"
185 DEPENDS "${jni_file}" ${java_hdr_deps}
186 COMMENT "Generating jni/${jni_file_name}"
188 list(APPEND android_copied_files "${OpenCV_BINARY_DIR}/jni/${jni_file_name}")
192 # step 3.5: generate Android library project
193 if(ANDROID AND ANDROID_EXECUTABLE)
194 set(lib_target_files ${ANDROID_LIB_PROJECT_FILES})
195 ocv_list_add_prefix(lib_target_files "${OpenCV_BINARY_DIR}/")
197 android_get_compatible_target(lib_target_sdk_target ${ANDROID_NATIVE_API_LEVEL} ${ANDROID_SDK_TARGET} 14)
198 configure_file("${CMAKE_CURRENT_SOURCE_DIR}/android_lib/${ANDROID_MANIFEST_FILE}" "${CMAKE_CURRENT_BINARY_DIR}/${ANDROID_MANIFEST_FILE}" @ONLY)
200 add_custom_command(OUTPUT ${lib_target_files} "${OpenCV_BINARY_DIR}/${ANDROID_MANIFEST_FILE}"
201 COMMAND ${CMAKE_COMMAND} -E remove ${lib_target_files}
202 COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_BINARY_DIR}/${ANDROID_MANIFEST_FILE}" "${OpenCV_BINARY_DIR}/${ANDROID_MANIFEST_FILE}"
203 COMMAND ${ANDROID_EXECUTABLE} --silent create lib-project --path \"${OpenCV_BINARY_DIR}\" --target \"${lib_target_sdk_target}\" --name OpenCV --package org.opencv 2>\"${CMAKE_CURRENT_BINARY_DIR}/create_lib_project.log\"
204 COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_BINARY_DIR}/${ANDROID_MANIFEST_FILE}" "${OpenCV_BINARY_DIR}/${ANDROID_MANIFEST_FILE}"
205 MAIN_DEPENDENCY "${CMAKE_CURRENT_BINARY_DIR}/${ANDROID_MANIFEST_FILE}"
206 DEPENDS ${android_step3_input_files} ${android_copied_files}
207 COMMENT "Generating OpenCV Android library project. SDK target: ${lib_target_sdk_target}"
209 list(APPEND copied_files ${lib_target_files} "${OpenCV_BINARY_DIR}/${ANDROID_MANIFEST_FILE}")
210 list(APPEND step3_input_files "${CMAKE_CURRENT_BINARY_DIR}/${ANDROID_MANIFEST_FILE}")
212 install(FILES "${OpenCV_BINARY_DIR}/${ANDROID_PROJECT_PROPERTIES_FILE}" DESTINATION ${JAVA_INSTALL_ROOT} COMPONENT java)
213 install(FILES "${OpenCV_BINARY_DIR}/${ANDROID_MANIFEST_FILE}" DESTINATION ${JAVA_INSTALL_ROOT} COMPONENT java)
214 # creating empty 'gen' and 'res' folders
215 install(CODE "MAKE_DIRECTORY(\"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${JAVA_INSTALL_ROOT}/gen\")" COMPONENT java)
216 install(CODE "MAKE_DIRECTORY(\"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${JAVA_INSTALL_ROOT}/res\")" COMPONENT java)
217 endif(ANDROID AND ANDROID_EXECUTABLE)
219 set(step3_depends ${step2_depends} ${step3_input_files} ${copied_files})
222 set(LIB_NAME_SUFIX "")
224 set(LIB_NAME_SUFIX "${OPENCV_VERSION_MAJOR}${OPENCV_VERSION_MINOR}${OPENCV_VERSION_PATCH}")
229 set(JAR_FILE "${OpenCV_BINARY_DIR}/bin/classes.jar")
230 if(ANDROID_TOOLS_Pkg_Revision GREATER 13)
231 # build the library project
232 # normally we should do this after a native part, but for a library project we can build the java part first
233 add_custom_command(OUTPUT "${JAR_FILE}" "${JAR_FILE}.dephelper"
234 COMMAND ${ANT_EXECUTABLE} -q -noinput -k debug
235 COMMAND ${CMAKE_COMMAND} -E touch "${JAR_FILE}.dephelper" # can not rely on classes.jar because different versions of SDK update timestamp at different times
236 WORKING_DIRECTORY "${OpenCV_BINARY_DIR}"
237 DEPENDS ${step3_depends}
238 COMMENT "Building OpenCV Android library project"
242 add_custom_command(OUTPUT "${JAR_FILE}" "${JAR_FILE}.dephelper"
243 COMMAND ${CMAKE_COMMAND} -E touch "${JAR_FILE}"
244 COMMAND ${CMAKE_COMMAND} -E touch "${JAR_FILE}.dephelper"
245 WORKING_DIRECTORY "${OpenCV_BINARY_DIR}"
246 DEPENDS ${step3_depends}
251 set(JAR_NAME opencv-${LIB_NAME_SUFIX}.jar)
252 set(JAR_FILE "${OpenCV_BINARY_DIR}/bin/${JAR_NAME}")
253 configure_file("${CMAKE_CURRENT_SOURCE_DIR}/build.xml.in" "${OpenCV_BINARY_DIR}/build.xml" @ONLY)
254 list(APPEND step3_depends "${OpenCV_BINARY_DIR}/build.xml")
256 add_custom_command(OUTPUT "${JAR_FILE}" "${JAR_FILE}.dephelper"
257 COMMAND ${ANT_EXECUTABLE} -q -noinput -k jar
258 COMMAND ${CMAKE_COMMAND} -E touch "${JAR_FILE}.dephelper"
259 WORKING_DIRECTORY "${OpenCV_BINARY_DIR}"
260 DEPENDS ${step3_depends}
261 COMMENT "Generating ${JAR_NAME}"
265 set(JAR_INSTALL_DIR java)
267 set(JAR_INSTALL_DIR share/OpenCV/java)
269 install(FILES ${JAR_FILE} DESTINATION ${JAR_INSTALL_DIR} COMPONENT java)
272 # step 5: build native part
274 # workarounding lack of `__attribute__ ((visibility("default")))` in jni_md.h/JNIEXPORT
275 string(REPLACE "-fvisibility=hidden" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
277 add_library(${the_module} SHARED ${handwrittren_h_sources} ${handwrittren_cpp_sources} ${generated_cpp_sources}
279 "${JAR_FILE}" "${JAR_FILE}.dephelper")
280 set_target_properties(${the_module} PROPERTIES COMPILE_DEFINITIONS OPENCV_NOSTL)
282 if(BUILD_FAT_JAVA_LIB)
283 set(__deps ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULES_BUILD})
284 foreach(m ${OPENCV_MODULES_BUILD}) # filterout INTERNAL (like opencv_ts) and BINDINGS (like opencv_python) modules
285 ocv_assert(DEFINED OPENCV_MODULE_${m}_CLASS)
286 if(NOT OPENCV_MODULE_${m}_CLASS STREQUAL "PUBLIC")
287 list(REMOVE_ITEM __deps ${m})
290 ocv_list_unique(__deps)
291 set(__extradeps ${__deps})
292 ocv_list_filterout(__extradeps "^opencv_")
294 list(REMOVE_ITEM __deps ${__extradeps})
297 foreach(_dep ${__deps})
298 target_link_libraries(${the_module} -Wl,-force_load "${_dep}")
301 target_link_libraries(${the_module} -Wl,-whole-archive ${__deps} -Wl,-no-whole-archive)
303 target_link_libraries(${the_module} ${__extradeps} ${OPENCV_LINKER_LIBS})
305 target_link_libraries(${the_module} ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_LINKER_LIBS})
309 target_link_libraries(${the_module} jnigraphics) # for Mat <=> Bitmap converters
311 # force strip library after the build command
312 # because samples and tests will make a copy of the library before install
313 get_target_property(__opencv_java_location ${the_module} LOCATION)
314 # Turn off stripping in debug build
315 if ( NOT (CMAKE_BUILD_TYPE MATCHES "Debug"))
316 add_custom_command(TARGET ${the_module} POST_BUILD COMMAND ${CMAKE_STRIP} --strip-unneeded "${__opencv_java_location}")
320 # Additional target properties
321 set_target_properties(${the_module} PROPERTIES
322 OUTPUT_NAME "${the_module}${LIB_NAME_SUFIX}"
323 ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
324 LIBRARY_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
325 RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
326 INSTALL_NAME_DIR ${OPENCV_LIB_INSTALL_PATH}
327 LINK_INTERFACE_LIBRARIES ""
331 set_target_properties(${the_module} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH})
334 if(ENABLE_SOLUTION_FOLDERS)
335 set_target_properties(${the_module} PROPERTIES FOLDER "bindings")
339 ocv_install_target(${the_module} EXPORT OpenCVModules
340 LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT java
341 ARCHIVE DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT java)
343 if(NOT INSTALL_CREATE_DISTRIB)
344 ocv_install_target(${the_module} EXPORT OpenCVModules
345 RUNTIME DESTINATION ${JAR_INSTALL_DIR} COMPONENT java
346 LIBRARY DESTINATION ${JAR_INSTALL_DIR} COMPONENT java)
348 ocv_install_target(${the_module} EXPORT OpenCVModules
349 RUNTIME DESTINATION ${JAR_INSTALL_DIR}/${OpenCV_ARCH} COMPONENT java
350 LIBRARY DESTINATION ${JAR_INSTALL_DIR}/${OpenCV_ARCH} COMPONENT java)
354 ######################################################################################################################################
358 add_subdirectory(android_test)
360 add_subdirectory(test)