Repository: opencv
Issue: Build fails on macOS if '-D WITH_VULKAN=ON'
Labels: feature,category: build/install,platform: ios/osx

<!--
If you have a question rather than reporting a bug please go to https://forum.opencv.org where you get much faster responses.
If you need further assistance please read [How To Contribute](https://github.com/opencv/opencv/wiki/How_to_contribute).

This is a template helping you to create an issue which can be processed as quickly as possible. This is the bug reporting section for the OpenCV library.
-->

##### System information (version)
<!-- Example
- OpenCV => 4.2
- Operating System / Platform => Windows 64 Bit
- Compiler => Visual Studio 2017
-->

- OpenCV => 4.x from sources, master branch, latest commit (e4abf6e)
- Operating System / Platform => macOS 12.3 Monterey, Apple M1 (arm64)
- Compiler => cmake 3.22.3

##### Detailed description

I'm not sure how stupid this question is, so apologies if it is 😛 

Building OpenCV fails if cmake is ran with ```-D WITH_VULKAN=ON```
FWIW, I use the [LunarG](https://vulkan.lunarg.com/) Vulkan SDK, which uses MoltenVK on macOS for leveraging Metal.

The error comes from [here](https://github.com/opencv/opencv/blob/e4abf6e723e80133934a0d979dedb61812e9956e/modules/dnn/src/vkcom/vulkan/vk_loader.cpp#L45)
```error: unknown type name 'VulkanHandle'```

Indeed, there is no ```#if defined``` block for Apple, only for Windows and Linux.

##### Steps to reproduce

Here is the cmake command (minus irrelevant things)
```bash
cmake \
    -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_SYSTEM_PROCESSOR=arm64 \
    -D CMAKE_OSX_ARCHITECTURES=arm64 \
    -D BUILD_opencv_python2=OFF \
    -D BUILD_opencv_python3=ON \
    -D VTK_DIR='/opt/homebrew/Cellar/vtk@8.2/8.2.0_7/lib/cmake/vtk-8.2' \
    -D OPENCV_EXTRA_MODULES_PATH='../opencv_contrib/modules' \
    -D PYTHON3_EXECUTABLE=$(pyenv which python3.9) \
    -D PYTHON3_INCLUDE_DIR=$(python3.9-config --includes | awk '{print $1}' | sed 's/^\-I//') \
    -D PYTHON3_NUMPY_INCLUDE_DIRS=$(python3.9 -c "import numpy; print(numpy.get_include())") \
    -D WITH_VULKAN=ON \
    -D OPENCV_ENABLE_NONFREE=ON \
    ../opencv
```

##### Steps to fix

Added this to the vk_loader.cpp file:
```
#if defined(__APPLE__)
#include <dlfcn.h>
#include <stdio.h>
typedef void* VulkanHandle;
#define DEFAULT_VK_LIBRARY_PATH "libvulkan.dylib"
#define LOAD_VK_LIBRARY(path) dlopen(path, RTLD_LAZY | RTLD_GLOBAL)
#define FREE_VK_LIBRARY(handle) dlclose(handle)
#define GET_VK_ENTRY_POINT(handle) \
        (PFN_vkGetInstanceProcAddr)dlsym(handle, "vkGetInstanceProcAddr");
#endif // __apple__
```

Also added these to the cmake command:
```
    -D VULKAN_INCLUDE_DIRS='<path-to-VulkanSDK>/macOS/include' \
    -D VULKAN_LIBRARIES='<path-to-VulkanSDK>/macOS/lib/libvulkan.dylib' \
```

Build and install completed fine. I didn't test yet if it's all working as expected though...

##### Issue submission checklist

 - [x] I report the issue, it's not a question
   <!--
   OpenCV team works with forum.opencv.org, Stack Overflow and other communities
   to discuss problems. Tickets with questions without a real issue statement will be
   closed.
   -->
 - [x] I checked the problem with documentation, FAQ, open issues,
       forum.opencv.org, Stack Overflow, etc and have not found any solution
   <!--
   Places to check:
   * OpenCV documentation: https://docs.opencv.org
   * FAQ page: https://github.com/opencv/opencv/wiki/FAQ
   * OpenCV forum: https://forum.opencv.org
   * OpenCV issue tracker: https://github.com/opencv/opencv/issues?q=is%3Aissue
   * Stack Overflow branch: https://stackoverflow.com/questions/tagged/opencv
   -->
 - [x] I updated to the latest OpenCV version and the issue is still there
   <!--
   master branch for OpenCV 4.x and 3.4 branch for OpenCV 3.x releases.
   OpenCV team supports only the latest release for each branch.
   The ticket is closed if the problem is not reproduced with the modern version.
   -->
 - [x] There is reproducer code and related data files: videos, images, onnx, etc
   <!--
   The best reproducer -- test case for OpenCV that we can add to the library.
   Recommendations for media files and binary files:
   * Try to reproduce the issue with images and videos in opencv_extra repository
     to reduce attachment size
   * Use PNG for images, if you report some CV related bug, but not image reader
     issue
   * Attach the image as an archive to the ticket, if you report some reader issue.
     Image hosting services compress images and it breaks the repro code.
   * Provide ONNX file for some public model or ONNX file with random weights,
     if you report ONNX parsing or handling issue. Architecture details diagram
     from netron tool can be very useful too. See https://lutzroeder.github.io/netron/
   -->