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