Skip to content

Commit

Permalink
Use SDL3_gpu_shadercross instead of unversioned SDL_gpu_shadercross
Browse files Browse the repository at this point in the history
  • Loading branch information
madebr committed Oct 25, 2024
1 parent 406484e commit 15d4bea
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 107 deletions.
113 changes: 58 additions & 55 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# CMake Project for SDL_gpu_shadercross - Simple DirectMedia Layer Shader Cross Compiler
# CMake Project for SDL3_gpu_shadercross - Simple DirectMedia Layer Shader Cross Compiler
# Written by @thatcosmonaut
cmake_minimum_required(VERSION 3.22)

# Version
set(MAJOR_VERSION "1")
set(MINOR_VERSION "0")
set(MICRO_VERSION "0")
set(MAJOR_VERSION 3)
set(MINOR_VERSION 0)
set(MICRO_VERSION 0)
set(SDL_REQUIRED_VERSION "3.1.3")

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

include(CMakeDependentOption)
include("${CMAKE_CURRENT_LIST_DIR}/cmake/PrivateSdlFunctions.cmake")
Expand Down Expand Up @@ -40,81 +40,84 @@ endif()
# Source lists
set(SOURCE_FILES
# Public Headers
include/SDL_gpu_shadercross.h
include/SDL3_gpu_shadercross/SDL_gpu_shadercross.h
# Source Files
src/SDL_gpu_shadercross.c
src/spirv_cross_c.h
src/spirv.h
)

add_library(SDL_gpu_shadercross-shared SHARED ${SOURCE_FILES})
add_library(SDL_gpu_shadercross::SDL_gpu_shadercross ALIAS SDL_gpu_shadercross-shared)
add_library(SDL3_gpu_shadercross-shared SHARED ${SOURCE_FILES})
add_library(SDL3_gpu_shadercross::SDL3_gpu_shadercross ALIAS SDL3_gpu_shadercross-shared)

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

# Build flags
if(WIN32)
target_sources(SDL_gpu_shadercross-shared PRIVATE "src/version.rc")
set_property(TARGET SDL_gpu_shadercross-shared PROPERTY PREFIX "")
target_sources(SDL3_gpu_shadercross-shared PRIVATE "src/version.rc")
set_property(TARGET SDL3_gpu_shadercross-shared PROPERTY PREFIX "")
endif()
target_compile_features(SDL_gpu_shadercross-shared PRIVATE c_std_99)
target_compile_features(SDL3_gpu_shadercross-shared PRIVATE c_std_99)

# SDL_gpu_shadercross folders as includes, for other targets to consume
target_include_directories(SDL_gpu_shadercross-shared PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)
# SDL3_gpu_shadercross folders as includes, for other targets to consume
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_libraries(SDL_gpu_shadercross-shared PRIVATE -static-libgcc)
target_link_options(SDL3_gpu_shadercross-shared PRIVATE "-static-libgcc")
endif()

# Soname
set_target_properties(SDL_gpu_shadercross-shared PROPERTIES OUTPUT_NAME "SDL_gpu_shadercross"
set_target_properties(SDL3_gpu_shadercross-shared PROPERTIES
OUTPUT_NAME "SDL3_gpu_shadercross"
SOVERSION "${SO_VERSION_MAJOR}"
VERSION "${SO_VERSION}"
)

target_link_libraries(SDL_gpu_shadercross-shared PRIVATE
target_link_libraries(SDL3_gpu_shadercross-shared PRIVATE
SDL3::SDL3-shared
)

if(BUILD_STATIC)
find_package(SDL3 REQUIRED COMPONENTS Headers)

add_library(SDL_gpu_shadercross-static STATIC ${SOURCE_FILES})
add_library(SDL_gpu_shadercross::SDL_gpu_shadercross-static ALIAS SDL_gpu_shadercross-static)
sdl_add_warning_options(SDL_gpu_shadercross-static WARNING_AS_ERROR ${ENABLE_WERROR})
target_compile_features(SDL_gpu_shadercross-static PRIVATE c_std_99)
add_library(SDL3_gpu_shadercross-static STATIC ${SOURCE_FILES})
add_library(SDL3_gpu_shadercross::SDL3_gpu_shadercross-static ALIAS SDL3_gpu_shadercross-static)
sdl_add_warning_options(SDL3_gpu_shadercross-static WARNING_AS_ERROR ${ENABLE_WERROR})
target_compile_features(SDL3_gpu_shadercross-static PRIVATE c_std_99)

if(NOT MSVC)
set_property(TARGET SDL_gpu_shadercross-static PROPERTY OUTPUT_NAME "SDL_gpu_shadercross")
set_property(TARGET SDL3_gpu_shadercross-static PROPERTY OUTPUT_NAME "SDL3_gpu_shadercross")
endif()

# SDL_gpu_shadercross folders as includes, for other targets to consume
target_include_directories(SDL_gpu_shadercross-static PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)
# SDL3_gpu_shadercross folders as includes, for other targets to consume
target_include_directories(SDL3_gpu_shadercross-static PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>")
target_include_directories(SDL3_gpu_shadercross-static PUBLIC "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")

target_link_libraries(SDL_gpu_shadercross-static PUBLIC
target_link_libraries(SDL3_gpu_shadercross-static PUBLIC
SDL3::Headers
)
endif()

if(NOT TARGET SDL_gpu_shadercross::SDL_gpu_shadercross)
if(TARGET SDL_gpu_shadercross-shared)
add_library(SDL_gpu_shadercross::SDL_gpu_shadercross ALIAS SDL_gpu_shadercross-shared)
if(NOT TARGET SDL3_gpu_shadercross::SDL3_gpu_shadercross)
if(TARGET SDL3_gpu_shadercross-shared)
add_library(SDL3_gpu_shadercross::SDL3_gpu_shadercross ALIAS SDL3_gpu_shadercross-shared)
else()
add_library(SDL_gpu_shadercross::SDL_gpu_shadercross ALIAS SDL_gpu_shadercross-static)
add_library(SDL3_gpu_shadercross::SDL3_gpu_shadercross ALIAS SDL3_gpu_shadercross-static)
endif()
endif()

if(BUILD_CLI)
add_executable(shadercross src/cli.c)

if(BUILD_CLI_STATIC)
target_link_libraries(shadercross PRIVATE SDL_gpu_shadercross::SDL_gpu_shadercross-static)
target_link_libraries(shadercross PRIVATE SDL3_gpu_shadercross::SDL3_gpu_shadercross-static)
target_link_libraries(shadercross PRIVATE SDL3::SDL3-static)
else()
target_link_libraries(shadercross PRIVATE SDL_gpu_shadercross::SDL_gpu_shadercross)
target_link_libraries(shadercross PRIVATE SDL3_gpu_shadercross::SDL3_gpu_shadercross)
target_link_libraries(shadercross PRIVATE SDL3::SDL3)
endif()
endif()
Expand All @@ -126,60 +129,60 @@ if(ENABLE_INSTALL)
else()
set(INSTALL_CMAKEDIR_ROOT_DEFAULT "${CMAKE_INSTALL_LIBDIR}/cmake")
endif()
set(SDLGPUSHADERCROSS_INSTALL_CMAKEDIR_ROOT "${INSTALL_CMAKEDIR_ROOT_DEFAULT}" CACHE STRING "Root folder where to install SDL_gpu_shadercross cmake related files (SDL_gpu_shadercross subfolder for MSVC projects)")
set(SDLGPUSHADERCROSS_INSTALL_CMAKEDIR_ROOT "${INSTALL_CMAKEDIR_ROOT_DEFAULT}" CACHE STRING "Root folder where to install SDL3_gpu_shadercross cmake related files (SDL3_gpu_shadercross subfolder for MSVC projects)")
set(SDLGPUSHADERCROSS_PKGCONFIG_INSTALLDIR "${CMAKE_INSTALL_LIBDIR}/pkgconfig")

if(WIN32 AND NOT MINGW)
set(SDLGPUSHADERCROSS_INSTALL_CMAKEDIR "${SDLGPUSHADERCROSS_INSTALL_CMAKEDIR_ROOT}")
else()
set(SDLGPUSHADERCROSS_INSTALL_CMAKEDIR "${SDLGPUSHADERCROSS_INSTALL_CMAKEDIR_ROOT}/SDL_gpu_shadercross")
set(SDLGPUSHADERCROSS_INSTALL_CMAKEDIR "${SDLGPUSHADERCROSS_INSTALL_CMAKEDIR_ROOT}/SDL3_gpu_shadercross")
endif()

if(TARGET SDL_gpu_shadercross-shared)
install(TARGETS SDL_gpu_shadercross-shared EXPORT SDL_gpu_shadercross-shared-export
if(TARGET SDL3_gpu_shadercross-shared)
install(TARGETS SDL3_gpu_shadercross-shared EXPORT SDL3_gpu_shadercross-shared-export
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT devel
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT library
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT library
)
install(EXPORT SDL_gpu_shadercross-shared-export
FILE SDL_gpu_shadercross-shared-targets.cmake
NAMESPACE SDL_gpu_shadercross::
install(EXPORT SDL3_gpu_shadercross-shared-export
FILE SDL3_gpu_shadercross-shared-targets.cmake
NAMESPACE SDL3_gpu_shadercross::
DESTINATION "${SDLGPUSHADERCROSS_INSTALL_CMAKEDIR}"
COMPONENT devel
)
endif()
if(TARGET SDL_gpu_shadercross-static)
install(TARGETS SDL_gpu_shadercross-static EXPORT SDL_gpu_shadercross-static-export
if(TARGET SDL3_gpu_shadercross-static)
install(TARGETS SDL3_gpu_shadercross-static EXPORT SDL3_gpu_shadercross-static-export
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT devel
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT library
)
install(EXPORT SDL_gpu_shadercross-static-export
FILE SDL_gpu_shadercross-static-targets.cmake
NAMESPACE SDL_gpu_shadercross::
install(EXPORT SDL3_gpu_shadercross-static-export
FILE SDL3_gpu_shadercross-static-targets.cmake
NAMESPACE SDL3_gpu_shadercross::
DESTINATION "${SDLGPUSHADERCROSS_INSTALL_CMAKEDIR}"
COMPONENT devel
)
endif()
install(
FILES "${CMAKE_CURRENT_SOURCE_DIR}/include/SDL_gpu_shadercross.h"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" COMPONENT DEVEL
FILES "${CMAKE_CURRENT_SOURCE_DIR}/include/SDL3_gpu_shadercross/SDL_gpu_shadercross.h"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/SDL3_gpu_shadercross" COMPONENT DEVEL
)
if(BUILD_CLI)
install(TARGETS shadercross RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
endif()

include(CMakePackageConfigHelpers)
configure_package_config_file(cmake/SDL_gpu_shadercrossConfig.cmake.in SDL_gpu_shadercrossConfig.cmake
configure_package_config_file(cmake/SDL3_gpu_shadercrossConfig.cmake.in SDL3_gpu_shadercrossConfig.cmake
NO_SET_AND_CHECK_MACRO
INSTALL_DESTINATION "${SDLGPUSHADERCROSS_INSTALL_CMAKEDIR}"
)
write_basic_package_version_file("${PROJECT_BINARY_DIR}/SDL_gpu_shadercrossConfigVersion.cmake"
write_basic_package_version_file("${PROJECT_BINARY_DIR}/SDL3_gpu_shadercrossConfigVersion.cmake"
COMPATIBILITY AnyNewerVersion
)
install(
FILES
"${CMAKE_CURRENT_BINARY_DIR}/SDL_gpu_shadercrossConfig.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/SDL_gpu_shadercrossConfigVersion.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/SDL3_gpu_shadercrossConfig.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/SDL3_gpu_shadercrossConfigVersion.cmake"
DESTINATION "${SDLGPUSHADERCROSS_INSTALL_CMAKEDIR}"
COMPONENT devel
)
Expand All @@ -189,10 +192,10 @@ if(ENABLE_INSTALL)
set(SDL_PKGCONFIG_PREFIX "\${pcfiledir}/${SDL_PATH_PREFIX_RELATIVE_TO_PKGCONFIG}")
set(PC_REQUIRED "")
set(PC_LIBS "")
configure_file(cmake/sdl-gpu-shadercross.pc.in sdl-gpu-shadercross.pc @ONLY)
configure_file(cmake/sdl3-gpu-shadercross.pc.in sdl3-gpu-shadercross.pc @ONLY)

# Always install sdl-gpu-shadercross.pc file: libraries might be different between config modes
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/sdl-gpu-shadercross.pc"
# Always install sdl3-gpu-shadercross.pc file: libraries might be different between config modes
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/sdl3-gpu-shadercross.pc"
DESTINATION "${SDLGPUSHADERCROSS_PKGCONFIG_INSTALLDIR}" COMPONENT devel)

install(FILES "LICENSE.txt"
Expand All @@ -210,7 +213,7 @@ if(ENABLE_INSTALL)
configure_file(cmake/CPackProjectConfig.cmake.in CPackProjectConfig.cmake @ONLY)
set(CPACK_PROJECT_CONFIG_FILE "${PROJECT_BINARY_DIR}/CPackProjectConfig.cmake")
# CPACK_SOURCE_PACKAGE_FILE_NAME must end with "-src" (so we can block creating a source archive)
set(CPACK_SOURCE_PACKAGE_FILE_NAME "SDL_gpu_shadercross-${PROJECT_VERSION}-src")
set(CPACK_SOURCE_PACKAGE_FILE_NAME "SDL3_gpu_shadercross-${PROJECT_VERSION}-src")
set(CPACK_PACKAGE_DIRECTORY "${CMAKE_BINARY_DIR}/dist")
include(CPack)
endif()
Expand Down
81 changes: 81 additions & 0 deletions build-scripts/test-versioning.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/bin/sh
# Copyright 2022 Collabora Ltd.
# SPDX-License-Identifier: Zlib

set -eu

cd `dirname $0`/..

# Needed so sed doesn't report illegal byte sequences on macOS
export LC_CTYPE=C

header=include/SDL3_gpu_shadercross/SDL_gpu_shadercross.h
ref_major=$(sed -ne 's/^#define SDL_GPU_SHADERCROSS_MAJOR_VERSION *//p' $header)
ref_minor=$(sed -ne 's/^#define SDL_GPU_SHADERCROSS_MINOR_VERSION *//p' $header)
ref_micro=$(sed -ne 's/^#define SDL_GPU_SHADERCROSS_MICRO_VERSION *//p' $header)
ref_version="${ref_major}.${ref_minor}.${ref_micro}"

tests=0
failed=0

ok () {
tests=$(( tests + 1 ))
echo "ok - $*"
}

not_ok () {
tests=$(( tests + 1 ))
echo "not ok - $*"
failed=1
}

major=$(sed -ne 's/^set(MAJOR_VERSION \([0-9]*\))$/\1/p' CMakeLists.txt)
minor=$(sed -ne 's/^set(MINOR_VERSION \([0-9]*\))$/\1/p' CMakeLists.txt)
micro=$(sed -ne 's/^set(MICRO_VERSION \([0-9]*\))$/\1/p' CMakeLists.txt)
ref_sdl_req=$(sed -ne 's/^set(SDL_REQUIRED_VERSION \([0-9.]*\))$/\1/p' CMakeLists.txt)
version="${major}.${minor}.${micro}"

if [ "$ref_version" = "$version" ]; then
ok "CMakeLists.txt $version"
else
not_ok "CMakeLists.txt $version disagrees with SDL_gpu_shadercross.h $ref_version"
fi

for rcfile in src/version.rc; do
tuple=$(sed -ne 's/^ *FILEVERSION *//p' "$rcfile" | tr -d '\r')
ref_tuple="${ref_major},${ref_minor},${ref_micro},0"

if [ "$ref_tuple" = "$tuple" ]; then
ok "$rcfile FILEVERSION $tuple"
else
not_ok "$rcfile FILEVERSION $tuple disagrees with SDL_gpu_shadercross.h $ref_tuple"
fi

tuple=$(sed -ne 's/^ *PRODUCTVERSION *//p' "$rcfile" | tr -d '\r')

if [ "$ref_tuple" = "$tuple" ]; then
ok "$rcfile PRODUCTVERSION $tuple"
else
not_ok "$rcfile PRODUCTVERSION $tuple disagrees with SDL_gpu_shadercross.h $ref_tuple"
fi

tuple=$(sed -Ene 's/^ *VALUE "FileVersion", "([0-9, ]*)\\0"\r?$/\1/p' "$rcfile" | tr -d '\r')
ref_tuple="${ref_major}, ${ref_minor}, ${ref_micro}, 0"

if [ "$ref_tuple" = "$tuple" ]; then
ok "$rcfile FileVersion $tuple"
else
not_ok "$rcfile FileVersion $tuple disagrees with SDL_gpu_shadercross.h $ref_tuple"
fi

tuple=$(sed -Ene 's/^ *VALUE "ProductVersion", "([0-9, ]*)\\0"\r?$/\1/p' "$rcfile" | tr -d '\r')

if [ "$ref_tuple" = "$tuple" ]; then
ok "$rcfile ProductVersion $tuple"
else
not_ok "$rcfile ProductVersion $tuple disagrees with SDL_gpu_shadercross.h $ref_tuple"
fi
done

echo "1..$tests"
exit "$failed"
45 changes: 45 additions & 0 deletions cmake/SDL3_gpu_shadercrossConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# sdl3_gpu_shadercross cmake project-config input for CMakeLists.txt script

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"
)

set(SDL3_gpu_shadercross_FOUND ON)

set(SDL3_gpu_SHADERCROSS_REQUIRED_VERSION @SDL_REQUIRED_VERSION@)

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")
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")
set(SDL3_gpu_shadercross_SDL3_gpu_shadercross-static_FOUND TRUE)
endif()

function(_sdl_create_target_alias_compat NEW_TARGET TARGET)
if(CMAKE_VERSION VERSION_LESS "3.18")
# Aliasing local targets is not supported on CMake < 3.18, so make it global.
add_library(${NEW_TARGET} INTERFACE IMPORTED)
set_target_properties(${NEW_TARGET} PROPERTIES INTERFACE_LINK_LIBRARIES "${TARGET}")
else()
add_library(${NEW_TARGET} ALIAS ${TARGET})
endif()
endfunction()

# Make sure SDL3_gpu_shadercross::SDL3_gpu_shadercross always exists
if(NOT TARGET SDL3_gpu_shadercross::SDL3_gpu_shadercross)
if(TARGET SDL3_gpu_shadercross::SDL3_gpu_shadercross)
_sdl_create_target_alias_compat(SDL3_gpu_shadercross::SDL3_gpu_shadercross SDL3_gpu_shadercross::SDL3_gpu_shadercross-shared)
elseif(TARGET SDL3_gpu_shadercross::SDL3_gpu_shadercross-static)
_sdl_create_target_alias_compat(SDL3_gpu_shadercross::SDL3_gpu_shadercross SDL3_gpu_shadercross::SDL3_gpu_shadercross-static)
endif()
endif()

@PACKAGE_INIT@
check_required_components(SDL3_gpu_shadercross)
Loading

0 comments on commit 15d4bea

Please sign in to comment.