Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
cmake_minimum_required(VERSION 3.22)

if (PLATFORM MATCHES "^(iOS|tvOS)$" AND NOT DEFINED CMAKE_SYSTEM_NAME AND NOT CMAKE_TOOLCHAIN_FILE)
set(CMAKE_SYSTEM_NAME "${PLATFORM}")
endif ()
if (CMAKE_SYSTEM_NAME MATCHES "^(iOS|tvOS)$" AND NOT DEFINED CMAKE_OSX_DEPLOYMENT_TARGET)
set(CMAKE_OSX_DEPLOYMENT_TARGET "17.0" CACHE STRING "Minimum Apple OS version")
endif ()

project(raylib)

# Avoid excessive expansion of variables in conditionals. In particular, if
Expand Down Expand Up @@ -37,6 +45,20 @@ endif()
# Main sources directory (the second parameter sets the output directory name to raylib)
add_subdirectory(src raylib)

if (PROJECT_IS_TOP_LEVEL AND CMAKE_HOST_APPLE AND CMAKE_SYSTEM_NAME STREQUAL "Darwin")
add_custom_target(raylib_xcframework
COMMAND ${CMAKE_COMMAND}
"-DRAYLIB_APPLE_SOURCE=${CMAKE_CURRENT_SOURCE_DIR}"
"-DRAYLIB_APPLE_OUTPUT=${CMAKE_CURRENT_BINARY_DIR}/apple/$<CONFIG>"
"-DRAYLIB_APPLE_CONFIG=$<CONFIG>"
"-DRAYLIB_APPLE_AUDIO=${SUPPORT_MODULE_RAUDIO}"
"-DRAYLIB_APPLE_MODELS=${SUPPORT_MODULE_RMODELS}"
-P "${CMAKE_CURRENT_SOURCE_DIR}/cmake/AppleXCFramework.cmake"
USES_TERMINAL
VERBATIM
)
endif ()

# Uninstall target, only create when building raylib by itself
# Avoid conflicting target names when using raylib with other libraries
if(NOT TARGET uninstall AND PROJECT_IS_TOP_LEVEL)
Expand Down
5 changes: 4 additions & 1 deletion CMakeOptions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ if(EMSCRIPTEN)
# When configuring web builds with "emcmake cmake -B build -S .", set PLATFORM to Web by default
SET(PLATFORM Web CACHE STRING "Platform to build for.")
endif()
enum_option(PLATFORM "Desktop;Win32;Web;WebRGFW;Android;Raspberry Pi;DRM;SDL;RGFW;Memory" "Platform to build for.")
if (CMAKE_SYSTEM_NAME MATCHES "^(iOS|tvOS)$" AND NOT DEFINED PLATFORM)
set(PLATFORM "${CMAKE_SYSTEM_NAME}" CACHE STRING "Platform to build for.")
endif ()
enum_option(PLATFORM "Desktop;Win32;Web;WebRGFW;Android;iOS;tvOS;Raspberry Pi;DRM;SDL;RGFW;Memory" "Platform to build for.")

enum_option(OPENGL_VERSION "OFF;4.3;3.3;2.1;1.1;ES 2.0;ES 3.0;Software" "Force a specific OpenGL Version?")

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Ready to learn? Jump to [code examples!](https://www.raylib.com/examples.html)
features
--------
- **NO external dependencies**, all required libraries are [included in raylib](https://github.com/raysan5/raylib/tree/master/src/external)
- Multiple platforms supported: **Windows, Linux, MacOS, RPI, Android, HTML5... and more!**
- Multiple platforms supported: **Windows, Linux, macOS, iOS, iPadOS, tvOS, RPI, Android, HTML5... and more!**
- Written in plain C code (C99) using PascalCase/camelCase notation
- Hardware accelerated with OpenGL: **1.1, 2.1, 3.3, 4.3, ES 2.0, ES 3.0**
- **Unique OpenGL abstraction layer** (usable as standalone module): [rlgl](https://github.com/raysan5/raylib/blob/master/src/rlgl.h)
Expand Down
52 changes: 52 additions & 0 deletions cmake/AppleXCFramework.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Build and package each Apple platform and simulator variant from the raylib sources
cmake_minimum_required(VERSION 3.22)
find_program(XCODEBUILD xcodebuild REQUIRED)

set(xcframework_args)
foreach (sdk IN ITEMS macosx iphoneos iphonesimulator appletvos appletvsimulator)
set(platform Desktop)
set(system_args)
set(deployment_target 17.0)
set(architectures arm64)
if (sdk STREQUAL "macosx")
set(deployment_target 11.0)
elseif (sdk MATCHES "^iphone")
set(platform iOS)
set(system_args -DCMAKE_SYSTEM_NAME=iOS)
else ()
set(platform tvOS)
set(system_args -DCMAKE_SYSTEM_NAME=tvOS)
endif ()
if (sdk MATCHES "simulator$" OR sdk STREQUAL "macosx")
list(APPEND architectures x86_64)
endif ()

set(build_dir "${RAYLIB_APPLE_OUTPUT}/${sdk}")
set(install_dir "${build_dir}/install")
execute_process(COMMAND "${CMAKE_COMMAND}" -G "Unix Makefiles"
-S "${RAYLIB_APPLE_SOURCE}" -B "${build_dir}"
${system_args}
"-DPLATFORM=${platform}"
"-DCMAKE_BUILD_TYPE=${RAYLIB_APPLE_CONFIG}"
"-DCMAKE_OSX_SYSROOT=${sdk}"
"-DCMAKE_OSX_DEPLOYMENT_TARGET=${deployment_target}"
"-DCMAKE_OSX_ARCHITECTURES=${architectures}"
-DCMAKE_INSTALL_LIBDIR=lib -DCMAKE_INSTALL_INCLUDEDIR=include
-DBUILD_EXAMPLES=OFF -DBUILD_SHARED_LIBS=OFF -DCUSTOMIZE_BUILD=ON
"-DSUPPORT_MODULE_RAUDIO=${RAYLIB_APPLE_AUDIO}" "-DSUPPORT_MODULE_RMODELS=${RAYLIB_APPLE_MODELS}"
COMMAND_ERROR_IS_FATAL ANY
)
execute_process(COMMAND "${CMAKE_COMMAND}" --build "${build_dir}" --target raylib COMMAND_ERROR_IS_FATAL ANY)
execute_process(COMMAND "${CMAKE_COMMAND}" --install "${build_dir}" --prefix "${install_dir}" COMMAND_ERROR_IS_FATAL ANY)
list(APPEND xcframework_args -library "${install_dir}/lib/libraylib.a" -headers "${install_dir}/include")
endforeach ()

set(staging_dir "${RAYLIB_APPLE_OUTPUT}/staging")
set(output_dir "${RAYLIB_APPLE_OUTPUT}/raylib.xcframework")
file(REMOVE_RECURSE "${staging_dir}/raylib.xcframework")
execute_process(COMMAND "${XCODEBUILD}" -create-xcframework ${xcframework_args}
-output "${staging_dir}/raylib.xcframework" COMMAND_ERROR_IS_FATAL ANY
)
file(REMOVE_RECURSE "${output_dir}")
file(RENAME "${staging_dir}/raylib.xcframework" "${output_dir}")
message(STATUS "Created ${output_dir}")
3 changes: 3 additions & 0 deletions cmake/InstallConfigurations.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ if (NOT BUILD_SHARED_LIBS)
include(LibraryPathToLinkerFlags)
set(PKG_CONFIG_LIBS_PRIVATE ${GLFW_PKG_LIBS})
string(REPLACE ";" " " PKG_CONFIG_LIBS_PRIVATE "${PKG_CONFIG_LIBS_PRIVATE}")
foreach (framework IN LISTS RAYLIB_APPLE_FRAMEWORKS)
string(APPEND PKG_CONFIG_LIBS_PRIVATE " -framework ${framework}")
endforeach ()
elseif (BUILD_SHARED_LIBS)
set(PKG_CONFIG_LIBS_EXTRA "")
endif ()
Expand Down
18 changes: 18 additions & 0 deletions cmake/LibraryConfigurations.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,24 @@ elseif (${PLATFORM} STREQUAL "Android")
set(LIBS_PRIVATE log android EGL GLESv2 OpenSLES atomic c)
set(LIBS_PUBLIC m)

elseif (PLATFORM MATCHES "^(iOS|tvOS)$")
if (NOT CMAKE_SYSTEM_NAME STREQUAL PLATFORM)
message(FATAL_ERROR "PLATFORM=${PLATFORM} requires the matching Apple SDK and CMAKE_SYSTEM_NAME.")
endif ()
if (BUILD_SHARED_LIBS)
message(FATAL_ERROR "Build raylib as a static library for ${PLATFORM}.")
endif ()
if (NOT OPENGL_VERSION MATCHES "^(OFF|ES 3\\.0)$")
message(FATAL_ERROR "${PLATFORM} uses OpenGL ES 3.0.")
endif ()

set(PLATFORM_CPP PLATFORM_APPLE_MOBILE)
set(GRAPHICS GRAPHICS_API_OPENGL_ES3)
set(RAYLIB_APPLE_FRAMEWORKS Foundation UIKit CoreGraphics QuartzCore OpenGLES GameController)
if (SUPPORT_MODULE_RAUDIO)
list(APPEND RAYLIB_APPLE_FRAMEWORKS AVFoundation AudioToolbox CoreFoundation)
endif ()

elseif ("${PLATFORM}" STREQUAL "DRM")
set(PLATFORM_CPP "PLATFORM_DRM")

Expand Down
9 changes: 9 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ if (NOT DEFINED PLATFORM)
set(PLATFORM "Desktop" CACHE STRING "Platform to build examples for")
endif ()

if (CMAKE_SYSTEM_NAME MATCHES "^(iOS|tvOS)$")
if (NOT TARGET raylib)
find_package(raylib REQUIRED)
endif ()
include("${CMAKE_CURRENT_LIST_DIR}/../projects/CMake/AppleApp.cmake")
raylib_add_apple_app(core_basic_window "${CMAKE_CURRENT_SOURCE_DIR}/core/core_basic_window.c")
return()
endif ()

# Directories that contain examples
set(example_dirs
audio
Expand Down
37 changes: 37 additions & 0 deletions projects/CMake/AppleApp.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Package an existing C game as a UIKit app
function(raylib_add_apple_app target game_source)
add_executable(${target} MACOSX_BUNDLE "${game_source}" "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/apple_entry.c")
set_property(SOURCE "${game_source}" APPEND PROPERTY COMPILE_DEFINITIONS main=RaylibGameMain)
target_link_libraries(${target} PRIVATE raylib)

if (ARGC GREATER 2 AND IS_DIRECTORY "${ARGV2}")
file(GLOB_RECURSE app_resources CONFIGURE_DEPENDS "${ARGV2}/*")
foreach (resource IN LISTS app_resources)
file(RELATIVE_PATH relative_path "${ARGV2}" "${resource}")
get_filename_component(resource_dir "${relative_path}" DIRECTORY)
# A top-level Resources directory can be mistaken for a macOS bundle layout
set_source_files_properties("${resource}" PROPERTIES MACOSX_PACKAGE_LOCATION "Resources/raylib_resources/${resource_dir}")
endforeach ()
target_sources(${target} PRIVATE ${app_resources})
endif ()

string(REPLACE "_" "-" bundle_name "${target}")
if (CMAKE_SYSTEM_NAME STREQUAL "iOS")
set(device_family "1,2")
else ()
set(device_family "3")
endif ()
set_target_properties(${target} PROPERTIES
MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/apple_info.plist"
MACOSX_BUNDLE_GUI_IDENTIFIER "org.raylib.${bundle_name}"
MACOSX_BUNDLE_BUNDLE_VERSION "1"
MACOSX_BUNDLE_SHORT_VERSION_STRING "1.0"
XCODE_ATTRIBUTE_TARGETED_DEVICE_FAMILY "${device_family}"
)
if (CMAKE_GENERATOR STREQUAL "Xcode")
set_property(TARGET ${target} PROPERTY MACOSX_BUNDLE_GUI_IDENTIFIER "$(PRODUCT_BUNDLE_IDENTIFIER)")
if (NOT DEFINED CMAKE_XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER)
set_property(TARGET ${target} PROPERTY XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER "org.raylib.${bundle_name}")
endif ()
endif ()
endfunction()
20 changes: 18 additions & 2 deletions projects/CMake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
cmake_minimum_required(VERSION 3.11) # FetchContent is available in 3.11+
if (CMAKE_SYSTEM_NAME MATCHES "^(iOS|tvOS)$")
cmake_minimum_required(VERSION 3.22)
if (NOT DEFINED CMAKE_OSX_DEPLOYMENT_TARGET)
set(CMAKE_OSX_DEPLOYMENT_TARGET "17.0" CACHE STRING "Minimum Apple OS version")
endif ()
endif ()
project(example)

# Generate compile_commands.json
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Dependencies
set(RAYLIB_VERSION 6.0)
find_package(raylib ${RAYLIB_VERSION} QUIET) # QUIET or REQUIRED
if (NOT raylib_FOUND) # If there's none, fetch and build raylib
if (CMAKE_SYSTEM_NAME MATCHES "^(iOS|tvOS)$" AND
EXISTS "${CMAKE_CURRENT_LIST_DIR}/../../src/raylib_apple.h" AND NOT DEFINED raylib_DIR)
add_subdirectory(../.. raylib)
else ()
find_package(raylib ${RAYLIB_VERSION} QUIET) # QUIET or REQUIRED
endif ()
if (NOT TARGET raylib AND NOT raylib_FOUND) # If there's none, fetch and build raylib
include(FetchContent)
FetchContent_Declare(
raylib
Expand All @@ -22,6 +33,11 @@ if (NOT raylib_FOUND) # If there's none, fetch and build raylib
endif()

# Our Project
if (CMAKE_SYSTEM_NAME MATCHES "^(iOS|tvOS)$")
include("${CMAKE_CURRENT_LIST_DIR}/AppleApp.cmake")
raylib_add_apple_app(${PROJECT_NAME} "${CMAKE_CURRENT_SOURCE_DIR}/core_basic_window.c" "${CMAKE_CURRENT_SOURCE_DIR}/resources")
return()
endif ()

add_executable(${PROJECT_NAME} core_basic_window.c)
#set(raylib_VERBOSE 1)
Expand Down
45 changes: 45 additions & 0 deletions projects/CMake/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,48 @@ cd build
emcmake cmake .. -DPLATFORM=Web -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXECUTABLE_SUFFIX=".html"
emmake make
```

### Apple platforms

Apple builds require macOS, Xcode with the target SDK, and CMake 3.22 or newer.
iOS, iPadOS, and tvOS use UIKit and OpenGL ES 3.0.

From the template directory, build the app for the iPhone or iPad simulator.

```sh
cmake -S . -B build-ios -G Xcode -DCMAKE_SYSTEM_NAME=iOS \
-DCMAKE_OSX_SYSROOT=iphonesimulator \
-DCMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED=NO
cmake --build build-ios --config Debug --target example
```

The app is at `build-ios/Debug-iphonesimulator/example.app`. Open
`build-ios/example.xcodeproj` in Xcode and select a simulator to run it.

Use a separate build directory for each SDK. For the Apple TV simulator, set
`CMAKE_SYSTEM_NAME=tvOS` and `CMAKE_OSX_SYSROOT=appletvsimulator`. For a physical
device, use `iphoneos` or `appletvos`, omit the flag that disables code signing,
and set your signing team and bundle identifier in Xcode.

The template can use a raylib checkout or an installed static library for the
same SDK. Set `raylib_DIR` to the installed CMake package directory to select
that library.

Replace the template's `core_basic_window.c` with a game using `int main(void)`.
Only that file is compiled with `main=RaylibGameMain`. The launcher calls
`RaylibRunAppleApp()` on the main thread and the game uses the normal raylib
window and drawing loop on the rendering thread. Keep graphics and input calls
on that thread and check `WindowShouldClose()` each frame.

The template bundles files placed in its `resources/` directory as
`raylib_resources/`. The launcher exposes them at `resources/` relative to
the working directory. Write saves and screenshots elsewhere in the working
directory, which is Application Support on iOS and iPadOS and the system's
purgeable Caches directory on tvOS.
`InitWindow()` sets the logical canvas size. UIKit fits it to the drawable, whose
pixel dimensions are available through `GetRenderWidth()` and `GetRenderHeight()`.
iOS touch also drives the primary mouse button. On tvOS, the Siri Remote
directions, Select and Play/Pause map to arrow keys, Enter and Space. Back/Menu
follows system navigation. Extended gamepads supply axes and buttons.
Desktop window controls, clipboard, file drops, and gamepad mappings and
vibration are unavailable on the UIKit backend.
16 changes: 16 additions & 0 deletions projects/CMake/apple_entry.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**********************************************************************************************
*
* raylib - UIKit entry point for a C game
*
* This software is provided under the zlib/libpng license. See LICENSE for details.
*
**********************************************************************************************/

#include "raylib_apple.h"

extern int RaylibGameMain(void);

int main(int argc, char **argv)
{
return RaylibRunAppleApp(argc, argv, RaylibGameMain);
}
38 changes: 38 additions & 0 deletions projects/CMake/apple_info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "https://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleExecutable</key>
<string>${MACOSX_BUNDLE_EXECUTABLE_NAME}</string>
<key>CFBundleIdentifier</key>
<string>${MACOSX_BUNDLE_GUI_IDENTIFIER}</string>
<key>CFBundleName</key>
<string>${MACOSX_BUNDLE_EXECUTABLE_NAME}</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>${MACOSX_BUNDLE_SHORT_VERSION_STRING}</string>
<key>CFBundleVersion</key>
<string>${MACOSX_BUNDLE_BUNDLE_VERSION}</string>
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
<false/>
</dict>
<key>UILaunchScreen</key>
<dict/>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>
19 changes: 18 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ if (NOT DEFINED SUPPORT_MODULE_RMODELS OR SUPPORT_MODULE_RMODELS)
endif()

# <root>/cmake/GlfwImport.cmake handles the details around the inclusion of glfw
if (NOT ${PLATFORM} MATCHES "Web")
if (NOT PLATFORM MATCHES "Web|^(iOS|tvOS)$")
include(GlfwImport)
endif ()

Expand All @@ -56,6 +56,16 @@ endif ()
# Produces a variable LIBS_PRIVATE that will be used later
include(LibraryConfigurations)

if (PLATFORM_CPP STREQUAL "PLATFORM_APPLE_MOBILE")
enable_language(OBJC)
list(APPEND raylib_sources platforms/rcore_apple.m)
list(APPEND raylib_public_headers raylib_apple.h)
set_source_files_properties(platforms/rcore_apple.m PROPERTIES COMPILE_OPTIONS "-fobjc-arc")
if (SUPPORT_MODULE_RAUDIO)
set_source_files_properties(raudio.c PROPERTIES LANGUAGE OBJC COMPILE_OPTIONS "-fno-objc-arc")
endif ()
endif ()

if (DEFINED SUPPORT_MODULE_RAUDIO AND NOT SUPPORT_MODULE_RAUDIO)
MESSAGE(STATUS "Audio Backend: None (-DCUSTOMIZE_BUILD=ON -DSUPPORT_MODULE_RAUDIO=OFF)")
else ()
Expand All @@ -64,6 +74,13 @@ endif ()

add_library(raylib ${raylib_sources} ${raylib_public_headers})

if (PLATFORM_CPP STREQUAL "PLATFORM_APPLE_MOBILE")
target_compile_definitions(raylib PRIVATE GLES_SILENCE_DEPRECATION)
foreach (framework IN LISTS RAYLIB_APPLE_FRAMEWORKS)
target_link_options(raylib INTERFACE "SHELL:-framework ${framework}")
endforeach ()
endif ()

if (NOT BUILD_SHARED_LIBS)
MESSAGE(STATUS "Building raylib static library")
add_library(raylib_static ALIAS raylib)
Expand Down
Loading