CMakeLists.txt 15 KB

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