CMakeLists.txt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. cmake_minimum_required(VERSION 3.0)
  2. project(SDL3_test)
  3. include(CheckCCompilerFlag)
  4. include(CheckIncludeFile)
  5. include(CMakeParseArguments)
  6. include(CMakePushCheckState)
  7. set(SDL_TEST_EXECUTABLES)
  8. set(SDL_TESTS_NONINTERACTIVE)
  9. set(SDL_TESTS_NEEDS_ESOURCES)
  10. macro(add_sdl_test_executable TARGET)
  11. cmake_parse_arguments(AST "NONINTERACTIVE;NEEDS_RESOURCES" "" "" ${ARGN})
  12. add_executable(${TARGET} ${AST_UNPARSED_ARGUMENTS})
  13. list(APPEND SDL_TEST_EXECUTABLES ${TARGET})
  14. if(AST_NONINTERACTIVE)
  15. list(APPEND SDL_TESTS_NONINTERACTIVE ${TARGET})
  16. endif()
  17. if(AST_NEEDS_RESOURCES)
  18. list(APPEND SDL_TESTS_NEEDS_ESOURCES ${TARGET})
  19. endif()
  20. endmacro()
  21. if(NOT TARGET SDL3::SDL3-static)
  22. find_package(SDL3 3.0.0 REQUIRED COMPONENTS SDL3-static SDL3_test)
  23. endif()
  24. enable_testing()
  25. if(SDL_INSTALL_TESTS)
  26. include(GNUInstallDirs)
  27. endif()
  28. if(N3DS)
  29. link_libraries(SDL3::SDL3_main)
  30. endif()
  31. if(PSP)
  32. link_libraries(
  33. SDL3::SDL3_main
  34. SDL3::SDL3_test
  35. SDL3::SDL3-static
  36. GL
  37. pspvram
  38. pspvfpu
  39. pspdisplay
  40. pspgu
  41. pspge
  42. pspaudio
  43. pspctrl
  44. psphprm
  45. psppower
  46. )
  47. elseif(PS2)
  48. link_libraries(
  49. SDL3_main
  50. SDL3_test
  51. SDL3-static
  52. patches
  53. gskit
  54. dmakit
  55. ps2_drivers
  56. )
  57. else()
  58. link_libraries(SDL3::SDL3_test SDL3::SDL3-static)
  59. endif()
  60. if(WINDOWS)
  61. # mingw32 must come before SDL3_main to link successfully
  62. if(MINGW OR CYGWIN)
  63. link_libraries(mingw32)
  64. endif()
  65. # CET support was added in VS 16.7
  66. if(MSVC_VERSION GREATER 1926 AND CMAKE_GENERATOR_PLATFORM MATCHES "Win32|x64")
  67. link_libraries(-CETCOMPAT)
  68. endif()
  69. # FIXME: Parent directory CMakeLists.txt only sets these for mingw/cygwin,
  70. # but we need them for VS as well.
  71. link_libraries(SDL3_main)
  72. add_definitions(-Dmain=SDL_main)
  73. endif()
  74. # CMake incorrectly detects opengl32.lib being present on MSVC ARM64
  75. if(NOT MSVC OR NOT CMAKE_GENERATOR_PLATFORM STREQUAL "ARM64")
  76. # Prefer GLVND, if present
  77. set(OpenGL_GL_PREFERENCE GLVND)
  78. find_package(OpenGL)
  79. endif()
  80. if (OPENGL_FOUND)
  81. add_definitions(-DHAVE_OPENGL)
  82. endif()
  83. check_include_file(signal.h HAVE_SIGNAL_H)
  84. if (HAVE_SIGNAL_H)
  85. add_definitions(-DHAVE_SIGNAL_H)
  86. endif()
  87. check_include_file(libudev.h HAVE_LIBUDEV_H)
  88. if (HAVE_LIBUDEV_H)
  89. add_definitions(-DHAVE_LIBUDEV_H)
  90. endif()
  91. add_sdl_test_executable(checkkeys checkkeys.c)
  92. add_sdl_test_executable(checkkeysthreads checkkeysthreads.c)
  93. add_sdl_test_executable(loopwave NEEDS_RESOURCES loopwave.c testutils.c)
  94. add_sdl_test_executable(loopwavequeue NEEDS_RESOURCES loopwavequeue.c testutils.c)
  95. add_sdl_test_executable(testsurround testsurround.c)
  96. add_sdl_test_executable(testresample NEEDS_RESOURCES testresample.c)
  97. add_sdl_test_executable(testaudioinfo testaudioinfo.c)
  98. file(GLOB TESTAUTOMATION_SOURCE_FILES testautomation*.c)
  99. add_sdl_test_executable(testautomation NEEDS_RESOURCES ${TESTAUTOMATION_SOURCE_FILES})
  100. add_sdl_test_executable(testmultiaudio NEEDS_RESOURCES testmultiaudio.c testutils.c)
  101. add_sdl_test_executable(testaudiohotplug NEEDS_RESOURCES testaudiohotplug.c testutils.c)
  102. add_sdl_test_executable(testaudiocapture testaudiocapture.c)
  103. add_sdl_test_executable(testatomic NONINTERACTIVE testatomic.c)
  104. add_sdl_test_executable(testintersections testintersections.c)
  105. add_sdl_test_executable(testrelative testrelative.c)
  106. add_sdl_test_executable(testhittesting testhittesting.c)
  107. add_sdl_test_executable(testdraw2 testdraw2.c)
  108. add_sdl_test_executable(testdrawchessboard testdrawchessboard.c)
  109. add_sdl_test_executable(testdropfile testdropfile.c)
  110. add_sdl_test_executable(testerror NONINTERACTIVE testerror.c)
  111. if(LINUX AND TARGET sdl-build-options)
  112. add_sdl_test_executable(testevdev NONINTERACTIVE testevdev.c)
  113. target_include_directories(testevdev BEFORE PRIVATE $<TARGET_PROPERTY:sdl-build-options,INTERFACE_INCLUDE_DIRECTORIES>)
  114. target_include_directories(testevdev BEFORE PRIVATE ${SDL3_SOURCE_DIR}/src)
  115. endif()
  116. add_sdl_test_executable(testfile testfile.c)
  117. add_sdl_test_executable(testgamecontroller NEEDS_RESOURCES testgamecontroller.c testutils.c)
  118. add_sdl_test_executable(testgeometry testgeometry.c testutils.c)
  119. add_sdl_test_executable(testgesture testgesture.c)
  120. add_sdl_test_executable(testgl2 testgl2.c)
  121. add_sdl_test_executable(testgles testgles.c)
  122. add_sdl_test_executable(testgles2 testgles2.c)
  123. add_sdl_test_executable(testgles2_sdf testgles2_sdf.c testutils.c)
  124. add_sdl_test_executable(testhaptic testhaptic.c)
  125. add_sdl_test_executable(testhotplug testhotplug.c)
  126. add_sdl_test_executable(testrumble testrumble.c)
  127. add_sdl_test_executable(testthread NONINTERACTIVE testthread.c)
  128. add_sdl_test_executable(testiconv NEEDS_RESOURCES testiconv.c testutils.c)
  129. add_sdl_test_executable(testime NEEDS_RESOURCES testime.c testutils.c)
  130. add_sdl_test_executable(testjoystick testjoystick.c)
  131. add_sdl_test_executable(testkeys testkeys.c)
  132. add_sdl_test_executable(testloadso testloadso.c)
  133. add_sdl_test_executable(testlocale NONINTERACTIVE testlocale.c)
  134. add_sdl_test_executable(testlock testlock.c)
  135. add_sdl_test_executable(testmouse testmouse.c)
  136. if(APPLE)
  137. add_sdl_test_executable(testnative NEEDS_RESOURCES
  138. testnative.c
  139. testnativecocoa.m
  140. testnativex11.c
  141. testutils.c
  142. )
  143. cmake_push_check_state()
  144. check_c_compiler_flag(-Wno-error=deprecated-declarations HAVE_WNO_ERROR_DEPRECATED_DECLARATIONS)
  145. cmake_pop_check_state()
  146. if(HAVE_WNO_ERROR_DEPRECATED_DECLARATIONS)
  147. set_property(SOURCE "testnativecocoa.m" APPEND_STRING PROPERTY COMPILE_FLAGS " -Wno-error=deprecated-declarations")
  148. endif()
  149. elseif(WINDOWS)
  150. add_sdl_test_executable(testnative NEEDS_RESOURCES testnative.c testnativew32.c testutils.c)
  151. elseif(HAVE_X11)
  152. add_sdl_test_executable(testnative NEEDS_RESOURCES testnative.c testnativex11.c testutils.c)
  153. target_link_libraries(testnative X11)
  154. endif()
  155. add_sdl_test_executable(testoverlay2 NEEDS_RESOURCES testoverlay2.c testyuv_cvt.c testutils.c)
  156. add_sdl_test_executable(testplatform NONINTERACTIVE testplatform.c)
  157. add_sdl_test_executable(testpower NONINTERACTIVE testpower.c)
  158. add_sdl_test_executable(testfilesystem NONINTERACTIVE testfilesystem.c)
  159. add_sdl_test_executable(testrendertarget NEEDS_RESOURCES testrendertarget.c testutils.c)
  160. add_sdl_test_executable(testscale NEEDS_RESOURCES testscale.c testutils.c)
  161. add_sdl_test_executable(testsem testsem.c)
  162. add_sdl_test_executable(testsensor testsensor.c)
  163. add_sdl_test_executable(testshader NEEDS_RESOURCES testshader.c)
  164. add_sdl_test_executable(testshape NEEDS_RESOURCES testshape.c)
  165. add_sdl_test_executable(testsprite2 NEEDS_RESOURCES testsprite2.c testutils.c)
  166. add_sdl_test_executable(testspriteminimal NEEDS_RESOURCES testspriteminimal.c testutils.c)
  167. add_sdl_test_executable(teststreaming NEEDS_RESOURCES teststreaming.c testutils.c)
  168. add_sdl_test_executable(testtimer NONINTERACTIVE testtimer.c)
  169. add_sdl_test_executable(testurl testurl.c)
  170. add_sdl_test_executable(testver NONINTERACTIVE testver.c)
  171. add_sdl_test_executable(testviewport NEEDS_RESOURCES testviewport.c testutils.c)
  172. add_sdl_test_executable(testwm2 testwm2.c)
  173. add_sdl_test_executable(testyuv NEEDS_RESOURCES testyuv.c testyuv_cvt.c)
  174. add_sdl_test_executable(torturethread torturethread.c)
  175. add_sdl_test_executable(testrendercopyex NEEDS_RESOURCES testrendercopyex.c testutils.c)
  176. add_sdl_test_executable(testmessage testmessage.c)
  177. add_sdl_test_executable(testdisplayinfo testdisplayinfo.c)
  178. add_sdl_test_executable(testqsort NONINTERACTIVE testqsort.c)
  179. add_sdl_test_executable(testbounds testbounds.c)
  180. add_sdl_test_executable(testcustomcursor testcustomcursor.c)
  181. add_sdl_test_executable(controllermap NEEDS_RESOURCES controllermap.c testutils.c)
  182. add_sdl_test_executable(testvulkan testvulkan.c)
  183. add_sdl_test_executable(testoffscreen testoffscreen.c)
  184. cmake_push_check_state()
  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. check_c_compiler_flag(-Wformat-extra-args HAVE_WFORMAT_EXTRA_ARGS)
  194. if(HAVE_WFORMAT_EXTRA_ARGS)
  195. target_compile_definitions(testautomation PRIVATE HAVE_WFORMAT_EXTRA_ARGS)
  196. endif()
  197. cmake_pop_check_state()
  198. if(SDL_DUMMYAUDIO)
  199. list(APPEND SDL_TESTS_NONINTERACTIVE
  200. testaudioinfo
  201. testsurround
  202. )
  203. endif()
  204. if(SDL_DUMMYVIDEO)
  205. list(APPEND SDL_TESTS_NONINTERACTIVE
  206. testkeys
  207. testbounds
  208. testdisplayinfo
  209. )
  210. endif()
  211. if(OPENGL_FOUND)
  212. if(TARGET OpenGL::GL)
  213. target_link_libraries(testshader OpenGL::GL)
  214. target_link_libraries(testgl2 OpenGL::GL)
  215. else()
  216. if(EMSCRIPTEN AND OPENGL_gl_LIBRARY STREQUAL "nul")
  217. set(OPENGL_gl_LIBRARY GL)
  218. endif()
  219. # emscripten's FindOpenGL.cmake does not create OpenGL::GL
  220. target_link_libraries(testshader ${OPENGL_gl_LIBRARY})
  221. target_link_libraries(testgl2 ${OPENGL_gl_LIBRARY})
  222. endif()
  223. endif()
  224. if(EMSCRIPTEN)
  225. target_link_libraries(testshader -sLEGACY_GL_EMULATION)
  226. endif()
  227. file(GLOB RESOURCE_FILES *.bmp *.wav *.hex moose.dat utf8.txt)
  228. file(COPY ${RESOURCE_FILES} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
  229. if(PSP)
  230. # Build EBOOT files if building for PSP
  231. set(BUILD_EBOOT
  232. ${SDL_TESTS_NEEDS_ESOURCES}
  233. testatomic
  234. testaudiocapture
  235. testaudioinfo
  236. testbounds
  237. testdisplayinfo
  238. testdraw2
  239. testdrawchessboard
  240. testerror
  241. testfile
  242. testfilesystem
  243. testgeometry
  244. testgl2
  245. testhittesting
  246. testiconv
  247. testintersections
  248. testjoystick
  249. testlock
  250. testmessage
  251. testoverlay2
  252. testplatform
  253. testpower
  254. testqsort
  255. testsem
  256. teststreaming
  257. testsurround
  258. testthread
  259. testtimer
  260. testver
  261. testviewport
  262. testwm2
  263. torturethread
  264. )
  265. foreach(APP IN LISTS BUILD_EBOOT)
  266. create_pbp_file(
  267. TARGET ${APP}
  268. TITLE SDL-${APP}
  269. ICON_PATH NULL
  270. BACKGROUND_PATH NULL
  271. PREVIEW_PATH NULL
  272. )
  273. add_custom_command(
  274. TARGET ${APP} POST_BUILD
  275. COMMAND ${CMAKE_COMMAND} -E make_directory
  276. $<TARGET_FILE_DIR:${ARG_TARGET}>/sdl-${APP}
  277. )
  278. add_custom_command(
  279. TARGET ${APP} POST_BUILD
  280. COMMAND ${CMAKE_COMMAND} -E rename
  281. $<TARGET_FILE_DIR:${ARG_TARGET}>/EBOOT.PBP
  282. $<TARGET_FILE_DIR:${ARG_TARGET}>/sdl-${APP}/EBOOT.PBP
  283. )
  284. if(${BUILD_PRX})
  285. add_custom_command(
  286. TARGET ${APP} POST_BUILD
  287. COMMAND ${CMAKE_COMMAND} -E copy
  288. $<TARGET_FILE_DIR:${ARG_TARGET}>/${APP}
  289. $<TARGET_FILE_DIR:${ARG_TARGET}>/sdl-${APP}/${APP}
  290. )
  291. add_custom_command(
  292. TARGET ${APP} POST_BUILD
  293. COMMAND ${CMAKE_COMMAND} -E rename
  294. $<TARGET_FILE_DIR:${ARG_TARGET}>/${APP}.prx
  295. $<TARGET_FILE_DIR:${ARG_TARGET}>/sdl-${APP}/${APP}.prx
  296. )
  297. endif()
  298. add_custom_command(
  299. TARGET ${APP} POST_BUILD
  300. COMMAND ${CMAKE_COMMAND} -E remove
  301. $<TARGET_FILE_DIR:${ARG_TARGET}>/PARAM.SFO
  302. )
  303. endforeach()
  304. endif()
  305. if(N3DS)
  306. set(ROMFS_DIR "${CMAKE_CURRENT_BINARY_DIR}/romfs")
  307. file(COPY ${RESOURCE_FILES} DESTINATION "${ROMFS_DIR}")
  308. foreach(APP IN LISTS SDL_TEST_EXECUTABLES)
  309. get_target_property(TARGET_BINARY_DIR ${APP} BINARY_DIR)
  310. set(SMDH_FILE "${TARGET_BINARY_DIR}/${APP}.smdh")
  311. ctr_generate_smdh("${SMDH_FILE}"
  312. NAME "SDL-${APP}"
  313. DESCRIPTION "SDL3 Test suite"
  314. AUTHOR "SDL3 Contributors"
  315. ICON "${CMAKE_CURRENT_SOURCE_DIR}/n3ds/logo48x48.png"
  316. )
  317. ctr_create_3dsx(
  318. ${APP}
  319. ROMFS "${ROMFS_DIR}"
  320. SMDH "${SMDH_FILE}"
  321. )
  322. endforeach()
  323. endif()
  324. if(RISCOS)
  325. set(SDL_TEST_EXECUTABLES_AIF)
  326. foreach(APP IN LISTS SDL_TEST_EXECUTABLES)
  327. target_link_options(${APP} PRIVATE -static)
  328. add_custom_command(
  329. OUTPUT ${APP},ff8
  330. COMMAND elf2aif ${APP} ${APP},ff8
  331. DEPENDS ${APP}
  332. )
  333. add_custom_target(${APP}-aif ALL DEPENDS ${APP},ff8)
  334. list(APPEND SDL_TEST_EXECUTABLES_AIF ${CMAKE_CURRENT_BINARY_DIR}/${APP},ff8)
  335. endforeach()
  336. endif()
  337. foreach(APP IN LISTS SDL_TESTS_NEEDS_RESOURCES)
  338. foreach(RESOURCE_FILE ${RESOURCE_FILES})
  339. if(PSP OR PS2)
  340. add_custom_command(TARGET ${APP} POST_BUILD COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${RESOURCE_FILE} $<TARGET_FILE_DIR:${APP}>/sdl-${APP})
  341. else()
  342. add_custom_command(TARGET ${APP} POST_BUILD COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${RESOURCE_FILE} $<TARGET_FILE_DIR:${APP}>)
  343. endif()
  344. endforeach(RESOURCE_FILE)
  345. if(APPLE)
  346. # Make sure resource files get installed into macOS/iOS .app bundles.
  347. target_sources(${APP} PRIVATE "${RESOURCE_FILES}")
  348. set_target_properties(${APP} PROPERTIES RESOURCE "${RESOURCE_FILES}")
  349. endif()
  350. endforeach()
  351. # Set Apple App ID / Bundle ID. This is needed to launch apps on some Apple
  352. # platforms (iOS, for example).
  353. if(APPLE)
  354. if(${CMAKE_VERSION} VERSION_LESS "3.7.0")
  355. # CMake's 'BUILDSYSTEM_TARGETS' property is only available in
  356. # CMake 3.7 and above.
  357. message(WARNING "Unable to set Bundle ID for Apple .app builds due to old CMake (pre 3.7).")
  358. else()
  359. get_property(TARGETS DIRECTORY ${CMAKE_CURRENT_LIST_DIR} PROPERTY BUILDSYSTEM_TARGETS)
  360. foreach(CURRENT_TARGET IN LISTS TARGETS)
  361. get_property(TARGET_TYPE TARGET ${CURRENT_TARGET} PROPERTY TYPE)
  362. if(TARGET_TYPE STREQUAL "EXECUTABLE")
  363. set_target_properties("${CURRENT_TARGET}" PROPERTIES
  364. MACOSX_BUNDLE_GUI_IDENTIFIER "org.libsdl.${CURRENT_TARGET}"
  365. MACOSX_BUNDLE_BUNDLE_VERSION "${SDL_VERSION}"
  366. MACOSX_BUNDLE_SHORT_VERSION_STRING "${SDL_VERSION}"
  367. )
  368. endif()
  369. endforeach()
  370. endif()
  371. endif()
  372. set(TESTS_ENVIRONMENT
  373. SDL_AUDIODRIVER=dummy
  374. SDL_VIDEODRIVER=dummy
  375. )
  376. foreach(TESTCASE ${SDL_TESTS_NONINTERACTIVE})
  377. add_test(
  378. NAME ${TESTCASE}
  379. COMMAND ${TESTCASE}
  380. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  381. )
  382. set_tests_properties(${TESTCASE}
  383. PROPERTIES
  384. ENVIRONMENT "${TESTS_ENVIRONMENT}"
  385. TIMEOUT 10
  386. )
  387. if(SDL_INSTALL_TESTS)
  388. set(exe ${TESTCASE})
  389. set(installedtestsdir "${CMAKE_INSTALL_FULL_LIBEXECDIR}/installed-tests/SDL3")
  390. configure_file(template.test.in "${exe}.test" @ONLY)
  391. install(
  392. FILES "${CMAKE_CURRENT_BINARY_DIR}/${exe}.test"
  393. DESTINATION ${CMAKE_INSTALL_DATADIR}/installed-tests/SDL3
  394. )
  395. endif()
  396. endforeach()
  397. set_tests_properties(testthread PROPERTIES TIMEOUT 40)
  398. set_tests_properties(testtimer PROPERTIES TIMEOUT 60)
  399. if(SDL_INSTALL_TESTS)
  400. if(RISCOS)
  401. install(
  402. FILES ${SDL_TEST_EXECUTABLES_AIF}
  403. DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/installed-tests/SDL3
  404. )
  405. else()
  406. install(
  407. TARGETS ${SDL_TEST_EXECUTABLES}
  408. DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/installed-tests/SDL3
  409. )
  410. endif()
  411. install(
  412. FILES ${RESOURCE_FILES}
  413. DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/installed-tests/SDL3
  414. )
  415. endif()