CMakeLists.txt 15 KB

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