From 467509d7674d62deb9aaca6c9d68628eb108f146 Mon Sep 17 00:00:00 2001 From: Ryan Kawicki Date: Thu, 25 Jun 2020 22:14:55 -0500 Subject: FindVulkan: Support for finding glslc The GLSL SPIR-V compiler is part of the Vulkan SDK and may be used by projects for compiling shaders as part of the build process. This is not strictly required to build a Vulkan application, which is why the variable is not part of the REQUIRED_VARs for the module. --- Help/release/dev/FindVulkan-glslc.rst | 10 ++++++++++ Modules/FindVulkan.cmake | 30 ++++++++++++++++++++++++++---- Tests/FindVulkan/Test/CMakeLists.txt | 9 +++++++++ Tests/FindVulkan/Test/Run-glslc.cmake | 20 ++++++++++++++++++++ Tests/FindVulkan/Test/main.c | 4 ++-- 5 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 Help/release/dev/FindVulkan-glslc.rst create mode 100644 Tests/FindVulkan/Test/Run-glslc.cmake diff --git a/Help/release/dev/FindVulkan-glslc.rst b/Help/release/dev/FindVulkan-glslc.rst new file mode 100644 index 0000000..246bc8f --- /dev/null +++ b/Help/release/dev/FindVulkan-glslc.rst @@ -0,0 +1,10 @@ +FindVulkan-glslc +---------------- + +* The :module:`FindVulkan` module gained a new output variable + ``Vulkan_GLSLC_EXECUTABLE`` which contains the path to the + GLSL SPIR-V compiler. + +* The :module:`FindVulkan` module gained a new target + ``Vulkan::glslc`` which contains the path to the + GLSL SPIR-V compiler. diff --git a/Modules/FindVulkan.cmake b/Modules/FindVulkan.cmake index 4b999b6..89510b7 100644 --- a/Modules/FindVulkan.cmake +++ b/Modules/FindVulkan.cmake @@ -14,6 +14,9 @@ IMPORTED Targets This module defines :prop_tgt:`IMPORTED` target ``Vulkan::Vulkan``, if Vulkan has been found. +This module defines :prop_tgt:`IMPORTED` target ``Vulkan::glslc``, if +Vulkan and the GLSLC SPIR-V compiler has been found. + Result Variables ^^^^^^^^^^^^^^^^ @@ -23,10 +26,11 @@ This module defines the following variables:: Vulkan_INCLUDE_DIRS - include directories for Vulkan Vulkan_LIBRARIES - link against this library to use Vulkan -The module will also define two cache variables:: +The module will also define three cache variables:: - Vulkan_INCLUDE_DIR - the Vulkan include directory - Vulkan_LIBRARY - the path to the Vulkan library + Vulkan_INCLUDE_DIR - the Vulkan include directory + Vulkan_LIBRARY - the path to the Vulkan library + Vulkan_GLSLC_EXECUTABLE - the path to the GLSL SPIR-V compiler Hints ^^^^^ @@ -53,6 +57,11 @@ if(WIN32) "$ENV{VULKAN_SDK}/Lib" "$ENV{VULKAN_SDK}/Bin" ) + find_program(Vulkan_GLSLC_EXECUTABLE + NAMES glslc + HINTS + "$ENV{VULKAN_SDK}/Bin" + ) elseif(CMAKE_SIZEOF_VOID_P EQUAL 4) find_library(Vulkan_LIBRARY NAMES vulkan-1 @@ -60,6 +69,11 @@ if(WIN32) "$ENV{VULKAN_SDK}/Lib32" "$ENV{VULKAN_SDK}/Bin32" ) + find_program(Vulkan_GLSLC_EXECUTABLE + NAMES glslc + HINTS + "$ENV{VULKAN_SDK}/Bin32" + ) endif() else() find_path(Vulkan_INCLUDE_DIR @@ -68,6 +82,9 @@ else() find_library(Vulkan_LIBRARY NAMES vulkan HINTS "$ENV{VULKAN_SDK}/lib") + find_program(Vulkan_GLSLC_EXECUTABLE + NAMES glslc + HINTS "$ENV{VULKAN_SDK}/bin") endif() set(Vulkan_LIBRARIES ${Vulkan_LIBRARY}) @@ -78,7 +95,7 @@ find_package_handle_standard_args(Vulkan DEFAULT_MSG Vulkan_LIBRARY Vulkan_INCLUDE_DIR) -mark_as_advanced(Vulkan_INCLUDE_DIR Vulkan_LIBRARY) +mark_as_advanced(Vulkan_INCLUDE_DIR Vulkan_LIBRARY Vulkan_GLSLC_EXECUTABLE) if(Vulkan_FOUND AND NOT TARGET Vulkan::Vulkan) add_library(Vulkan::Vulkan UNKNOWN IMPORTED) @@ -86,3 +103,8 @@ if(Vulkan_FOUND AND NOT TARGET Vulkan::Vulkan) IMPORTED_LOCATION "${Vulkan_LIBRARIES}" INTERFACE_INCLUDE_DIRECTORIES "${Vulkan_INCLUDE_DIRS}") endif() + +if(Vulkan_FOUND AND Vulkan_GLSLC_EXECUTABLE AND NOT TARGET Vulkan::glslc) + add_executable(Vulkan::glslc IMPORTED) + set_property(TARGET Vulkan::glslc PROPERTY IMPORTED_LOCATION "${Vulkan_GLSLC_EXECUTABLE}") +endif() diff --git a/Tests/FindVulkan/Test/CMakeLists.txt b/Tests/FindVulkan/Test/CMakeLists.txt index 0b13d53..9d36a0d 100644 --- a/Tests/FindVulkan/Test/CMakeLists.txt +++ b/Tests/FindVulkan/Test/CMakeLists.txt @@ -13,3 +13,12 @@ add_executable(test_var main.c) target_include_directories(test_var PRIVATE ${Vulkan_INCLUDE_DIRS}) target_link_libraries(test_var PRIVATE ${Vulkan_LIBRARIES}) add_test(NAME test_var COMMAND test_var) + +if(Vulkan_GLSLC_EXECUTABLE) + add_test(NAME test_glslc + COMMAND ${CMAKE_COMMAND} + "-DVULKAN_GLSLC_EXECUTABLE=${Vulkan_GLSLC_EXECUTABLE}" + "-DVULKAN_GLSLC_EXECUTABLE_TARGET=$" + -P "${CMAKE_CURRENT_LIST_DIR}/Run-glslc.cmake" + ) +endif() diff --git a/Tests/FindVulkan/Test/Run-glslc.cmake b/Tests/FindVulkan/Test/Run-glslc.cmake new file mode 100644 index 0000000..086eb9d --- /dev/null +++ b/Tests/FindVulkan/Test/Run-glslc.cmake @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 3.12) + +function(run_glslc exe exe_display) + execute_process(COMMAND ${exe} --help + OUTPUT_VARIABLE output + OUTPUT_STRIP_TRAILING_WHITESPACE + RESULT_VARIABLE result + ) + + if(NOT result EQUAL 0) + message(SEND_ERROR "Result of ${exe_display} --help is ${result}, should be 0") + endif() + + if(NOT output MATCHES "^glslc - Compile shaders into SPIR-V") + message(SEND_ERROR "Output of ${exe_display} --help is \"${output}\", should begin with \"glslc - Compile shaders into SPIR-V\"") + endif() +endfunction() + +run_glslc("${VULKAN_GLSLC_EXECUTABLE}" "\${VULKAN_GLSLC_EXECUTABLE}") +run_glslc("${VULKAN_GLSLC_EXECUTABLE_TARGET}" "Vulkan::glslc") diff --git a/Tests/FindVulkan/Test/main.c b/Tests/FindVulkan/Test/main.c index b29c9ec..1bff651 100644 --- a/Tests/FindVulkan/Test/main.c +++ b/Tests/FindVulkan/Test/main.c @@ -2,10 +2,10 @@ int main() { - VkInstanceCreateInfo instanceCreateInfo = {}; + VkInstanceCreateInfo instanceCreateInfo = { 0 }; instanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; - VkApplicationInfo applicationInfo = {}; + VkApplicationInfo applicationInfo = { 0 }; applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; applicationInfo.apiVersion = VK_API_VERSION_1_0; applicationInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0); -- cgit v0.12