cmake_minimum_required(VERSION 3.0.0...4.0)
project(sdl12_compat_tests C)

if(NOT TARGET SDL::SDL)
    find_package(SDL REQUIRED)
endif()
if(NOT TARGET SDL::SDLmain)
    add_library(SDL::SDLmain STATIC IMPORTED)
    if(SDLMAIN_LIBRARY)
        message("SDLMAIN_LIBRARY=${SDLMAIN_LIBRARY}")
        set_property(TARGET SDL::SDLmain PROPERTY IMPORTED_LOCATION "${SDLMAIN_LIBRARY}")
    endif()
    if(MINGW OR CYGWIN)
        if(CMAKE_SIZEOF_VOID_P EQUAL 4)
            set_property(TARGET SDL::SDLmain APPEND INTERFACE_LINK_LIBRARIES "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:-Wl,--undefined=_WinMain@16>")
        else()
            set_property(TARGET SDL::SDLmain APPEND INTERFACE_LINK_LIBRARIES "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:-Wl,--undefined=WinMain>")
        endif()
    endif()
endif()

if(NOT (WIN32 OR APPLE OR CYGWIN OR HAIKU OR BEOS))
    find_library(MATH_LIBRARY m)
endif()

find_package(OpenGL COMPONENTS OpenGL)
if(OPENGL_FOUND)
    set(HAVE_OPENGL_DEFINE "HAVE_OPENGL")
    if(WIN32)
        set(OPENGL_gl_LIBRARY "opengl32")
        set(OPENGL_opengl_LIBRARY "opengl32")
    elseif(APPLE)
        set(OPENGL_gl_LIBRARY "-Wl,-framework,OpenGL")
        set(OPENGL_opengl_LIBRARY "-Wl,-framework,OpenGL")
    endif()
endif()

macro(test_program _NAME _SRCS)
    add_executable(${_NAME} ${_SRCS})
    target_include_directories(${_NAME} PRIVATE "include/SDL")
    target_link_libraries(${_NAME} PRIVATE SDL::SDLmain SDL::SDL)
    # Turn off MSVC's aggressive C runtime warnings for the old test programs.
    if(MSVC)
        set_target_properties(${_NAME} PROPERTIES COMPILE_DEFINITIONS "${HAVE_OPENGL_DEFINE};_CRT_SECURE_NO_WARNINGS;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE")
    elseif(APPLE)
        set_target_properties(${_NAME} PROPERTIES COMPILE_DEFINITIONS "${HAVE_OPENGL_DEFINE};GL_SILENCE_DEPRECATION=1")
        target_link_libraries(${_NAME} PRIVATE "-Wl,-framework,Cocoa")
    else()
        set_target_properties(${_NAME} PROPERTIES COMPILE_DEFINITIONS "${HAVE_OPENGL_DEFINE}")
    endif()
    if(MATH_LIBRARY)
        target_link_libraries(${_NAME} PRIVATE ${MATH_LIBRARY})
    endif()
endmacro()

test_program(checkkeys "checkkeys.c")
test_program(graywin "graywin.c")
test_program(loopwave "loopwave.c")
test_program(testalpha "testalpha.c")
test_program(testbitmap "testbitmap.c")
test_program(testblitspeed "testblitspeed.c")
test_program(testcdrom "testcdrom.c")
test_program(testcursor "testcursor.c")
test_program(testerror "testerror.c")
test_program(testfile "testfile.c")
test_program(testgamma "testgamma.c")
test_program(testthread "testthread.c")
test_program(testiconv "testiconv.c")
test_program(testjoystick "testjoystick.c")
test_program(testkeys "testkeys.c")
test_program(testloadso "testloadso.c")
test_program(testlock "testlock.c")
test_program(testoverlay "testoverlay.c")
test_program(testoverlay2 "testoverlay2.c")
test_program(testpalette "testpalette.c")
test_program(testplatform "testplatform.c")
test_program(testsem "testsem.c")
test_program(testsprite "testsprite.c")
test_program(testtimer "testtimer.c")
test_program(testver "testver.c")
test_program(testvidinfo "testvidinfo.c")
test_program(testwin "testwin.c")
test_program(testwm "testwm.c")
test_program(threadwin "threadwin.c")
test_program(torturethread "torturethread.c")
test_program(testdyngl "testdyngl.c")
test_program(testgl "testgl.c")
if(OPENGL_FOUND)
    if(CMAKE_VERSION VERSION_LESS 3.10 OR NOT OPENGL_opengl_LIBRARY)
        target_link_libraries(testgl PRIVATE ${OPENGL_gl_LIBRARY})
    else()
        target_link_libraries(testgl PRIVATE ${OPENGL_opengl_LIBRARY})
    endif()
endif()

foreach(fname "icon.bmp" "moose.dat" "picture.xbm" "sail.bmp" "sample.bmp" "sample.wav" "utf8.txt")
    file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/${fname}" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
endforeach(fname)
