We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 4178

Summary: Support for building SDL2 iOS apps through CMake/Xcode
Product: SDL Reporter: Steve Robinson <ssrobins>
Component: buildAssignee: Sam Lantinga <slouken>
Status: RESOLVED ABANDONED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2    
Version: 2.0.8   
Hardware: iPhone/iPod touch   
OS: Other   
Attachments: Modified SDL2 CMakeLists.txt file

Description Steve Robinson 2018-05-27 06:02:32 UTC
Created attachment 3252 [details]
Modified SDL2 CMakeLists.txt file

When I tried to build the 'rectangles' example along with SDL2 by using CMake to generate an Xcode project, I get several errors.

I perform the CMake project generation and build steps with the following commands:
mkdir _build
cd _build
cmake -G Xcode -DCMAKE_TOOLCHAIN_FILE=../ios.toolchain.cmake -DIOS_ARCH="armv7 armv7s" -DENABLE_BITCODE=FALSE ..
cmake --build . --config Release

The toolchain file, ios.toolchain.cmake, is from here: https://github.com/ssrobins/ios-cmake (have some changes in a pending PR back to this project: https://github.com/leetal/ios-cmake).

I've taken the SDL 2.0.8 version of the CMakeLists.txt file and made a few change to it to get the above process to work.  The modified file is attached.

Here are some notes on the changes.  It'll probably make the most sense if you diff my version of the file with the one from SDL 2.0.8.

I append SDL2main to EXTRA_LIBS on every platform except iOS where we don't use it.  In fact, its presence on iOS caused the program not to start correctly, but it's required on Windows, Linux, Mac, and Android.  By adding the library here, I no longer have to add it as a dependency in all my apps.  I just have to set the SDL2 dependency.

The change from this bug is added to the attached file:
https://bugzilla.libsdl.org/show_bug.cgi?id=4143

For iOS only, I use this command to copy over the iOS SDL_config.h:
configure_file("${SDL2_SOURCE_DIR}/include/SDL_config_iphoneos.h"
    "${SDL2_BINARY_DIR}/include/SDL_config.h"
Since check_function_exists isn't working properly on iOS, the auto-generated SDL_config.h falsely determines several functions exist when they actually don't.

I add several -framework linker flags to both SDL2 and SDL2-static targets so there aren't any linker errors when linking to an app that uses SDL2.

To test these changes I have built the 'rectangles' example on Android, iOS, Linux, Mac, and Windows.  You'll find the code here:
https://gitlab.com/ssrobins/sdl2-example

It also has a CI set up, which runs the following script:
https://gitlab.com/ssrobins/sdl2-example/blob/master/.gitlab-ci.yml

You can see the pipeline results here:
https://gitlab.com/ssrobins/sdl2-example/pipelines

Feel free to incorporate this example CMake project into SDL2.  I think it would really help people if they had a base CMake project that works on several platforms as a starting point.
Comment 1 Steve Robinson 2020-12-25 07:33:45 UTC
Not needed anymore in SDL 2.0.14