1# Copyright 2018 The Amber Authors.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15# Include this file to find Vulkan and and set up compilation and linking.
16
17# Export these settings to the includer.
18set(Vulkan_FOUND FALSE)
19set(VULKAN_CTS_HEADER FALSE)
20set(VULKAN_LIB "")
21
22if (NOT ${Vulkan_FOUND})
23  if (${AMBER_USE_LOCAL_VULKAN})
24    set(Vulkan_FOUND TRUE)
25    set(VulkanHeaders_INCLUDE_DIR
26      ${PROJECT_SOURCE_DIR}/third_party/vulkan-headers/include
27      CACHE PATH "vk headers dir" FORCE)
28    set(VulkanHeaders_INCLUDE_DIRS ${VulkanHeaders_INCLUDE_DIR}
29      CACHE PATH "vk headers dir" FORCE)
30    set(VulkanRegistry_DIR
31      ${PROJECT_SOURCE_DIR}/third_party/vulkan-headers/registry
32      CACHE PATH "vk_registry_dir" FORCE)
33    set(VulkanRegistry_DIRS ${VulkanRegistry_DIR}
34      CACHE PATH "vk_registry_dir" FORCE)
35    set(VULKAN_LIB vulkan)
36    message(STATUS "Amber: using local vulkan")
37  endif()
38endif()
39
40if (NOT ${Vulkan_FOUND})
41  # Our first choice is to pick up the Vulkan headers from an enclosing project.
42  # And if that's the case, then use Vulkan libraries as specified by
43  # Vulkan_LIBRARIES, with a default library of "vulkan".
44  set(X "${Vulkan-Headers_SOURCE_DIR}/include")
45  if (IS_DIRECTORY "${X}")
46    message(STATUS "Amber: Using Vulkan header dir ${X}")
47    list(APPEND CMAKE_REQUIRED_INCLUDES "${X}")
48
49    include_directories(BEFORE "${X}")
50    CHECK_INCLUDE_FILE(vulkan/vulkan.h HAVE_VULKAN_HEADER)
51
52    if (${HAVE_VULKAN_HEADER})
53      if ("${Vulkan_LIBRARIES}" STREQUAL "")
54        message(STATUS "Amber: Defaulting to Vulkan library: vulkan")
55        set(VULKAN_LIB vulkan)
56      else()
57        message(STATUS "Amber: Using specified Vulkan libraries: ${Vulkan_LIBRARIES}")
58        set(VULKAN_LIB "${Vulkan_LIBRARIES}")
59      endif()
60
61      # For now assume we have Vulkan.  We have its header, but we haven't checked
62      # for the library.
63      # TODO(dneto): Actually check for the libraries.
64      set(Vulkan_FOUND TRUE)
65      set(VulkanHeaders_INCLUDE_DIR "${X}")
66      set(VulkanHeaders_INCLUDE_DIRS "${VulkanHeaders_INCLUDE_DIR}")
67    endif()
68  endif()
69  unset(X)
70endif()
71
72# Check if we're in the CTS
73if (NOT ${Vulkan_FOUND})
74  message(STATUS "Amber: Checking for CTS Vulkan header")
75  set(X "${Vulkan-Headers_SOURCE_DIR}")
76  if (IS_DIRECTORY "${X}")
77    message(STATUS "Amber: Using Vulkan header dir ${X}")
78    list(APPEND CMAKE_REQUIRED_INCLUDES "${X}")
79
80    include_directories(BEFORE "${X}")
81
82    if (EXISTS "${X}/vkDefs.h")
83      set(VULKAN_CTS_HEADER TRUE)
84      set(Vulkan_FOUND TRUE)
85      set(VulkanHeaders_INCLUDE_DIR "${X}")
86      set(VulkanHeaders_INCLUDE_DIRS "${VulkanHeaders_INCLUDE_DIR}")
87    endif()
88  endif()
89  unset(X)
90endif()
91
92if (NOT ${Vulkan_FOUND})
93  # If we aren't already building a Vulkan library, then use CMake to find it.
94  if(NOT ${CMAKE_VERSION} VERSION_LESS "3.7")
95    # LunarG added FindVulkan support to CMake 3.7.  If you have the Vulkan SDK
96    # published by LunarG, then set environment variables:
97    #  VULKAN_SDK should point to the platform-specific SDK directory containing
98    #    the include and lib directories.
99    #  VK_ICD_FILENAMES should point to ICD JSON file.
100
101    # Example, with the LunarG SDK macOS edition with MoltenVK:
102    #  export VULKAN_SDK="$HOME/vulkan-macos-1.1.85.0/macOS"
103    #  export VK_ICD_FILENAMES="$VULKAN_SDK/etc/vulkan/icd/MoltenVK_icd.json"
104    # See https://cmake.org/cmake/help/v3.7/module/FindVulkan.html
105    find_package(Vulkan)
106    if(${Vulkan_FOUND})
107      message(STATUS "Amber: Using Vulkan from Vulkan SDK at $ENV{VULKAN_SDK}")
108      # Use the imported library target set up by find_package.
109      set(VULKAN_LIB Vulkan::Vulkan)
110      set(VulkanHeaders_INCLUDE_DIR "${Vulkan_INCLUDE_DIR}"
111          CACHE PATH "vk headers dir" FORCE)
112      set(VulkanHeaders_INCLUDE_DIRS "${Vulkan_INCLUDE_DIRS}"
113          CACHE PATH "vk headers dir" FORCE)
114    endif()
115  endif()
116endif()
117
118if (NOT ${Vulkan_FOUND})
119  message(STATUS "Amber: Did not find Vulkan")
120endif()
121