CMakeLists.txt 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. set(build_options_dependent_tests )
  119. add_sdl_test_executable(testevdev NONINTERACTIVE testevdev.c)
  120. list(APPEND build_options_dependent_tests testevdev)
  121. if(APPLE)
  122. add_sdl_test_executable(testnative NEEDS_RESOURCES TESTUTILS
  123. testnative.c
  124. testnativecocoa.m
  125. testnativex11.c
  126. )
  127. target_compile_definitions(testnative PRIVATE TEST_NATIVE_COCOA)
  128. cmake_push_check_state()
  129. check_c_compiler_flag(-Wno-error=deprecated-declarations HAVE_WNO_ERROR_DEPRECATED_DECLARATIONS)
  130. cmake_pop_check_state()
  131. if(HAVE_WNO_ERROR_DEPRECATED_DECLARATIONS)
  132. set_property(SOURCE "testnativecocoa.m" APPEND_STRING PROPERTY COMPILE_FLAGS " -Wno-error=deprecated-declarations")
  133. endif()
  134. list(APPEND build_options_dependent_tests testnative)
  135. elseif(WINDOWS)
  136. add_sdl_test_executable(testnative NEEDS_RESOURCES TESTUTILS testnative.c testnativew32.c)
  137. list(APPEND build_options_dependent_tests testnative)
  138. elseif(HAVE_X11)
  139. add_sdl_test_executable(testnative NEEDS_RESOURCES TESTUTILS testnative.c testnativex11.c)
  140. target_link_libraries(testnative PRIVATE X11)
  141. list(APPEND build_options_dependent_tests testnative)
  142. endif()
  143. foreach(t ${build_options_dependent_tests})
  144. target_include_directories(${t} BEFORE PRIVATE $<TARGET_PROPERTY:sdl-build-options,INTERFACE_INCLUDE_DIRECTORIES>)
  145. target_include_directories(${t} BEFORE PRIVATE ${SDL3_SOURCE_DIR}/src)
  146. endforeach()
  147. endif()
  148. add_sdl_test_executable(testfile testfile.c)
  149. add_sdl_test_executable(testgamepad NEEDS_RESOURCES TESTUTILS testgamepad.c)
  150. add_sdl_test_executable(testgeometry TESTUTILS testgeometry.c)
  151. add_sdl_test_executable(testgl testgl.c)
  152. add_sdl_test_executable(testgles testgles.c)
  153. add_sdl_test_executable(testgles2 testgles2.c)
  154. add_sdl_test_executable(testgles2_sdf TESTUTILS testgles2_sdf.c)
  155. add_sdl_test_executable(testhaptic testhaptic.c)
  156. add_sdl_test_executable(testhotplug testhotplug.c)
  157. add_sdl_test_executable(testrumble testrumble.c)
  158. add_sdl_test_executable(testthread NONINTERACTIVE testthread.c)
  159. add_sdl_test_executable(testiconv NEEDS_RESOURCES TESTUTILS testiconv.c)
  160. add_sdl_test_executable(testime NEEDS_RESOURCES TESTUTILS testime.c)
  161. add_sdl_test_executable(testjoystick testjoystick.c)
  162. add_sdl_test_executable(testkeys testkeys.c)
  163. add_sdl_test_executable(testloadso testloadso.c)
  164. add_sdl_test_executable(testlocale NONINTERACTIVE testlocale.c)
  165. add_sdl_test_executable(testlock testlock.c)
  166. add_sdl_test_executable(testmouse testmouse.c)
  167. add_sdl_test_executable(testoverlay NEEDS_RESOURCES TESTUTILS testoverlay.c)
  168. add_sdl_test_executable(testplatform NONINTERACTIVE testplatform.c)
  169. add_sdl_test_executable(testpower NONINTERACTIVE testpower.c)
  170. add_sdl_test_executable(testfilesystem NONINTERACTIVE testfilesystem.c)
  171. add_sdl_test_executable(testrendertarget NEEDS_RESOURCES TESTUTILS testrendertarget.c)
  172. add_sdl_test_executable(testscale NEEDS_RESOURCES TESTUTILS testscale.c)
  173. add_sdl_test_executable(testsem testsem.c)
  174. add_sdl_test_executable(testsensor testsensor.c)
  175. add_sdl_test_executable(testshader NEEDS_RESOURCES TESTUTILS testshader.c)
  176. add_sdl_test_executable(testshape NEEDS_RESOURCES testshape.c)
  177. add_sdl_test_executable(testsprite NEEDS_RESOURCES TESTUTILS testsprite.c)
  178. add_sdl_test_executable(testspriteminimal NEEDS_RESOURCES TESTUTILS testspriteminimal.c)
  179. add_sdl_test_executable(teststreaming NEEDS_RESOURCES TESTUTILS teststreaming.c)
  180. add_sdl_test_executable(testtimer NONINTERACTIVE testtimer.c)
  181. add_sdl_test_executable(testurl testurl.c)
  182. add_sdl_test_executable(testver NONINTERACTIVE testver.c)
  183. add_sdl_test_executable(testviewport NEEDS_RESOURCES TESTUTILS testviewport.c)
  184. add_sdl_test_executable(testwm testwm.c)
  185. add_sdl_test_executable(testyuv NEEDS_RESOURCES TESTUTILS testyuv.c testyuv_cvt.c)
  186. add_sdl_test_executable(torturethread torturethread.c)
  187. add_sdl_test_executable(testrendercopyex NEEDS_RESOURCES TESTUTILS testrendercopyex.c)
  188. add_sdl_test_executable(testmessage testmessage.c)
  189. add_sdl_test_executable(testdisplayinfo testdisplayinfo.c)
  190. add_sdl_test_executable(testqsort NONINTERACTIVE testqsort.c)
  191. add_sdl_test_executable(testbounds testbounds.c)
  192. add_sdl_test_executable(testcustomcursor testcustomcursor.c)
  193. add_sdl_test_executable(gamepadmap NEEDS_RESOURCES TESTUTILS gamepadmap.c)
  194. add_sdl_test_executable(testvulkan testvulkan.c)
  195. add_sdl_test_executable(testoffscreen testoffscreen.c)
  196. add_sdl_test_executable(testpopup testpopup.c)
  197. check_c_compiler_flag(-Wformat-overflow HAVE_WFORMAT_OVERFLOW)
  198. if(HAVE_WFORMAT_OVERFLOW)
  199. target_compile_definitions(testautomation PRIVATE HAVE_WFORMAT_OVERFLOW)
  200. endif()
  201. check_c_compiler_flag(-Wformat HAVE_WFORMAT)
  202. if(HAVE_WFORMAT)
  203. target_compile_definitions(testautomation PRIVATE HAVE_WFORMAT)
  204. endif()
  205. cmake_push_check_state()
  206. if(HAVE_WFORMAT)
  207. # Some compilers ignore -Wformat-extra-args without -Wformat
  208. set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Wformat")
  209. endif()
  210. check_c_compiler_flag(-Wformat-extra-args HAVE_WFORMAT_EXTRA_ARGS)
  211. cmake_pop_check_state()
  212. if(HAVE_WFORMAT_EXTRA_ARGS)
  213. target_compile_definitions(testautomation PRIVATE HAVE_WFORMAT_EXTRA_ARGS)
  214. endif()
  215. if(NOT HAVE_X11)
  216. target_compile_definitions(testautomation PRIVATE SDL_DISABLE_SYSWM_X11)
  217. endif()
  218. if(SDL_DUMMYAUDIO)
  219. list(APPEND SDL_TESTS_NONINTERACTIVE
  220. testaudioinfo
  221. testsurround
  222. )
  223. endif()
  224. if(SDL_DUMMYVIDEO)
  225. list(APPEND SDL_TESTS_NONINTERACTIVE
  226. testkeys
  227. testbounds
  228. testdisplayinfo
  229. )
  230. endif()
  231. if(OPENGL_FOUND)
  232. if(TARGET OpenGL::GL)
  233. target_link_libraries(testshader PRIVATE OpenGL::GL)
  234. target_link_libraries(testgl PRIVATE OpenGL::GL)
  235. else()
  236. if(EMSCRIPTEN AND OPENGL_gl_LIBRARY STREQUAL "nul")
  237. set(OPENGL_gl_LIBRARY GL)
  238. endif()
  239. # emscripten's FindOpenGL.cmake does not create OpenGL::GL
  240. target_link_libraries(testshader PRIVATE ${OPENGL_gl_LIBRARY})
  241. target_link_libraries(testgl PRIVATE ${OPENGL_gl_LIBRARY})
  242. endif()
  243. endif()
  244. if(EMSCRIPTEN)
  245. set_property(TARGET testshader APPEND_STRING PROPERTY LINK_FLAGS " -sLEGACY_GL_EMULATION")
  246. endif()
  247. if(PSP)
  248. # Build EBOOT files if building for PSP
  249. foreach(APP ${SDL_TEST_EXECUTABLES})
  250. create_pbp_file(
  251. TARGET ${APP}
  252. TITLE SDL-${APP}
  253. ICON_PATH NULL
  254. BACKGROUND_PATH NULL
  255. PREVIEW_PATH NULL
  256. )
  257. add_custom_command(
  258. TARGET ${APP} POST_BUILD
  259. COMMAND ${CMAKE_COMMAND} -E make_directory
  260. $<TARGET_FILE_DIR:${ARG_TARGET}>/sdl-${APP}
  261. )
  262. add_custom_command(
  263. TARGET ${APP} POST_BUILD
  264. COMMAND ${CMAKE_COMMAND} -E rename
  265. $<TARGET_FILE_DIR:${ARG_TARGET}>/EBOOT.PBP
  266. $<TARGET_FILE_DIR:${ARG_TARGET}>/sdl-${APP}/EBOOT.PBP
  267. )
  268. if(BUILD_PRX)
  269. add_custom_command(
  270. TARGET ${APP} POST_BUILD
  271. COMMAND ${CMAKE_COMMAND} -E copy
  272. $<TARGET_FILE_DIR:${ARG_TARGET}>/${APP}
  273. $<TARGET_FILE_DIR:${ARG_TARGET}>/sdl-${APP}/${APP}
  274. )
  275. add_custom_command(
  276. TARGET ${APP} POST_BUILD
  277. COMMAND ${CMAKE_COMMAND} -E rename
  278. $<TARGET_FILE_DIR:${ARG_TARGET}>/${APP}.prx
  279. $<TARGET_FILE_DIR:${ARG_TARGET}>/sdl-${APP}/${APP}.prx
  280. )
  281. endif()
  282. add_custom_command(
  283. TARGET ${APP} POST_BUILD
  284. COMMAND ${CMAKE_COMMAND} -E remove
  285. $<TARGET_FILE_DIR:${ARG_TARGET}>/PARAM.SFO
  286. )
  287. endforeach()
  288. endif()
  289. if(N3DS)
  290. set(ROMFS_DIR "${CMAKE_CURRENT_BINARY_DIR}/romfs")
  291. file(COPY ${RESOURCE_FILES} DESTINATION "${ROMFS_DIR}")
  292. foreach(APP ${SDL_TEST_EXECUTABLES})
  293. get_target_property(TARGET_BINARY_DIR ${APP} BINARY_DIR)
  294. set(SMDH_FILE "${TARGET_BINARY_DIR}/${APP}.smdh")
  295. ctr_generate_smdh("${SMDH_FILE}"
  296. NAME "SDL-${APP}"
  297. DESCRIPTION "SDL3 Test suite"
  298. AUTHOR "SDL3 Contributors"
  299. ICON "${CMAKE_CURRENT_SOURCE_DIR}/n3ds/logo48x48.png"
  300. )
  301. ctr_create_3dsx(
  302. ${APP}
  303. ROMFS "${ROMFS_DIR}"
  304. SMDH "${SMDH_FILE}"
  305. )
  306. endforeach()
  307. endif()
  308. if(RISCOS)
  309. set(SDL_TEST_EXECUTABLES_AIF)
  310. foreach(APP ${SDL_TEST_EXECUTABLES})
  311. set_property(TARGET ${APP} APPEND_STRING PROPERTY LINK_FLAGS " -static")
  312. add_custom_command(
  313. OUTPUT ${APP},ff8
  314. COMMAND elf2aif ${APP} ${APP},ff8
  315. DEPENDS ${APP}
  316. )
  317. add_custom_target(${APP}-aif ALL DEPENDS ${APP},ff8)
  318. list(APPEND SDL_TEST_EXECUTABLES_AIF ${CMAKE_CURRENT_BINARY_DIR}/${APP},ff8)
  319. endforeach()
  320. endif()
  321. # Set Apple App ID / Bundle ID. This is needed to launch apps on some Apple
  322. # platforms (iOS, for example).
  323. if(APPLE)
  324. if(CMAKE_VERSION VERSION_LESS "3.7.0")
  325. # CMake's 'BUILDSYSTEM_TARGETS' property is only available in
  326. # CMake 3.7 and above.
  327. message(WARNING "Unable to set Bundle ID for Apple .app builds due to old CMake (pre 3.7).")
  328. else()
  329. foreach(CURRENT_TARGET ${SDL_TEST_EXECUTABLES})
  330. set_target_properties("${CURRENT_TARGET}" PROPERTIES
  331. MACOSX_BUNDLE_GUI_IDENTIFIER "org.libsdl.${CURRENT_TARGET}"
  332. MACOSX_BUNDLE_BUNDLE_VERSION "${SDL3_VERSION}"
  333. MACOSX_BUNDLE_SHORT_VERSION_STRING "${SDL3_VERSION}"
  334. )
  335. endforeach()
  336. endif()
  337. endif()
  338. set(TESTS_ENVIRONMENT
  339. SDL_AUDIO_DRIVER=dummy
  340. SDL_VIDEO_DRIVER=dummy
  341. PATH=$<TARGET_FILE_DIR:SDL3::${sdl_name_component}>
  342. )
  343. function(sdl_set_test_timeout TEST TIMEOUT)
  344. math(EXPR TIMEOUT "${TIMEOUT}*${SDL_TESTS_TIMEOUT_MULTIPLIER}")
  345. set_tests_properties(${test} PROPERTIES TIMEOUT "${TIMEOUT}")
  346. endfunction()
  347. foreach(TESTCASE ${SDL_TESTS_NONINTERACTIVE})
  348. add_test(
  349. NAME ${TESTCASE}
  350. COMMAND ${TESTCASE}
  351. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  352. )
  353. set_tests_properties(${TESTCASE} PROPERTIES ENVIRONMENT "${TESTS_ENVIRONMENT}")
  354. sdl_set_test_timeout(${TESTCASE} 10)
  355. if(SDL_INSTALL_TESTS)
  356. set(exe ${TESTCASE})
  357. set(installedtestsdir "${CMAKE_INSTALL_FULL_LIBEXECDIR}/installed-tests/SDL3")
  358. configure_file(template.test.in "${exe}.test" @ONLY)
  359. install(
  360. FILES "${CMAKE_CURRENT_BINARY_DIR}/${exe}.test"
  361. DESTINATION ${CMAKE_INSTALL_DATADIR}/installed-tests/SDL3
  362. )
  363. endif()
  364. endforeach()
  365. sdl_set_test_timeout(testthread 40)
  366. sdl_set_test_timeout(testtimer 60)
  367. if(SDL_INSTALL_TESTS)
  368. if(RISCOS)
  369. install(
  370. FILES ${SDL_TEST_EXECUTABLES_AIF}
  371. DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/installed-tests/SDL3
  372. )
  373. else()
  374. install(
  375. TARGETS ${SDL_TEST_EXECUTABLES}
  376. DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/installed-tests/SDL3
  377. )
  378. endif()
  379. install(
  380. FILES ${RESOURCE_FILES}
  381. DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/installed-tests/SDL3
  382. )
  383. endif()