SDL_image: cmake: pass -Wall -Wextra to compiler

From ca93569d9bc2f6b05745bff9ab8f38426cdadd36 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Thu, 5 Jan 2023 19:39:49 +0100
Subject: [PATCH] cmake: pass -Wall -Wextra to compiler

---
 .github/workflows/main.yml |  1 +
 CMakeLists.txt             | 17 +++++++++++++++++
 test/CMakeLists.txt        |  3 +++
 3 files changed, 21 insertions(+)

diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index 4d31d4fe..087e81c8 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -112,6 +112,7 @@ jobs:
           -DSDL3IMAGE_JXL=ON \
           -DSDL3IMAGE_TIF=ON \
           -DSDL3IMAGE_WEBP=ON \
+          -DSDL3IMAGE_WERROR=ON \
           -DCMAKE_INSTALL_PREFIX=prefix_cmake \
           ${NULL+}
 
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 296b473c..a77be9d3 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -51,6 +51,7 @@ option(BUILD_SHARED_LIBS "Build the library as a shared library" ON)
 option(SDL3IMAGE_INSTALL "Enable SDL3_image install target" ${SDL3IMAGE_ROOTPROJECT})
 option(SDL3IMAGE_DEPS_SHARED "Load dependencies dynamically" ON)
 option(SDL3IMAGE_VENDORED "Use vendored third-party libraries" ${vendored_default})
+option(SDL3IMAGE_WERROR "Treat warnings as errors" OFF)
 
 option(SDL3IMAGE_SAMPLES "Build the SDL3_image sample program(s)" ${SDL3IMAGE_SAMPLES_DEFAULT})
 cmake_dependent_option(SDL3IMAGE_SAMPLES_INSTALL "Install the SDL3_image sample program(s)" OFF "SDL3IMAGE_SAMPLES;SDL3IMAGE_INSTALL" OFF)
@@ -176,6 +177,20 @@ if(NOT DEFINED OpenGL_GL_PREFERENCE)
     set(OpenGL_GL_PREFERENCE GLVND)
 endif()
 
+add_library(sdl3image_build_options INTERFACE)
+if(MSVC)
+    target_compile_options(sdl3image_build_options INTERFACE /W2)
+else()
+    target_compile_options(sdl3image_build_options INTERFACE -Wall -Wextra)
+endif()
+if(SDL3IMAGE_WERROR)
+    if(MSVC)
+        target_compile_options(sdl3image_build_options INTERFACE /WX)
+    else()
+        target_compile_options(sdl3image_build_options INTERFACE -Werror)
+    endif()
+endif()
+
 add_library(SDL3_image
     IMG.c
     IMG_WIC.c
@@ -213,6 +228,7 @@ target_compile_definitions(SDL3_image PRIVATE
     SDL_BUILD_MICRO_VERSION=${MICRO_VERSION}
 )
 target_link_libraries(SDL3_image PRIVATE $<BUILD_INTERFACE:${sdl3_target_name}>)
+target_link_libraries(SDL3_image PRIVATE $<BUILD_INTERFACE:sdl3image_build_options>)
 if(WIN32 AND SDL3IMAGE_BUILD_SHARED_LIBS)
     target_sources(SDL3_image PRIVATE
         version.rc
@@ -223,6 +239,7 @@ set_target_properties(SDL3_image PROPERTIES
     EXPORT_NAME ${sdl3_image_export_name}
     C_VISIBILITY_PRESET "hidden"
 )
+
 if(NOT ANDROID)
     if(APPLE)
         # the SOVERSION property corresponds to the compatibility version and VERSION corresponds to the current version
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 3a1f42ca..fc54a698 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -69,6 +69,9 @@ foreach(prog ${ALL_TESTS})
     target_compile_definitions(${prog} PRIVATE $<TARGET_PROPERTY:SDL3_image,COMPILE_DEFINITIONS>)
     target_link_libraries(${prog} PRIVATE ${sdlimage_target_name})
     target_link_libraries(${prog} PRIVATE SDL3::SDL3_test ${sdl_target_name})
+    if(TARGET sdl3image_build_options)
+        target_link_libraries(${prog} PRIVATE $<BUILD_INTERFACE:sdl3image_build_options>)
+    endif()
 
     add_test(
         NAME ${prog}