• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
26    set(VulkanHeaders_INCLUDE_DIR
27      ${PROJECT_SOURCE_DIR}/third_party/vulkan-headers/include
28      CACHE PATH "vk headers dir" FORCE)
29    set(VulkanHeaders_INCLUDE_DIRS ${VulkanHeaders_INCLUDE_DIR}
30      CACHE PATH "vk headers dir" FORCE)
31    set(VulkanRegistry_DIR
32      ${PROJECT_SOURCE_DIR}/third_party/vulkan-headers/registry
33      CACHE PATH "vk_registry_dir" FORCE)
34    set(VulkanRegistry_DIRS ${VulkanRegistry_DIR}
35      CACHE PATH "vk_registry_dir" FORCE)
36    set(VULKAN_LIB vulkan)
37
38    message(STATUS "Amber: using local vulkan ${VulkanHeaders_INCLUDE_DIR}")
39  endif()
40endif()
41
42if (NOT ${Vulkan_FOUND})
43  # Our first choice is to pick up the Vulkan headers from an enclosing project.
44  # And if that's the case, then use Vulkan libraries as specified by
45  # Vulkan_LIBRARIES, with a default library of "vulkan".
46  set(X "${Vulkan-Headers_SOURCE_DIR}/include")
47  if (IS_DIRECTORY "${X}")
48    message(STATUS "Amber: Using Vulkan header dir ${X}")
49    list(APPEND CMAKE_REQUIRED_INCLUDES "${X}")
50
51    include_directories(BEFORE "${X}")
52    CHECK_INCLUDE_FILE(vulkan/vulkan.h HAVE_VULKAN_HEADER)
53
54    if (${HAVE_VULKAN_HEADER})
55      if ("${Vulkan_LIBRARIES}" STREQUAL "")
56        message(STATUS "Amber: Defaulting to Vulkan library: vulkan")
57        set(VULKAN_LIB vulkan)
58      else()
59        message(STATUS "Amber: Using specified Vulkan libraries: ${Vulkan_LIBRARIES}")
60        set(VULKAN_LIB "${Vulkan_LIBRARIES}")
61      endif()
62
63      # For now assume we have Vulkan.  We have its header, but we haven't checked
64      # for the library.
65      # TODO(dneto): Actually check for the libraries.
66      set(Vulkan_FOUND TRUE)
67      set(VulkanHeaders_INCLUDE_DIR "${X}")
68      set(VulkanHeaders_INCLUDE_DIRS "${VulkanHeaders_INCLUDE_DIR}")
69    endif()
70  endif()
71  unset(X)
72endif()
73
74# Check if we're in the CTS
75if (NOT ${Vulkan_FOUND})
76  message(STATUS "Amber: Checking for CTS Vulkan header")
77  set(X "${Vulkan-Headers_SOURCE_DIR}")
78  if (IS_DIRECTORY "${X}")
79    message(STATUS "Amber: Using Vulkan header dir ${X}")
80    list(APPEND CMAKE_REQUIRED_INCLUDES "${X}")
81
82    include_directories(BEFORE "${X}")
83
84    if (EXISTS "${X}/vkDefs.h")
85      set(VULKAN_CTS_HEADER TRUE)
86      set(Vulkan_FOUND TRUE)
87      set(VulkanHeaders_INCLUDE_DIR "${X}")
88      set(VulkanHeaders_INCLUDE_DIRS "${VulkanHeaders_INCLUDE_DIR}")
89    endif()
90  endif()
91  unset(X)
92endif()
93
94if (NOT ${Vulkan_FOUND})
95  # If we aren't already building a Vulkan library, then use CMake to find it.
96  if(NOT ${CMAKE_VERSION} VERSION_LESS "3.7")
97    # LunarG added FindVulkan support to CMake 3.7.  If you have the Vulkan SDK
98    # published by LunarG, then set environment variables:
99    #  VULKAN_SDK should point to the platform-specific SDK directory containing
100    #    the include and lib directories.
101    #  VK_ICD_FILENAMES should point to ICD JSON file.
102
103    # Example, with the LunarG SDK macOS edition with MoltenVK:
104    #  export VULKAN_SDK="$HOME/vulkan-macos-1.1.85.0/macOS"
105    #  export VK_ICD_FILENAMES="$VULKAN_SDK/etc/vulkan/icd/MoltenVK_icd.json"
106    # See https://cmake.org/cmake/help/v3.7/module/FindVulkan.html
107    find_package(Vulkan)
108    if(${Vulkan_FOUND})
109      message(STATUS "Amber: Using Vulkan from Vulkan SDK at $ENV{VULKAN_SDK}")
110      # Use the imported library target set up by find_package.
111      set(VULKAN_LIB Vulkan::Vulkan)
112      set(VulkanHeaders_INCLUDE_DIR "${Vulkan_INCLUDE_DIR}"
113          CACHE PATH "vk headers dir" FORCE)
114      set(VulkanHeaders_INCLUDE_DIRS "${Vulkan_INCLUDE_DIRS}"
115          CACHE PATH "vk headers dir" FORCE)
116    endif()
117  endif()
118endif()
119
120if (NOT ${Vulkan_FOUND})
121  message(STATUS "Amber: Did not find Vulkan")
122endif()
123