Fixed bug 4092 - CMake support for building everything in the "test" directory
authorSam Lantinga <slouken@libsdl.org>
Sat, 24 Feb 2018 08:59:58 -0800
changeset 11901e4d03d8733f3
parent 11900 0a7f9aec16a6
child 11902 ce4bc0462ae5
Fixed bug 4092 - CMake support for building everything in the "test" directory

Eric Wasylishen

Patch to support building the tests with cmake.
Disabled by default, use: "cmake .. -DSDL_TEST=YES" to enable the tests.

Tested on macOS 10.13 with the ninja, makefile, and Xcode generators, and Windows 10 with the Visual Studio 2017 generator.
CMakeLists.txt
test/CMakeLists.txt
     1.1 --- a/CMakeLists.txt	Sat Feb 24 08:58:22 2018 -0800
     1.2 +++ b/CMakeLists.txt	Sat Feb 24 08:59:58 2018 -0800
     1.3 @@ -340,6 +340,7 @@
     1.4  set(SDL_STATIC ON CACHE BOOL "Build a static version of the library")
     1.5  
     1.6  dep_option(SDL_STATIC_PIC      "Static version of the library should be built with Position Independent Code" OFF "SDL_STATIC" OFF)
     1.7 +set_option(SDL_TEST            "Build the test directory" OFF)
     1.8  
     1.9  # General source files
    1.10  file(GLOB SOURCE_FILES
    1.11 @@ -1746,6 +1747,15 @@
    1.12    target_include_directories(SDL2-static PUBLIC $<INSTALL_INTERFACE:include>)
    1.13  endif()
    1.14  
    1.15 +##### Tests #####
    1.16 +
    1.17 +if(SDL_TEST)
    1.18 +  file(GLOB TEST_SOURCES ${SDL2_SOURCE_DIR}/src/test/*.c)
    1.19 +  add_library(SDL2_test STATIC ${TEST_SOURCES})
    1.20 +  
    1.21 +  add_subdirectory(test)
    1.22 +endif()
    1.23 +
    1.24  ##### Installation targets #####
    1.25  install(TARGETS ${_INSTALL_LIBS} EXPORT SDL2Targets
    1.26    LIBRARY DESTINATION "lib${LIB_SUFFIX}"
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/CMakeLists.txt	Sat Feb 24 08:59:58 2018 -0800
     2.3 @@ -0,0 +1,122 @@
     2.4 +cmake_minimum_required(VERSION 2.8.11)
     2.5 +project(SDL2 C)
     2.6 +
     2.7 +# Global settings for all of the test targets
     2.8 +# FIXME: is this wrong?
     2.9 +remove_definitions(-DUSING_GENERATED_CONFIG_H)
    2.10 +link_libraries(SDL2_test SDL2-static)
    2.11 +
    2.12 +# FIXME: Parent directory CMakeLists.txt only sets these for mingw/cygwin,
    2.13 +# but we need them for VS as well.
    2.14 +if(WINDOWS)
    2.15 +    link_libraries(SDL2main)
    2.16 +    add_definitions(-Dmain=SDL_main)
    2.17 +endif()
    2.18 +
    2.19 +add_executable(checkkeys checkkeys.c)
    2.20 +add_executable(loopwave loopwave.c)
    2.21 +add_executable(loopwavequeue loopwavequeue.c)
    2.22 +add_executable(testresample testresample.c)
    2.23 +add_executable(testaudioinfo testaudioinfo.c)
    2.24 +
    2.25 +file(GLOB TESTAUTOMATION_SOURCE_FILES testautomation*.c)
    2.26 +add_executable(testautomation ${TESTAUTOMATION_SOURCE_FILES})
    2.27 +
    2.28 +add_executable(testmultiaudio testmultiaudio.c)
    2.29 +add_executable(testaudiohotplug testaudiohotplug.c)
    2.30 +add_executable(testaudiocapture testaudiocapture.c)
    2.31 +add_executable(testatomic testatomic.c)
    2.32 +add_executable(testintersections testintersections.c)
    2.33 +add_executable(testrelative testrelative.c)
    2.34 +add_executable(testhittesting testhittesting.c)
    2.35 +add_executable(testdraw2 testdraw2.c)
    2.36 +add_executable(testdrawchessboard testdrawchessboard.c)
    2.37 +add_executable(testdropfile testdropfile.c)
    2.38 +add_executable(testerror testerror.c)
    2.39 +add_executable(testfile testfile.c)
    2.40 +add_executable(testgamecontroller testgamecontroller.c)
    2.41 +add_executable(testgesture testgesture.c)
    2.42 +add_executable(testgl2 testgl2.c)
    2.43 +add_executable(testgles testgles.c)
    2.44 +add_executable(testgles2 testgles2.c)
    2.45 +add_executable(testhaptic testhaptic.c)
    2.46 +add_executable(testhotplug testhotplug.c)
    2.47 +add_executable(testrumble testrumble.c)
    2.48 +add_executable(testthread testthread.c)
    2.49 +add_executable(testiconv testiconv.c)
    2.50 +add_executable(testime testime.c)
    2.51 +add_executable(testjoystick testjoystick.c)
    2.52 +add_executable(testkeys testkeys.c)
    2.53 +add_executable(testloadso testloadso.c)
    2.54 +add_executable(testlock testlock.c)
    2.55 +
    2.56 +if(APPLE)
    2.57 +    add_executable(testnative testnative.c
    2.58 +                              testnativecocoa.m
    2.59 +                              testnativex11.c)
    2.60 +elseif(WINDOWS)
    2.61 +    add_executable(testnative testnative.c testnativew32.c)
    2.62 +elseif(UNIX)
    2.63 +    add_executable(testnative testnative.c testnativex11.c)
    2.64 +endif()
    2.65 +
    2.66 +add_executable(testoverlay2 testoverlay2.c testyuv_cvt.c)
    2.67 +add_executable(testplatform testplatform.c)
    2.68 +add_executable(testpower testpower.c)
    2.69 +add_executable(testfilesystem testfilesystem.c)
    2.70 +add_executable(testrendertarget testrendertarget.c)
    2.71 +add_executable(testscale testscale.c)
    2.72 +add_executable(testsem testsem.c)
    2.73 +add_executable(testshader testshader.c)
    2.74 +add_executable(testshape testshape.c)
    2.75 +add_executable(testsprite2 testsprite2.c)
    2.76 +add_executable(testspriteminimal testspriteminimal.c)
    2.77 +add_executable(teststreaming teststreaming.c)
    2.78 +add_executable(testtimer testtimer.c)
    2.79 +add_executable(testver testver.c)
    2.80 +add_executable(testviewport testviewport.c)
    2.81 +add_executable(testwm2 testwm2.c)
    2.82 +add_executable(testyuv testyuv.c testyuv_cvt.c)
    2.83 +add_executable(torturethread torturethread.c)
    2.84 +add_executable(testrendercopyex testrendercopyex.c)
    2.85 +add_executable(testmessage testmessage.c)
    2.86 +add_executable(testdisplayinfo testdisplayinfo.c)
    2.87 +add_executable(testqsort testqsort.c)
    2.88 +add_executable(testbounds testbounds.c)
    2.89 +add_executable(testcustomcursor testcustomcursor.c)
    2.90 +add_executable(controllermap controllermap.c)
    2.91 +add_executable(testvulkan testvulkan.c)
    2.92 +
    2.93 +# HACK: Dummy target to cause the resource files to be copied to the build directory.
    2.94 +# Need to make it an executable so we can use the TARGET_FILE_DIR generator expression.
    2.95 +# This is needed so they get copied to the correct Debug/Release subdirectory in Xcode.
    2.96 +file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/resources_dummy.c "int main(int argc, const char **argv){ return 1; }\n")
    2.97 +add_executable(SDL2_test_resoureces ${CMAKE_CURRENT_BINARY_DIR}/resources_dummy.c)
    2.98 +
    2.99 +file(GLOB RESOURCE_FILES *.bmp *.wav)
   2.100 +foreach(RESOURCE_FILE ${RESOURCE_FILES})
   2.101 +    add_custom_command(TARGET SDL2_test_resoureces POST_BUILD COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${RESOURCE_FILE} $<TARGET_FILE_DIR:SDL2_test_resoureces>)
   2.102 +endforeach(RESOURCE_FILE)
   2.103 +
   2.104 +file(COPY ${RESOURCE_FILES} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
   2.105 +
   2.106 +# TODO: Might be easier to make all targets depend on the resources...?
   2.107 +add_dependencies(testscale SDL2_test_resoureces)
   2.108 +add_dependencies(testrendercopyex SDL2_test_resoureces)
   2.109 +add_dependencies(controllermap SDL2_test_resoureces)
   2.110 +add_dependencies(testyuv SDL2_test_resoureces)
   2.111 +add_dependencies(testgamecontroller SDL2_test_resoureces)
   2.112 +add_dependencies(testshape SDL2_test_resoureces)
   2.113 +add_dependencies(testshader SDL2_test_resoureces)
   2.114 +add_dependencies(testnative SDL2_test_resoureces)
   2.115 +add_dependencies(testspriteminimal SDL2_test_resoureces)
   2.116 +add_dependencies(testautomation SDL2_test_resoureces)
   2.117 +add_dependencies(testcustomcursor SDL2_test_resoureces)
   2.118 +add_dependencies(testrendertarget SDL2_test_resoureces)
   2.119 +add_dependencies(testsprite2 SDL2_test_resoureces)
   2.120 +
   2.121 +add_dependencies(loopwave SDL2_test_resoureces)
   2.122 +add_dependencies(loopwavequeue SDL2_test_resoureces)
   2.123 +add_dependencies(testresample SDL2_test_resoureces)
   2.124 +add_dependencies(testaudiohotplug SDL2_test_resoureces)
   2.125 +add_dependencies(testmultiaudio SDL2_test_resoureces)