CMakeLists.txt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. cmake_minimum_required(VERSION 3.0)
  2. project(SDL3_test)
  3. enable_testing()
  4. include("${CMAKE_CURRENT_LIST_DIR}/../cmake/sdlplatform.cmake")
  5. SDL_DetectCMakePlatform()
  6. include(CheckCCompilerFlag)
  7. include(CheckIncludeFile)
  8. include(CMakeParseArguments)
  9. include(CMakePushCheckState)
  10. include(GNUInstallDirs)
  11. set(SDL_TESTS_LINK_SHARED_DEFAULT ON)
  12. if(EMSCRIPTEN OR N3DS OR PS2 OR PSP OR RISCOS OR VITA)
  13. set(SDL_TESTS_LINK_SHARED_DEFAULT OFF)
  14. endif()
  15. option(SDL_TESTS_LINK_SHARED "link tests to shared SDL library" ${SDL_TESTS_LINK_SHARED_DEFAULT})
  16. set(SDL_TESTS_TIMEOUT_MULTIPLIER "1" CACHE STRING "Timeout multiplier to account for really slow machines")
  17. if(SDL_TESTS_LINK_SHARED)
  18. set(sdl_name_component SDL3)
  19. else()
  20. set(sdl_name_component SDL3-static)
  21. endif()
  22. if(NOT TARGET SDL3::${sdl_name_component})
  23. find_package(SDL3 3.0.0 REQUIRED COMPONENTS ${sdl_name_component} SDL3_test)
  24. endif()
  25. # CMake incorrectly detects opengl32.lib being present on MSVC ARM64
  26. if(NOT MSVC OR NOT CMAKE_GENERATOR_PLATFORM STREQUAL "ARM64")
  27. # Prefer GLVND, if present
  28. set(OpenGL_GL_PREFERENCE GLVND)
  29. find_package(OpenGL)
  30. endif()
  31. set(SDL_TEST_EXECUTABLES)
  32. set(SDL_TESTS_NONINTERACTIVE)
  33. add_library(sdltests_utils OBJECT
  34. testutils.c
  35. )
  36. target_link_libraries(sdltests_utils PRIVATE SDL3::${sdl_name_component})
  37. file(GLOB RESOURCE_FILES *.bmp *.wav *.hex moose.dat utf8.txt)
  38. macro(add_sdl_test_executable TARGET)
  39. cmake_parse_arguments(AST "NONINTERACTIVE;NEEDS_RESOURCES;TESTUTILS" "" "" ${ARGN})
  40. set(SOURCES ${AST_UNPARSED_ARGUMENTS})
  41. if(AST_TESTUTILS)
  42. list(APPEND SOURCES $<TARGET_OBJECTS:sdltests_utils>)
  43. endif()
  44. if(AST_NEEDS_RESOURCES)
  45. list(APPEND SOURCES ${RESOURCE_FILES})
  46. endif()
  47. add_executable(${TARGET} ${SOURCES})
  48. target_link_libraries(${TARGET} PRIVATE SDL3::SDL3_test SDL3::${sdl_name_component})
  49. list(APPEND SDL_TEST_EXECUTABLES ${TARGET})
  50. if(AST_NONINTERACTIVE)
  51. list(APPEND SDL_TESTS_NONINTERACTIVE ${TARGET})
  52. endif()
  53. if(AST_NEEDS_RESOURCES)
  54. if(PSP OR PS2)
  55. add_custom_command(TARGET ${TARGET} POST_BUILD
  56. COMMAND ${CMAKE_COMMAND} ARGS -E make_directory $<TARGET_FILE_DIR:${TARGET}>/sdl-${TARGET}
  57. COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${RESOURCE_FILES} $<TARGET_FILE_DIR:${TARGET}>/sdl-${TARGET})
  58. else()
  59. add_custom_command(TARGET ${TARGET} POST_BUILD
  60. COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${RESOURCE_FILES} $<TARGET_FILE_DIR:${TARGET}>)
  61. endif()
  62. if(APPLE)
  63. # Make sure resource files get installed into macOS/iOS .app bundles.
  64. set_target_properties(${TARGET} PROPERTIES RESOURCE "${RESOURCE_FILES}")
  65. endif()
  66. endif()
  67. if(WINDOWS)
  68. # CET support was added in VS 16.7
  69. if(MSVC_VERSION GREATER 1926 AND CMAKE_GENERATOR_PLATFORM MATCHES "Win32|x64")
  70. set_property(TARGET ${TARGET} APPEND_STRING PROPERTY LINK_FLAGS " -CETCOMPAT")
  71. endif()
  72. elseif(PSP)
  73. target_link_libraries(${TARGET} PRIVATE GL)
  74. endif()
  75. if(OPENGL_FOUND)
  76. target_compile_definitions(${TARGET} PRIVATE HAVE_OPENGL)
  77. endif()
  78. if(TARGET sdl-global-options)
  79. target_link_libraries(${TARGET} PRIVATE $<BUILD_INTERFACE:sdl-global-options>)
  80. endif()
  81. endmacro()
  82. check_include_file(signal.h HAVE_SIGNAL_H)
  83. if(HAVE_SIGNAL_H)
  84. add_definitions(-DHAVE_SIGNAL_H)
  85. endif()
  86. check_include_file(libudev.h HAVE_LIBUDEV_H)
  87. if(HAVE_LIBUDEV_H)
  88. add_definitions(-DHAVE_LIBUDEV_H)
  89. endif()
  90. add_sdl_test_executable(checkkeys checkkeys.c)
  91. add_sdl_test_executable(checkkeysthreads checkkeysthreads.c)
  92. add_sdl_test_executable(loopwave NEEDS_RESOURCES TESTUTILS loopwave.c)
  93. add_sdl_test_executable(loopwavequeue NEEDS_RESOURCES TESTUTILS loopwavequeue.c)
  94. add_sdl_test_executable(testsurround testsurround.c)
  95. add_sdl_test_executable(testresample NEEDS_RESOURCES testresample.c)
  96. add_sdl_test_executable(testaudioinfo testaudioinfo.c)
  97. file(GLOB TESTAUTOMATION_SOURCE_FILES testautomation*.c)
  98. add_sdl_test_executable(testautomation NEEDS_RESOURCES ${TESTAUTOMATION_SOURCE_FILES})
  99. add_sdl_test_executable(testmultiaudio NEEDS_RESOURCES TESTUTILS testmultiaudio.c)
  100. add_sdl_test_executable(testaudiohotplug NEEDS_RESOURCES TESTUTILS testaudiohotplug.c)
  101. add_sdl_test_executable(testaudiocapture testaudiocapture.c)
  102. add_sdl_test_executable(testatomic NONINTERACTIVE testatomic.c)
  103. add_sdl_test_executable(testintersections testintersections.c)
  104. add_sdl_test_executable(testrelative testrelative.c)
  105. add_sdl_test_executable(testhittesting testhittesting.c)
  106. add_sdl_test_executable(testdraw testdraw.c)
  107. add_sdl_test_executable(testdrawchessboard testdrawchessboard.c)
  108. add_sdl_test_executable(testdropfile testdropfile.c)
  109. add_sdl_test_executable(testerror NONINTERACTIVE testerror.c)
  110. if(LINUX AND TARGET sdl-build-options)
  111. add_sdl_test_executable(testevdev NONINTERACTIVE testevdev.c)
  112. target_include_directories(testevdev BEFORE PRIVATE $<TARGET_PROPERTY:sdl-build-options,INTERFACE_INCLUDE_DIRECTORIES>)
  113. target_include_directories(testevdev BEFORE PRIVATE ${SDL3_SOURCE_DIR}/src)
  114. endif()
  115. add_sdl_test_executable(testfile testfile.c)
  116. add_sdl_test_executable(testgamepad NEEDS_RESOURCES TESTUTILS testgamepad.c)
  117. add_sdl_test_executable(testgeometry TESTUTILS testgeometry.c)
  118. add_sdl_test_executable(testgl testgl.c)
  119. add_sdl_test_executable(testgles testgles.c)
  120. add_sdl_test_executable(testgles2 testgles2.c)
  121. add_sdl_test_executable(testgles2_sdf TESTUTILS testgles2_sdf.c)
  122. add_sdl_test_executable(testhaptic testhaptic.c)
  123. add_sdl_test_executable(testhotplug testhotplug.c)
  124. add_sdl_test_executable(testrumble testrumble.c)
  125. add_sdl_test_executable(testthread NONINTERACTIVE testthread.c)
  126. add_sdl_test_executable(testiconv NEEDS_RESOURCES TESTUTILS testiconv.c)
  127. add_sdl_test_executable(testime NEEDS_RESOURCES TESTUTILS testime.c)
  128. add_sdl_test_executable(testjoystick testjoystick.c)
  129. add_sdl_test_executable(testkeys testkeys.c)
  130. add_sdl_test_executable(testloadso testloadso.c)
  131. add_sdl_test_executable(testlocale NONINTERACTIVE testlocale.c)
  132. add_sdl_test_executable(testlock testlock.c)
  133. add_sdl_test_executable(testmouse testmouse.c)
  134. if(APPLE)
  135. add_sdl_test_executable(testnative NEEDS_RESOURCES TESTUTILS
  136. testnative.c
  137. testnativecocoa.m
  138. testnativex11.c
  139. )
  140. cmake_push_check_state()
  141. check_c_compiler_flag(-Wno-error=deprecated-declarations HAVE_WNO_ERROR_DEPRECATED_DECLARATIONS)
  142. cmake_pop_check_state()
  143. if(HAVE_WNO_ERROR_DEPRECATED_DECLARATIONS)
  144. set_property(SOURCE "testnativecocoa.m" APPEND_STRING PROPERTY COMPILE_FLAGS " -Wno-error=deprecated-declarations")
  145. endif()
  146. elseif(WINDOWS)
  147. add_sdl_test_executable(testnative NEEDS_RESOURCES TESTUTILS testnative.c testnativew32.c)
  148. elseif(HAVE_X11)
  149. add_sdl_test_executable(testnative NEEDS_RESOURCES TESTUTILS testnative.c testnativex11.c)
  150. target_link_libraries(testnative PRIVATE X11)
  151. endif()
  152. add_sdl_test_executable(testcpuinfo testcpuinfo.c)
  153. add_sdl_test_executable(testoverlay NEEDS_RESOURCES TESTUTILS testoverlay.c)
  154. add_sdl_test_executable(testplatform NONINTERACTIVE testplatform.c)
  155. add_sdl_test_executable(testpower NONINTERACTIVE testpower.c)
  156. add_sdl_test_executable(testfilesystem NONINTERACTIVE testfilesystem.c)
  157. add_sdl_test_executable(testrendertarget NEEDS_RESOURCES TESTUTILS testrendertarget.c)
  158. add_sdl_test_executable(testscale NEEDS_RESOURCES TESTUTILS testscale.c)
  159. add_sdl_test_executable(testsem testsem.c)
  160. add_sdl_test_executable(testsensor testsensor.c)
  161. add_sdl_test_executable(testshader NEEDS_RESOURCES testshader.c)
  162. add_sdl_test_executable(testshape NEEDS_RESOURCES testshape.c)
  163. add_sdl_test_executable(testsprite NEEDS_RESOURCES TESTUTILS testsprite.c)
  164. add_sdl_test_executable(testspriteminimal NEEDS_RESOURCES TESTUTILS testspriteminimal.c)
  165. add_sdl_test_executable(teststreaming NEEDS_RESOURCES TESTUTILS teststreaming.c)
  166. add_sdl_test_executable(testtimer NONINTERACTIVE testtimer.c)
  167. add_sdl_test_executable(testurl testurl.c)
  168. add_sdl_test_executable(testver NONINTERACTIVE testver.c)
  169. add_sdl_test_executable(testviewport NEEDS_RESOURCES TESTUTILS testviewport.c)
  170. add_sdl_test_executable(testwm testwm.c)
  171. add_sdl_test_executable(testyuv NEEDS_RESOURCES testyuv.c testyuv_cvt.c)
  172. add_sdl_test_executable(torturethread torturethread.c)
  173. add_sdl_test_executable(testrendercopyex NEEDS_RESOURCES TESTUTILS testrendercopyex.c)
  174. add_sdl_test_executable(testmessage testmessage.c)
  175. add_sdl_test_executable(testdisplayinfo testdisplayinfo.c)
  176. add_sdl_test_executable(testqsort NONINTERACTIVE testqsort.c)
  177. add_sdl_test_executable(testbounds testbounds.c)
  178. add_sdl_test_executable(testcustomcursor testcustomcursor.c)
  179. add_sdl_test_executable(gamepadmap NEEDS_RESOURCES TESTUTILS gamepadmap.c)
  180. add_sdl_test_executable(testvulkan testvulkan.c)
  181. add_sdl_test_executable(testoffscreen testoffscreen.c)
  182. check_c_compiler_flag(-Wformat-overflow HAVE_WFORMAT_OVERFLOW)
  183. if(HAVE_WFORMAT_OVERFLOW)
  184. target_compile_definitions(testautomation PRIVATE HAVE_WFORMAT_OVERFLOW)
  185. endif()
  186. check_c_compiler_flag(-Wformat HAVE_WFORMAT)
  187. if(HAVE_WFORMAT)
  188. target_compile_definitions(testautomation PRIVATE HAVE_WFORMAT)
  189. endif()
  190. cmake_push_check_state()
  191. if(HAVE_WFORMAT)
  192. # Some compilers ignore -Wformat-extra-args without -Wformat
  193. set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Wformat")
  194. endif()
  195. check_c_compiler_flag(-Wformat-extra-args HAVE_WFORMAT_EXTRA_ARGS)
  196. cmake_pop_check_state()
  197. if(HAVE_WFORMAT_EXTRA_ARGS)
  198. target_compile_definitions(testautomation PRIVATE HAVE_WFORMAT_EXTRA_ARGS)
  199. endif()
  200. if(SDL_DUMMYAUDIO)
  201. list(APPEND SDL_TESTS_NONINTERACTIVE
  202. testaudioinfo
  203. testsurround
  204. )
  205. endif()
  206. if(SDL_DUMMYVIDEO)
  207. list(APPEND SDL_TESTS_NONINTERACTIVE
  208. testkeys
  209. testbounds
  210. testdisplayinfo
  211. )
  212. endif()
  213. if(OPENGL_FOUND)
  214. if(TARGET OpenGL::GL)
  215. target_link_libraries(testshader PRIVATE OpenGL::GL)
  216. target_link_libraries(testgl PRIVATE OpenGL::GL)
  217. else()
  218. if(EMSCRIPTEN AND OPENGL_gl_LIBRARY STREQUAL "nul")
  219. set(OPENGL_gl_LIBRARY GL)
  220. endif()
  221. # emscripten's FindOpenGL.cmake does not create OpenGL::GL
  222. target_link_libraries(testshader PRIVATE ${OPENGL_gl_LIBRARY})
  223. target_link_libraries(testgl PRIVATE ${OPENGL_gl_LIBRARY})
  224. endif()
  225. endif()
  226. if(EMSCRIPTEN)
  227. set_property(TARGET testshader APPEND_STRING PROPERTY LINK_FLAGS " -sLEGACY_GL_EMULATION")
  228. endif()
  229. if(PSP)
  230. # Build EBOOT files if building for PSP
  231. foreach(APP ${SDL_TEST_EXECUTABLES})
  232. create_pbp_file(
  233. TARGET ${APP}
  234. TITLE SDL-${APP}
  235. ICON_PATH NULL
  236. BACKGROUND_PATH NULL
  237. PREVIEW_PATH NULL
  238. )
  239. add_custom_command(
  240. TARGET ${APP} POST_BUILD
  241. COMMAND ${CMAKE_COMMAND} -E make_directory
  242. $<TARGET_FILE_DIR:${ARG_TARGET}>/sdl-${APP}
  243. )
  244. add_custom_command(
  245. TARGET ${APP} POST_BUILD
  246. COMMAND ${CMAKE_COMMAND} -E rename
  247. $<TARGET_FILE_DIR:${ARG_TARGET}>/EBOOT.PBP
  248. $<TARGET_FILE_DIR:${ARG_TARGET}>/sdl-${APP}/EBOOT.PBP
  249. )
  250. if(BUILD_PRX)
  251. add_custom_command(
  252. TARGET ${APP} POST_BUILD
  253. COMMAND ${CMAKE_COMMAND} -E copy
  254. $<TARGET_FILE_DIR:${ARG_TARGET}>/${APP}
  255. $<TARGET_FILE_DIR:${ARG_TARGET}>/sdl-${APP}/${APP}
  256. )
  257. add_custom_command(
  258. TARGET ${APP} POST_BUILD
  259. COMMAND ${CMAKE_COMMAND} -E rename
  260. $<TARGET_FILE_DIR:${ARG_TARGET}>/${APP}.prx
  261. $<TARGET_FILE_DIR:${ARG_TARGET}>/sdl-${APP}/${APP}.prx
  262. )
  263. endif()
  264. add_custom_command(
  265. TARGET ${APP} POST_BUILD
  266. COMMAND ${CMAKE_COMMAND} -E remove
  267. $<TARGET_FILE_DIR:${ARG_TARGET}>/PARAM.SFO
  268. )
  269. endforeach()
  270. endif()
  271. if(N3DS)
  272. set(ROMFS_DIR "${CMAKE_CURRENT_BINARY_DIR}/romfs")
  273. file(COPY ${RESOURCE_FILES} DESTINATION "${ROMFS_DIR}")
  274. foreach(APP ${SDL_TEST_EXECUTABLES})
  275. get_target_property(TARGET_BINARY_DIR ${APP} BINARY_DIR)
  276. set(SMDH_FILE "${TARGET_BINARY_DIR}/${APP}.smdh")
  277. ctr_generate_smdh("${SMDH_FILE}"
  278. NAME "SDL-${APP}"
  279. DESCRIPTION "SDL3 Test suite"
  280. AUTHOR "SDL3 Contributors"
  281. ICON "${CMAKE_CURRENT_SOURCE_DIR}/n3ds/logo48x48.png"
  282. )
  283. ctr_create_3dsx(
  284. ${APP}
  285. ROMFS "${ROMFS_DIR}"
  286. SMDH "${SMDH_FILE}"
  287. )
  288. endforeach()
  289. endif()
  290. if(RISCOS)
  291. set(SDL_TEST_EXECUTABLES_AIF)
  292. foreach(APP ${SDL_TEST_EXECUTABLES})
  293. set_property(TARGET ${APP} APPEND_STRING PROPERTY LINK_FLAGS " -static")
  294. add_custom_command(
  295. OUTPUT ${APP},ff8
  296. COMMAND elf2aif ${APP} ${APP},ff8
  297. DEPENDS ${APP}
  298. )
  299. add_custom_target(${APP}-aif ALL DEPENDS ${APP},ff8)
  300. list(APPEND SDL_TEST_EXECUTABLES_AIF ${CMAKE_CURRENT_BINARY_DIR}/${APP},ff8)
  301. endforeach()
  302. endif()
  303. # Set Apple App ID / Bundle ID. This is needed to launch apps on some Apple
  304. # platforms (iOS, for example).
  305. if(APPLE)
  306. if(CMAKE_VERSION VERSION_LESS "3.7.0")
  307. # CMake's 'BUILDSYSTEM_TARGETS' property is only available in
  308. # CMake 3.7 and above.
  309. message(WARNING "Unable to set Bundle ID for Apple .app builds due to old CMake (pre 3.7).")
  310. else()
  311. foreach(CURRENT_TARGET ${SDL_TEST_EXECUTABLES})
  312. set_target_properties("${CURRENT_TARGET}" PROPERTIES
  313. MACOSX_BUNDLE_GUI_IDENTIFIER "org.libsdl.${CURRENT_TARGET}"
  314. MACOSX_BUNDLE_BUNDLE_VERSION "${SDL3_VERSION}"
  315. MACOSX_BUNDLE_SHORT_VERSION_STRING "${SDL3_VERSION}"
  316. )
  317. endforeach()
  318. endif()
  319. endif()
  320. set(TESTS_ENVIRONMENT
  321. SDL_AUDIO_DRIVER=dummy
  322. SDL_VIDEO_DRIVER=dummy
  323. PATH=$<TARGET_FILE_DIR:SDL3::${sdl_name_component}>
  324. )
  325. function(sdl_set_test_timeout TEST TIMEOUT)
  326. math(EXPR TIMEOUT "${TIMEOUT}*${SDL_TESTS_TIMEOUT_MULTIPLIER}")
  327. set_tests_properties(${test} PROPERTIES TIMEOUT "${TIMEOUT}")
  328. endfunction()
  329. foreach(TESTCASE ${SDL_TESTS_NONINTERACTIVE})
  330. add_test(
  331. NAME ${TESTCASE}
  332. COMMAND ${TESTCASE}
  333. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  334. )
  335. set_tests_properties(${TESTCASE} PROPERTIES ENVIRONMENT "${TESTS_ENVIRONMENT}")
  336. sdl_set_test_timeout(${TESTCASE} 10)
  337. if(SDL_INSTALL_TESTS)
  338. set(exe ${TESTCASE})
  339. set(installedtestsdir "${CMAKE_INSTALL_FULL_LIBEXECDIR}/installed-tests/SDL3")
  340. configure_file(template.test.in "${exe}.test" @ONLY)
  341. install(
  342. FILES "${CMAKE_CURRENT_BINARY_DIR}/${exe}.test"
  343. DESTINATION ${CMAKE_INSTALL_DATADIR}/installed-tests/SDL3
  344. )
  345. endif()
  346. endforeach()
  347. sdl_set_test_timeout(testthread 40)
  348. sdl_set_test_timeout(testtimer 60)
  349. if(SDL_INSTALL_TESTS)
  350. if(RISCOS)
  351. install(
  352. FILES ${SDL_TEST_EXECUTABLES_AIF}
  353. DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/installed-tests/SDL3
  354. )
  355. else()
  356. install(
  357. TARGETS ${SDL_TEST_EXECUTABLES}
  358. DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/installed-tests/SDL3
  359. )
  360. endif()
  361. install(
  362. FILES ${RESOURCE_FILES}
  363. DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/installed-tests/SDL3
  364. )
  365. endif()