CMakeLists.txt 16 KB

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