1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-30 15:53:03 +04:00

Merge pull request #17165 from komakai:objc-binding

Objc binding

* Initial work on Objective-C wrapper

* Objective-C generator script; update manually generated wrappers

* Add Mat tests

* Core Tests

* Imgproc wrapper generation and tests

* Fixes for Imgcodecs wrapper

* Miscellaneous fixes. Swift build support

* Objective-C wrapper build/install

* Add Swift wrappers for videoio/objdetect/feature2d

* Framework build;iOS support

* Fix toArray functions;Use enum types whenever possible

* Use enum types where possible;prepare test build

* Update test

* Add test runner scripts for iOS and macOS

* Add test scripts and samples

* Build fixes

* Fix build (cmake 3.17.x compatibility)

* Fix warnings

* Fix enum name conflicting handling

* Add support for document generation with Jazzy

* Swift/Native fast accessor functions

* Add Objective-C wrapper for calib3d, dnn, ml, photo and video modules

* Remove IntOut/FloatOut/DoubleOut classes

* Fix iOS default test platform value

* Fix samples

* Revert default framework name to opencv2

* Add converter util functions

* Fix failing test

* Fix whitespace

* Add handling for deprecated methods;fix warnings;define __OPENCV_BUILD

* Suppress cmake warnings

* Reduce severity of "jazzy not found" log message

* Fix incorrect #include of compatibility header in ios.h

* Use explicit returns in subscript/get implementation

* Reduce minimum required cmake version to 3.15 for Objective-C/Swift binding
This commit is contained in:
Giles Payne
2020-06-09 03:32:53 +09:00
committed by GitHub
parent f30b5995b6
commit 02385472b6
237 changed files with 24107 additions and 28 deletions
+86
View File
@@ -0,0 +1,86 @@
set(MODULE_NAME "objc")
set(OPENCV_MODULE_IS_PART_OF_WORLD FALSE)
ocv_add_module(${MODULE_NAME} INTERNAL opencv_core opencv_imgproc)
set(OPENCV_OBJC_SIGNATURES_FILE "${CMAKE_CURRENT_BINARY_DIR}/opencv_objc_signatures.json" CACHE INTERNAL "")
set(OPENCV_OBJC_BINDINGS_DIR "${CMAKE_CURRENT_BINARY_DIR}" CACHE INTERNAL "")
file(REMOVE_RECURSE "${OPENCV_OBJC_BINDINGS_DIR}/gen")
file(REMOVE "${OPENCV_DEPHELPER}/gen_opencv_objc_source") # force re-run after CMake
# This file is included from a subdirectory
set(OBJC_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/..")
include(${OBJC_SOURCE_DIR}/common.cmake)
# common files
file(GLOB_RECURSE deps "${CMAKE_CURRENT_SOURCE_DIR}/templates/*")
set(__modules_config "") # list of OpenCV modules
foreach(m ${OPENCV_OBJC_MODULES})
set(module_objc_dir "${OPENCV_MODULE_${m}_LOCATION}/misc/objc")
list(APPEND deps ${OPENCV_MODULE_${m}_HEADERS})
file(GLOB_RECURSE misc_files "${module_objc_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}\" }")
endforeach(m)
set(CONFIG_FILE "${CMAKE_CURRENT_BINARY_DIR}/gen_objc.json")
set(__config_str
"{
\"rootdir\": \"${OpenCV_SOURCE_DIR}\",
\"modules\": [
${__modules_config}
]
}
")
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_objc_source")
endif()
unset(__config_str)
set(objc_generated_files
# "${OPENCV_OBJC_SIGNATURES_FILE}"
"${OPENCV_DEPHELPER}/gen_opencv_objc_source"
)
string(REPLACE "opencv_" "" MODULES "${OPENCV_OBJC_MODULES}")
if(IOS)
set(TARGET "ios")
else()
set(TARGET "osx")
endif()
add_custom_command(
OUTPUT ${objc_generated_files}
COMMAND ${PYTHON_DEFAULT_EXECUTABLE} "${OBJC_SOURCE_DIR}/generator/gen_objc.py" -p "${OBJC_SOURCE_DIR}/../python/src2/gen2.py" -c "${CONFIG_FILE}" -t "${TARGET}" -f "${FRAMEWORK_NAME}"
COMMAND ${CMAKE_COMMAND} -E touch "${OPENCV_DEPHELPER}/gen_opencv_objc_source"
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
DEPENDS "${OBJC_SOURCE_DIR}/generator/gen_objc.py"
"${OBJC_SOURCE_DIR}/../python/src2/gen2.py"
"${OBJC_SOURCE_DIR}/../python/src2/hdr_parser.py"
# don't, result of file(WRITE): "${CMAKE_CURRENT_BINARY_DIR}/gen_objc.json"
${deps}
# not allowed (file(WRITE) result): "${CONFIG_FILE}"
COMMENT "Generate files for Objective-C bindings"
)
add_custom_target(gen_opencv_objc_source DEPENDS ${objc_generated_files}
SOURCES "${OBJC_SOURCE_DIR}/generator/gen_objc.py"
"${OBJC_SOURCE_DIR}/generator/templates/cmakelists.template"
"${CMAKE_CURRENT_BINARY_DIR}/gen_objc.json"
)
add_dependencies(opencv_world gen_opencv_objc_source)
+1384
View File
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,59 @@
cmake_minimum_required(VERSION 3.15)
project($framework)
set(MODULES "$modules")
# Enable C++11
set (CMAKE_CXX_STANDARD 11)
set (CMAKE_CXX_STANDARD_REQUIRED TRUE)
set (OBJC_COMPILE_FLAGS "-fobjc-arc -fobjc-weak -fvisibility=hidden -D__OPENCV_BUILD=1")
set (SUPPRESS_WARNINGS_FLAGS "-Wno-incomplete-umbrella")
set (CMAKE_CXX_FLAGS "$${CMAKE_CXX_FLAGS} $${OBJC_COMPILE_FLAGS} $${SUPPRESS_WARNINGS_FLAGS}")
# grab the files
file(GLOB_RECURSE objc_sources "objc/*\.h" "objc/*\.m" "objc/*\.mm" "objc/*\.swift")
file(GLOB_RECURSE objc_headers "*\.h")
add_library(opencv_objc_framework STATIC $${objc_sources})
set_target_properties(opencv_objc_framework PROPERTIES LINKER_LANGUAGE CXX)
target_include_directories(opencv_objc_framework PRIVATE "$${BUILD_ROOT}")
target_include_directories(opencv_objc_framework PRIVATE "$${BUILD_ROOT}/install/include")
target_include_directories(opencv_objc_framework PRIVATE "$${BUILD_ROOT}/install/include/opencv2")
foreach(m $${MODULES})
target_include_directories(opencv_objc_framework PRIVATE "$${BUILD_ROOT}/modules/objc/gen/objc/$${m}")
endforeach()
install(TARGETS opencv_objc_framework LIBRARY DESTINATION lib)
enable_language(Swift)
# Additional target properties
set_target_properties(opencv_objc_framework PROPERTIES
OUTPUT_NAME "$framework"
ARCHIVE_OUTPUT_DIRECTORY "$${BUILD_ROOT}/lib"
XCODE_ATTRIBUTE_SWIFT_VERSION 5.0
XCODE_ATTRIBUTE_OTHER_SWIFT_FLAGS "-Xcc $${SUPPRESS_WARNINGS_FLAGS}"
FRAMEWORK TRUE
MACOSX_FRAMEWORK_IDENTIFIER org.opencv.$framework
PUBLIC_HEADER "$${objc_headers}"
DEFINE_SYMBOL CVAPI_EXPORTS
)
find_program(JAZZY jazzy)
if(JAZZY)
add_custom_command(
OUTPUT "$${BUILD_ROOT}/modules/objc/doc_build/doc/index.html"
COMMAND $${JAZZY} --objc --author OpenCV --author_url http://opencv.org --github_url https://github.com/opencv/opencv --umbrella-header "$${BUILD_ROOT}/lib/$${CMAKE_BUILD_TYPE}/$framework.framework/Headers/$framework.h" --framework-root "$${BUILD_ROOT}/lib/$${CMAKE_BUILD_TYPE}/$framework.framework" --module $framework --sdk iphonesimulator --undocumented-text \"\"
WORKING_DIRECTORY "$${BUILD_ROOT}/modules/objc/doc_build"
COMMENT "Generating Documentation"
)
add_custom_target(opencv_objc_doc ALL DEPENDS "$${BUILD_ROOT}/modules/objc/doc_build/doc/index.html")
add_dependencies(opencv_objc_doc opencv_objc_framework)
else()
message("jazzy not found - documentation will not be generated!")
endif()
@@ -0,0 +1,16 @@
//
// This file is auto-generated. Please don't modify it!
//
#import "$objcName.h"
#import "CVObjcUtil.h"
$imports
@implementation $objcName
$nativePointerHandling
$methodImplementations
@end
@@ -0,0 +1,29 @@
//
// This file is auto-generated. Please don't modify it!
//
#pragma once
#ifdef __cplusplus
#import "opencv.hpp"
$additionalImports
#endif
#import <Foundation/Foundation.h>
$importBaseClass
$forwardDeclarations
$enumDeclarations
NS_ASSUME_NONNULL_BEGIN
$docs
@interface $objcName : $base
$nativePointerHandling
$methodDeclarations
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,14 @@
//
// This file is auto-generated. Please don't modify it!
//
#import "$module.h"
#import "CVObjcUtil.h"
$imports
@implementation $module
$methodImplementations
@end
@@ -0,0 +1,26 @@
//
// This file is auto-generated. Please don't modify it!
//
#pragma once
#ifdef __cplusplus
#import "opencv.hpp"
$additionalImports
#endif
#import <Foundation/Foundation.h>
$forwardDeclarations
$enumDeclarations
NS_ASSUME_NONNULL_BEGIN
$docs
@interface $module : $base
$methodDeclarations
@end
NS_ASSUME_NONNULL_END