From cfbdbffcad2b599b402828d0b1ba93cff9a4790c Mon Sep 17 00:00:00 2001
From: Alexander Alekhin <alexander.a.alekhin@gmail.com>
Date: Sat, 14 Nov 2020 22:04:57 +0000
Subject: [PATCH] objc: rework headers import

- 'AdditionalImports' can be removed from misc/objc/gen_dict.json
- avoid using of legacy 'opencv.hpp'
---
 modules/core/misc/objc/common/Converters.h    |  2 +-
 modules/core/misc/objc/common/CvType.h        |  2 +-
 modules/core/misc/objc/common/DMatch.h        |  2 +-
 modules/core/misc/objc/common/Double2.h       |  2 +-
 modules/core/misc/objc/common/Double3.h       |  2 +-
 modules/core/misc/objc/common/Float4.h        |  2 +-
 modules/core/misc/objc/common/Float6.h        |  2 +-
 modules/core/misc/objc/common/Int4.h          |  2 +-
 modules/core/misc/objc/common/KeyPoint.h      |  2 +-
 modules/core/misc/objc/common/Mat.h           |  2 +-
 .../core/misc/objc/common/MinMaxLocResult.h   |  2 +-
 modules/core/misc/objc/common/Point2d.h       |  2 +-
 modules/core/misc/objc/common/Point2f.h       |  2 +-
 modules/core/misc/objc/common/Point2i.h       |  2 +-
 modules/core/misc/objc/common/Point3d.h       |  2 +-
 modules/core/misc/objc/common/Point3f.h       |  2 +-
 modules/core/misc/objc/common/Point3i.h       |  2 +-
 modules/core/misc/objc/common/Range.h         |  2 +-
 modules/core/misc/objc/common/Rect2d.h        |  2 +-
 modules/core/misc/objc/common/Rect2f.h        |  2 +-
 modules/core/misc/objc/common/Rect2i.h        |  2 +-
 modules/core/misc/objc/common/RotatedRect.h   |  2 +-
 modules/core/misc/objc/common/Scalar.h        |  2 +-
 modules/core/misc/objc/common/Size2d.h        |  2 +-
 modules/core/misc/objc/common/Size2f.h        |  2 +-
 modules/core/misc/objc/common/Size2i.h        |  2 +-
 modules/core/misc/objc/common/TermCriteria.h  |  2 +-
 .../imgcodecs/misc/objc/ios/Mat+Converters.h  |  4 +-
 .../misc/objc/macosx/Mat+Converters.h         |  4 +-
 modules/imgproc/misc/objc/common/Moments.h    |  2 +-
 modules/objc/generator/gen_objc.py            | 46 +++++++++++++++----
 .../templates/objc_class_header.template      |  2 +-
 .../templates/objc_module_header.template     |  2 +-
 33 files changed, 70 insertions(+), 44 deletions(-)

diff --git a/modules/core/misc/objc/common/Converters.h b/modules/core/misc/objc/common/Converters.h
index 9a238deb82..29d1b91eb5 100755
--- a/modules/core/misc/objc/common/Converters.h
+++ b/modules/core/misc/objc/common/Converters.h
@@ -7,7 +7,7 @@
 #pragma once
 
 #ifdef __cplusplus
-#import <opencv2/opencv.hpp>
+#import <opencv2/core.hpp>
 #else
 #define CV_EXPORTS
 #endif
diff --git a/modules/core/misc/objc/common/CvType.h b/modules/core/misc/objc/common/CvType.h
index fb6f86aa48..b1fd71d487 100644
--- a/modules/core/misc/objc/common/CvType.h
+++ b/modules/core/misc/objc/common/CvType.h
@@ -5,7 +5,7 @@
 //
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+#import "opencv2/core.hpp"
 #else
 #define CV_EXPORTS
 #endif
diff --git a/modules/core/misc/objc/common/DMatch.h b/modules/core/misc/objc/common/DMatch.h
index 51bed493b8..91c2c59bfa 100644
--- a/modules/core/misc/objc/common/DMatch.h
+++ b/modules/core/misc/objc/common/DMatch.h
@@ -7,7 +7,7 @@
 #pragma once
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+#import "opencv2/core.hpp"
 #else
 #define CV_EXPORTS
 #endif
diff --git a/modules/core/misc/objc/common/Double2.h b/modules/core/misc/objc/common/Double2.h
index 2162acb6d0..8e46c883d0 100644
--- a/modules/core/misc/objc/common/Double2.h
+++ b/modules/core/misc/objc/common/Double2.h
@@ -7,7 +7,7 @@
 #pragma once
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+#import "opencv2/core.hpp"
 #else
 #define CV_EXPORTS
 #endif
diff --git a/modules/core/misc/objc/common/Double3.h b/modules/core/misc/objc/common/Double3.h
index 2aaba9af80..5c741648f7 100644
--- a/modules/core/misc/objc/common/Double3.h
+++ b/modules/core/misc/objc/common/Double3.h
@@ -7,7 +7,7 @@
 #pragma once
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+#import "opencv2/core.hpp"
 #else
 #define CV_EXPORTS
 #endif
diff --git a/modules/core/misc/objc/common/Float4.h b/modules/core/misc/objc/common/Float4.h
index 2a89278040..c78e88b72e 100644
--- a/modules/core/misc/objc/common/Float4.h
+++ b/modules/core/misc/objc/common/Float4.h
@@ -7,7 +7,7 @@
 #pragma once
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+#import "opencv2/core.hpp"
 #else
 #define CV_EXPORTS
 #endif
diff --git a/modules/core/misc/objc/common/Float6.h b/modules/core/misc/objc/common/Float6.h
index d2ec19a60e..7e09772c5c 100644
--- a/modules/core/misc/objc/common/Float6.h
+++ b/modules/core/misc/objc/common/Float6.h
@@ -7,7 +7,7 @@
 #pragma once
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+#import "opencv2/core.hpp"
 #else
 #define CV_EXPORTS
 #endif
diff --git a/modules/core/misc/objc/common/Int4.h b/modules/core/misc/objc/common/Int4.h
index 1a17266572..11cc12db14 100644
--- a/modules/core/misc/objc/common/Int4.h
+++ b/modules/core/misc/objc/common/Int4.h
@@ -7,7 +7,7 @@
 #pragma once
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+#import "opencv2/core.hpp"
 #else
 #define CV_EXPORTS
 #endif
diff --git a/modules/core/misc/objc/common/KeyPoint.h b/modules/core/misc/objc/common/KeyPoint.h
index 547960dc9d..096a1089c9 100644
--- a/modules/core/misc/objc/common/KeyPoint.h
+++ b/modules/core/misc/objc/common/KeyPoint.h
@@ -7,7 +7,7 @@
 #pragma once
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+#import "opencv2/core.hpp"
 #else
 #define CV_EXPORTS
 #endif
diff --git a/modules/core/misc/objc/common/Mat.h b/modules/core/misc/objc/common/Mat.h
index 229337f524..fd1dce27ba 100644
--- a/modules/core/misc/objc/common/Mat.h
+++ b/modules/core/misc/objc/common/Mat.h
@@ -7,7 +7,7 @@
 #pragma once
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+#import "opencv2/core.hpp"
 #else
 #define CV_EXPORTS
 #endif
diff --git a/modules/core/misc/objc/common/MinMaxLocResult.h b/modules/core/misc/objc/common/MinMaxLocResult.h
index e8daed4cc3..5ec6029e31 100644
--- a/modules/core/misc/objc/common/MinMaxLocResult.h
+++ b/modules/core/misc/objc/common/MinMaxLocResult.h
@@ -7,7 +7,7 @@
 #pragma once
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+#import "opencv2/core.hpp"
 #else
 #define CV_EXPORTS
 #endif
diff --git a/modules/core/misc/objc/common/Point2d.h b/modules/core/misc/objc/common/Point2d.h
index dbb8d55efa..0426b11d9a 100644
--- a/modules/core/misc/objc/common/Point2d.h
+++ b/modules/core/misc/objc/common/Point2d.h
@@ -7,7 +7,7 @@
 #pragma once
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+#import "opencv2/core.hpp"
 #else
 #define CV_EXPORTS
 #endif
diff --git a/modules/core/misc/objc/common/Point2f.h b/modules/core/misc/objc/common/Point2f.h
index 0da4fba5d8..6d13c774d8 100644
--- a/modules/core/misc/objc/common/Point2f.h
+++ b/modules/core/misc/objc/common/Point2f.h
@@ -7,7 +7,7 @@
 #pragma once
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+#import "opencv2/core.hpp"
 #else
 #define CV_EXPORTS
 #endif
diff --git a/modules/core/misc/objc/common/Point2i.h b/modules/core/misc/objc/common/Point2i.h
index 9e5d74624a..e43ee3a8ec 100644
--- a/modules/core/misc/objc/common/Point2i.h
+++ b/modules/core/misc/objc/common/Point2i.h
@@ -7,7 +7,7 @@
 #pragma once
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+#import "opencv2/core.hpp"
 #else
 #define CV_EXPORTS
 #endif
diff --git a/modules/core/misc/objc/common/Point3d.h b/modules/core/misc/objc/common/Point3d.h
index 72b0d39ea8..618ded35fa 100644
--- a/modules/core/misc/objc/common/Point3d.h
+++ b/modules/core/misc/objc/common/Point3d.h
@@ -7,7 +7,7 @@
 #pragma once
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+#import "opencv2/core.hpp"
 #else
 #define CV_EXPORTS
 #endif
diff --git a/modules/core/misc/objc/common/Point3f.h b/modules/core/misc/objc/common/Point3f.h
index 2370fffeaa..c98add1cec 100644
--- a/modules/core/misc/objc/common/Point3f.h
+++ b/modules/core/misc/objc/common/Point3f.h
@@ -7,7 +7,7 @@
 #pragma once
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+#import "opencv2/core.hpp"
 #else
 #define CV_EXPORTS
 #endif
diff --git a/modules/core/misc/objc/common/Point3i.h b/modules/core/misc/objc/common/Point3i.h
index b0edeaa470..9eab2ee0ea 100644
--- a/modules/core/misc/objc/common/Point3i.h
+++ b/modules/core/misc/objc/common/Point3i.h
@@ -7,7 +7,7 @@
 #pragma once
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+#import "opencv2/core.hpp"
 #else
 #define CV_EXPORTS
 #endif
diff --git a/modules/core/misc/objc/common/Range.h b/modules/core/misc/objc/common/Range.h
index dd84edf6aa..df0c01398f 100644
--- a/modules/core/misc/objc/common/Range.h
+++ b/modules/core/misc/objc/common/Range.h
@@ -7,7 +7,7 @@
 #pragma once
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+#import "opencv2/core.hpp"
 #else
 #define CV_EXPORTS
 #endif
diff --git a/modules/core/misc/objc/common/Rect2d.h b/modules/core/misc/objc/common/Rect2d.h
index ba91509b77..0ffcae9ab6 100644
--- a/modules/core/misc/objc/common/Rect2d.h
+++ b/modules/core/misc/objc/common/Rect2d.h
@@ -7,7 +7,7 @@
 #pragma once
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+#import "opencv2/core.hpp"
 #else
 #define CV_EXPORTS
 #endif
diff --git a/modules/core/misc/objc/common/Rect2f.h b/modules/core/misc/objc/common/Rect2f.h
index 6a8863800f..1f44f56263 100644
--- a/modules/core/misc/objc/common/Rect2f.h
+++ b/modules/core/misc/objc/common/Rect2f.h
@@ -7,7 +7,7 @@
 #pragma once
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+#import "opencv2/core.hpp"
 #else
 #define CV_EXPORTS
 #endif
diff --git a/modules/core/misc/objc/common/Rect2i.h b/modules/core/misc/objc/common/Rect2i.h
index 2e4e55cf30..6ed86d50bd 100644
--- a/modules/core/misc/objc/common/Rect2i.h
+++ b/modules/core/misc/objc/common/Rect2i.h
@@ -7,7 +7,7 @@
 #pragma once
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+#import "opencv2/core.hpp"
 #else
 #define CV_EXPORTS
 #endif
diff --git a/modules/core/misc/objc/common/RotatedRect.h b/modules/core/misc/objc/common/RotatedRect.h
index c94053b6c1..a2049e6bf0 100644
--- a/modules/core/misc/objc/common/RotatedRect.h
+++ b/modules/core/misc/objc/common/RotatedRect.h
@@ -7,7 +7,7 @@
 #pragma once
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+#import "opencv2/core.hpp"
 #else
 #define CV_EXPORTS
 #endif
diff --git a/modules/core/misc/objc/common/Scalar.h b/modules/core/misc/objc/common/Scalar.h
index 63c3d1de58..d565155010 100644
--- a/modules/core/misc/objc/common/Scalar.h
+++ b/modules/core/misc/objc/common/Scalar.h
@@ -7,7 +7,7 @@
 #pragma once
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+#import "opencv2/core.hpp"
 #else
 #define CV_EXPORTS
 #endif
diff --git a/modules/core/misc/objc/common/Size2d.h b/modules/core/misc/objc/common/Size2d.h
index 11c6c50a02..cd2e4e4bc0 100644
--- a/modules/core/misc/objc/common/Size2d.h
+++ b/modules/core/misc/objc/common/Size2d.h
@@ -7,7 +7,7 @@
 #pragma once
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+#import "opencv2/core.hpp"
 #else
 #define CV_EXPORTS
 #endif
diff --git a/modules/core/misc/objc/common/Size2f.h b/modules/core/misc/objc/common/Size2f.h
index 2d1f2865c3..73ae9a2da0 100644
--- a/modules/core/misc/objc/common/Size2f.h
+++ b/modules/core/misc/objc/common/Size2f.h
@@ -7,7 +7,7 @@
 #pragma once
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+#import "opencv2/core.hpp"
 #else
 #define CV_EXPORTS
 #endif
diff --git a/modules/core/misc/objc/common/Size2i.h b/modules/core/misc/objc/common/Size2i.h
index 61aa8da885..cd74e2c84a 100644
--- a/modules/core/misc/objc/common/Size2i.h
+++ b/modules/core/misc/objc/common/Size2i.h
@@ -7,7 +7,7 @@
 #pragma once
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+#import "opencv2/core.hpp"
 #else
 #define CV_EXPORTS
 #endif
diff --git a/modules/core/misc/objc/common/TermCriteria.h b/modules/core/misc/objc/common/TermCriteria.h
index c7396582b2..ff6bfd565c 100644
--- a/modules/core/misc/objc/common/TermCriteria.h
+++ b/modules/core/misc/objc/common/TermCriteria.h
@@ -7,7 +7,7 @@
 #pragma once
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+#import "opencv2/core.hpp"
 #else
 #define CV_EXPORTS
 #endif
diff --git a/modules/imgcodecs/misc/objc/ios/Mat+Converters.h b/modules/imgcodecs/misc/objc/ios/Mat+Converters.h
index d33abbf4f9..8c185f884a 100644
--- a/modules/imgcodecs/misc/objc/ios/Mat+Converters.h
+++ b/modules/imgcodecs/misc/objc/ios/Mat+Converters.h
@@ -7,14 +7,14 @@
 #pragma once
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+#import "opencv2/core.hpp"
 #else
 #define CV_EXPORTS
 #endif
 
+#import "Mat.h"
 #import <Foundation/Foundation.h>
 #import <UIKit/UIKit.h>
-#import "Mat.h"
 
 NS_ASSUME_NONNULL_BEGIN
 
diff --git a/modules/imgcodecs/misc/objc/macosx/Mat+Converters.h b/modules/imgcodecs/misc/objc/macosx/Mat+Converters.h
index 4abf806d1e..d87887372d 100644
--- a/modules/imgcodecs/misc/objc/macosx/Mat+Converters.h
+++ b/modules/imgcodecs/misc/objc/macosx/Mat+Converters.h
@@ -7,14 +7,14 @@
 #pragma once
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+#import "opencv2/core.hpp"
 #else
 #define CV_EXPORTS
 #endif
 
+#import "Mat.h"
 #import <Foundation/Foundation.h>
 #import <AppKit/AppKit.h>
-#import "Mat.h"
 
 NS_ASSUME_NONNULL_BEGIN
 
diff --git a/modules/imgproc/misc/objc/common/Moments.h b/modules/imgproc/misc/objc/common/Moments.h
index dfa5653bac..8ce3f75ea8 100644
--- a/modules/imgproc/misc/objc/common/Moments.h
+++ b/modules/imgproc/misc/objc/common/Moments.h
@@ -7,7 +7,7 @@
 #pragma once
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+#import "opencv2/core.hpp"
 #else
 #define CV_EXPORTS
 #endif
diff --git a/modules/objc/generator/gen_objc.py b/modules/objc/generator/gen_objc.py
index e6637a7c4c..a5ffa7e874 100755
--- a/modules/objc/generator/gen_objc.py
+++ b/modules/objc/generator/gen_objc.py
@@ -104,6 +104,15 @@ def mkdir_p(path):
         else:
             raise
 
+def header_import(hdr):
+    """ converts absolute header path to import parameter """
+    pos = hdr.find('/include/')
+    hdr = hdr[pos+9 if pos >= 0 else 0:]
+    #pos = hdr.find('opencv2/')
+    #hdr = hdr[pos+8 if pos >= 0 else 0:]
+    return hdr
+
+
 T_OBJC_CLASS_HEADER = read_contents(os.path.join(SCRIPT_DIR, 'templates/objc_class_header.template'))
 T_OBJC_CLASS_BODY = read_contents(os.path.join(SCRIPT_DIR, 'templates/objc_class_body.template'))
 T_OBJC_MODULE_HEADER = read_contents(os.path.join(SCRIPT_DIR, 'templates/objc_module_header.template'))
@@ -693,17 +702,17 @@ class ObjectiveCWrapperGenerator(object):
         classinfo = ClassInfo(decl, namespaces=self.namespaces)
         if classinfo.name in class_ignore_list:
             logging.info('ignored: %s', classinfo)
-            return
+            return None
         if classinfo.name != self.Module:
             self.classes[self.Module].member_classes.append(classinfo.objc_name)
         name = classinfo.cname
         if self.isWrapped(name) and not classinfo.base:
             logging.warning('duplicated: %s', classinfo)
-            return
+            return None
         self.classes[name] = classinfo
         if name in type_dict and not classinfo.base:
             logging.warning('duplicated: %s', classinfo)
-            return
+            return None
         if name != self.Module:
             type_dict.setdefault(name, {}).update(
                 { "objc_type" : classinfo.objc_name + "*",
@@ -731,6 +740,7 @@ class ObjectiveCWrapperGenerator(object):
             )
 
         logging.info('ok: class %s, name: %s, base: %s', classinfo, name, classinfo.base)
+        return classinfo
 
     def add_const(self, decl, scope=None, enumType=None): # [ "const cname", val, [], [] ]
         constinfo = ConstInfo(decl, namespaces=self.namespaces, enumType=enumType)
@@ -837,27 +847,30 @@ class ObjectiveCWrapperGenerator(object):
         # TODO: support UMat versions of declarations (implement UMat-wrapper for Java)
         parser = hdr_parser.CppHeaderParser(generate_umat_decls=False)
 
-        self.add_class( ['class ' + self.Module, '', [], []]) # [ 'class/struct cname', ':bases', [modlist] [props] ]
+        module_ci = self.add_class( ['class ' + self.Module, '', [], []]) # [ 'class/struct cname', ':bases', [modlist] [props] ]
+        module_ci.header_import = module + '.hpp'
 
         # scan the headers and build more descriptive maps of classes, consts, functions
         includes = []
         for hdr in common_headers:
             logging.info("\n===== Common header : %s =====", hdr)
-            includes.append('#include "' + hdr + '"')
+            includes.append(header_import(hdr))
         for hdr in srcfiles:
             decls = parser.parse(hdr)
             self.namespaces = parser.namespaces
             logging.info("\n\n===== Header: %s =====", hdr)
             logging.info("Namespaces: %s", parser.namespaces)
             if decls:
-                includes.append('#include "' + hdr + '"')
+                includes.append(header_import(hdr))
             else:
                 logging.info("Ignore header: %s", hdr)
             for decl in decls:
                 logging.info("\n--- Incoming ---\n%s", pformat(decl[:5], 4)) # without docstring
                 name = decl[0]
                 if name.startswith("struct") or name.startswith("class"):
-                    self.add_class(decl)
+                    ci = self.add_class(decl)
+                    if ci:
+                        ci.header_import = header_import(hdr)
                 elif name.startswith("const"):
                     self.add_const(decl)
                 elif name.startswith("enum"):
@@ -1190,13 +1203,26 @@ $unrefined_call$epilogue$ret
 
     def gen_class(self, ci, module, extension_implementations, extension_signatures):
         logging.info("%s", ci)
-        if module in AdditionalImports and (ci.name in AdditionalImports[module] or "*" in AdditionalImports[module]):
-            additional_imports = []
+        additional_imports = []
+        if module in AdditionalImports:
             if "*" in AdditionalImports[module]:
                 additional_imports += AdditionalImports[module]["*"]
             if ci.name in AdditionalImports[module]:
                 additional_imports += AdditionalImports[module][ci.name]
-            ci.additionalImports.write("\n".join(["#import %s" % h for h in additional_imports]))
+        if hasattr(ci, 'header_import'):
+            h = '"{}"'.format(ci.header_import)
+            if not h in additional_imports:
+                additional_imports.append(h)
+
+        h = '"{}.hpp"'.format(module)
+        if h in additional_imports:
+            additional_imports.remove(h)
+        h = '"opencv2/{}.hpp"'.format(module)
+        if not h in additional_imports:
+            additional_imports.insert(0, h)
+
+        if additional_imports:
+            ci.additionalImports.write('\n'.join(['#import %s' % h for h in additional_imports]))
 
         # constants
         wrote_consts_pragma = False
diff --git a/modules/objc/generator/templates/objc_class_header.template b/modules/objc/generator/templates/objc_class_header.template
index 0bad670685..77697e8c93 100644
--- a/modules/objc/generator/templates/objc_class_header.template
+++ b/modules/objc/generator/templates/objc_class_header.template
@@ -4,7 +4,7 @@
 #pragma once
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+//#import "opencv.hpp"
 $additionalImports
 #else
 #define CV_EXPORTS
diff --git a/modules/objc/generator/templates/objc_module_header.template b/modules/objc/generator/templates/objc_module_header.template
index fa9e7df6a3..88f45a11cf 100644
--- a/modules/objc/generator/templates/objc_module_header.template
+++ b/modules/objc/generator/templates/objc_module_header.template
@@ -4,7 +4,7 @@
 #pragma once
 
 #ifdef __cplusplus
-#import "opencv.hpp"
+//#import "opencv.hpp"
 $additionalImports
 #else
 #define CV_EXPORTS