0
0
mirror of https://github.com/opencv/opencv.git synced 2026-01-18 17:21:42 +01:00
Files
opencv/modules/java/generator/CMakeLists.txt
Alexander Smorkalov c03734475f Merge pull request #28159 from asmorkalov:as/java_cleaners
Introduce option to generate Java code with finalize() or Cleaners interface #28159
 
Closes https://github.com/opencv/opencv/issues/22260
Replaces https://github.com/opencv/opencv/pull/23467

The PR introduce configuration option to generate Java code with Cleaner interface for Java 9+ and old-fashion finalize() method for old Java and Android. Mat class and derivatives are manually written. The PR introduce 2 base classes for it depending on the generator configuration.

Pros:
1. No need to implement complex and error prone cleaner on library side.
2. No new CMake templates, easier to modify code in IDE.

Cons:
1. More generator branches and different code for modern desktop and Android.

TODO: 
- [x] Add Java version check to cmake
- [x] Use Cleaners for ANDROID API 33+

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [ ] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
2025-12-16 14:21:34 +03:00

133 lines
4.7 KiB
CMake

set(MODULE_NAME "java_bindings_generator")
set(OPENCV_MODULE_IS_PART_OF_WORLD FALSE)
ocv_add_module(${MODULE_NAME} INTERNAL)
set(OPENCV_JAVA_SIGNATURES_FILE "${CMAKE_CURRENT_BINARY_DIR}/opencv_java_signatures.json" CACHE INTERNAL "")
set(OPENCV_JAVA_BINDINGS_DIR "${CMAKE_CURRENT_BINARY_DIR}" CACHE INTERNAL "")
file(REMOVE_RECURSE "${OPENCV_JAVA_BINDINGS_DIR}/gen")
file(REMOVE "${OPENCV_DEPHELPER}/gen_opencv_java_source") # force re-run after CMake
# This file is included from a subdirectory
set(JAVA_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/..")
include(${JAVA_SOURCE_DIR}/common.cmake)
set(__remap_config "") # list of remapped ".in" files (configure_file)
set(__remap_targets "")
macro(ocv_remap_files files_list_var)
set(target_dir "${OpenCV_BINARY_DIR}/configured")
foreach(f ${${files_list_var}})
if(NOT "${f}" MATCHES "^(.*)\\.in$")
#continue() # since CMake 3.2+
else()
set(f_ "${CMAKE_MATCH_1}")
file(RELATIVE_PATH rel_path0 "${OpenCV_SOURCE_DIR}" "${f}")
file(RELATIVE_PATH rel_path1 "${OpenCV_SOURCE_DIR}" "${f_}")
set(__target_file "${target_dir}/${rel_path1}")
configure_file("${f}" "${__target_file}" @ONLY)
if(__remap_config)
set(__remap_config "${__remap_config},\n")
endif()
set(__remap_config "${__remap_config} { \"src\": \"${rel_path0}\", \"target\": \"${__target_file}\" }")
list(APPEND __remap_targets "${__target_file}")
endif()
endforeach()
endmacro()
# common files
file(GLOB_RECURSE deps "${CMAKE_CURRENT_SOURCE_DIR}/src/*" "${CMAKE_CURRENT_SOURCE_DIR}/android*/*" "${CMAKE_CURRENT_SOURCE_DIR}/templates/*")
ocv_remap_files(deps)
set(__modules_config "") # list of OpenCV modules
foreach(m ${OPENCV_JAVA_MODULES})
set(module_java_dir "${OPENCV_MODULE_${m}_LOCATION}/misc/java")
list(APPEND deps ${OPENCV_MODULE_${m}_HEADERS})
file(GLOB_RECURSE misc_files "${module_java_dir}/*")
list(APPEND deps ${misc_files})
string(REGEX REPLACE "^opencv_" "" m_ "${m}")
if(__modules_config)
set(__modules_config "${__modules_config},\n")
endif()
file(RELATIVE_PATH rel_path "${OpenCV_SOURCE_DIR}" "${OPENCV_MODULE_${m}_LOCATION}")
set(__modules_config "${__modules_config} { \"name\": \"${m_}\", \"location\": \"${rel_path}\" }")
ocv_remap_files(misc_files)
endforeach(m)
include("${OpenCV_SOURCE_DIR}/cmake/OpenCVBindingsPreprocessorDefinitions.cmake")
ocv_bindings_generator_populate_preprocessor_definitions(
OPENCV_MODULES_BUILD
opencv_preprocessor_defs
)
if(OPENCV_JAVA_CLEANING_API)
if(OPENCV_JAVA_CLEANING_API STREQUAL "finalize")
set(opencv_supported_cleaners "false")
elseif(OPENCV_JAVA_CLEANING_API STREQUAL "cleaner")
set(opencv_supported_cleaners "true")
else()
message(FATAL_ERROR "OPENCV_JAVA_CLEANING_API should be one of \"finalize\" or \"cleaner\"")
endif()
else()
if(ANDROID OR (Java_VERSION VERSION_LESS 9))
message(STATUS "Set Cleaners to False")
set(opencv_supported_cleaners "false")
else()
message(STATUS "Set Cleaners to True")
set(opencv_supported_cleaners "true")
endif()
endif(OPENCV_JAVA_CLEANING_API)
set(CONFIG_FILE "${CMAKE_CURRENT_BINARY_DIR}/gen_java.json")
set(__config_str
"{
\"rootdir\": \"${OpenCV_SOURCE_DIR}\",
\"modules\": [
${__modules_config}
],
\"preprocessor_definitions\": {
${opencv_preprocessor_defs}
},
\"files_remap\": [
${__remap_config}
],
\"support_cleaners\": ${opencv_supported_cleaners}
}
")
if(EXISTS "${CONFIG_FILE}")
file(READ "${CONFIG_FILE}" __content)
else()
set(__content "")
endif()
if(NOT "${__content}" STREQUAL "${__config_str}")
file(WRITE "${CONFIG_FILE}" "${__config_str}")
file(REMOVE "${OPENCV_DEPHELPER}/gen_opencv_java_source")
endif()
unset(__config_str)
set(java_generated_files
# "${OPENCV_JAVA_SIGNATURES_FILE}"
"${OPENCV_DEPHELPER}/gen_opencv_java_source"
)
add_custom_command(
OUTPUT ${java_generated_files}
COMMAND ${PYTHON_DEFAULT_EXECUTABLE} "${JAVA_SOURCE_DIR}/generator/gen_java.py" -p "${JAVA_SOURCE_DIR}/../python/src2/gen2.py" -c "${CONFIG_FILE}"
COMMAND ${CMAKE_COMMAND} -E touch "${OPENCV_DEPHELPER}/gen_opencv_java_source"
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
DEPENDS "${JAVA_SOURCE_DIR}/generator/gen_java.py"
"${JAVA_SOURCE_DIR}/../python/src2/gen2.py"
"${JAVA_SOURCE_DIR}/../python/src2/hdr_parser.py"
# don't, result of file(WRITE): "${CMAKE_CURRENT_BINARY_DIR}/gen_java.json"
${deps} ${__remap_targets}
# not allowed (file(WRITE) result): "${CONFIG_FILE}"
COMMENT "Generate files for Java bindings"
)
add_custom_target(gen_opencv_java_source DEPENDS ${java_generated_files}
SOURCES "${JAVA_SOURCE_DIR}/generator/gen_java.py"
"${CMAKE_CURRENT_BINARY_DIR}/gen_java.json"
)