Skip to content

Commit

Permalink
Link SPIRV-Cross instead of using SDL_LoadObject (#46)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Anonymous Maarten <[email protected]>
  • Loading branch information
thatcosmonaut and madebr authored Nov 5, 2024
1 parent 1d60b24 commit 3385fb5
Show file tree
Hide file tree
Showing 15 changed files with 355 additions and 4,289 deletions.
15 changes: 14 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,24 @@ jobs:
- name: Configure (CMake)
run: |
cmake -S . -B build -GNinja \
-DCMAKE_BUILD_TYPE=Release \
-DSDLGPUSHADERCROSS_SHARED=ON \
-DSDLGPUSHADERCROSS_STATIC=ON \
-DSDLGPUSHADERCROSS_VENDORED=ON \
-DSDLGPUSHADERCROSS_CLI=ON \
-DSDLGPUSHADERCROSS_WERROR=ON \
-DSDLGPUSHADERCROSS_INSTALL=ON \
-DSDLGPUSHADERCROSS_INSTALL_DEPS=ON \
-DSDLGPUSHADERCROSS_INSTALL_CPACK=ON \
-DCMAKE_INSTALL_PREFIX=prefix
-DCMAKE_INSTALL_PREFIX="${PWD}/prefix"
- name: Build (CMake)
id: build
run: |
cmake --build build --config Release --parallel --verbose
- name: Install (CMake)
id: install
if: ${{ always() && steps.build.outcome == 'success' }}
run: |
cmake --install build/ --config Release
Expand All @@ -96,6 +100,15 @@ jobs:
run: |
cmake --build build/ --target package
- name: Verify CMake configuration files
if: ${{ always() && steps.install.outcome == 'success' }}
run: |
cmake -S cmake/test -B cmake_config_build \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_PREFIX_PATH="${PWD}/prefix" \
-GNinja
cmake --build cmake_config_build --verbose
- uses: actions/upload-artifact@v4
if: ${{ always() && steps.package.outcome == 'success' }}
with:
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "spirv-cross"]
path = external/SPIRV-Cross
url = https://github.com/KhronosGroup/SPIRV-Cross.git
115 changes: 89 additions & 26 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ set(MINOR_VERSION 0)
set(MICRO_VERSION 0)
set(SDL_REQUIRED_VERSION "3.1.3")

# option() honors normal variables.
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)

project(SDL3_gpu_shadercross LANGUAGES C VERSION "${MAJOR_VERSION}.${MINOR_VERSION}.${MICRO_VERSION}")

include(CMakeDependentOption)
Expand All @@ -30,14 +33,14 @@ endif()
# Options
option(SDLGPUSHADERCROSS_SHARED "Build shared SDL_gpu_shadercross library" ${SDLGPUSHADERCROSS_SHARED_DEFAULT})
option(SDLGPUSHADERCROSS_STATIC "Build static SDL_gpu_shadercross library" ${SDLGPUSHADERCROSS_STATIC_DEFAULT})
option(SDLGPUSHADERCROSS_LOAD_DYNAMIC "Load dependencies using SDL_LoadObject" ON)
cmake_dependent_option(SDLGPUSHADERCROSS_LINK_STATIC "Link to static library variants of dependencies" OFF "NOT SDLGPUSHADERCROSS_LOAD_DYNAMIC" OFF)
option(SDLGPUSHADERCROSS_LINK_STATIC "Link to static library variants of dependencies" OFF)
option(SDLGPUSHADERCROSS_VENDORED "Use vendored dependencies" OFF)
option(SDLGPUSHADERCROSS_CLI "Build command line executable" ON)
cmake_dependent_option(SDLGPUSHADERCROSS_CLI_STATIC "Link CLI with static libraries" OFF "SDLGPUSHADERCROSS_CLI;SDLGPUSHADERCROSS_STATIC;TARGET SDL3::SDL3-static" OFF)
option(SDLGPUSHADERCROSS_WERROR "Enable Werror" OFF)
option(SDLGPUSHADERCROSS_INSTALL "Enable installation" OFF)
cmake_dependent_option(SDLGPUSHADERCROSS_INSTALL_CPACK "Enable CPack installation" OFF "SDLGPUSHADERCROSS_INSTALL" OFF)
cmake_dependent_option(SDLGPUSHADERCROSS_INSTALL_DEPS "Download, build and install dependencies" OFF "SDLGPUSHADERCROSS_INSTALL" OFF)
cmake_dependent_option(SDLGPUSHADERCROSS_INSTALL_DEPS "Download, build and install optional dependencies" OFF "SDLGPUSHADERCROSS_INSTALL" OFF)

sdl_calculate_derived_version_variables(${MAJOR_VERSION} ${MINOR_VERSION} ${MICRO_VERSION})
SDL_DetectTargetCPUArchitectures(SDL_CPU_NAMES)
Expand All @@ -57,14 +60,63 @@ if(NOT MSVC)
add_compile_options(-pedantic) # -Wno-strict-aliasing
endif()

set(pc_requires )
set(install_extra_targets )
if(SDLGPUSHADERCROSS_VENDORED)
set(SPIRV_CROSS_SKIP_INSTALL ON)
set(SPIRV_CROSS_CLI OFF)
set(SPIRV_CROSS_ENABLE_TESTS OFF)
if(SDLGPUSHADERCROSS_LINK_STATIC)
set(SPIRV_CROSS_SHARED OFF)
set(SPIRV_CROSS_STATIC ON)
else()
set(SPIRV_CROSS_SHARED ON)
set(SPIRV_CROSS_STATIC OFF)
endif()

sdl_check_project_in_subfolder(external/SPIRV-Cross SPIRV-Cross SDLGPUSHADERCROSS_VENDORED)
set(SPIRV_CROSS_ENABLE_TESTS ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
add_subdirectory(external/SPIRV-Cross EXCLUDE_FROM_ALL)

if(SDLGPUSHADERCROSS_LINK_STATIC)
enable_language(CXX)
if(SDLGPUSHADERCROSS_STATIC)
list(APPEND install_extra_targets spirv-cross-c)
list(APPEND pc_requires "spirv-cross-c")
set(spirv_extra_targets spirv-cross-glsl spirv-cross-hlsl spirv-cross-msl spirv-cross-cpp spirv-cross-reflect spirv-cross-core)
foreach(extra IN LISTS spirv_extra_targets)
if(TARGET ${extra})
list(APPEND install_extra_targets ${extra})
list(APPEND pc_requires "${extra}")
endif()
endforeach()
endif()
else()
list(APPEND install_extra_targets spirv-cross-c-shared)
list(APPEND pc_requires "spirv-cross-c-shared")
endif()
else()
if(SDLGPUSHADERCROSS_LINK_STATIC)
enable_language(CXX)
find_package(spirv_cross_core QUIET)
find_package(spirv_cross_glsl QUIET)
find_package(spirv_cross_hlsl QUIET)
find_package(spirv_cross_msl QUIET)
find_package(spirv_cross_cpp QUIET)
find_package(spirv_cross_reflect QUIET)
find_package(spirv_cross_c REQUIRED)
else()
find_package(spirv_cross_c_shared REQUIRED)
endif()
endif()

# Source lists
set(SOURCE_FILES
# Public Headers
include/SDL3_gpu_shadercross/SDL_gpu_shadercross.h
# Source Files
src/SDL_gpu_shadercross.c
src/spirv_cross_c.h
src/spirv.h
)

set(SDL3_gpu_shadercross_targets)
Expand All @@ -75,6 +127,7 @@ if(SDLGPUSHADERCROSS_SHARED)

set_property(TARGET SDL3_gpu_shadercross-shared PROPERTY DEFINE_SYMBOL DLL_EXPORT)
sdl_target_link_option_version_file(SDL3_gpu_shadercross-shared "${CMAKE_CURRENT_SOURCE_DIR}/src/SDL_gpu_shadercross.sym")
sdl_target_link_options_no_undefined(SDL3_gpu_shadercross-shared)

# Build flags
if(WIN32)
Expand All @@ -86,11 +139,6 @@ if(SDLGPUSHADERCROSS_SHARED)
target_include_directories(SDL3_gpu_shadercross-shared PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>")
target_include_directories(SDL3_gpu_shadercross-shared PUBLIC "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")

# MinGW builds should statically link libgcc
if(MINGW)
target_link_options(SDL3_gpu_shadercross-shared PRIVATE "-static-libgcc")
endif()

# Soname
set_target_properties(SDL3_gpu_shadercross-shared PROPERTIES
OUTPUT_NAME "SDL3_gpu_shadercross"
Expand Down Expand Up @@ -129,6 +177,16 @@ endif()
foreach(target IN LISTS SDL3_gpu_shadercross_targets)
sdl_add_warning_options(${target} WARNING_AS_ERROR ${SDLGPUSHADERCROSS_WERROR})
target_compile_features(${target} PRIVATE c_std_99)

if(SDLGPUSHADERCROSS_LINK_STATIC)
target_link_libraries(${target} PRIVATE spirv-cross-c)
else()
target_link_libraries(${target} PRIVATE spirv-cross-c-shared)
endif()
if(SDLGPUSHADERCROSS_LINK_STATIC)
# spirv-cross uses C++
set_property(TARGET ${target} PROPERTY LINKER_LANGUAGE CXX)
endif()
endforeach()

if(NOT TARGET SDL3_gpu_shadercross::SDL3_gpu_shadercross)
Expand All @@ -142,6 +200,7 @@ endif()
if(SDLGPUSHADERCROSS_CLI)
add_executable(shadercross src/cli.c)
sdl_add_warning_options(shadercross WARNING_AS_ERROR ${SDLGPUSHADERCROSS_WERROR})
sdl_target_link_options_no_undefined(shadercross)

if(SDLGPUSHADERCROSS_CLI_STATIC)
target_link_libraries(shadercross PRIVATE SDL3_gpu_shadercross::SDL3_gpu_shadercross-static)
Expand Down Expand Up @@ -193,6 +252,19 @@ if(SDLGPUSHADERCROSS_INSTALL)
COMPONENT devel
)
endif()
if(install_extra_targets)
install(TARGETS ${install_extra_targets} EXPORT SDL3_gpu_shadercross-vendored
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT devel
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT library
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT library
)
install(EXPORT SDL3_gpu_shadercross-vendored
FILE SDL3_gpu_shadercross-vendored-targets.cmake
NAMESPACE SDL3_gpu_shadercross::vendored::
DESTINATION "${SDLGPUSHADERCROSS_INSTALL_CMAKEDIR}"
COMPONENT devel
)
endif()
install(
FILES "${CMAKE_CURRENT_SOURCE_DIR}/include/SDL3_gpu_shadercross/SDL_gpu_shadercross.h"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/SDL3_gpu_shadercross" COMPONENT DEVEL
Expand Down Expand Up @@ -220,7 +292,13 @@ if(SDLGPUSHADERCROSS_INSTALL)
file(RELATIVE_PATH SDL_PATH_PREFIX_RELATIVE_TO_PKGCONFIG "${CMAKE_INSTALL_PREFIX}/${SDLGPUSHADERCROSS_PKGCONFIG_INSTALLDIR}" "${CMAKE_INSTALL_PREFIX}")
string(REGEX REPLACE "[/]+$" "" SDL_PATH_PREFIX_RELATIVE_TO_PKGCONFIG "${SDL_PATH_PREFIX_RELATIVE_TO_PKGCONFIG}")
set(SDL_PKGCONFIG_PREFIX "\${pcfiledir}/${SDL_PATH_PREFIX_RELATIVE_TO_PKGCONFIG}")
set(PC_REQUIRED "")
if(NOT SDLGPUSHADERCROSS_VENDORED)
if(SDLGPUSHADERCROSS_LINK_STATIC)
set(PC_REQUIRES "spirv-cross-c")
else()
set(PC_REQUIRES "spirv-cross-c-shared")
endif()
endif()
set(PC_LIBS "")
configure_file(cmake/sdl3-gpu-shadercross.pc.in sdl3-gpu-shadercross.pc @ONLY)

Expand Down Expand Up @@ -305,21 +383,6 @@ if(SDLGPUSHADERCROSS_INSTALL_DEPS)
install(FILES "${SOURCE_DIR}/LICENSE-LLVM.txt" "${SOURCE_DIR}/LICENSE-MS.txt" "${SOURCE_DIR}/LICENSE-MIT.txt" DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/licenses/dxc")
endif()

# Download, configure, build and install spirv-cross
ExternalProject_Add(spirv_cross
GIT_REPOSITORY "https://github.com/KhronosGroup/SPIRV-Cross.git"
GIT_TAG "main"
CMAKE_ARGS "-DCMAKE_BUILD_TYPE=Release" "-DSPIRV_CROSS_SHARED=ON" "-DSPIRV_CROSS_STATIC=OFF" "-DSPIRV_CROSS_CLI=OFF" "-DSPIRV_CROSS_ENABLE_TESTS=OFF" "-DCMAKE_INSTALL_LIBDIR=lib" "-DCMAKE_INSTALL_BINDIR=bin" "-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>" "-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}" "-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}"
BUILD_COMMAND "${CMAKE_COMMAND}" "--build" "<BINARY_DIR>" --config "Release"
INSTALL_COMMAND "${CMAKE_COMMAND}" "--install" "<BINARY_DIR>" --config "Release"
)
ExternalProject_Get_property(spirv_cross INSTALL_DIR)
ExternalProject_Get_property(spirv_cross SOURCE_DIR)
install(FILES "${SOURCE_DIR}/LICENSE" DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/licenses/spirv-cross")
install(DIRECTORY "${INSTALL_DIR}/lib/" DESTINATION "${CMAKE_INSTALL_LIBDIR}" FILES_MATCHING PATTERN "*.so*" PERMISSIONS ${chmod_0755})
install(DIRECTORY "${INSTALL_DIR}/lib/" DESTINATION "${CMAKE_INSTALL_LIBDIR}" FILES_MATCHING PATTERN "*.dylib*" PERMISSIONS ${chmod_0755})
install(DIRECTORY "${INSTALL_DIR}/bin/" DESTINATION "${CMAKE_INSTALL_BINDIR}" FILES_MATCHING PATTERN "*.dll" PERMISSIONS ${chmod_0755})

if(NOT WIN32)
ExternalProject_Add(spirv_headers
GIT_REPOSITORY "https://github.com/KhronosGroup/SPIRV-Headers.git"
Expand Down
10 changes: 7 additions & 3 deletions cmake/SDL3_gpu_shadercrossConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@
include(FeatureSummary)
set_package_properties(SDL3_gpu_shadercross PROPERTIES
URL "https://github.com/libsdl-org/SDL_gpu_shadercross/"
DESCRIPTION "Support support SPIR-V and HLSL on various backends"
DESCRIPTION "Support SPIR-V and HLSL on various backends"
)

set(SDL3_gpu_shadercross_FOUND ON)

set(SDL3_gpu_SHADERCROSS_REQUIRED_VERSION @SDL_REQUIRED_VERSION@)

if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/SDL3_gpu_shadercross-vendored-targets.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/SDL3_gpu_shadercross-vendored-targets.cmake")
endif()

set(SDL3_gpu_shadercross_SDL3_gpu_shadercross-shared_FOUND FALSE)
if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/SDL3_gpu_shadercross-shared-targets.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/SDL3_gpu_shadercross-shared.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/SDL3_gpu_shadercross-shared-targets.cmake")
set(SDL3_gpu_shadercross_SDL3_gpu_shadercross-shared_FOUND TRUE)
endif()

set(SDL3_gpu_shadercross_SDL3_gpu_shadercross-static FALSE)
if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/SDL3_gpu_shadercross-static-targets.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/SDL3_gpu_shadercross-static.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/SDL3_gpu_shadercross-static-targets.cmake")
set(SDL3_gpu_shadercross_SDL3_gpu_shadercross-static_FOUND TRUE)
endif()

Expand Down
4 changes: 2 additions & 2 deletions cmake/sdl3-gpu-shadercross.pc.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@

Name: @PROJECT_NAME@
Description: image loading library for Simple DirectMedia Layer
Description: Support SPIR-V and HLSL on various backends
Version: @PROJECT_VERSION@
Requires: sdl3 >= @SDL_REQUIRED_VERSION@
Libs: -L${libdir} -lSDL_gpu_shadercross
Libs: -L${libdir} -lSDL3_gpu_shadercross
Requires.private: @PC_REQUIRES@
Libs.private: @PC_LIBS@
Cflags: -I${includedir}
44 changes: 44 additions & 0 deletions cmake/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# This cmake build script is meant for verifying the various CMake configuration script.

cmake_minimum_required(VERSION 3.12)
project(sdl_test LANGUAGES C)

cmake_policy(SET CMP0074 NEW)

# Override CMAKE_FIND_ROOT_PATH_MODE to allow search for SDL3 outside of sysroot
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE NEVER)

include(FeatureSummary)

option(TEST_SHARED "Test linking to shared SDL3_gpu_shadercross library" ON)
add_feature_info("TEST_SHARED" TEST_SHARED "Test linking with shared library")

option(TEST_STATIC "Test linking to static SDL3_gpu_shadercross library" ON)
add_feature_info("TEST_STATIC" TEST_STATIC "Test linking with static library")

if(ANDROID)
macro(add_executable NAME)
set(args ${ARGN})
list(REMOVE_ITEM args WIN32)
add_library(${NAME} SHARED ${args})
unset(args)
endmacro()
endif()

if(TEST_SHARED)
find_package(SDL3 REQUIRED CONFIG COMPONENTS SDL3)
find_package(SDL3_gpu_shadercross REQUIRED CONFIG)
add_executable(main_shared main.c)
target_link_libraries(main_shared PRIVATE SDL3_gpu_shadercross::SDL3_gpu_shadercross-shared SDL3::SDL3)
endif()

if(TEST_STATIC)
find_package(SDL3 REQUIRED CONFIG COMPONENTS SDL3)
# some static vendored libraries use c++ (enable CXX after `find_package` might show a warning)
enable_language(CXX)
find_package(SDL3_gpu_shadercross REQUIRED CONFIG)
add_executable(main_static main.c)
target_link_libraries(main_static PRIVATE SDL3_gpu_shadercross::SDL3_gpu_shadercross-static SDL3::SDL3)
endif()

feature_summary(WHAT ALL)
16 changes: 16 additions & 0 deletions cmake/test/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <SDL3/SDL.h>
#include <SDL3_gpu_shadercross/SDL_gpu_shadercross.h>

int main(int argc, char *argv[]) {
if (!SDL_Init(0)) {
SDL_Log("SDL_Init failed (%s)", SDL_GetError());
return 1;
}
if (!SDL_ShaderCross_Init()) {
SDL_Log("SDL_ShaderCross_Init failed (%s)", SDL_GetError());
return 1;
}
SDL_ShaderCross_Quit();
SDL_Quit();
return 0;
}
36 changes: 36 additions & 0 deletions external/Get-GitModules.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<#
.SYNOPSIS
Downloads the Git modules specified in ../.gitmodules
.DESCRIPTION
Parses and downloads the Github repositories specified in the .gitmodules file
.EXAMPLE
PS> .\Get-GitModules.ps1
< Downloads and parses the repositories in the .gitmodules file. >
#>

#------- Variables -------------------------------------------------------------
[String] $PathRegex = "path\s*=\s*(?<path>.*)"
[String] $URLRegex = "url\s*=\s*(?<url>.*)"
[String] $BranchRegex = "branch\s*=\s*(?<Branch>.*)"

#------- Script ----------------------------------------------------------------
foreach ($Line in Get-Content $PSScriptRoot\..\.gitmodules) {
if ($Line -match $PathRegex) {
$Match = Select-String -InputObject $Line -Pattern $PathRegex
$Path = $Match.Matches[0].Groups[1].Value
}
elseif ($Line -match $URLRegex) {
$Match = Select-String -InputObject $Line -Pattern $URLRegex
$URL = $Match.Matches[0].Groups[1].Value
}
elseif ($Line -match $BranchRegex) {
$Match = Select-String -InputObject $Line -Pattern $BranchRegex
$Branch = $Match.Matches[0].Groups[1].Value

Write-Host "git clone --filter=blob:none $URL $Path -b $Branch --recursive" `
-ForegroundColor Blue
git clone --filter=blob:none $URL $PSScriptRoot/../$Path -b $Branch --recursive
}
}
1 change: 1 addition & 0 deletions external/SPIRV-Cross
Submodule SPIRV-Cross added at d1d4ad
Loading

0 comments on commit 3385fb5

Please sign in to comment.