Commit 3b20e0ecef9623cac2fba60b9113be1d9ef5f255

Anonymous Maarten 2022-09-19T02:38:36

cmake: only add -Wl,--undefined=WinMain when building an executable

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 733fda1..c5ffc40 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -3049,9 +3049,9 @@ if(NOT WINDOWS_STORE AND NOT SDL2_DISABLE_SDL2MAIN)
   if(MINGW OR CYGWIN)
     cmake_minimum_required(VERSION 3.13)
     if(CMAKE_SIZEOF_VOID_P EQUAL 4)
-      target_link_options(SDL2main PUBLIC "-Wl,--undefined=_WinMain@16")
+      target_link_options(SDL2main PUBLIC "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:-Wl,--undefined=_WinMain@16>")
     else()
-      target_link_options(SDL2main PUBLIC "-Wl,--undefined=WinMain")
+      target_link_options(SDL2main PUBLIC "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:-Wl,--undefined=WinMain>")
     endif()
   endif()
   if (NOT ANDROID)
diff --git a/cmake/test/CMakeLists.txt b/cmake/test/CMakeLists.txt
index db9e2f8..0695e9d 100644
--- a/cmake/test/CMakeLists.txt
+++ b/cmake/test/CMakeLists.txt
@@ -3,6 +3,8 @@
 cmake_minimum_required(VERSION 3.12)
 project(sdl_test LANGUAGES C)
 
+include(GenerateExportHeader)
+
 if(ANDROID)
     macro(add_executable NAME)
         set(args ${ARGN})
@@ -41,6 +43,12 @@ if(TEST_SHARED)
         )
     endif()
 
+    add_library(sharedlib-shared SHARED main_lib.c)
+    target_link_libraries(sharedlib-shared PRIVATE SDL2::SDL2)
+    generate_export_header(sharedlib-shared EXPORT_MACRO_NAME MYLIBRARY_EXPORT)
+    target_compile_definitions(sharedlib-shared PRIVATE "EXPORT_HEADER=\"${CMAKE_CURRENT_BINARY_DIR}/sharedlib-shared_export.h\"")
+    set_target_properties(sharedlib-shared PROPERTIES C_VISIBILITY_PRESET "hidden")
+
     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})
@@ -60,6 +68,13 @@ if(TEST_SHARED)
         target_link_libraries(cli-shared-vars PRIVATE ${SDL2_LIBRARIES})
         target_include_directories(cli-shared-vars PRIVATE ${SDL2_INCLUDE_DIRS})
     endif()
+
+    add_library(sharedlib-shared-vars SHARED main_lib.c)
+    target_link_libraries(sharedlib-shared-vars PRIVATE ${SDL2_LIBRARIES})
+    target_include_directories(sharedlib-shared-vars PRIVATE ${SDL2_INCLUDE_DIRS})
+    generate_export_header(sharedlib-shared-vars EXPORT_MACRO_NAME MYLIBRARY_EXPORT)
+    target_compile_definitions(sharedlib-shared-vars PRIVATE "EXPORT_HEADER=\"${CMAKE_CURRENT_BINARY_DIR}/sharedlib-shared-vars_export.h\"")
+    set_target_properties(sharedlib-shared-vars PROPERTIES C_VISIBILITY_PRESET "hidden")
 endif()
 
 if(TEST_STATIC)
@@ -73,6 +88,15 @@ if(TEST_STATIC)
     endif()
     target_link_libraries(gui-static PRIVATE SDL2::SDL2-static)
 
+    option(SDL_STATIC_PIC "SDL static library has been built with PIC")
+    if(SDL_STATIC_PIC OR WIN32)
+        add_library(sharedlib-static SHARED main_lib.c)
+        target_link_libraries(sharedlib-static PRIVATE SDL2::SDL2-static)
+        generate_export_header(sharedlib-static EXPORT_MACRO_NAME MYLIBRARY_EXPORT)
+        target_compile_definitions(sharedlib-static PRIVATE "EXPORT_HEADER=\"${CMAKE_CURRENT_BINARY_DIR}/sharedlib-static_export.h\"")
+        set_target_properties(sharedlib-static PROPERTIES C_VISIBILITY_PRESET "hidden")
+    endif()
+
     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})
diff --git a/cmake/test/main_lib.c b/cmake/test/main_lib.c
new file mode 100644
index 0000000..9801ed5
--- /dev/null
+++ b/cmake/test/main_lib.c
@@ -0,0 +1,33 @@
+#include "SDL.h"
+#include <stdio.h>
+
+#include EXPORT_HEADER
+
+#if defined(_WIN32)
+#include <windows.h>
+BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
+    return TRUE;
+}
+#endif
+
+int MYLIBRARY_EXPORT mylibrary_init(void);
+void MYLIBRARY_EXPORT mylibrary_quit(void);
+int MYLIBRARY_EXPORT mylibrary_work(void);
+
+int mylibrary_init(void) {
+    SDL_SetMainReady();
+    if (SDL_Init(0) < 0) {
+        fprintf(stderr, "could not initialize sdl2: %s\n", SDL_GetError());
+        return 1;
+    }
+    return 0;
+}
+
+void mylibrary_quit(void) {
+    SDL_Quit();
+}
+
+int mylibrary_work(void) {
+    SDL_Delay(100);
+    return 0;
+}