SDL_gpu_shadercross: Restructure project

From 2041b68f5ec7301c9f090287a31715cfd0f8895d Mon Sep 17 00:00:00 2001
From: cosmonaut <[EMAIL REDACTED]>
Date: Wed, 23 Oct 2024 11:09:16 -0700
Subject: [PATCH] Restructure project

---
 .gitignore                                    |   1 +
 CMakeLists.txt                                |  97 ++++++++++++++
 include/SDL_gpu_shadercross.h                 | 126 ++++++++++++++++++
 .../SDL_gpu_shadercross.c                     | 110 +--------------
 src/cli.c                                     |  27 ++++
 spirv.h => src/spirv.h                        |   0
 spirv_cross_c.h => src/spirv_cross_c.h        |   0
 test.c                                        |   3 -
 8 files changed, 252 insertions(+), 112 deletions(-)
 create mode 100644 .gitignore
 create mode 100644 CMakeLists.txt
 create mode 100644 include/SDL_gpu_shadercross.h
 rename SDL_gpu_shadercross.h => src/SDL_gpu_shadercross.c (89%)
 create mode 100644 src/cli.c
 rename spirv.h => src/spirv.h (100%)
 rename spirv_cross_c.h => src/spirv_cross_c.h (100%)
 delete mode 100644 test.c

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..567609b
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+build/
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..1338f2a
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,97 @@
+# CMake Project for SDL_gpu_shadercross - Simple DirectMedia Layer Shader Cross Compiler
+# Written by @thatcosmonaut
+cmake_minimum_required(VERSION 3.5)
+project(SDL_gpu_shadercross C)
+find_package(SDL3 REQUIRED)
+
+# Options
+option(BUILD_CLI "Build command line executable" ON)
+option(BUILD_SHARED_LIBS "Build shared library" ON)
+
+# Version
+SET(LIB_MAJOR_VERSION "1")
+SET(LIB_MINOR_VERSION "0")
+SET(LIB_REVISION "0")
+SET(LIB_VERSION "${LIB_MAJOR_VERSION}.${LIB_MINOR_VERSION}.${LIB_REVISION}")
+
+# Build Type
+if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
+	# By default, we use Release
+	message(STATUS "Setting build type to 'Release' as none was specified.")
+	set(CMAKE_BUILD_TYPE "Release" CACHE
+		STRING "Choose the type of build." FORCE
+	)
+
+	# Set the possible values of build type for cmake-gui
+	set_property(CACHE CMAKE_BUILD_TYPE PROPERTY
+		STRINGS "Debug" "Release" "RelWithDebInfo"
+	)
+endif()
+
+# Platform Flags
+if(APPLE)
+	set(CMAKE_MACOSX_RPATH ON)
+	set(CMAKE_OSX_DEPLOYMENT_TARGET 11.0)
+	set(LOBJC "objc")
+elseif(WIN32)
+	# "SDL_gpu_shadercross.dll", not "libSDL_gpu_shadercross.dll"
+	set(CMAKE_SHARED_LIBRARY_PREFIX "")
+endif()
+
+# Source lists
+
+file(GLOB SOURCE_FILES
+	# Public Headers
+	SDL_gpu_shadercross.h
+	# Source Files
+    src/SDL_gpu_shadercross.c
+    src/spirv_cross_c.h
+    src/spirv.h
+)
+
+if(BUILD_SHARED_LIBS)
+    add_library(SDL_gpu_shadercross SHARED ${SOURCE_FILES})
+else()
+    add_library(SDL_gpu_shadercross STATIC ${SOURCE_FILES})
+endif()
+
+# Build flags
+if(NOT MSVC)
+	set_property(TARGET SDL_gpu_shadercross PROPERTY COMPILE_FLAGS "-std=gnu99 -Wall -Wno-strict-aliasing -pedantic")
+endif()
+
+# SDL_gpu_shadercross folders as includes, for other targets to consume
+target_include_directories(SDL_gpu_shadercross PUBLIC
+	$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
+	$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
+)
+
+# MinGW builds should statically link libgcc
+if(MINGW)
+	target_link_libraries(SDL_gpu_shadercross PRIVATE -static-libgcc)
+endif()
+
+# Soname
+set_target_properties(SDL_gpu_shadercross PROPERTIES OUTPUT_NAME "SDL_gpu_shadercross"
+	VERSION ${LIB_VERSION}
+	SOVERSION ${LIB_MAJOR_VERSION}
+)
+
+target_link_libraries(SDL_gpu_shadercross PRIVATE
+    SDL3::SDL3
+    SDL3::Headers
+)
+
+if(BUILD_CLI)
+    file(GLOB CLI_SOURCES
+        src/cli.c
+    )
+
+    add_executable(SDL_gpu_shadercross_cli ${CLI_SOURCES})
+
+    if(BUILD_SHARED_LIBS)
+        target_link_libraries(SDL_gpu_shadercross_cli PUBLIC SDL_gpu_shadercross)
+    else()
+        target_link_libraries(SDL_gpu_shadercross_cli PRIVATE SDL_gpu_shadercross)
+    endif()
+endif()
diff --git a/include/SDL_gpu_shadercross.h b/include/SDL_gpu_shadercross.h
new file mode 100644
index 0000000..cae3475
--- /dev/null
+++ b/include/SDL_gpu_shadercross.h
@@ -0,0 +1,126 @@
+/*
+  Simple DirectMedia Layer Shader Cross Compiler
+  Copyright (C) 2024 Sam Lantinga <slouken@libsdl.org>
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely, subject to the following restrictions:
+
+  1. The origin of this software must not be misrepresented; you must not
+     claim that you wrote the original software. If you use this software
+     in a product, an acknowledgment in the product documentation would be
+     appreciated but is not required.
+  2. Altered source versions must be plainly marked as such, and must not be
+     misrepresented as being the original software.
+  3. This notice may not be removed or altered from any source distribution.
+*/
+
+#ifndef SDL_GPU_SHADERCROSS_H
+#define SDL_GPU_SHADERCROSS_H
+
+#include <SDL3/SDL.h>
+
+#ifndef SDL_GPU_SHADERCROSS_SPIRVCROSS
+#define SDL_GPU_SHADERCROSS_SPIRVCROSS 1
+#endif /* SDL_GPU_SHADERCROSS_SPIRVCROSS */
+
+#ifndef SDL_GPU_SHADERCROSS_HLSL
+#define SDL_GPU_SHADERCROSS_HLSL 1
+#endif /* SDL_GPU_SHADERCROSS_HLSL */
+
+#ifndef SDL_GPU_SHADERCROSS_EXPORT
+#define SDL_GPU_SHADERCROSS_EXPORT
+#endif
+
+/**
+ * Initializes SDL_gpu_shadercross
+ *
+ * \threadsafety This should only be called once, from a single thread.
+ */
+extern bool SDL_ShaderCross_Init(void);
+/**
+ * De-initializes SDL_gpu_shadercross
+ *
+ * \threadsafety This should only be called once, from a single thread.
+ */
+extern void SDL_ShaderCross_Quit(void);
+
+#if SDL_GPU_SHADERCROSS_SPIRVCROSS
+/**
+ * Get the supported shader formats that SPIRV cross-compilation can output
+ *
+ * \threadsafety It is safe to call this function from any thread.
+ */
+extern SDL_GPUShaderFormat SDL_ShaderCross_GetSPIRVShaderFormats(void);
+
+/**
+ * Compile an SDL GPU shader from SPIRV code.
+ *
+ * \param device the SDL GPU device.
+ * \param createInfo a pointer to an SDL_GPUShaderCreateInfo.
+ * \returns a compiled SDL_GPUShader
+ *
+ * \threadsafety It is safe to call this function from any thread.
+ */
+extern SDL_GPUShader *SDL_ShaderCross_CompileGraphicsShaderFromSPIRV(SDL_GPUDevice *device,
+                                              const SDL_GPUShaderCreateInfo *createInfo);
+
+/**
+ * Compile an SDL GPU compute pipeline from SPIRV code.
+ *
+ * \param device the SDL GPU device.
+ * \param createInfo a pointer to an SDL_GPUComputePipelineCreateInfo.
+ * \returns a compiled SDL_GPUComputePipeline
+ *
+ * \threadsafety It is safe to call this function from any thread.
+ */
+extern SDL_GPUComputePipeline *SDL_ShaderCross_CompileComputePipelineFromSPIRV(SDL_GPUDevice *device,
+                                              const SDL_GPUComputePipelineCreateInfo *createInfo);
+#endif /* SDL_GPU_SHADERCROSS_SPIRVCROSS */
+
+#if SDL_GPU_SHADERCROSS_HLSL
+/**
+ * Get the supported shader formats that HLSL cross-compilation can output
+ *
+ * \threadsafety It is safe to call this function from any thread.
+ */
+extern SDL_GPUShaderFormat SDL_ShaderCross_GetHLSLShaderFormats(void);
+
+/**
+ * Compile an SDL GPU shader from HLSL code.
+ *
+ * \param device the SDL GPU device.
+ * \param createInfo a pointer to an SDL_GPUShaderCreateInfo.
+ * \param hlslSource the HLSL source code for the shader.
+ * \param shaderProfile the shader profile to compile the shader with.
+ * \returns a compiled SDL_GPUShader
+ *
+ * \threadsafety It is safe to call this function from any thread.
+ */
+extern SDL_GPUShader *SDL_ShaderCross_CompileGraphicsShaderFromHLSL(SDL_GPUDevice *device,
+                                             const SDL_GPUShaderCreateInfo *createInfo,
+                                             const char *hlslSource,
+                                             const char *shaderProfile);
+
+/**
+ * Compile an SDL GPU compute pipeline from HLSL code.
+ *
+ * \param device the SDL GPU device.
+ * \param createInfo a pointer to an SDL_GPUComputePipelineCreateInfo.
+ * \param hlslSource the HLSL source code for the shader.
+ * \param shaderProfile the shader profile to compile the shader with.
+ * \returns a compiled SDL_GPUComputePipeline
+ *
+ * \threadsafety It is safe to call this function from any thread.
+ */
+extern SDL_GPUComputePipeline *SDL_ShaderCross_CompileComputePipelineFromHLSL(SDL_GPUDevice *device,
+                                             const SDL_GPUComputePipelineCreateInfo *createInfo,
+                                             const char *hlslSource,
+                                             const char *shaderProfile);
+#endif /* SDL_GPU_SHADERCROSS_HLSL */
+
+#endif /* SDL_GPU_SHADERCROSS_H */
diff --git a/SDL_gpu_shadercross.h b/src/SDL_gpu_shadercross.c
similarity index 89%
rename from SDL_gpu_shadercross.h
rename to src/SDL_gpu_shadercross.c
index 302d0e2..e8a77d6 100644
--- a/SDL_gpu_shadercross.h
+++ b/src/SDL_gpu_shadercross.c
@@ -19,113 +19,7 @@
   3. This notice may not be removed or altered from any source distribution.
 */
 
-#ifndef SDL_GPU_SHADERCROSS_H
-#define SDL_GPU_SHADERCROSS_H
-
-#include <SDL3/SDL.h>
-
-#ifndef SDL_GPU_SHADERCROSS_SPIRVCROSS
-#define SDL_GPU_SHADERCROSS_SPIRVCROSS 1
-#endif /* SDL_GPU_SHADERCROSS_SPIRVCROSS */
-
-#ifndef SDL_GPU_SHADERCROSS_HLSL
-#define SDL_GPU_SHADERCROSS_HLSL 1
-#endif /* SDL_GPU_SHADERCROSS_HLSL */
-
-#ifndef SDL_GPU_SHADERCROSS_EXPORT
-#define SDL_GPU_SHADERCROSS_EXPORT
-#endif
-
-/**
- * Initializes SDL_gpu_shadercross
- *
- * \threadsafety This should only be called once, from a single thread.
- */
-extern bool SDL_ShaderCross_Init(void);
-/**
- * De-initializes SDL_gpu_shadercross
- *
- * \threadsafety This should only be called once, from a single thread.
- */
-extern void SDL_ShaderCross_Quit(void);
-
-#if SDL_GPU_SHADERCROSS_SPIRVCROSS
-/**
- * Get the supported shader formats that SPIRV cross-compilation can output
- *
- * \threadsafety It is safe to call this function from any thread.
- */
-extern SDL_GPUShaderFormat SDL_ShaderCross_GetSPIRVShaderFormats(void);
-
-/**
- * Compile an SDL GPU shader from SPIRV code.
- *
- * \param device the SDL GPU device.
- * \param createInfo a pointer to an SDL_GPUShaderCreateInfo.
- * \returns a compiled SDL_GPUShader
- *
- * \threadsafety It is safe to call this function from any thread.
- */
-extern SDL_GPUShader *SDL_ShaderCross_CompileGraphicsShaderFromSPIRV(SDL_GPUDevice *device,
-                                              const SDL_GPUShaderCreateInfo *createInfo);
-
-/**
- * Compile an SDL GPU compute pipeline from SPIRV code.
- *
- * \param device the SDL GPU device.
- * \param createInfo a pointer to an SDL_GPUComputePipelineCreateInfo.
- * \returns a compiled SDL_GPUComputePipeline
- *
- * \threadsafety It is safe to call this function from any thread.
- */
-extern SDL_GPUComputePipeline *SDL_ShaderCross_CompileComputePipelineFromSPIRV(SDL_GPUDevice *device,
-                                              const SDL_GPUComputePipelineCreateInfo *createInfo);
-#endif /* SDL_GPU_SHADERCROSS_SPIRVCROSS */
-
-#if SDL_GPU_SHADERCROSS_HLSL
-/**
- * Get the supported shader formats that HLSL cross-compilation can output
- *
- * \threadsafety It is safe to call this function from any thread.
- */
-extern SDL_GPUShaderFormat SDL_ShaderCross_GetHLSLShaderFormats(void);
-
-/**
- * Compile an SDL GPU shader from HLSL code.
- *
- * \param device the SDL GPU device.
- * \param createInfo a pointer to an SDL_GPUShaderCreateInfo.
- * \param hlslSource the HLSL source code for the shader.
- * \param shaderProfile the shader profile to compile the shader with.
- * \returns a compiled SDL_GPUShader
- *
- * \threadsafety It is safe to call this function from any thread.
- */
-extern SDL_GPUShader *SDL_ShaderCross_CompileGraphicsShaderFromHLSL(SDL_GPUDevice *device,
-                                             const SDL_GPUShaderCreateInfo *createInfo,
-                                             const char *hlslSource,
-                                             const char *shaderProfile);
-
-/**
- * Compile an SDL GPU compute pipeline from HLSL code.
- *
- * \param device the SDL GPU device.
- * \param createInfo a pointer to an SDL_GPUComputePipelineCreateInfo.
- * \param hlslSource the HLSL source code for the shader.
- * \param shaderProfile the shader profile to compile the shader with.
- * \returns a compiled SDL_GPUComputePipeline
- *
- * \threadsafety It is safe to call this function from any thread.
- */
-extern SDL_GPUComputePipeline *SDL_ShaderCross_CompileComputePipelineFromHLSL(SDL_GPUDevice *device,
-                                             const SDL_GPUComputePipelineCreateInfo *createInfo,
-                                             const char *hlslSource,
-                                             const char *shaderProfile);
-#endif /* SDL_GPU_SHADERCROSS_HLSL */
-
-#endif /* SDL_GPU_SHADERCROSS_H */
-
-#ifdef SDL_GPU_SHADERCROSS_IMPLEMENTATION
+#include "SDL_gpu_shadercross.h"
 
 #if SDL_GPU_SHADERCROSS_HLSL
 
@@ -1095,5 +989,3 @@ SDL_GPU_SHADERCROSS_EXPORT SDL_GPUShaderFormat SDL_ShaderCross_GetHLSLShaderForm
 }
 
 #endif /* SDL_GPU_SHADERCROSS_HLSL */
-
-#endif /* SDL_GPU_SHADERCROSS_IMPLEMENTATION */
diff --git a/src/cli.c b/src/cli.c
new file mode 100644
index 0000000..7c54460
--- /dev/null
+++ b/src/cli.c
@@ -0,0 +1,27 @@
+/*
+  Simple DirectMedia Layer Shader Cross Compiler
+  Copyright (C) 2024 Sam Lantinga <slouken@libsdl.org>
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely, subject to the following restrictions:
+
+  1. The origin of this software must not be misrepresented; you must not
+     claim that you wrote the original software. If you use this software
+     in a product, an acknowledgment in the product documentation would be
+     appreciated but is not required.
+  2. Altered source versions must be plainly marked as such, and must not be
+     misrepresented as being the original software.
+  3. This notice may not be removed or altered from any source distribution.
+*/
+
+#define SDL_GPU_SHADERCROSS_IMPLEMENTATION
+
+int main(int argc, char *argv[])
+{
+    return 0;
+}
diff --git a/spirv.h b/src/spirv.h
similarity index 100%
rename from spirv.h
rename to src/spirv.h
diff --git a/spirv_cross_c.h b/src/spirv_cross_c.h
similarity index 100%
rename from spirv_cross_c.h
rename to src/spirv_cross_c.h
diff --git a/test.c b/test.c
deleted file mode 100644
index 3a890eb..0000000
--- a/test.c
+++ /dev/null
@@ -1,3 +0,0 @@
-/* cc test.c -I. -fpic -fPIC -shared -Wl,--no-undefined -lSDL3 */
-#define SDL_GPU_SHADERCROSS_IMPLEMENTATION
-#include "SDL_gpu_shadercross.h"