Skip to content

Commit d0c4103

Browse files
author
Jens Weggemann
committed
The CMake files now build two targets: volk and volk_headers. The first is a static library where platform defines can be set using VOLK_STATIC_DEFINES, the latter is for the header-only approach.
- Refactored the tests to test all relevant situations, and added testing for whether setting the platform defines actually worked. - Added a bash script to run the test cases locally. The travis script now just calls that script. - Changed the Vulkan header version detection in CMakeLists.txt to be more in line with the volk approach: it is now written at generate time just like the volk sources. - Fixed a bug from trunk that could break the Win32 build (missing defined() in volk.h:27)
1 parent 51f8af5 commit d0c4103

File tree

20 files changed

+434
-186
lines changed

20 files changed

+434
-186
lines changed

.travis.yml

+1-23
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,4 @@ script:
1212
- git clone --depth 1 https://github.com/KhronosGroup/Vulkan-Headers
1313
- export VULKAN_SDK=$PWD/Vulkan-Headers
1414
- popd
15-
#
16-
# Install volk into local directory.
17-
#
18-
- mkdir installed
19-
- mkdir build
20-
- pushd build
21-
- cmake -DVOLK_INSTALL=ON -DCMAKE_INSTALL_PREFIX=../installed ..
22-
- cmake --build . --config Debug
23-
- cmake --build . --config Release
24-
- cmake --build . --target install
25-
- popd
26-
- export INSTALL_DIR=$PWD/installed
27-
#
28-
# Build examples
29-
#
30-
- pushd test
31-
- mkdir build
32-
- pushd build
33-
- cmake -DCMAKE_PREFIX_PATH=${INSTALL_DIR}/lib/cmake ..
34-
- cmake --build . --config Debug
35-
- cmake --build . --config Release
36-
- popd
37-
- popd
15+
- test/run_tests.sh

CMakeLists.txt

+59-19
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,72 @@ cmake_minimum_required(VERSION 3.0)
22
cmake_policy(PUSH)
33
cmake_policy(SET CMP0048 NEW) # project(... VERSION ...) support
44

5-
# Set the project version to the vulkan header version.
6-
file(STRINGS volk.h matched_line REGEX "#define[ \t]+VOLK_HEADER_VERSION" LIMIT_INPUT 1000)
7-
string(REGEX MATCH "([0-9\\.]+)$" parsed_version ${matched_line})
8-
if(NOT parsed_version)
9-
message(FATAL_ERROR "Failed to parse VOLK_HEADER_VERSION!")
5+
project(volk VERSION
6+
# VOLK_GENERATE_VERSION
7+
121
8+
# VOLK_GENERATE_VERSION
9+
LANGUAGES C
10+
)
11+
12+
# CMake 3.12 changes the default behaviour of option() to leave local variables
13+
# unchanged if they exist (which we want), but we must work with older CMake versions.
14+
if(NOT DEFINED VOLK_STATIC_DEFINES)
15+
option(VOLK_STATIC_DEFINES "Additional defines for building the volk static library, e.g. Vulkan platform defines" "")
16+
endif()
17+
if(NOT DEFINED VOLK_PULL_IN_VULKAN)
18+
option(VOLK_PULL_IN_VULKAN "Vulkan as a transitive dependency" ON)
19+
endif()
20+
if(NOT DEFINED VOLK_INSTALL)
21+
option(VOLK_INSTALL "Create installation targets" OFF)
1022
endif()
1123

12-
project(volk VERSION ${parsed_version} LANGUAGES NONE)
24+
# -----------------------------------------------------
25+
# Static library
1326

14-
add_library(volk INTERFACE)
15-
target_include_directories(volk INTERFACE
16-
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>
17-
$<INSTALL_INTERFACE:include>
27+
add_library(volk STATIC volk.h volk.c)
28+
target_include_directories(volk PUBLIC
29+
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>
30+
$<INSTALL_INTERFACE:include>
1831
)
32+
if(VOLK_STATIC_DEFINES)
33+
target_compile_definitions(volk PUBLIC ${VOLK_STATIC_DEFINES})
34+
endif()
1935
if (NOT WIN32)
20-
target_link_libraries(volk INTERFACE dl)
36+
target_link_libraries(volk PUBLIC dl)
2137
endif()
2238

23-
# If CMake has the FindVulkan module and it works, use it.
24-
# Otherwise silently rely on the environment variable.
25-
find_package(Vulkan QUIET)
39+
# -----------------------------------------------------
40+
# Interface library
2641

27-
if(NOT VOLK_INSTALL)
42+
add_library(volk_headers INTERFACE)
43+
target_include_directories(volk_headers INTERFACE
44+
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>
45+
$<INSTALL_INTERFACE:include>
46+
)
47+
if (NOT WIN32)
48+
target_link_libraries(volk_headers INTERFACE dl)
49+
endif()
50+
51+
# -----------------------------------------------------
52+
# Vulkan transitive dependency
53+
54+
if(VOLK_PULL_IN_VULKAN)
55+
# If CMake has the FindVulkan module and it works, use it.
56+
# Otherwise try the environment variable.
57+
find_package(Vulkan QUIET)
2858
if(TARGET Vulkan::Vulkan)
29-
target_link_libraries(volk INTERFACE Vulkan::Vulkan)
59+
message(" Vulkan as target")
60+
target_link_libraries(volk PUBLIC Vulkan::Vulkan)
61+
target_link_libraries(volk_headers INTERFACE Vulkan::Vulkan)
3062
elseif(DEFINED ENV{VULKAN_SDK})
31-
target_include_directories(volk INTERFACE "$ENV{VULKAN_SDK}/include")
63+
message(" Vulkan as path")
64+
target_include_directories(volk PUBLIC "$ENV{VULKAN_SDK}/include")
65+
target_include_directories(volk_headers INTERFACE "$ENV{VULKAN_SDK}/include")
3266
endif()
3367
endif()
3468

35-
option(VOLK_INSTALL "Create installation target" OFF)
69+
# -----------------------------------------------------
70+
# Installation
3671

3772
if(VOLK_INSTALL)
3873

@@ -43,7 +78,12 @@ if(VOLK_INSTALL)
4378
install(FILES volk.h volk.c DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
4479

4580
# Install library target and add it and any dependencies to export set.
46-
install(TARGETS volk EXPORT volk-targets)
81+
install(TARGETS volk volk_headers
82+
EXPORT volk-targets
83+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
84+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
85+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
86+
)
4787

4888
# Actually write exported config w/ imported targets
4989
install(EXPORT volk-targets

cmake/volkConfig.cmake.in

+14-6
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@ get_filename_component(volk_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
33
if(NOT TARGET volk::volk)
44
include("${volk_CMAKE_DIR}/volkTargets.cmake")
55
endif()
6-
set(volk_LIBRARIES volk::volk)
76

8-
find_package(Vulkan QUIET)
9-
if(TARGET Vulkan::Vulkan)
10-
add_dependencies(volk::volk Vulkan::Vulkan)
11-
elseif(DEFINED ENV{VULKAN_SDK})
12-
target_include_directories(volk::volk INTERFACE "$ENV{VULKAN_SDK}/include")
7+
# Mirror the default behaviour of the respective option.
8+
if(NOT DEFINED VOLK_PULL_IN_VULKAN)
9+
set(VOLK_PULL_IN_VULKAN ON)
10+
endif()
11+
12+
if(VOLK_PULL_IN_VULKAN)
13+
find_package(Vulkan QUIET)
14+
if(TARGET Vulkan::Vulkan)
15+
add_dependencies(volk::volk Vulkan::Vulkan)
16+
add_dependencies(volk::volk_headers Vulkan::Vulkan)
17+
elseif(DEFINED ENV{VULKAN_SDK})
18+
target_include_directories(volk::volk INTERFACE "$ENV{VULKAN_SDK}/include")
19+
target_include_directories(volk::volk_headers INTERFACE "$ENV{VULKAN_SDK}/include")
20+
endif()
1321
endif()

generate.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,14 @@ def patch_file(path, blocks):
2525
block = None
2626
else:
2727
result.append(line)
28+
# C comment marker
2829
if line.strip().startswith('/* VOLK_GENERATE_'):
2930
block = line
3031
result.append(blocks[line.strip()[17:-3]])
32+
# Shell/CMake comment marker
33+
elif line.strip().startswith('# VOLK_GENERATE_'):
34+
block = line
35+
result.append(blocks[line.strip()[16:]])
3136

3237
with open(path, 'w') as file:
3338
for line in result:
@@ -60,7 +65,8 @@ def defined(key):
6065
blocks = {}
6166

6267
version = spec.find('types/type[name="VK_HEADER_VERSION"]')
63-
blocks['VERSION'] = '#define VOLK_HEADER_VERSION ' + version.find('name').tail.strip() + '\n'
68+
blocks['VERSION'] = version.find('name').tail.strip() + '\n'
69+
blocks['VERSION_DEFINE'] = '#define VOLK_HEADER_VERSION ' + version.find('name').tail.strip() + '\n'
6470

6571
command_groups = OrderedDict()
6672
instance_commands = set()
@@ -160,3 +166,4 @@ def defined(key):
160166

161167
patch_file('volk.h', blocks)
162168
patch_file('volk.c', blocks)
169+
patch_file('CMakeLists.txt', blocks)

test/CMakeLists.txt

-7
This file was deleted.

test/cmake_usage_source/CMakeLists.txt

-32
This file was deleted.

test/cmake_usage_source/main.c

-25
This file was deleted.

test/cmake_usage_target_installed/CMakeLists.txt

-13
This file was deleted.

test/cmake_usage_target_installed/main.c

-26
This file was deleted.

test/cmake_usage_target_subdir/main.c

-26
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
cmake_minimum_required(VERSION 3.0)
2+
project(volk_test LANGUAGES C)
3+
4+
# Include volk from a CMake package config.
5+
# CMAKE_PREFIX_PATH or volk_DIR must be set properly.
6+
find_package(volk CONFIG REQUIRED)
7+
8+
add_executable(volk_test main.c)
9+
target_link_libraries(volk_test PRIVATE volk::volk_headers)
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* Set platform defines at build time for volk to pick up. */
2+
#if defined(_WIN32)
3+
# define VK_USE_PLATFORM_WIN32_KHR
4+
#elif defined(__linux__) || defined(__unix__)
5+
# define VK_USE_PLATFORM_XLIB_KHR
6+
#elif defined(__APPLE__)
7+
# define VK_USE_PLATFORM_MACOS_MVK
8+
#else
9+
# error "Platform not supported by this example."
10+
#endif
11+
12+
#define VOLK_IMPLEMENTATION
13+
#include "volk.h"
14+
15+
#include "stdio.h"
16+
#include "stdlib.h"
17+
18+
int main()
19+
{
20+
VkResult r;
21+
uint32_t version;
22+
void* ptr;
23+
24+
/* This won't compile if the appropriate Vulkan platform define isn't set. */
25+
ptr =
26+
#if defined(_WIN32)
27+
&vkCreateWin32SurfaceKHR;
28+
#elif defined(__linux__) || defined(__unix__)
29+
&vkCreateXlibSurfaceKHR;
30+
#elif defined(__APPLE__)
31+
&vkCreateMacOSSurfaceMVK;
32+
#else
33+
/* Platform not recogized for testing. */
34+
NULL;
35+
#endif
36+
37+
/* Try to initialize volk. This might not work on CI builds, but the
38+
* above should have compiled at least. */
39+
r = volkInitialize();
40+
if (r != VK_SUCCESS) {
41+
printf("volkInitialize failed!\n");
42+
return -1;
43+
}
44+
45+
version = volkGetInstanceVersion();
46+
printf("Vulkan version %d.%d.%d initialized.\n",
47+
VK_VERSION_MAJOR(version),
48+
VK_VERSION_MINOR(version),
49+
VK_VERSION_PATCH(version));
50+
51+
52+
return 0;
53+
}
54+

0 commit comments

Comments
 (0)