From 72dfd4846e184c480f27f137f3b078b538d1b017 Mon Sep 17 00:00:00 2001 From: Giles Payne Date: Fri, 23 Oct 2020 20:19:36 +0900 Subject: [PATCH] Merge pull request #18637 from komakai:build-for-distribution Add support for Swift version independence * Build for distribution (Swift version independence) when new Xcode build system is available * Add module map and set "Defines Modules" flag --- modules/objc/generator/gen_objc.py | 12 +++- .../generator/templates/cmakelists.template | 56 +++++++++++++------ 2 files changed, 49 insertions(+), 19 deletions(-) diff --git a/modules/objc/generator/gen_objc.py b/modules/objc/generator/gen_objc.py index c7eabdfb0d..87e42e821d 100755 --- a/modules/objc/generator/gen_objc.py +++ b/modules/objc/generator/gen_objc.py @@ -1347,7 +1347,17 @@ typedef NS_ENUM(int, {2}) {{ def finalize(self, output_objc_path): opencv_header_file = os.path.join(output_objc_path, framework_name + ".h") - self.save(opencv_header_file, '\n'.join(['#import "%s"' % os.path.basename(f) for f in self.header_files])) + opencv_header = "#import \n\n" + opencv_header += "// ! Project version number\nFOUNDATION_EXPORT double " + framework_name + "VersionNumber;\n\n" + opencv_header += "// ! Project version string\nFOUNDATION_EXPORT const unsigned char " + framework_name + "VersionString[];\n\n" + opencv_header += "\n".join(["#import <" + framework_name + "/%s>" % os.path.basename(f) for f in self.header_files]) + self.save(opencv_header_file, opencv_header) + opencv_modulemap_file = os.path.join(output_objc_path, framework_name + ".modulemap") + opencv_modulemap = "framework module " + framework_name + " {\n" + opencv_modulemap += " umbrella header \"" + framework_name + ".h\"\n" + opencv_modulemap += "\n".join([" header \"%s\"" % os.path.basename(f) for f in self.header_files]) + opencv_modulemap += "\n export *\n module * {export *}\n}\n" + self.save(opencv_modulemap_file, opencv_modulemap) cmakelist_template = read_contents(os.path.join(SCRIPT_DIR, 'templates/cmakelists.template')) cmakelist = Template(cmakelist_template).substitute(modules = ";".join(modules), framework = framework_name) self.save(os.path.join(dstdir, "CMakeLists.txt"), cmakelist) diff --git a/modules/objc/generator/templates/cmakelists.template b/modules/objc/generator/templates/cmakelists.template index e928a6d21a..2cfc2474cd 100644 --- a/modules/objc/generator/templates/cmakelists.template +++ b/modules/objc/generator/templates/cmakelists.template @@ -13,32 +13,52 @@ 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_sources "objc/*\.h" "objc/*\.m" "objc/*\.mm" "objc/*\.swift" "objc/*\.modulemap") file(GLOB_RECURSE objc_headers "*\.h") -add_library(opencv_objc_framework STATIC $${objc_sources}) +add_library($framework STATIC $${objc_sources}) -set_target_properties(opencv_objc_framework PROPERTIES LINKER_LANGUAGE CXX) +set_target_properties($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") +target_include_directories($framework PRIVATE "$${BUILD_ROOT}") +target_include_directories($framework PRIVATE "$${BUILD_ROOT}/install/include") +target_include_directories($framework PRIVATE "$${BUILD_ROOT}/install/include/opencv2") foreach(m $${MODULES}) - target_include_directories(opencv_objc_framework PRIVATE "$${BUILD_ROOT}/modules/objc/gen/objc/$${m}") + target_include_directories($framework PRIVATE "$${BUILD_ROOT}/modules/objc/gen/objc/$${m}") endforeach() -install(TARGETS opencv_objc_framework LIBRARY DESTINATION lib) +install(TARGETS $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 - ) +if (CMAKE_XCODE_BUILD_SYSTEM GREATER_EQUAL 12) + set_target_properties($framework PROPERTIES + OUTPUT_NAME "$framework" + ARCHIVE_OUTPUT_DIRECTORY "$${BUILD_ROOT}/lib" + XCODE_ATTRIBUTE_SWIFT_VERSION 5.0 + XCODE_ATTRIBUTE_DEFINES_MODULE YES + XCODE_ATTRIBUTE_BUILD_LIBRARY_FOR_DISTRIBUTION YES + XCODE_ATTRIBUTE_OTHER_SWIFT_FLAGS "-Xcc $${SUPPRESS_WARNINGS_FLAGS}" + XCODE_ATTRIBUTE_MODULEMAP_FILE objc/$framework.modulemap + XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER org.opencv.$framework + FRAMEWORK TRUE + MACOSX_FRAMEWORK_IDENTIFIER org.opencv.$framework + PUBLIC_HEADER "$${objc_headers}" + DEFINE_SYMBOL CVAPI_EXPORTS + ) +else() + set_target_properties($framework PROPERTIES + OUTPUT_NAME "$framework" + ARCHIVE_OUTPUT_DIRECTORY "$${BUILD_ROOT}/lib" + XCODE_ATTRIBUTE_SWIFT_VERSION 5.0 + XCODE_ATTRIBUTE_DEFINES_MODULE YES + XCODE_ATTRIBUTE_OTHER_SWIFT_FLAGS "-Xcc $${SUPPRESS_WARNINGS_FLAGS}" + XCODE_ATTRIBUTE_MODULEMAP_FILE objc/$framework.modulemap + XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER org.opencv.$framework + FRAMEWORK TRUE + MACOSX_FRAMEWORK_IDENTIFIER org.opencv.$framework + PUBLIC_HEADER "$${objc_headers}" + DEFINE_SYMBOL CVAPI_EXPORTS + ) +endif()