cmake: Add mini-SDL2 CMake project to test SDL2 prefixes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
diff --git a/cmake/test/CMakeLists.txt b/cmake/test/CMakeLists.txt
new file mode 100644
index 0000000..0cb729d
--- /dev/null
+++ b/cmake/test/CMakeLists.txt
@@ -0,0 +1,91 @@
+# 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 SDL2 outside of sysroot
+set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE NEVER)
+
+include(FeatureSummary)
+
+option(TEST_SHARED "Test linking to shared SDL2 library" ON)
+add_feature_info("TEST_SHARED" TEST_SHARED "Test linking with shared library")
+
+option(TEST_STATIC "Test linking to static SDL2 libary" ON)
+add_feature_info("TEST_STATIC" TEST_STATIC "Test linking with static library")
+
+if(TEST_SHARED)
+ find_package(SDL2 REQUIRED CONFIG COMPONENTS SDL2)
+ if(EMSCRIPTEN OR (WIN32 AND NOT WINDOWS_STORE))
+ find_package(SDL2 REQUIRED CONFIG COMPONENTS SDL2main)
+ endif()
+ add_executable(gui-shared WIN32 main_gui.c)
+ if(TARGET SDL2::SDL2main)
+ target_link_libraries(gui-shared PRIVATE SDL2::SDL2main)
+ endif()
+ target_link_libraries(gui-shared PRIVATE SDL2::SDL2)
+ if(WIN32)
+ add_custom_command(TARGET gui-shared POST_BUILD
+ COMMAND ${CMAKE_COMMAND} -E copy_if_different "$<TARGET_FILE:SDL2::SDL2>" "$<TARGET_FILE_DIR:gui-shared>"
+ )
+ endif()
+
+ add_executable(gui-shared-vars WIN32 main_gui.c)
+ target_link_libraries(gui-shared-vars PRIVATE ${SDL2_LIBRARIES})
+ target_include_directories(gui-shared-vars PRIVATE ${SDL2_INCLUDE_DIRS})
+
+ add_executable(cli-shared main_cli.c)
+ target_link_libraries(cli-shared PRIVATE SDL2::SDL2)
+ if(WIN32)
+ add_custom_command(TARGET cli-shared POST_BUILD
+ COMMAND ${CMAKE_COMMAND} -E copy_if_different "$<TARGET_FILE:SDL2::SDL2>" "$<TARGET_FILE_DIR:cli-shared>"
+ )
+ endif()
+
+ # SDL2_LIBRARIES does not support creating a cli SDL2 application
+ # (it is possible that SDL2main is a stub, but we don't know for sure)
+ if(NOT TARGET SDL2::SDL2main)
+ add_executable(cli-shared-vars main_cli.c)
+ target_link_libraries(cli-shared-vars PRIVATE ${SDL2_LIBRARIES})
+ target_include_directories(cli-shared-vars PRIVATE ${SDL2_INCLUDE_DIRS})
+ endif()
+endif()
+
+if(TEST_STATIC)
+ find_package(SDL2 REQUIRED CONFIG COMPONENTS SDL2-static)
+ if(EMSCRIPTEN OR (WIN32 AND NOT WINDOWS_STORE))
+ find_package(SDL2 REQUIRED CONFIG COMPONENTS SDL2main)
+ endif()
+ add_executable(gui-static WIN32 main_gui.c)
+ if(TARGET SDL2::SDL2main)
+ target_link_libraries(gui-static PRIVATE SDL2::SDL2main)
+ endif()
+ target_link_libraries(gui-static PRIVATE SDL2::SDL2-static)
+
+ add_executable(gui-static-vars WIN32 main_gui.c)
+ target_link_libraries(gui-static-vars PRIVATE ${SDL2MAIN_LIBRARY} ${SDL2_STATIC_LIBRARIES})
+ target_include_directories(gui-static-vars PRIVATE ${SDL2_INCLUDE_DIRS})
+
+ add_executable(cli-static main_cli.c)
+ target_link_libraries(cli-static PRIVATE SDL2::SDL2-static)
+
+ # SDL2_LIBRARIES does not support creating a cli SDL2 application (when SDL2::SDL2main is available)
+ # (it is possible that SDL2main is a stub, but we don't know for sure)
+ if(NOT TARGET SDL2::SDL2main)
+ add_executable(cli-static-vars main_cli.c)
+ target_link_libraries(cli-static-vars PRIVATE ${SDL2_STATIC_LIBRARIES})
+ target_include_directories(cli-static-vars PRIVATE ${SDL2_INCLUDE_DIRS})
+ endif()
+endif()
+
+message(STATUS "SDL2_PREFIX: ${SDL2_PREFIX}")
+message(STATUS "SDL2_INCLUDE_DIR: ${SDL2_INCLUDE_DIR}")
+message(STATUS "SDL2_INCLUDE_DIRS: ${SDL2_INCLUDE_DIRS}")
+message(STATUS "SDL2_LIBRARIES: ${SDL2_LIBRARIES}")
+message(STATUS "SDL2_STATIC_LIBRARIES: ${SDL2_STATIC_LIBRARIES}")
+message(STATUS "SDL2MAIN_LIBRARY: ${SDL2MAIN_LIBRARY}")
+message(STATUS "SDL2TEST_LIBRARY: ${SDL2TEST_LIBRARY}")
+
+feature_summary(WHAT ALL)
diff --git a/cmake/test/main_cli.c b/cmake/test/main_cli.c
new file mode 100644
index 0000000..f6b0836
--- /dev/null
+++ b/cmake/test/main_cli.c
@@ -0,0 +1,14 @@
+#define SDL_MAIN_HANDLED
+#include "SDL.h"
+#include <stdio.h>
+
+int main(int argc, char *argv[]) {
+ SDL_SetMainReady();
+ if (SDL_Init(0) < 0) {
+ fprintf(stderr, "could not initialize sdl2: %s\n", SDL_GetError());
+ return 1;
+ }
+ SDL_Delay(100);
+ SDL_Quit();
+ return 0;
+}
diff --git a/cmake/test/main_gui.c b/cmake/test/main_gui.c
new file mode 100644
index 0000000..4ffe9be
--- /dev/null
+++ b/cmake/test/main_gui.c
@@ -0,0 +1,28 @@
+#include "SDL.h"
+#include <stdio.h>
+
+int main(int argc, char *argv[]) {
+ SDL_Window *window = NULL;
+ SDL_Surface *screenSurface = NULL;
+ if (SDL_Init(SDL_INIT_VIDEO) < 0) {
+ fprintf(stderr, "could not initialize sdl2: %s\n", SDL_GetError());
+ return 1;
+ }
+ window = SDL_CreateWindow(
+ "hello_sdl2",
+ SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
+ 640, 480,
+ SDL_WINDOW_SHOWN
+ );
+ if (window == NULL) {
+ fprintf(stderr, "could not create window: %s\n", SDL_GetError());
+ return 1;
+ }
+ screenSurface = SDL_GetWindowSurface(window);
+ SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xff, 0xff, 0xff));
+ SDL_UpdateWindowSurface(window);
+ SDL_Delay(100);
+ SDL_DestroyWindow(window);
+ SDL_Quit();
+ return 0;
+}