summaryrefslogtreecommitdiff
path: root/src/contrib/SDL-3.2.20/docs/README-cmake.md
diff options
context:
space:
mode:
Diffstat (limited to 'src/contrib/SDL-3.2.20/docs/README-cmake.md')
-rw-r--r--src/contrib/SDL-3.2.20/docs/README-cmake.md364
1 files changed, 364 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/docs/README-cmake.md b/src/contrib/SDL-3.2.20/docs/README-cmake.md
new file mode 100644
index 0000000..f2e4759
--- /dev/null
+++ b/src/contrib/SDL-3.2.20/docs/README-cmake.md
@@ -0,0 +1,364 @@
1# CMake
2
3[www.cmake.org](https://www.cmake.org/)
4
5The CMake build system is supported with the following environments:
6
7* Android
8* Emscripten
9* FreeBSD
10* Haiku
11* Linux
12* macOS, iOS, tvOS, and visionOS with support for XCode
13* Microsoft Visual Studio
14* MinGW and Msys
15* NetBSD
16* Nintendo 3DS
17* PlayStation 2
18* PlayStation Portable
19* PlayStation Vita
20* RISC OS
21
22## Building SDL on Windows
23
24Assuming you're in the SDL source directory, building and installing to C:/SDL can be done with:
25```sh
26cmake -S . -B build
27cmake --build build --config RelWithDebInfo
28cmake --install build --config RelWithDebInfo --prefix C:/SDL
29```
30
31## Building SDL on UNIX
32
33SDL will build with very few dependencies, but for full functionality you should install the packages detailed in [README-linux.md](README-linux.md).
34
35Assuming you're in the SDL source directory, building and installing to /usr/local can be done with:
36```sh
37cmake -S . -B build
38cmake --build build
39sudo cmake --install build --prefix /usr/local
40```
41
42## Building SDL on macOS
43
44Assuming you're in the SDL source directory, building and installing to ~/SDL can be done with:
45```sh
46cmake -S . -B build -DSDL_FRAMEWORK=ON -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64"
47cmake --build build
48cmake --install build --prefix ~/SDL
49```
50
51## Building SDL tests
52
53You can build the SDL test programs by adding `-DSDL_TESTS=ON` to the first cmake command above:
54```sh
55cmake -S . -B build -DSDL_TESTS=ON
56```
57and then building normally. The test programs will be built and can be run from `build/test/`.
58
59## Building SDL examples
60
61You can build the SDL example programs by adding `-DSDL_EXAMPLES=ON` to the first cmake command above:
62```sh
63cmake -S . -B build -DSDL_EXAMPLES=ON
64```
65and then building normally. The example programs will be built and can be run from `build/examples/`.
66
67## Including SDL in your project
68
69SDL can be included in your project in 2 major ways:
70- using a system SDL library, provided by your (UNIX) distribution or a package manager
71- using a vendored SDL library: this is SDL copied or symlinked in a subfolder.
72
73The following CMake script supports both, depending on the value of `MYGAME_VENDORED`.
74
75```cmake
76cmake_minimum_required(VERSION 3.5)
77project(mygame)
78
79# Create an option to switch between a system sdl library and a vendored SDL library
80option(MYGAME_VENDORED "Use vendored libraries" OFF)
81
82if(MYGAME_VENDORED)
83 # This assumes you have added SDL as a submodule in vendored/SDL
84 add_subdirectory(vendored/SDL EXCLUDE_FROM_ALL)
85else()
86 # 1. Look for a SDL3 package,
87 # 2. look for the SDL3-shared component, and
88 # 3. fail if the shared component cannot be found.
89 find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3-shared)
90endif()
91
92# Create your game executable target as usual
93add_executable(mygame WIN32 mygame.c)
94
95# Link to the actual SDL3 library.
96target_link_libraries(mygame PRIVATE SDL3::SDL3)
97```
98
99### A system SDL library
100
101For 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).
102
103The following components are available, to be used as an argument of `find_package`.
104
105| Component name | Description |
106|----------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------|
107| SDL3-shared | The SDL3 shared library, available through the `SDL3::SDL3-shared` target |
108| SDL3-static | The SDL3 static library, available through the `SDL3::SDL3-static` target |
109| SDL3_test | The SDL3_test static library, available through the `SDL3::SDL3_test` target |
110| SDL3 | The SDL3 library, available through the `SDL3::SDL3` target. This is an alias of `SDL3::SDL3-shared` or `SDL3::SDL3-static`. This component is always available. |
111| Headers | The SDL3 headers, available through the `SDL3::Headers` target. This component is always available. |
112
113SDL's CMake support guarantees a `SDL3::SDL3` target.
114Neither `SDL3::SDL3-shared` nor `SDL3::SDL3-static` are guaranteed to exist.
115
116### Using a vendored SDL
117
118This only requires a copy of SDL in a subdirectory + `add_subdirectory`.
119Alternatively, use [FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html).
120Depending on the configuration, the same targets as a system SDL package are available.
121
122## CMake configuration options
123
124### Build optimized library
125
126By default, CMake provides 4 build types: `Debug`, `Release`, `RelWithDebInfo` and `MinSizeRel`.
127The main difference(s) between these are the optimization options and the generation of debug info.
128To configure SDL as an optimized `Release` library, configure SDL with:
129```sh
130cmake ~/SDL -DCMAKE_BUILD_TYPE=Release
131```
132To build it, run:
133```sh
134cmake --build . --config Release
135```
136
137### Shared or static
138
139By default, only a dynamic (=shared) SDL library is built and installed.
140The options `-DSDL_SHARED=` and `-DSDL_STATIC=` accept boolean values to change this.
141
142Exceptions exist:
143- some platforms don't support dynamic libraries, so only `-DSDL_STATIC=ON` makes sense.
144- a static Apple framework is not supported
145
146### Man pages
147
148Configuring with `-DSDL_INSTALL_DOCS=TRUE` installs man pages.
149
150We recommend package managers of unix distributions to install SDL3's man pages.
151This adds an extra build-time dependency on Perl.
152
153### Pass custom compile options to the compiler
154
155- Use [`CMAKE_<LANG>_FLAGS`](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_FLAGS.html) to pass extra
156flags to the compiler.
157- Use [`CMAKE_EXE_LINKER_FLAGS`](https://cmake.org/cmake/help/latest/variable/CMAKE_EXE_LINKER_FLAGS.html) to pass extra option to the linker for executables.
158- Use [`CMAKE_SHARED_LINKER_FLAGS`](https://cmake.org/cmake/help/latest/variable/CMAKE_SHARED_LINKER_FLAGS.html) to pass extra options to the linker for shared libraries.
159
160#### Examples
161
162- build a SDL library optimized for (more) modern x64 microprocessor architectures.
163
164 With gcc or clang:
165 ```sh
166 cmake ~/sdl -DCMAKE_C_FLAGS="-march=x86-64-v3" -DCMAKE_CXX_FLAGS="-march=x86-64-v3"
167 ```
168 With Visual C:
169 ```sh
170 cmake .. -DCMAKE_C_FLAGS="/ARCH:AVX2" -DCMAKE_CXX_FLAGS="/ARCH:AVX2"
171 ```
172
173### Apple
174
175CMake documentation for cross building for Apple:
176[link](https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html#cross-compiling-for-ios-tvos-visionos-or-watchos)
177
178#### iOS/tvOS/visionOS
179
180CMake 3.14+ natively includes support for iOS, tvOS and watchOS. visionOS requires CMake 3.28+.
181SDL binaries may be built using Xcode or Make, possibly among other build-systems.
182
183When using a compatible version of CMake, it should be possible to:
184
185- build SDL dylibs, both static and dynamic dylibs
186- build SDL frameworks, only shared
187- build SDL test apps
188
189#### Frameworks
190
191Configure with `-DSDL_FRAMEWORK=ON` to build a SDL framework instead of a dylib shared library.
192Only shared frameworks are supported, no static ones.
193
194#### Platforms
195
196Use `-DCMAKE_SYSTEM_NAME=<value>` to configure the platform. CMake can target only one platform at a time.
197
198| Apple platform | `CMAKE_SYSTEM_NAME` value |
199|-----------------|---------------------------|
200| macOS (MacOS X) | `Darwin` |
201| iOS | `iOS` |
202| tvOS | `tvOS` |
203| visionOS | `visionOS` |
204| watchOS | `watchOS` |
205
206#### Universal binaries
207
208A universal binaries, can be built by configuring CMake with
209`-DCMAKE_OSX_ARCHITECTURES=<semicolon-separated list of CPU architectures>`.
210
211For example `-DCMAKE_OSX_ARCHITECTURES="arm64;x86_64"` will build binaries that run on both Intel cpus and Apple silicon.
212
213SDL supports following Apple architectures:
214
215| Platform | `CMAKE_OSX_ARCHITECTURES` value |
216|----------------------------|---------------------------------|
217| 64-bit ARM (Apple Silicon) | `arm64` |
218| x86_64 | `x86_64` |
219| 32-bit ARM | `armv7s` |
220
221CMake documentation: [link](https://cmake.org/cmake/help/latest/variable/CMAKE_OSX_ARCHITECTURES.html)
222
223#### Simulators and/or non-default macOS platform SDK
224
225Use `-DCMAKE_OSX_SYSROOT=<value>` to configure a different platform SDK.
226The value can be either the name of the SDK, or a full path to the sdk (e.g. `/full/path/to/iPhoneOS.sdk`).
227
228| SDK | `CMAKE_OSX_SYSROOT` value |
229|----------------------|---------------------------|
230| iphone | `iphoneos` |
231| iphonesimulator | `iphonesimulator` |
232| appleTV | `appletvos` |
233| appleTV simulator | `appletvsimulator` |
234| visionOS | `xr` |
235| visionOS simulator | `xrsimulator` |
236| watchOS | `watchos` |
237| watchOS simulator | `watchsimulator` |
238
239Append with a version number to target a specific SDK revision: e.g. `iphoneos12.4`, `appletvos12.4`.
240
241CMake documentation: [link](https://cmake.org/cmake/help/latest/variable/CMAKE_OSX_SYSROOT.html)
242
243#### Examples
244
245- for macOS, building a dylib and/or static library for x86_64 and arm64:
246
247 ```bash
248 cmake ~/sdl -DCMAKE_SYSTEM_NAME=Darwin -DCMAKE_OSX_ARCHITECTURES="x86_64;arm64" -DCMAKE_OSX_DEPLOYMENT_TARGET=10.11
249
250- for macOS, building an universal framework for x86_64 and arm64:
251
252 ```bash
253 cmake ~/sdl -DSDL_FRAMEWORK=ON -DCMAKE_SYSTEM_NAME=Darwin -DCMAKE_OSX_ARCHITECTURES="x86_64;arm64" -DCMAKE_OSX_DEPLOYMENT_TARGET=10.11
254
255- for iOS-Simulator, using the latest, installed SDK:
256
257 ```bash
258 cmake ~/sdl -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_SYSROOT=iphonesimulator -DCMAKE_OSX_ARCHITECTURES=x86_64 -DCMAKE_OSX_DEPLOYMENT_TARGET=9.0
259 ```
260
261- for iOS-Device, using the latest, installed SDK, 64-bit only
262
263 ```bash
264 cmake ~/sdl -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_SYSROOT=iphoneos -DCMAKE_OSX_ARCHITECTURES=arm64 -DCMAKE_OSX_DEPLOYMENT_TARGET=9.0
265 ```
266
267- for iOS-Device, using the latest, installed SDK, mixed 32/64 bit
268
269 ```cmake
270 cmake ~/sdl -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_SYSROOT=iphoneos -DCMAKE_OSX_ARCHITECTURES="arm64;armv7s" -DCMAKE_OSX_DEPLOYMENT_TARGET=9.0
271 ```
272
273- for iOS-Device, using a specific SDK revision (iOS 12.4, in this example):
274
275 ```cmake
276 cmake ~/sdl -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_SYSROOT=iphoneos12.4 -DCMAKE_OSX_ARCHITECTURES=arm64
277 ```
278
279- for iOS-Simulator, using the latest, installed SDK, and building SDL test apps (as .app bundles):
280
281 ```cmake
282 cmake ~/sdl -DSDL_TESTS=1 -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_SYSROOT=iphonesimulator -DCMAKE_OSX_ARCHITECTURES=x86_64 -DCMAKE_OSX_DEPLOYMENT_TARGET=9.0
283 ```
284
285- for tvOS-Simulator, using the latest, installed SDK:
286
287 ```cmake
288 cmake ~/sdl -DCMAKE_SYSTEM_NAME=tvOS -DCMAKE_OSX_SYSROOT=appletvsimulator -DCMAKE_OSX_ARCHITECTURES=x86_64 -DCMAKE_OSX_DEPLOYMENT_TARGET=9.0
289 ```
290
291- for tvOS-Device, using the latest, installed SDK:
292
293 ```cmake
294 cmake ~/sdl -DCMAKE_SYSTEM_NAME=tvOS -DCMAKE_OSX_SYSROOT=appletvos -DCMAKE_OSX_ARCHITECTURES=arm64` -DCMAKE_OSX_DEPLOYMENT_TARGET=9.0
295 ```
296
297- for QNX/aarch64, using the latest, installed SDK:
298
299 ```cmake
300 cmake ~/sdl -DCMAKE_TOOLCHAIN_FILE=~/sdl/build-scripts/cmake-toolchain-qnx-aarch64le.cmake -DSDL_X11=0
301 ```
302
303## SDL-specific CMake options
304
305SDL can be customized through (platform-specific) CMake options.
306The following table shows generic options that are available for most platforms.
307At the end of SDL CMake configuration, a table shows all CMake options along with its detected value.
308
309| CMake option | Valid values | Description |
310|-------------------------------|--------------|-----------------------------------------------------------------------------------------------------|
311| `-DSDL_SHARED=` | `ON`/`OFF` | Build SDL shared library (not all platforms support this) (`libSDL3.so`/`libSDL3.dylib`/`SDL3.dll`) |
312| `-DSDL_STATIC=` | `ON`/`OFF` | Build SDL static library (`libSDL3.a`/`SDL3-static.lib`) |
313| `-DSDL_TEST_LIBRARY=` | `ON`/`OFF` | Build SDL test library (`libSDL3_test.a`/`SDL3_test.lib`) |
314| `-DSDL_TESTS=` | `ON`/`OFF` | Build SDL test programs (**requires `-DSDL_TEST_LIBRARY=ON`**) |
315| `-DSDL_DISABLE_INSTALL=` | `ON`/`OFF` | Don't create a SDL install target |
316| `-DSDL_DISABLE_INSTALL_DOCS=` | `ON`/`OFF` | Don't install the SDL documentation |
317| `-DSDL_INSTALL_TESTS=` | `ON`/`OFF` | Install the SDL test programs |
318
319### Incompatibilities
320
321#### `SDL_LIBC=OFF` and sanitizers
322
323Building with `-DSDL_LIBC=OFF` will make it impossible to use the sanitizer, such as the address sanitizer.
324Configure your project with `-DSDL_LIBC=ON` to make use of sanitizers.
325
326## CMake FAQ
327
328### CMake fails to build without X11 or Wayland support
329
330Install the required system packages prior to running CMake.
331See [README-linux](linux#build-dependencies) for the list of dependencies on Linux.
332Other unix operationg systems should provide similar packages.
333
334If you **really** don't need to show windows, add `-DSDL_UNIX_CONSOLE_BUILD=ON` to the CMake configure command.
335
336### How do I copy a SDL3 dynamic library to another location?
337
338Use [CMake generator expressions](https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html#target-dependent-expressions).
339Generator expressions support multiple configurations, and are evaluated during build system generation time.
340
341On Windows, the following example copies `SDL3.dll` to the directory where `mygame.exe` is built.
342```cmake
343if(WIN32)
344 add_custom_command(
345 TARGET mygame POST_BUILD
346 COMMAND "${CMAKE_COMMAND}" -E copy $<TARGET_FILE:SDL3::SDL3-shared> $<TARGET_FILE_DIR:mygame>
347 VERBATIM
348 )
349endif()
350```
351On Unix systems, `$<TARGET_FILE:...>` will refer to the dynamic library (or framework),
352and you might need to use `$<TARGET_SONAME_FILE:tgt>` instead.
353
354Most often, you can avoid copying libraries by configuring your project with absolute [`CMAKE_LIBRARY_OUTPUT_DIRECTORY`](https://cmake.org/cmake/help/latest/variable/CMAKE_LIBRARY_OUTPUT_DIRECTORY.html)
355and [`CMAKE_RUNTIME_OUTPUT_DIRECTORY`](https://cmake.org/cmake/help/latest/variable/CMAKE_RUNTIME_OUTPUT_DIRECTORY.html) paths.
356When using a multi-config generator (such as Visual Studio or Ninja Multi-Config), eventually add `/$<CONFIG>` to both paths.
357
358### Linking against a static SDL library fails due to relocation errors
359
360On unix platforms, all code that ends up in shared libraries needs to be built as relocatable (=position independent) code.
361However, by default CMake builds static libraries as non-relocatable.
362Configuring SDL with `-DCMAKE_POSITION_INDEPENDENT_CODE=ON` will result in a static `libSDL3.a` library
363which you can link against to create a shared library.
364