CMakeLists.txt 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. set(MODULE_NAME "java_bindings_generator")
  2. set(OPENCV_MODULE_IS_PART_OF_WORLD FALSE)
  3. ocv_add_module(${MODULE_NAME} INTERNAL)
  4. set(OPENCV_JAVA_SIGNATURES_FILE "${CMAKE_CURRENT_BINARY_DIR}/opencv_java_signatures.json" CACHE INTERNAL "")
  5. set(OPENCV_JAVA_BINDINGS_DIR "${CMAKE_CURRENT_BINARY_DIR}" CACHE INTERNAL "")
  6. file(REMOVE_RECURSE "${OPENCV_JAVA_BINDINGS_DIR}/gen")
  7. file(REMOVE "${OPENCV_DEPHELPER}/gen_opencv_java_source") # force re-run after CMake
  8. # This file is included from a subdirectory
  9. set(JAVA_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/..")
  10. include(${JAVA_SOURCE_DIR}/common.cmake)
  11. set(__remap_config "") # list of remapped ".in" files (configure_file)
  12. set(__remap_targets "")
  13. macro(ocv_remap_files files_list_var)
  14. set(target_dir "${OpenCV_BINARY_DIR}/configured")
  15. foreach(f ${${files_list_var}})
  16. if(NOT "${f}" MATCHES "^(.*)\\.in$")
  17. #continue() # since CMake 3.2+
  18. else()
  19. set(f_ "${CMAKE_MATCH_1}")
  20. file(RELATIVE_PATH rel_path0 "${OpenCV_SOURCE_DIR}" "${f}")
  21. file(RELATIVE_PATH rel_path1 "${OpenCV_SOURCE_DIR}" "${f_}")
  22. set(__target_file "${target_dir}/${rel_path1}")
  23. configure_file("${f}" "${__target_file}" @ONLY)
  24. if(__remap_config)
  25. set(__remap_config "${__remap_config},\n")
  26. endif()
  27. set(__remap_config "${__remap_config} { \"src\": \"${rel_path0}\", \"target\": \"${__target_file}\" }")
  28. list(APPEND __remap_targets "${__target_file}")
  29. endif()
  30. endforeach()
  31. endmacro()
  32. # common files
  33. file(GLOB_RECURSE deps "${CMAKE_CURRENT_SOURCE_DIR}/src/*" "${CMAKE_CURRENT_SOURCE_DIR}/android*/*" "${CMAKE_CURRENT_SOURCE_DIR}/templates/*")
  34. ocv_remap_files(deps)
  35. set(__modules_config "") # list of OpenCV modules
  36. foreach(m ${OPENCV_JAVA_MODULES})
  37. set(module_java_dir "${OPENCV_MODULE_${m}_LOCATION}/misc/java")
  38. list(APPEND deps ${OPENCV_MODULE_${m}_HEADERS})
  39. file(GLOB_RECURSE misc_files "${module_java_dir}/*")
  40. list(APPEND deps ${misc_files})
  41. string(REGEX REPLACE "^opencv_" "" m_ "${m}")
  42. if(__modules_config)
  43. set(__modules_config "${__modules_config},\n")
  44. endif()
  45. file(RELATIVE_PATH rel_path "${OpenCV_SOURCE_DIR}" "${OPENCV_MODULE_${m}_LOCATION}")
  46. set(__modules_config "${__modules_config} { \"name\": \"${m_}\", \"location\": \"${rel_path}\" }")
  47. ocv_remap_files(misc_files)
  48. endforeach(m)
  49. set(CONFIG_FILE "${CMAKE_CURRENT_BINARY_DIR}/gen_java.json")
  50. set(__config_str
  51. "{
  52. \"rootdir\": \"${OpenCV_SOURCE_DIR}\",
  53. \"modules\": [
  54. ${__modules_config}
  55. ],
  56. \"files_remap\": [
  57. ${__remap_config}
  58. ]
  59. }
  60. ")
  61. if(EXISTS "${CONFIG_FILE}")
  62. file(READ "${CONFIG_FILE}" __content)
  63. else()
  64. set(__content "")
  65. endif()
  66. if(NOT "${__content}" STREQUAL "${__config_str}")
  67. file(WRITE "${CONFIG_FILE}" "${__config_str}")
  68. file(REMOVE "${OPENCV_DEPHELPER}/gen_opencv_java_source")
  69. endif()
  70. unset(__config_str)
  71. set(java_generated_files
  72. # "${OPENCV_JAVA_SIGNATURES_FILE}"
  73. "${OPENCV_DEPHELPER}/gen_opencv_java_source"
  74. )
  75. add_custom_command(
  76. OUTPUT ${java_generated_files}
  77. COMMAND ${PYTHON_DEFAULT_EXECUTABLE} "${JAVA_SOURCE_DIR}/generator/gen_java.py" -p "${JAVA_SOURCE_DIR}/../python/src2/gen2.py" -c "${CONFIG_FILE}"
  78. COMMAND ${CMAKE_COMMAND} -E touch "${OPENCV_DEPHELPER}/gen_opencv_java_source"
  79. WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
  80. DEPENDS "${JAVA_SOURCE_DIR}/generator/gen_java.py"
  81. "${JAVA_SOURCE_DIR}/../python/src2/gen2.py"
  82. "${JAVA_SOURCE_DIR}/../python/src2/hdr_parser.py"
  83. # don't, result of file(WRITE): "${CMAKE_CURRENT_BINARY_DIR}/gen_java.json"
  84. ${deps} ${__remap_targets}
  85. # not allowed (file(WRITE) result): "${CONFIG_FILE}"
  86. COMMENT "Generate files for Java bindings"
  87. )
  88. add_custom_target(gen_opencv_java_source DEPENDS ${java_generated_files}
  89. SOURCES "${JAVA_SOURCE_DIR}/generator/gen_java.py"
  90. "${CMAKE_CURRENT_BINARY_DIR}/gen_java.json"
  91. )