From b13c951cca352598be8cbba3d4f050862b2769d2 Mon Sep 17 00:00:00 2001 From: David Ludwig Date: Tue, 27 Aug 2019 11:07:43 -0400 Subject: [PATCH] CMake: iOS support added When using a recent version of CMake (3.14+), this should make it possible to: - build SDL for iOS, both static and dynamic - build SDL test apps (as iOS .app bundles) - generate a working SDL_config.h for iOS (using SDL_config.h.cmake as a basis) To use, set the following CMake variables when running CMake's configuration stage: - CMAKE_SYSTEM_NAME=iOS - CMAKE_OSX_SYSROOT= (examples: iphoneos, iphonesimulator, iphoneos12.4, /full/path/to/iPhoneOS.sdk, etc.) - CMAKE_OSX_ARCHITECTURES= (example: "arm64;armv7s") Examples: - for Simulator, using the latest, installed SDK: cmake path/to/SDL -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_SYSROOT=iphonesimulator -DCMAKE_OSX_ARCHITECTURES=x86_64 - for Device, using the latest, installed SDK, 64-bit only cmake path/to/SDL -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_SYSROOT=iphoneos -DCMAKE_OSX_ARCHITECTURES=arm64 - for Device, using the latest, installed SDK, mixed 32/64 bit cmake path/to/SDL -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_SYSROOT=iphoneos -DCMAKE_OSX_ARCHITECTURES="arm64;armv7s" - for Device, using a specific SDK revision (iOS 12.4, in this example): cmake path/to/SDL -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_SYSROOT=iphoneos12.4 -DCMAKE_OSX_ARCHITECTURES=arm64 - for Simulator, using the latest, installed SDK, and building SDL test apps (as .app bundles): cmake path/to/SDL -DSDL_TEST=1 -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_SYSROOT=iphonesimulator -DCMAKE_OSX_ARCHITECTURES=x86_64 --- CMakeLists.txt | 97 ++++++++++++++++++++++++++++++++------ include/SDL_config.h.cmake | 6 +++ test/CMakeLists.txt | 66 +++++++++++++++++++------- 3 files changed, 136 insertions(+), 33 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6962c5fa4..ce88eeb6a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1453,6 +1453,7 @@ elseif(APPLE) set(SDL_FRAMEWORK_COCOA 1) set(SDL_FRAMEWORK_CARBON 1) endif() + set(SDL_FRAMEWORK_FOUNDATION 1) # Requires the darwin file implementation if(SDL_FILE) @@ -1476,6 +1477,7 @@ elseif(APPLE) set(HAVE_SDL_AUDIO TRUE) set(SDL_FRAMEWORK_COREAUDIO 1) set(SDL_FRAMEWORK_AUDIOTOOLBOX 1) + set(SDL_FRAMEWORK_AVFOUNDATION 1) endif() if(SDL_JOYSTICK) @@ -1483,43 +1485,49 @@ elseif(APPLE) if(HAVE_HIDAPI) if(IOS) set(SOURCE_FILES ${SOURCE_FILES} ${SDL2_SOURCE_DIR}/src/hidapi/ios/hid.m) + set(SDL_FRAMEWORK_COREBLUETOOTH 1) endif() endif() - set(SDL_JOYSTICK_IOKIT 1) if (IOS) file(GLOB JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/iphoneos/*.m ${SDL2_SOURCE_DIR}/src/joystick/steam/*.c) + set(SDL_JOYSTICK_MFI 1) + set(SDL_FRAMEWORK_COREMOTION 1) + set(SDL_FRAMEWORK_GAMECONTROLLER 1) + set(HAVE_SDL_SENSORS 1) else() file(GLOB JOYSTICK_SOURCES ${SDL2_SOURCE_DIR}/src/joystick/darwin/*.c) + set(SDL_JOYSTICK_IOKIT 1) + set(SDL_FRAMEWORK_IOKIT 1) + set(SDL_FRAMEWORK_FF 1) endif() set(SOURCE_FILES ${SOURCE_FILES} ${JOYSTICK_SOURCES}) set(HAVE_SDL_JOYSTICK TRUE) - set(SDL_FRAMEWORK_IOKIT 1) - set(SDL_FRAMEWORK_FF 1) endif() if(SDL_HAPTIC) - set(SDL_HAPTIC_IOKIT 1) if (IOS) file(GLOB HAPTIC_SOURCES ${SDL2_SOURCE_DIR}/src/haptic/dummy/*.c) set(SDL_HAPTIC_DUMMY 1) else() file(GLOB HAPTIC_SOURCES ${SDL2_SOURCE_DIR}/src/haptic/darwin/*.c) + set(SDL_HAPTIC_IOKIT 1) + set(SDL_FRAMEWORK_IOKIT 1) + set(SDL_FRAMEWORK_FF 1) endif() set(SOURCE_FILES ${SOURCE_FILES} ${HAPTIC_SOURCES}) set(HAVE_SDL_HAPTIC TRUE) - set(SDL_FRAMEWORK_IOKIT 1) - set(SDL_FRAMEWORK_FF 1) if(NOT SDL_JOYSTICK) message(FATAL_ERROR "SDL_HAPTIC requires SDL_JOYSTICK to be enabled") endif() endif() if(SDL_POWER) - set(SDL_POWER_MACOSX 1) if (IOS) file(GLOB POWER_SOURCES ${SDL2_SOURCE_DIR}/src/power/uikit/*.m) + set(SDL_POWER_UIKIT 1) else() file(GLOB POWER_SOURCES ${SDL2_SOURCE_DIR}/src/power/macosx/*.c) + set(SDL_POWER_MACOSX 1) endif() set(SOURCE_FILES ${SOURCE_FILES} ${POWER_SOURCES}) set(HAVE_SDL_POWER TRUE) @@ -1542,10 +1550,24 @@ elseif(APPLE) set(HAVE_SDL_FILESYSTEM TRUE) endif() + if(SDL_SENSOR) + if(IOS) + set(SDL_SENSOR_COREMOTION 1) + set(HAVE_SDL_SENSORS TRUE) + file(GLOB SENSOR_SOURCES ${SDL2_SOURCE_DIR}/src/sensor/coremotion/*.m) + set(SOURCE_FILES ${SOURCE_FILES} ${SENSOR_SOURCES}) + endif() + endif() + # iOS hack needed - http://code.google.com/p/ios-cmake/ ? if(SDL_VIDEO) if (IOS) set(SDL_VIDEO_DRIVER_UIKIT 1) + set(SDL_FRAMEWORK_COREGRAPHICS 1) + set(SDL_FRAMEWORK_QUARTZCORE 1) + set(SDL_FRAMEWORK_UIKIT 1) + set(SDL_IPHONE_KEYBOARD 1) + set(SDL_IPHONE_LAUNCHSCREEN 1) file(GLOB UIKITVIDEO_SOURCES ${SDL2_SOURCE_DIR}/src/video/uikit/*.m) set(SOURCE_FILES ${SOURCE_FILES} ${UIKITVIDEO_SOURCES}) else() @@ -1556,15 +1578,21 @@ elseif(APPLE) set(SDL_VIDEO_RENDER_OGL 1) set(HAVE_VIDEO_OPENGL TRUE) endif() - - if(VIDEO_OPENGLES) - set(SDL_VIDEO_OPENGL_EGL 1) - set(SDL_VIDEO_OPENGL_ES2 1) - set(SDL_VIDEO_RENDER_OGL_ES2 1) - set(HAVE_VIDEO_OPENGLES TRUE) - endif() endif() - + + if(VIDEO_OPENGLES) + if(IOS) + set(SDL_FRAMEWORK_OPENGLES 1) + set(SDL_VIDEO_OPENGL_ES 1) + set(SDL_VIDEO_RENDER_OGL_ES 1) + else() + set(SDL_VIDEO_OPENGL_EGL 1) + endif() + set(SDL_VIDEO_OPENGL_ES2 1) + set(SDL_VIDEO_RENDER_OGL_ES2 1) + set(HAVE_VIDEO_OPENGLES TRUE) + endif() + if(VIDEO_VULKAN OR VIDEO_METAL OR RENDER_METAL) set(ORIG_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -x objective-c") @@ -1634,6 +1662,30 @@ elseif(APPLE) find_library(AUDIOTOOLBOX AudioToolbox) list(APPEND EXTRA_LIBS ${AUDIOTOOLBOX}) endif() + if(SDL_FRAMEWORK_AVFOUNDATION) + find_library(AVFOUNDATION AVFoundation) + list(APPEND EXTRA_LIBS ${AVFOUNDATION}) + endif() + if(SDL_FRAMEWORK_COREBLUETOOTH) + find_library(COREBLUETOOTH CoreBluetooth) + list(APPEND EXTRA_LIBS ${COREBLUETOOTH}) + endif() + if(SDL_FRAMEWORK_COREGRAPHICS) + find_library(COREGRAPHICS CoreGraphics) + list(APPEND EXTRA_LIBS ${COREGRAPHICS}) + endif() + if(SDL_FRAMEWORK_COREMOTION) + find_library(COREMOTION CoreMotion) + list(APPEND EXTRA_LIBS ${COREMOTION}) + endif() + if(SDL_FRAMEWORK_FOUNDATION) + find_library(FOUNDATION Foundation) + list(APPEND EXTRA_LIBS ${FOUNDATION}) + endif() + if(SDL_FRAMEWORK_GAMECONTROLLER) + find_library(GAMECONTROLLER GameController) + list(APPEND EXTRA_LIBS ${GAMECONTROLLER}) + endif() if(SDL_FRAMEWORK_METAL) if(IOS) find_library(METAL Metal) @@ -1642,6 +1694,10 @@ elseif(APPLE) list(APPEND EXTRA_LDFLAGS "-Wl,-weak_framework,Metal") endif() endif() + if(SDL_FRAMEWORK_OPENGLES) + find_library(OPENGLES OpenGLES) + list(APPEND EXTRA_LIBS ${OPENGLES}) + endif() if(SDL_FRAMEWORK_QUARTZCORE) if(IOS) find_library(QUARTZCORE QuartzCore) @@ -1650,6 +1706,10 @@ elseif(APPLE) list(APPEND EXTRA_LDFLAGS "-Wl,-weak_framework,QuartzCore") endif() endif() + if(SDL_FRAMEWORK_UIKIT) + find_library(UIKIT UIKit) + list(APPEND EXTRA_LIBS ${UIKIT}) + endif() CheckPTHREAD() @@ -1900,6 +1960,10 @@ if(SDL_SHARED) if (NOT ANDROID) set_target_properties(SDL2 PROPERTIES DEBUG_POSTFIX ${SDL_CMAKE_DEBUG_POSTFIX}) endif() + if(IOS) + set_property(TARGET SDL2 APPEND_STRING PROPERTY COMPILE_FLAGS "-fobjc-arc") + target_compile_definitions(SDL2 PRIVATE IOS_DYLIB=1) + endif() endif() if(ANDROID) @@ -1942,6 +2006,9 @@ if(SDL_STATIC) if (NOT ANDROID) set_target_properties(SDL2-static PROPERTIES DEBUG_POSTFIX ${SDL_CMAKE_DEBUG_POSTFIX}) endif() + if(IOS) + set_property(TARGET SDL2-static APPEND_STRING PROPERTY COMPILE_FLAGS "-fobjc-arc") + endif() endif() ##### Tests ##### diff --git a/include/SDL_config.h.cmake b/include/SDL_config.h.cmake index 65ba3ea70..205024d30 100644 --- a/include/SDL_config.h.cmake +++ b/include/SDL_config.h.cmake @@ -298,6 +298,7 @@ /* Enable various sensor drivers */ #cmakedefine SDL_SENSOR_ANDROID @SDL_SENSOR_ANDROID@ +#cmakedefine SDL_SENSOR_COREMOTION @SDL_SENSOR_COREMOTION@ #cmakedefine SDL_SENSOR_DUMMY @SDL_SENSOR_DUMMY@ /* Enable various shared object loading systems */ @@ -323,6 +324,7 @@ #cmakedefine SDL_VIDEO_DRIVER_ANDROID @SDL_VIDEO_DRIVER_ANDROID@ #cmakedefine SDL_VIDEO_DRIVER_HAIKU @SDL_VIDEO_DRIVER_HAIKU@ #cmakedefine SDL_VIDEO_DRIVER_COCOA @SDL_VIDEO_DRIVER_COCOA@ +#cmakedefine SDL_VIDEO_DRIVER_UIKIT @SDL_VIDEO_DRIVER_UIKIT@ #cmakedefine SDL_VIDEO_DRIVER_DIRECTFB @SDL_VIDEO_DRIVER_DIRECTFB@ #cmakedefine SDL_VIDEO_DRIVER_DIRECTFB_DYNAMIC @SDL_VIDEO_DRIVER_DIRECTFB_DYNAMIC@ #cmakedefine SDL_VIDEO_DRIVER_DUMMY @SDL_VIDEO_DRIVER_DUMMY@ @@ -396,6 +398,7 @@ #cmakedefine SDL_POWER_LINUX @SDL_POWER_LINUX@ #cmakedefine SDL_POWER_WINDOWS @SDL_POWER_WINDOWS@ #cmakedefine SDL_POWER_MACOSX @SDL_POWER_MACOSX@ +#cmakedefine SDL_POWER_UIKIT @SDL_POWER_UIKIT@ #cmakedefine SDL_POWER_HAIKU @SDL_POWER_HAIKU@ #cmakedefine SDL_POWER_EMSCRIPTEN @SDL_POWER_EMSCRIPTEN@ #cmakedefine SDL_POWER_HARDWIRED @SDL_POWER_HARDWIRED@ @@ -417,6 +420,9 @@ #cmakedefine SDL_LIBSAMPLERATE_DYNAMIC @SDL_LIBSAMPLERATE_DYNAMIC@ /* Platform specific definitions */ +#cmakedefine SDL_IPHONE_KEYBOARD @SDL_IPHONE_KEYBOARD@ +#cmakedefine SDL_IPHONE_LAUNCHSCREEN @SDL_IPHONE_LAUNCHSCREEN@ + #if !defined(__WIN32__) # if !defined(_STDINT_H_) && !defined(_STDINT_H) && !defined(HAVE_STDINT_H) && !defined(_HAVE_STDINT_H) typedef unsigned int size_t; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 3c25c5c7c..0423de8f7 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -101,22 +101,52 @@ endforeach(RESOURCE_FILE) file(COPY ${RESOURCE_FILES} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) # TODO: Might be easier to make all targets depend on the resources...? -add_dependencies(testscale SDL2_test_resoureces) -add_dependencies(testrendercopyex SDL2_test_resoureces) -add_dependencies(controllermap SDL2_test_resoureces) -add_dependencies(testyuv SDL2_test_resoureces) -add_dependencies(testgamecontroller SDL2_test_resoureces) -add_dependencies(testshape SDL2_test_resoureces) -add_dependencies(testshader SDL2_test_resoureces) -add_dependencies(testnative SDL2_test_resoureces) -add_dependencies(testspriteminimal SDL2_test_resoureces) -add_dependencies(testautomation SDL2_test_resoureces) -add_dependencies(testcustomcursor SDL2_test_resoureces) -add_dependencies(testrendertarget SDL2_test_resoureces) -add_dependencies(testsprite2 SDL2_test_resoureces) -add_dependencies(loopwave SDL2_test_resoureces) -add_dependencies(loopwavequeue SDL2_test_resoureces) -add_dependencies(testresample SDL2_test_resoureces) -add_dependencies(testaudiohotplug SDL2_test_resoureces) -add_dependencies(testmultiaudio SDL2_test_resoureces) +set(NEEDS_RESOURCES + testscale + testrendercopyex + controllermap + testyuv + testgamecontroller + testshape + testshader + testnative + testspriteminimal + testautomation + testcustomcursor + testrendertarget + testsprite2 + loopwave + loopwavequeue + testresample + testaudiohotplug + testmultiaudio +) +foreach(APP IN LISTS NEEDS_RESOURCES) + add_dependencies(${APP} SDL2_test_resoureces) + if(APPLE) + # Make sure resource files get installed into macOS/iOS .app bundles. + target_sources(${APP} PRIVATE "${RESOURCE_FILES}") + set_target_properties(${APP} PROPERTIES RESOURCE "${RESOURCE_FILES}") + endif() +endforeach() + +# Set Apple App ID / Bundle ID. This is needed to launch apps on some Apple +# platforms (iOS, for example). +if(APPLE) + if(${CMAKE_VERSION} VERSION_LESS "3.7.0") + # CMake's 'BUILDSYSTEM_TARGETS' property is only available in + # CMake 3.7 and above. + message(WARNING "Unable to set Bundle ID for Apple .app builds due to old CMake (pre 3.7).") + else() + get_property(TARGETS DIRECTORY ${CMAKE_CURRENT_LIST_DIR} PROPERTY BUILDSYSTEM_TARGETS) + foreach(CURRENT_TARGET IN LISTS TARGETS) + get_property(TARGET_TYPE TARGET ${CURRENT_TARGET} PROPERTY TYPE) + if(TARGET_TYPE STREQUAL "EXECUTABLE") + set_target_properties("${CURRENT_TARGET}" PROPERTIES + MACOSX_BUNDLE_GUI_IDENTIFIER "org.libsdl.${CURRENT_TARGET}" + ) + endif() + endforeach() + endif() +endif()