CMakeLists.txt 15 KB

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