1#.rst: 2# FindVulkanHeaders 3# ----------------- 4# 5# Try to find Vulkan Headers and Registry. 6# 7# This module is intended to be used by projects that build Vulkan 8# "system" components such as the loader and layers. 9# Vulkan applications should instead use the FindVulkan (or similar) 10# find module that locates the headers and the loader library. 11# 12# When using this find module to locate the headers and registry 13# in a Vulkan-Headers repository, the Vulkan-Headers repository 14# should be built with 'install' target and the following environment 15# or CMake variable set to the location of the install directory. 16# 17# VULKAN_HEADERS_INSTALL_DIR 18# 19# IMPORTED Targets 20# ^^^^^^^^^^^^^^^^ 21# 22# This module defines no IMPORTED targets 23# 24# Result Variables 25# ^^^^^^^^^^^^^^^^ 26# 27# This module defines the following variables:: 28# 29# VulkanHeaders_FOUND - True if VulkanHeaders was found 30# VulkanHeaders_INCLUDE_DIRS - include directories for VulkanHeaders 31# 32# VulkanRegistry_FOUND - True if VulkanRegistry was found 33# VulkanRegistry_DIRS - directories for VulkanRegistry 34# 35# VulkanHeaders_VERSION_MAJOR - The Major API version of the latest version 36# contained in the Vulkan header 37# VulkanHeaders_VERSION_MINOR - The Minor API version of the latest version 38# contained in the Vulkan header 39# VulkanHeaders_VERSION_PATCH - The Patch API version of the latest version 40# contained in the Vulkan header 41# 42# The module will also define two cache variables:: 43# 44# VulkanHeaders_INCLUDE_DIR - the VulkanHeaders include directory 45# VulkanRegistry_DIR - the VulkanRegistry directory 46# 47 48# Probe command-line arguments and the environment to see if they specify the 49# Vulkan headers installation path. 50if(NOT DEFINED VULKAN_HEADERS_INSTALL_DIR) 51 if (DEFINED ENV{VULKAN_HEADERS_INSTALL_DIR}) 52 set(VULKAN_HEADERS_INSTALL_DIR "$ENV{VULKAN_HEADERS_INSTALL_DIR}") 53 elseif(DEFINED ENV{VULKAN_SDK}) 54 set(VULKAN_HEADERS_INSTALL_DIR "$ENV{VULKAN_SDK}/include") 55 endif() 56endif() 57 58if(DEFINED VULKAN_HEADERS_INSTALL_DIR) 59 # When CMAKE_FIND_ROOT_PATH_INCLUDE is set to ONLY, the HINTS in find_path() 60 # are re-rooted, which prevents VULKAN_HEADERS_INSTALL_DIR to work as 61 # expected. So use NO_CMAKE_FIND_ROOT_PATH to avoid it. 62 63 # Use HINTS instead of PATH to search these locations before 64 # searching system environment variables like $PATH that may 65 # contain SDK directories. 66 find_path(VulkanHeaders_INCLUDE_DIR 67 NAMES vulkan/vulkan.h 68 HINTS ${VULKAN_HEADERS_INSTALL_DIR}/include 69 NO_CMAKE_FIND_ROOT_PATH) 70 find_path(VulkanRegistry_DIR 71 NAMES vk.xml 72 HINTS ${VULKAN_HEADERS_INSTALL_DIR}/share/vulkan/registry 73 NO_CMAKE_FIND_ROOT_PATH) 74else() 75 # If VULKAN_HEADERS_INSTALL_DIR, or one of its variants was not specified, 76 # do a normal search without hints. 77 find_path(VulkanHeaders_INCLUDE_DIR NAMES vulkan/vulkan.h HINTS "${CMAKE_CURRENT_SOURCE_DIR}/external/Vulkan-Headers/include" NO_CMAKE_FIND_ROOT_PATH) 78 get_filename_component(VULKAN_REGISTRY_PATH_HINT ${VulkanHeaders_INCLUDE_DIR} DIRECTORY) 79 find_path(VulkanRegistry_DIR NAMES vk.xml HINTS ${VULKAN_REGISTRY_PATH_HINT}/share/vulkan/registry 80 "${VULKAN_REGISTRY_PATH_HINT}/registry" NO_CMAKE_FIND_ROOT_PATH) 81endif() 82 83set(VulkanHeaders_INCLUDE_DIRS ${VulkanHeaders_INCLUDE_DIR}) 84set(VulkanRegistry_DIRS ${VulkanRegistry_DIR}) 85 86include(FindPackageHandleStandardArgs) 87find_package_handle_standard_args(VulkanHeaders 88 DEFAULT_MSG 89 VulkanHeaders_INCLUDE_DIR) 90set(FPHSA_NAME_MISMATCHED TRUE) 91find_package_handle_standard_args(VulkanRegistry 92 DEFAULT_MSG 93 VulkanRegistry_DIR) 94unset(FPHSA_NAME_MISMATCHED) 95 96mark_as_advanced(VulkanHeaders_INCLUDE_DIR VulkanRegistry_DIR) 97 98# Determine the major/minor/patch version from the vulkan header 99set(VulkanHeaders_VERSION_MAJOR "0") 100set(VulkanHeaders_VERSION_MINOR "0") 101set(VulkanHeaders_VERSION_PATCH "0") 102 103# First, determine which header we need to grab the version information from. 104# Starting with Vulkan 1.1, we should use vulkan_core.h, but prior to that, 105# the information was in vulkan.h. 106if (EXISTS "${VulkanHeaders_INCLUDE_DIR}/vulkan/vulkan_core.h") 107 set(VulkanHeaders_main_header ${VulkanHeaders_INCLUDE_DIR}/vulkan/vulkan_core.h) 108else() 109 set(VulkanHeaders_main_header ${VulkanHeaders_INCLUDE_DIR}/vulkan/vulkan.h) 110endif() 111 112# Find all lines in the header file that contain any version we may be interested in 113# NOTE: They start with #define and then have other keywords 114file(STRINGS 115 ${VulkanHeaders_main_header} 116 VulkanHeaders_lines 117 REGEX "^#define VK_HEADER_VERSION(_COMPLETE)? ") 118 119foreach(VulkanHeaders_line ${VulkanHeaders_lines}) 120 121 # First, handle the case where we have a major/minor version 122 # Format is: 123 # #define VK_HEADER_VERSION_COMPLETE VK_MAKE_API_VERSION(0, X, Y, VK_HEADER_VERSION) 124 # We grab the major version (X) and minor version (Y) out of the parentheses 125 string(REGEX MATCH "VK_HEADER_VERSION_COMPLETE VK_MAKE_API_VERSION\\(.*\\)" VulkanHeaders_out ${VulkanHeaders_line}) 126 string(REGEX MATCHALL "[0-9]+" VulkanHeaders_MAJOR_MINOR "${VulkanHeaders_out}") 127 if (VulkanHeaders_MAJOR_MINOR) 128 list (GET VulkanHeaders_MAJOR_MINOR 1 VulkanHeaders_cur_major) 129 list (GET VulkanHeaders_MAJOR_MINOR 2 VulkanHeaders_cur_minor) 130 if (${VulkanHeaders_cur_major} GREATER ${VulkanHeaders_VERSION_MAJOR}) 131 set(VulkanHeaders_VERSION_MAJOR ${VulkanHeaders_cur_major}) 132 set(VulkanHeaders_VERSION_MINOR ${VulkanHeaders_cur_minor}) 133 endif() 134 if (${VulkanHeaders_cur_major} EQUAL ${VulkanHeaders_VERSION_MAJOR} AND 135 ${VulkanHeaders_cur_minor} GREATER ${VulkanHeaders_VERSION_MINOR}) 136 set(VulkanHeaders_VERSION_MINOR ${VulkanHeaders_cur_minor}) 137 endif() 138 endif() 139 140 # Second, handle the case where we have the patch version 141 # Format is: 142 # #define VK_HEADER_VERSION Z 143 # Where Z is the patch version which we just grab off the end 144 string(REGEX MATCH "define.*VK_HEADER_VERSION[^_].*[0-9]+" VulkanHeaders_out ${VulkanHeaders_line}) 145 list(LENGTH VulkanHeaders_out VulkanHeaders_len) 146 if (VulkanHeaders_len) 147 string(REGEX MATCH "[0-9]+" VulkanHeaders_VERSION_PATCH "${VulkanHeaders_out}") 148 endif() 149 150endforeach() 151MESSAGE(STATUS 152 "Detected Vulkan Version ${VulkanHeaders_VERSION_MAJOR}." 153 "${VulkanHeaders_VERSION_MINOR}." 154 "${VulkanHeaders_VERSION_PATCH}") 155