2022-08-09 23:05:03 +03:00
# CMake
2016-10-08 03:46:58 +03:00
(www.cmake.org)
2015-06-21 18:33:46 +03:00
2022-08-09 23:05:03 +03:00
The CMake build system is supported on the following platforms:
2015-06-21 18:33:46 +03:00
* FreeBSD
* Linux
2022-08-09 23:05:03 +03:00
* Microsoft Visual C
2015-06-21 18:33:46 +03:00
* MinGW and Msys
2019-09-24 01:27:14 +03:00
* macOS, iOS, and tvOS, with support for XCode
2022-08-09 23:05:03 +03:00
* Android
* Emscripten
* RiscOS
2023-02-17 01:30:20 +03:00
* Playstation 2
2022-08-09 23:05:03 +03:00
* Playstation Vita
2023-02-17 01:30:20 +03:00
* Nintendo 3DS
2022-08-09 23:05:03 +03:00
## Building SDL
2023-02-28 20:30:22 +03:00
Assuming the source for SDL is located at `~/sdl` .
2022-08-09 23:05:03 +03:00
```sh
2023-02-17 01:30:20 +03:00
cmake -S ~/sdl -B ~/build
cmake --build ~/build
2022-08-09 23:05:03 +03:00
```
2023-02-17 01:30:20 +03:00
This will build SDL in the `~/build` directory.
2022-08-09 23:05:03 +03:00
Installation can be done using:
```sh
2023-02-17 01:30:20 +03:00
cmake --install ~/build --prefix /usr/local # '--install' requires CMake 3.15, or newer
2022-08-09 23:05:03 +03:00
```
2023-02-17 01:30:20 +03:00
This will install SDL to /usr/local.
2022-08-09 23:05:03 +03:00
## Including SDL in your project
SDL can be included in your project in 2 major ways:
- using a system SDL library, provided by your (*nix) distribution or a package manager
- using a vendored SDL library: this is SDL copied or symlinked in a subfolder.
The following CMake script supports both, depending on the value of `MYGAME_VENDORED` .
2015-06-21 18:33:46 +03:00
2022-08-09 23:05:03 +03:00
```cmake
cmake_minimum_required(VERSION 3.0)
project(mygame)
2015-06-21 18:33:46 +03:00
2022-08-09 23:05:03 +03:00
# Create an option to switch between a system sdl library and a vendored sdl library
option(MYGAME_VENDORED "Use vendored libraries" OFF)
2015-06-21 18:33:46 +03:00
2022-08-09 23:05:03 +03:00
if(MYGAME_VENDORED)
add_subdirectory(vendored/sdl EXCLUDE_FROM_ALL)
else()
2022-11-22 07:28:58 +03:00
# 1. Look for a SDL3 package, 2. look for the SDL3 component and 3. fail if none can be found
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3)
2022-08-09 23:05:03 +03:00
endif()
2015-06-21 18:33:46 +03:00
2023-02-28 20:30:22 +03:00
# Create your game executable target as usual
2022-08-09 23:05:03 +03:00
add_executable(mygame WIN32 mygame.c)
2015-06-21 18:33:46 +03:00
2022-11-22 07:28:58 +03:00
# Link to the actual SDL3 library. SDL3::SDL3 is the shared SDL library, SDL3::SDL3-static is the static SDL libarary.
target_link_libraries(mygame PRIVATE SDL3::SDL3)
2022-08-09 23:05:03 +03:00
```
2019-09-24 01:27:14 +03:00
2022-08-09 23:05:03 +03:00
### A system SDL library
For CMake to find SDL, it must be installed in [a default location CMake is looking for ](https://cmake.org/cmake/help/latest/command/find_package.html#config-mode-search-procedure ).
The following components are available, to be used as an argument of `find_package` .
2023-02-17 01:30:20 +03:00
| Component name | Description |
|----------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------|
| SDL3-shared | The SDL3 shared library, available through the `SDL3::SDL3-shared` target |
| SDL3-static | The SDL3 static library, available through the `SDL3::SDL3-static` target |
| SDL3_test | The SDL3_test static library, available through the `SDL3::SDL3_test` target |
| SDL3 | The SDL3 library, available through the `SDL3::SDL3` target. This is an alias of `SDL3::SDL3` or `SDL3::SDL3-static` . This component is always available. |
| Headers | The SDL3 headers, available through the `SDL3::Headers` target. This component is always available. |
2022-11-28 20:54:09 +03:00
2022-08-09 23:05:03 +03:00
### Using a vendored SDL
This only requires a copy of SDL in a subdirectory.
## CMake configuration options for platforms
### iOS/tvOS
2019-09-24 01:27:14 +03:00
CMake 3.14+ natively includes support for iOS and tvOS. SDL binaries may be built
using Xcode or Make, possibly among other build-systems.
When using a recent version of CMake (3.14+), it should be possible to:
- build SDL for iOS, both static and dynamic
- build SDL test apps (as iOS/tvOS .app bundles)
2022-11-26 12:41:46 +03:00
- generate a working SDL_build_config.h for iOS (using SDL_build_config.h.cmake as a basis)
2019-09-24 01:27:14 +03:00
To use, set the following CMake variables when running CMake's configuration stage:
- `CMAKE_SYSTEM_NAME=<OS>` (either `iOS` or `tvOS` )
- `CMAKE_OSX_SYSROOT=<SDK>` (examples: `iphoneos` , `iphonesimulator` , `iphoneos12.4` , `/full/path/to/iPhoneOS.sdk` ,
`appletvos` , `appletvsimulator` , `appletvos12.4` , `/full/path/to/AppleTVOS.sdk` , etc.)
- `CMAKE_OSX_ARCHITECTURES=<semicolon-separated list of CPU architectures>` (example: "arm64;armv7s;x86_64")
2022-08-09 23:05:03 +03:00
#### Examples
2019-09-24 01:27:14 +03:00
- for iOS-Simulator, using the latest, installed SDK:
2022-08-09 23:05:03 +03:00
```bash
cmake ~/sdl -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_SYSROOT=iphonesimulator -DCMAKE_OSX_ARCHITECTURES=x86_64
```
2019-09-24 01:27:14 +03:00
- for iOS-Device, using the latest, installed SDK, 64-bit only
2022-08-09 23:05:03 +03:00
```bash
cmake ~/sdl -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_SYSROOT=iphoneos -DCMAKE_OSX_ARCHITECTURES=arm64
```
2019-09-24 01:27:14 +03:00
- for iOS-Device, using the latest, installed SDK, mixed 32/64 bit
2022-08-09 23:05:03 +03:00
```cmake
cmake ~/sdl -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_SYSROOT=iphoneos -DCMAKE_OSX_ARCHITECTURES="arm64;armv7s"
```
2019-09-24 01:27:14 +03:00
- for iOS-Device, using a specific SDK revision (iOS 12.4, in this example):
2022-08-09 23:05:03 +03:00
```cmake
cmake ~/sdl -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_SYSROOT=iphoneos12.4 -DCMAKE_OSX_ARCHITECTURES=arm64
```
2019-09-24 01:27:14 +03:00
- for iOS-Simulator, using the latest, installed SDK, and building SDL test apps (as .app bundles):
2022-08-09 23:05:03 +03:00
```cmake
cmake ~/sdl -DSDL_TESTS=1 -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_SYSROOT=iphonesimulator -DCMAKE_OSX_ARCHITECTURES=x86_64
```
2019-09-24 01:27:14 +03:00
- for tvOS-Simulator, using the latest, installed SDK:
2022-08-09 23:05:03 +03:00
```cmake
cmake ~/sdl -DCMAKE_SYSTEM_NAME=tvOS -DCMAKE_OSX_SYSROOT=appletvsimulator -DCMAKE_OSX_ARCHITECTURES=x86_64
```
2019-09-24 01:27:14 +03:00
- for tvOS-Device, using the latest, installed SDK:
2022-08-09 23:05:03 +03:00
```cmake
cmake ~/sdl -DCMAKE_SYSTEM_NAME=tvOS -DCMAKE_OSX_SYSROOT=appletvos -DCMAKE_OSX_ARCHITECTURES=arm64`
```