SDL_net: cmake: Build samples with cmake

From 4481b1edb4128df2342f2ec20cc462f4b63b7e5e Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Tue, 21 Jun 2022 03:05:16 +0200
Subject: [PATCH] cmake: Build samples with cmake

---
 .github/workflows/main.yml |  2 +-
 CMakeLists.txt             | 23 +++++++++++++++++++++--
 chat.c                     | 10 +++++-----
 cmake/FindSDL2main.cmake   | 24 ++++++++++++++++++++++++
 cmake/FindSDL2test.cmake   | 31 +++++++++++++++++++++++++++++++
 5 files changed, 82 insertions(+), 8 deletions(-)
 create mode 100644 cmake/FindSDL2main.cmake
 create mode 100644 cmake/FindSDL2test.cmake

diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index a175531..7aae4cf 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -89,12 +89,12 @@ jobs:
       if: "matrix.platform.cmake"
       run: |
         set -eu
+        # FIXME: add -DSDL2NET_SAMPLES=ON when msys2 has SDL2test
         cmake -S . \
           -B build-cmake \
           -DBUILD_SHARED_LIBS=ON \
           -DCMAKE_BUILD_TYPE=Release \
           -DCMAKE_INSTALL_PREFIX=prefix_cmake \
-          -DSDL2NET_SHOWINTERFACES=ON \
           ${{ matrix.platform.cmake }}
 
     - name: Build (CMake)
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 20b70f4..19890b7 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -22,6 +22,8 @@ project(SDL2_net
     VERSION "${FULL_VERSION}"
 )
 
+find_package(SDL2test)
+
 # Set defaults preventing destination file conflicts
 set(SDL2NET_DEBUG_POSTFIX "d"
     CACHE STRING "Name suffix for debug builds")
@@ -34,7 +36,12 @@ option(CMAKE_POSITION_INDEPENDENT_CODE "Build static libraries with -fPIC" ON)
 option(BUILD_SHARED_LIBS "Build the library as a shared library" ON)
 
 option(SDL2NET_INSTALL "Enable SDL2_net install target" ON)
-option(SDL2NET_SHOWINTERFACES "Build the showinterfaces test program" OFF)
+
+option(SDL2NET_SAMPLES "Build SDL2_net samples" "${SDL2test_FOUND}")
+
+if(SDL2NET_SAMPLES)
+    find_package(SDL2test REQUIRED)
+endif()
 
 # Save BUILD_SHARED_LIBS variable
 set(SDL2NET_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
@@ -229,8 +236,20 @@ if(SDL2NET_INSTALL)
     )
 endif()
 
-if(SDL2NET_SHOWINTERFACES)
+if(SDL2NET_SAMPLES)
+    find_package(SDL2main)
+
     add_executable(showinterfaces showinterfaces.c)
     target_compile_definitions(showinterfaces PRIVATE SDL_MAIN_HANDLED)
     target_link_libraries(showinterfaces PRIVATE SDL2_net::${sdl2_net_export_name} ${sdl2_target_name})
+
+    add_executable(chat chat.c chat.h)
+    if(TARGET SDL2::SDL2main)
+        target_link_libraries(chat PRIVATE SDL2::SDL2main)
+    endif()
+    target_link_libraries(chat PRIVATE SDL2_net::${sdl2_net_export_name} SDL2::SDL2test ${sdl2_target_name})
+
+    add_executable(chatd chatd.c)
+    target_compile_definitions(chatd PRIVATE SDL_MAIN_HANDLED)
+    target_link_libraries(chatd PRIVATE SDL2_net::${sdl2_net_export_name} ${sdl2_target_name})
 endif()
diff --git a/chat.c b/chat.c
index c366b33..7c4c464 100644
--- a/chat.c
+++ b/chat.c
@@ -20,18 +20,18 @@
 */
 
 /* Note that this isn't necessarily the way to run a chat system.
-   This is designed to excercise the network code more than be really
+   This is designed to exercise the network code more than be really
    functional.
 */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
 #include "SDL_net.h"
 #include "SDL_test.h"
 #include "chat.h"
 
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
 
 /* Global variables */
 static TCPsocket tcpsock = NULL;
diff --git a/cmake/FindSDL2main.cmake b/cmake/FindSDL2main.cmake
new file mode 100644
index 0000000..cbdfee8
--- /dev/null
+++ b/cmake/FindSDL2main.cmake
@@ -0,0 +1,24 @@
+# FIXME: this should be provided by SDL2
+
+include(FindPackageHandleStandardArgs)
+include("${CMAKE_CURRENT_LIST_DIR}/CommonFindSDL2.cmake")
+
+find_library(SDL2_MAIN_LIBRARY
+    NAMES SDL2main
+    HINTS ${SDL2_DIR} ENV SDL2_DIR
+    PATH_SUFFIXES ${_lib_suffixes}
+)
+
+find_package_handle_standard_args(SDL2main
+    REQUIRED_VARS SDL2_MAIN_LIBRARY
+)
+
+if(SDL2main_FOUND)
+    if(NOT TARGET SDL2::SDL2main)
+        add_library(SDL2::SDL2main UNKNOWN IMPORTED)
+        set_target_properties(SDL2::SDL2main PROPERTIES
+            IMPORTED_LINK_INTERFACE_LANGUAGES "C"
+            IMPORTED_LOCATION "${SDL2_MAIN_LIBRARY}"
+        )
+    endif()
+endif()
diff --git a/cmake/FindSDL2test.cmake b/cmake/FindSDL2test.cmake
new file mode 100644
index 0000000..1318fba
--- /dev/null
+++ b/cmake/FindSDL2test.cmake
@@ -0,0 +1,31 @@
+# FIXME: this should be provided by SDL2
+
+include(FindPackageHandleStandardArgs)
+include("${CMAKE_CURRENT_LIST_DIR}/CommonFindSDL2.cmake")
+
+find_library(SDL2_TEST_LIBRARY
+    NAMES SDL2test SDL2_test
+    HINTS ${SDL2_DIR} ENV SDL2_DIR
+    PATH_SUFFIXES ${_lib_suffixes}
+)
+
+find_package_handle_standard_args(SDL2test
+    REQUIRED_VARS SDL2_TEST_LIBRARY
+)
+
+if(SDL2test_FOUND)
+    if(NOT TARGET SDL2::SDL2test)
+        add_library(SDL2::SDL2test UNKNOWN IMPORTED)
+        set_target_properties(SDL2::SDL2test PROPERTIES
+            IMPORTED_LINK_INTERFACE_LANGUAGES "C"
+            IMPORTED_LOCATION "${SDL2_TEST_LIBRARY}"
+        )
+        if(MSVC AND ((SDL2_VERSION AND SDL2_VERSION VERSION_LESS "2.0.20") OR NOT SDL2_VERSION))
+            # FIXME: remove once minimum required SDL library is >=2.0.20
+            # Until 2.0.18, SDL2test.lib used `printf` in SDL_test_common.c. instead of `SDL_log`. (fixed in 2.0.20)
+            set_target_properties(SDL2::SDL2test PROPERTIES
+                INTERFACE_LINK_LIBRARIES "legacy_stdio_definitions.lib"
+            )
+        endif()
+    endif()
+endif()