Merge pull request #20092 from komakai:disable_swift_build
authorGiles Payne <gilespayne@telepathix.net>
Tue, 18 May 2021 14:10:51 +0000 (23:10 +0900)
committerGitHub <noreply@github.com>
Tue, 18 May 2021 14:10:51 +0000 (17:10 +0300)
Fixes for Swift troubles

* Remove NS_SWIFT_NAME override for Point, Rect, and Size due to Darwin namespace conflict

* Fix swift_type overrides in objc generator

* Add backwards compatibility Swift typealiases for Point, Rect, Size

* Add disable-swift build option to iOS/macOS builds

* Add import directive to swift source when building with disable-swift

Co-authored-by: Chris Ballinger <cballinger@rightpoint.com>
modules/core/misc/objc/common/Point2i.h
modules/core/misc/objc/common/Rect2i.h
modules/core/misc/objc/common/Size2i.h
modules/core/misc/objc/common/Typealiases.swift [new file with mode: 0644]
modules/core/misc/objc/gen_dict.json
platforms/ios/build_framework.py
platforms/osx/build_framework.py

index e43ee3a8eccda5a563491985f52939bed08009f3..802c99d6130833dcf53d167aa88fb03c23eebeb9 100644 (file)
@@ -21,7 +21,6 @@ NS_ASSUME_NONNULL_BEGIN
 /**
 * Represents a two dimensional point the coordinate values of which are of type `int`
 */
-NS_SWIFT_NAME(Point)
 CV_EXPORTS @interface Point2i : NSObject
 
 # pragma mark - Properties
index 6ed86d50bdfd9c5fa1f212bb68aad96c88c8a646..a9c1c6e04adf08327584dea80711942b07af5165 100644 (file)
@@ -22,7 +22,6 @@ NS_ASSUME_NONNULL_BEGIN
 /**
 * Represents a rectange the coordinate and dimension values of which are of type `int`
 */
-NS_SWIFT_NAME(Rect)
 CV_EXPORTS @interface Rect2i : NSObject
 
 #pragma mark - Properties
index cd74e2c84a85298ce11a6a0c2c059ea69289dc94..473efa3b57eb8161ca52c54f3888f655e2f66aea 100644 (file)
@@ -21,7 +21,6 @@ NS_ASSUME_NONNULL_BEGIN
 /**
 * Represents the dimensions of a rectangle the values of which are of type `int`
 */
-NS_SWIFT_NAME(Size)
 CV_EXPORTS @interface Size2i : NSObject
 
 #pragma mark - Properties
diff --git a/modules/core/misc/objc/common/Typealiases.swift b/modules/core/misc/objc/common/Typealiases.swift
new file mode 100644 (file)
index 0000000..534dc49
--- /dev/null
@@ -0,0 +1,11 @@
+//
+//  Typealiases.swift
+//
+//  Created by Chris Ballinger on 2020/11/18.
+//
+
+import Foundation
+
+public typealias Rect = Rect2i
+public typealias Point = Point2i
+public typealias Size = Size2i
index 05082ce1ca26f31d76e13f0e6a67bdaee1b97505..9ade8ccb9fd3e2550ea4a5424cbe04785a2d27a3 100644 (file)
             "objc_type": "Point2i*",
             "to_cpp": "%(n)s.nativeRef",
             "from_cpp": "[Point2i fromNative:%(n)s]",
-            "swift_type": "Point"
+            "swift_type": "Point2i"
         },
         "Point2i": {
             "objc_type": "Point2i*",
             "to_cpp": "%(n)s.nativeRef",
             "from_cpp": "[Point2i fromNative:%(n)s]",
-            "swift_type": "Point"
+            "swift_type": "Point2i"
         },
         "Point2f": {
             "objc_type": "Point2f*",
             "objc_type": "Rect2i*",
             "to_cpp": "%(n)s.nativeRef",
             "from_cpp": "[Rect2i fromNative:%(n)s]",
-            "swift_type": "Rect"
+            "swift_type": "Rect2i"
         },
         "Rect2i": {
             "objc_type": "Rect2i*",
             "to_cpp": "%(n)s.nativeRef",
             "from_cpp": "[Rect2i fromNative:%(n)s]",
-            "swift_type": "Rect"
+            "swift_type": "Rect2i"
         },
         "Rect2f": {
             "objc_type": "Rect2f*",
             "objc_type": "Size2i*",
             "to_cpp": "%(n)s.nativeRef",
             "from_cpp": "[Size2i fromNative:%(n)s]",
-            "swift_type": "Size"
+            "swift_type": "Size2i"
         },
         "Size2i": {
             "objc_type": "Size2i*",
             "to_cpp": "%(n)s.nativeRef",
             "from_cpp": "[Size2i fromNative:%(n)s]",
-            "swift_type": "Size"
+            "swift_type": "Size2i"
         },
         "Size2f": {
             "objc_type": "Size2f*",
         "vector_Point": {
             "objc_type": "Point2i*",
             "v_type": "Point2i",
-            "swift_type": "[Point]"
+            "swift_type": "[Point2i]"
         },
         "vector_Point2f": {
             "objc_type": "Point2f*",
         "vector_Rect": {
             "objc_type": "Rect2i*",
             "v_type": "Rect2i",
-            "swift_type": "[Rect]"
+            "swift_type": "[Rect2i]"
         },
         "vector_Rect2d": {
             "objc_type": "Rect2d*",
         "vector_vector_Point": {
             "objc_type": "Point2i*",
             "v_v_type": "Point2i",
-            "swift_type": "[[Point]]"
+            "swift_type": "[[Point2i]]"
         },
         "vector_vector_Point2f": {
             "objc_type": "Point2f*",
index 77db86fc3cd3ffc886923c88fdd261d243275423..0ce56321bf355930fd4d9404b30998f84c425be2 100755 (executable)
@@ -32,7 +32,7 @@ Adding --dynamic parameter will build {framework_name}.framework as App Store dy
 """
 
 from __future__ import print_function, unicode_literals
-import glob, os, os.path, shutil, string, sys, argparse, traceback, multiprocessing
+import glob, os, os.path, shutil, string, sys, argparse, traceback, multiprocessing, codecs, io
 from subprocess import check_call, check_output, CalledProcessError
 from distutils.dir_util import copy_tree
 
@@ -42,7 +42,7 @@ from cv_build_utils import execute, print_error, get_xcode_major, get_xcode_sett
 IPHONEOS_DEPLOYMENT_TARGET='9.0'  # default, can be changed via command line options or environment variable
 
 class Builder:
-    def __init__(self, opencv, contrib, dynamic, bitcodedisabled, exclude, disable, enablenonfree, targets, debug, debug_info, framework_name, run_tests, build_docs):
+    def __init__(self, opencv, contrib, dynamic, bitcodedisabled, exclude, disable, enablenonfree, targets, debug, debug_info, framework_name, run_tests, build_docs, swiftdisabled):
         self.opencv = os.path.abspath(opencv)
         self.contrib = None
         if contrib:
@@ -63,6 +63,7 @@ class Builder:
         self.framework_name = framework_name
         self.run_tests = run_tests
         self.build_docs = build_docs
+        self.swiftdisabled = swiftdisabled
 
     def checkCMakeVersion(self):
         if get_xcode_version() >= (12, 2):
@@ -153,6 +154,22 @@ class Builder:
                 print("To build docs call:")
                 print(sys.argv[0].replace("build_framework", "build_docs") + " " + dirs[0] + "/modules/objc/framework_build")
             self.copy_samples(outdir)
+            if self.swiftdisabled:
+                swift_sources_dir = os.path.join(outdir, "SwiftSources")
+                if not os.path.exists(swift_sources_dir):
+                    os.makedirs(swift_sources_dir)
+                for root, dirs, files in os.walk(dirs[0]):
+                    for file in files:
+                        if file.endswith(".swift") and file.find("Test") == -1:
+                            with io.open(os.path.join(root, file), encoding="utf-8", errors="ignore") as file_in:
+                                body = file_in.read()
+                            if body.find("import Foundation") != -1:
+                                insert_pos = body.find("import Foundation") + len("import Foundation") + 1
+                                body = body[:insert_pos] + "import " + self.framework_name + "\n" + body[insert_pos:]
+                            else:
+                                body = "import " + self.framework_name + "\n\n" + body
+                            with codecs.open(os.path.join(swift_sources_dir, file), "w", "utf-8") as file_out:
+                                file_out.write(body)
 
     def build(self, outdir):
         try:
@@ -297,8 +314,8 @@ class Builder:
         execute(["cmake", "-DBUILD_TYPE=%s" % self.getConfiguration(), "-P", "cmake_install.cmake"], cwd = builddir)
         if self.build_objc_wrapper:
             cmakecmd = self.makeCMakeCmd(arch, target, builddir + "/modules/objc_bindings_generator/{}/gen".format(self.getObjcTarget(target)), cmakeargs)
-            # cmakecmd.append("-DCMAKE_Swift_FLAGS=" + "-target x86_64-apple-ios13.0-macabi")
-            # cmakecmd.append("-DCMAKE_EXE_LINKER_FLAGS=" + "-target x86_64-apple-ios13.0-macabi")
+            if self.swiftdisabled:
+                cmakecmd.append("-DSWIFT_DISABLED=1")
             cmakecmd.append("-DBUILD_ROOT=%s" % builddir)
             cmakecmd.append("-DCMAKE_INSTALL_NAME_TOOL=install_name_tool")
             cmakecmd.append("--no-warn-unused-cli")
@@ -509,6 +526,7 @@ if __name__ == "__main__":
     parser.add_argument('--legacy_build', default=False, dest='legacy_build', action='store_true', help='Build legacy opencv2 framework (default: False, equivalent to "--framework_name=opencv2 --without=objc")')
     parser.add_argument('--run_tests', default=False, dest='run_tests', action='store_true', help='Run tests')
     parser.add_argument('--build_docs', default=False, dest='build_docs', action='store_true', help='Build docs')
+    parser.add_argument('--disable-swift', default=False, dest='swiftdisabled', action='store_true', help='Disable building of Swift extensions')
 
     args, unknown_args = parser.parse_known_args()
     if unknown_args:
@@ -562,6 +580,6 @@ if __name__ == "__main__":
         if iphonesimulator_archs:
             targets.append((iphonesimulator_archs, "iPhoneSimulator"))
 
-    b = iOSBuilder(args.opencv, args.contrib, args.dynamic, args.bitcodedisabled, args.without, args.disable, args.enablenonfree, targets, args.debug, args.debug_info, args.framework_name, args.run_tests, args.build_docs)
+    b = iOSBuilder(args.opencv, args.contrib, args.dynamic, args.bitcodedisabled, args.without, args.disable, args.enablenonfree, targets, args.debug, args.debug_info, args.framework_name, args.run_tests, args.build_docs, args.swiftdisabled)
 
     b.build(args.out)
index 480f3e3957cf871d72663a0c31b60d5d770dc304..2dd5015ee5449b8ea4e34baa12a36b1a028180df 100755 (executable)
@@ -77,6 +77,7 @@ if __name__ == "__main__":
     parser.add_argument('--legacy_build', default=False, dest='legacy_build', action='store_true', help='Build legacy framework (default: False, equivalent to "--framework_name=opencv2 --without=objc")')
     parser.add_argument('--run_tests', default=False, dest='run_tests', action='store_true', help='Run tests')
     parser.add_argument('--build_docs', default=False, dest='build_docs', action='store_true', help='Build docs')
+    parser.add_argument('--disable-swift', default=False, dest='swiftdisabled', action='store_true', help='Disable building of Swift extensions')
 
     args, unknown_args = parser.parse_known_args()
     if unknown_args:
@@ -127,5 +128,5 @@ if __name__ == "__main__":
     if catalyst_archs:
         targets.append((catalyst_archs, "Catalyst")),
 
-    b = OSXBuilder(args.opencv, args.contrib, args.dynamic, True, args.without, args.disable, args.enablenonfree, targets, args.debug, args.debug_info, args.framework_name, args.run_tests, args.build_docs)
+    b = OSXBuilder(args.opencv, args.contrib, args.dynamic, True, args.without, args.disable, args.enablenonfree, targets, args.debug, args.debug_info, args.framework_name, args.run_tests, args.build_docs, args.swiftdisabled)
     b.build(args.out)