• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# ~~~
2# Copyright (c) 2014-2023 Valve Corporation
3# Copyright (c) 2014-2023 LunarG, Inc.
4# Copyright (c) 2021-2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5# Copyright (c) 2023-2023 RasterGrid Kft.
6#
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11#     http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18# ~~~
19cmake_minimum_required(VERSION 3.17.2)
20
21project(VULKAN_LOADER VERSION 1.3.275 LANGUAGES C)
22
23# This variable enables downstream users to customize the target API
24# variant (e.g. Vulkan SC)
25set(API_TYPE "vulkan")
26
27add_subdirectory(scripts)
28
29set(CMAKE_C_STANDARD 99)
30set(CMAKE_C_STANDARD_REQUIRED ON)
31set(CMAKE_C_EXTENSIONS OFF)
32set(CMAKE_C_VISIBILITY_PRESET "hidden")
33set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
34
35# By default, loader & tests are built without sanitizers
36# Use these options to force a specific sanitizer on the loader and test executables
37if (UNIX)
38    option(LOADER_ENABLE_ADDRESS_SANITIZER "Linux & macOS only: Advanced memory checking" OFF)
39    option(LOADER_ENABLE_THREAD_SANITIZER "Linux & macOS only: Advanced thread checking" OFF)
40endif()
41
42if(WIN32)
43    # Optional: Allow specify the exact version used in the loader dll
44    # Format is major.minor.patch.build
45    set(BUILD_DLL_VERSIONINFO "" CACHE STRING "Set the version to be used in the loader.rc file. Default value is the currently generated header version")
46endif()
47
48find_package(VulkanHeaders CONFIG QUIET)
49
50include(GNUInstallDirs)
51
52set(GIT_BRANCH_NAME "--unknown--")
53set(GIT_TAG_INFO "--unknown--")
54find_package (Git)
55if (GIT_FOUND AND EXISTS "${CMAKE_CURRENT_LIST_DIR}/.git/HEAD")
56    execute_process(
57        COMMAND ${GIT_EXECUTABLE} describe --tags --always
58        WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
59        OUTPUT_VARIABLE GIT_TAG_INFO)
60    string(REGEX REPLACE "\n$" "" GIT_TAG_INFO "${GIT_TAG_INFO}")
61
62    file(READ "${CMAKE_CURRENT_LIST_DIR}/.git/HEAD" GIT_HEAD_REF_INFO)
63    if (GIT_HEAD_REF_INFO)
64        string(REGEX MATCH "ref: refs/heads/(.*)" _ ${GIT_HEAD_REF_INFO})
65        if (CMAKE_MATCH_1)
66            set(GIT_BRANCH_NAME ${CMAKE_MATCH_1})
67        else()
68            set(GIT_BRANCH_NAME ${GIT_HEAD_REF_INFO})
69        endif()
70        string(REGEX REPLACE "\n$" "" GIT_BRANCH_NAME "${GIT_BRANCH_NAME}")
71    endif()
72endif()
73
74# Enable IDE GUI folders.  "Helper targets" that don't have interesting source code should set their FOLDER property to this
75set_property(GLOBAL PROPERTY USE_FOLDERS ON)
76set(LOADER_HELPER_FOLDER "Helper Targets")
77
78if(UNIX)
79    set(FALLBACK_CONFIG_DIRS "/etc/xdg" CACHE STRING
80            "Search path to use when XDG_CONFIG_DIRS is unset or empty or the current process is SUID/SGID. Default is freedesktop compliant.")
81    set(FALLBACK_DATA_DIRS "/usr/local/share:/usr/share" CACHE STRING
82            "Search path to use when XDG_DATA_DIRS is unset or empty or the current process is SUID/SGID. Default is freedesktop compliant.")
83    set(SYSCONFDIR "" CACHE STRING
84            "System-wide search directory. If not set or empty, CMAKE_INSTALL_FULL_SYSCONFDIR and /etc are used.")
85endif()
86
87if(WIN32)
88    option(ENABLE_WIN10_ONECORE "Link the loader with OneCore umbrella libraries" OFF)
89endif()
90
91add_library(platform_wsi INTERFACE)
92if(WIN32)
93    target_compile_definitions(platform_wsi INTERFACE VK_USE_PLATFORM_WIN32_KHR)
94elseif(ANDROID)
95    message(FATAL_ERROR "Android build not supported!")
96elseif(APPLE)
97    target_compile_definitions(platform_wsi INTERFACE VK_USE_PLATFORM_METAL_EXT)
98    if (IOS)
99        target_compile_definitions(platform_wsi INTERFACE VK_USE_PLATFORM_IOS_MVK)
100    endif()
101    if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
102        target_compile_definitions(platform_wsi INTERFACE VK_USE_PLATFORM_MACOS_MVK)
103    endif()
104elseif(CMAKE_SYSTEM_NAME MATCHES "Linux|BSD|DragonFly|GNU")
105    option(BUILD_WSI_XCB_SUPPORT "Build XCB WSI support" ON)
106    option(BUILD_WSI_XLIB_SUPPORT "Build Xlib WSI support" ON)
107    option(BUILD_WSI_WAYLAND_SUPPORT "Build Wayland WSI support" ON)
108    option(BUILD_WSI_DIRECTFB_SUPPORT "Build DirectFB WSI support" OFF)
109
110    find_package(PkgConfig REQUIRED QUIET) # Use PkgConfig to find Linux system libraries
111
112    if(BUILD_WSI_XCB_SUPPORT)
113        pkg_check_modules(XCB REQUIRED QUIET IMPORTED_TARGET xcb)
114        target_compile_definitions(platform_wsi INTERFACE VK_USE_PLATFORM_XCB_KHR)
115        target_link_libraries(platform_wsi INTERFACE PkgConfig::XCB)
116    endif()
117    if(BUILD_WSI_XLIB_SUPPORT)
118        pkg_check_modules(X11 REQUIRED QUIET IMPORTED_TARGET x11)
119        target_compile_definitions(platform_wsi INTERFACE VK_USE_PLATFORM_XLIB_KHR VK_USE_PLATFORM_XLIB_XRANDR_EXT)
120        target_link_libraries(platform_wsi INTERFACE PkgConfig::X11)
121    endif()
122    if(BUILD_WSI_WAYLAND_SUPPORT)
123        target_compile_definitions(platform_wsi INTERFACE VK_USE_PLATFORM_WAYLAND_KHR)
124    endif()
125    if(BUILD_WSI_DIRECTFB_SUPPORT)
126        pkg_check_modules(DirectFB QUIET REQUIRED IMPORTED_TARGET directfb)
127        target_compile_definitions(platform_wsi INTERFACE VK_USE_PLATFORM_DIRECTFB_EXT)
128        target_link_libraries(platform_wsi INTERFACE PkgConfig::DirectFB)
129    endif()
130elseif(CMAKE_SYSTEM_NAME MATCHES "QNX")
131    message(FATAL_ERROR "See BUILD.md for QNX build")
132elseif(CMAKE_SYSTEM_NAME MATCHES "Fuchsia")
133    message(FATAL_ERROR "Fuchsia uses Chromium build. See BUILD.gn")
134else()
135    message(FATAL_ERROR "Unsupported Platform!")
136endif()
137
138add_library(loader_common_options INTERFACE)
139target_link_libraries(loader_common_options INTERFACE platform_wsi)
140
141# Enable beta Vulkan extensions
142target_compile_definitions(loader_common_options INTERFACE VK_ENABLE_BETA_EXTENSIONS)
143
144option(BUILD_WERROR "Enable warnings as errors")
145
146# Set warnings as errors and the main diagnostic flags
147# Must be set first so the warning silencing later on works properly
148# Note that clang-cl.exe should use MSVC flavor flags, not GNU
149if (CMAKE_C_COMPILER_ID STREQUAL "MSVC" OR (CMAKE_C_COMPILER_ID STREQUAL "Clang" AND CMAKE_C_COMPILER_FRONTEND_VARIANT MATCHES "MSVC"))
150    if (BUILD_WERROR)
151        target_compile_options(loader_common_options INTERFACE /WX)
152    endif()
153    target_compile_options(loader_common_options INTERFACE /W4)
154elseif(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
155    # using GCC or Clang with the regular front end
156    if (BUILD_WERROR)
157        target_compile_options(loader_common_options INTERFACE -Werror)
158    endif()
159    target_compile_options(loader_common_options INTERFACE -Wall -Wextra)
160endif()
161
162if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
163    target_compile_options(loader_common_options INTERFACE -Wno-missing-field-initializers)
164
165    if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
166        target_compile_options(loader_common_options INTERFACE -Wno-stringop-truncation -Wno-stringop-overflow)
167        if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 7.1)
168            target_compile_options(loader_common_options INTERFACE -Wshadow=local) #only added in GCC 7
169        endif()
170    endif()
171
172    target_compile_options(loader_common_options INTERFACE -Wpointer-arith)
173endif()
174
175if(CMAKE_C_COMPILER_ID MATCHES "MSVC" OR (CMAKE_C_COMPILER_ID STREQUAL "Clang" AND CMAKE_C_COMPILER_FRONTEND_VARIANT MATCHES "MSVC"))
176    # /sdl: Enable additional security checks
177    # /GR-: Disable RTTI
178    # /guard:cf: Enable control flow guard
179    # /wd4152: Disable warning on conversion of a function pointer to a data pointer
180    # /wd4201: Disable warning on anonymous struct/unions
181    target_compile_options(loader_common_options INTERFACE /sdl /GR- /guard:cf /wd4152 /wd4201)
182
183    # Enable control flow guard
184    target_link_options(loader_common_options INTERFACE "LINKER:/guard:cf")
185
186    # Prevent <windows.h> from polluting the code. guards against things like MIN and MAX
187    target_compile_definitions(loader_common_options INTERFACE WIN32_LEAN_AND_MEAN)
188endif()
189
190# DEBUG enables runtime loader ICD verification
191# Add git branch and tag info in debug mode
192target_compile_definitions(loader_common_options INTERFACE $<$<CONFIG:DEBUG>:DEBUG;GIT_BRANCH_NAME="${GIT_BRANCH_NAME}";GIT_TAG_INFO="${GIT_TAG_INFO}">)
193
194if (NOT (WIN32 OR APPLE))
195    # Check for the existance of the secure_getenv or __secure_getenv commands
196    include(CheckFunctionExists)
197
198    check_function_exists(secure_getenv HAVE_SECURE_GETENV)
199    check_function_exists(__secure_getenv HAVE___SECURE_GETENV)
200
201    if (HAVE_SECURE_GETENV)
202        target_compile_definitions(loader_common_options INTERFACE HAVE_SECURE_GETENV)
203    endif()
204    if (HAVE___SECURE_GETENV)
205        target_compile_definitions(loader_common_options INTERFACE HAVE___SECURE_GETENV)
206    endif()
207    if (NOT (HAVE_SECURE_GETENV OR HAVE___SECURE_GETENV))
208        message(WARNING "Using non-secure environmental lookups. This loader will not properly disable environent variables when run with elevated permissions.")
209    endif()
210endif()
211
212option(LOADER_CODEGEN "Enable vulkan loader code generation")
213if(LOADER_CODEGEN)
214    find_package(Python3 REQUIRED)
215    add_custom_target(loader_codegen
216        COMMAND Python3::Interpreter ${PROJECT_SOURCE_DIR}/scripts/generate_source.py
217            "${VULKAN_HEADERS_INSTALL_DIR}/${CMAKE_INSTALL_DATADIR}/vulkan/registry"
218            --generated-version ${VulkanHeaders_VERSION} --incremental --api ${API_TYPE}
219    )
220endif()
221
222if(UNIX)
223    target_compile_definitions(loader_common_options INTERFACE FALLBACK_CONFIG_DIRS="${FALLBACK_CONFIG_DIRS}" FALLBACK_DATA_DIRS="${FALLBACK_DATA_DIRS}")
224
225    if(NOT (SYSCONFDIR STREQUAL ""))
226        # SYSCONFDIR is specified, use it and do not force /etc.
227        target_compile_definitions(loader_common_options INTERFACE SYSCONFDIR="${SYSCONFDIR}")
228    else()
229        target_compile_definitions(loader_common_options INTERFACE SYSCONFDIR="${CMAKE_INSTALL_FULL_SYSCONFDIR}")
230
231        # Make sure /etc is searched by the loader
232        if(NOT (CMAKE_INSTALL_FULL_SYSCONFDIR STREQUAL "/etc"))
233            target_compile_definitions(loader_common_options INTERFACE EXTRASYSCONFDIR="/etc")
234        endif()
235    endif()
236endif()
237
238add_subdirectory(loader)
239
240option(BUILD_TESTS "Build Tests")
241if (BUILD_TESTS)
242    enable_testing()
243    add_subdirectory(tests)
244endif()
245