🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
5.8 KiB

  1. # CMake
  2. (www.cmake.org)
  3. SDL's build system was traditionally based on autotools. Over time, this
  4. approach has suffered from several issues across the different supported
  5. platforms.
  6. To solve these problems, a new build system based on CMake was introduced.
  7. It is developed in parallel to the legacy autotools build system, so users
  8. can experiment with it without complication.
  9. The CMake build system is supported on the following platforms:
  10. * FreeBSD
  11. * Linux
  12. * Microsoft Visual C
  13. * MinGW and Msys
  14. * macOS, iOS, and tvOS, with support for XCode
  15. * Android
  16. * Emscripten
  17. * RiscOS
  18. * Playstation Vita
  19. ## Building SDL
  20. Assuming the source for SDL is located at `~/sdl`
  21. ```sh
  22. cd ~
  23. mkdir build
  24. cd build
  25. cmake ~/sdl
  26. cmake --build .
  27. ```
  28. This will build the static and dynamic versions of SDL in the `~/build` directory.
  29. Installation can be done using:
  30. ```sh
  31. cmake --install . # '--install' requires CMake 3.15, or newer
  32. ```
  33. ## Including SDL in your project
  34. SDL can be included in your project in 2 major ways:
  35. - using a system SDL library, provided by your (*nix) distribution or a package manager
  36. - using a vendored SDL library: this is SDL copied or symlinked in a subfolder.
  37. The following CMake script supports both, depending on the value of `MYGAME_VENDORED`.
  38. ```cmake
  39. cmake_minimum_required(VERSION 3.5)
  40. project(mygame)
  41. # Create an option to switch between a system sdl library and a vendored sdl library
  42. option(MYGAME_VENDORED "Use vendored libraries" OFF)
  43. if(MYGAME_VENDORED)
  44. add_subdirectory(vendored/sdl EXCLUDE_FROM_ALL)
  45. else()
  46. # 1. Look for a SDL2 package, 2. look for the SDL2 component and 3. fail if none can be found
  47. find_package(SDL2 REQUIRED CONFIG REQUIRED COMPONENTS SDL2)
  48. # 1. Look for a SDL2 package, 2. Look for the SDL2maincomponent and 3. DO NOT fail when SDL2main is not available
  49. find_package(SDL2 REQUIRED CONFIG COMPONENTS SDL2main)
  50. endif()
  51. # Create your game executable target as usual
  52. add_executable(mygame WIN32 mygame.c)
  53. # SDL2::SDL2main may or may not be available. It is e.g. required by Windows GUI applications
  54. if(TARGET SDL2::SDL2main)
  55. # It has an implicit dependency on SDL2 functions, so it MUST be added before SDL2::SDL2 (or SDL2::SDL2-static)
  56. target_link_libraries(mygame PRIVATE SDL2::SDL2main)
  57. endif()
  58. # Link to the actual SDL2 library. SDL2::SDL2 is the shared SDL library, SDL2::SDL2-static is the static SDL libarary.
  59. target_link_libraries(mygame PRIVATE SDL2::SDL2)
  60. ```
  61. ### A system SDL library
  62. 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).
  63. The following components are available, to be used as an argument of `find_package`.
  64. | Component name | Description |
  65. |----------------|--------------------------------------------------------------------------------------------|
  66. | SDL2 | The SDL2 shared library, available through the `SDL2::SDL2` target [^SDL_TARGET_EXCEPTION] |
  67. | SDL2-static | The SDL2 static library, available through the `SDL2::SDL2-static` target |
  68. | SDL2main | The SDL2main static library, available through the `SDL2::SDL2main` target |
  69. | SDL2test | The SDL2test static library, available through the `SDL2::SDL2test` target |
  70. ### Using a vendored SDL
  71. This only requires a copy of SDL in a subdirectory.
  72. ## CMake configuration options for platforms
  73. ### iOS/tvOS
  74. CMake 3.14+ natively includes support for iOS and tvOS. SDL binaries may be built
  75. using Xcode or Make, possibly among other build-systems.
  76. When using a recent version of CMake (3.14+), it should be possible to:
  77. - build SDL for iOS, both static and dynamic
  78. - build SDL test apps (as iOS/tvOS .app bundles)
  79. - generate a working SDL_config.h for iOS (using SDL_config.h.cmake as a basis)
  80. To use, set the following CMake variables when running CMake's configuration stage:
  81. - `CMAKE_SYSTEM_NAME=<OS>` (either `iOS` or `tvOS`)
  82. - `CMAKE_OSX_SYSROOT=<SDK>` (examples: `iphoneos`, `iphonesimulator`, `iphoneos12.4`, `/full/path/to/iPhoneOS.sdk`,
  83. `appletvos`, `appletvsimulator`, `appletvos12.4`, `/full/path/to/AppleTVOS.sdk`, etc.)
  84. - `CMAKE_OSX_ARCHITECTURES=<semicolon-separated list of CPU architectures>` (example: "arm64;armv7s;x86_64")
  85. #### Examples
  86. - for iOS-Simulator, using the latest, installed SDK:
  87. ```bash
  88. cmake ~/sdl -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_SYSROOT=iphonesimulator -DCMAKE_OSX_ARCHITECTURES=x86_64
  89. ```
  90. - for iOS-Device, using the latest, installed SDK, 64-bit only
  91. ```bash
  92. cmake ~/sdl -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_SYSROOT=iphoneos -DCMAKE_OSX_ARCHITECTURES=arm64
  93. ```
  94. - for iOS-Device, using the latest, installed SDK, mixed 32/64 bit
  95. ```cmake
  96. cmake ~/sdl -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_SYSROOT=iphoneos -DCMAKE_OSX_ARCHITECTURES="arm64;armv7s"
  97. ```
  98. - for iOS-Device, using a specific SDK revision (iOS 12.4, in this example):
  99. ```cmake
  100. cmake ~/sdl -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_SYSROOT=iphoneos12.4 -DCMAKE_OSX_ARCHITECTURES=arm64
  101. ```
  102. - for iOS-Simulator, using the latest, installed SDK, and building SDL test apps (as .app bundles):
  103. ```cmake
  104. cmake ~/sdl -DSDL_TESTS=1 -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_SYSROOT=iphonesimulator -DCMAKE_OSX_ARCHITECTURES=x86_64
  105. ```
  106. - for tvOS-Simulator, using the latest, installed SDK:
  107. ```cmake
  108. cmake ~/sdl -DCMAKE_SYSTEM_NAME=tvOS -DCMAKE_OSX_SYSROOT=appletvsimulator -DCMAKE_OSX_ARCHITECTURES=x86_64
  109. ```
  110. - for tvOS-Device, using the latest, installed SDK:
  111. ```cmake
  112. cmake ~/sdl -DCMAKE_SYSTEM_NAME=tvOS -DCMAKE_OSX_SYSROOT=appletvos -DCMAKE_OSX_ARCHITECTURES=arm64`
  113. ```
  114. [^SDL_TARGET_EXCEPTION]: `SDL2::SDL2` can be an ALIAS to a static `SDL2::SDL2-static` target for multiple reasons.