Skip to content

Introduce a way to add a 100% external custom platform / CMake #3596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 9 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
cmake_minimum_required(VERSION 3.0)

# Directory for easier includes
# Anywhere you see include(...) you can check <root>/cmake for that file
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

include(CustomPlatform)

custom_platform_include_if_exists("raylibPlatformCustomProjectConfig")

project(raylib)

# Avoid excessive expansion of variables in conditionals. In particular, if
@@ -18,9 +27,6 @@ cmake_policy(SET CMP0054 NEW)
# See https://cmake.org/cmake/help/latest/policy/CMP0063.html
cmake_policy(SET CMP0063 NEW)

# Directory for easier includes
# Anywhere you see include(...) you can check <root>/cmake for that file
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

# RAYLIB_IS_MAIN determines whether the project is being used from root
# or if it is added as a dependency (through add_subdirectory for example).
2 changes: 1 addition & 1 deletion CMakeOptions.txt
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
include(CMakeDependentOption)
include(EnumOption)

enum_option(PLATFORM "Desktop;Web;Android;Raspberry Pi;DRM" "Platform to build for.")
enum_option(PLATFORM "Desktop;Web;Android;Raspberry Pi;DRM;Custom" "Platform to build for.")

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

2 changes: 1 addition & 1 deletion cmake/BuildOptions.cmake
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ endif()
# This helps support the case where emsdk toolchain file is used
# either by setting it with -DCMAKE_TOOLCHAIN_FILE=<path_to_emsdk>/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake
# or by using "emcmake cmake -B build -S ." as described in https://emscripten.org/docs/compiling/Building-Projects.html
if(EMSCRIPTEN)
if(EMSCRIPTEN AND NOT ${PLATFORM} MATCHES "Custom")
SET(PLATFORM Web CACHE STRING "Forcing PLATFORM_WEB because EMSCRIPTEN was detected")
endif()

9 changes: 9 additions & 0 deletions cmake/CustomPlatform.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
macro(custom_platform_include_if_exists file)
if("${PLATFORM}" MATCHES "Custom")
if (EXISTS "${PLATFORM_CUSTOM_ROOT_DIR}/cmake/${file}.cmake")
include("${PLATFORM_CUSTOM_ROOT_DIR}/cmake/${file}.cmake")
else ()
message(STATUS "No file ${PLATFORM_CUSTOM_ROOT_DIR}/cmake/${file}.cmake detected... skipped")
endif ()
endif()
endmacro()
2 changes: 2 additions & 0 deletions cmake/LibraryConfigurations.cmake
Original file line number Diff line number Diff line change
@@ -91,6 +91,8 @@ elseif ("${PLATFORM}" MATCHES "DRM")
endif ()
set(LIBS_PRIVATE ${GLESV2} ${EGL} ${DRM} ${GBM} atomic pthread m dl)

elseif ("${PLATFORM}" MATCHES "Custom")
set(PLATFORM_CPP "PLATFORM_CUSTOM")
endif ()

if (NOT ${OPENGL_VERSION} MATCHES "OFF")
11 changes: 10 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ set(API_VERSION 450)

include(GNUInstallDirs)
include(JoinPaths)
include(CustomPlatform)

# Sets build type if not set by now
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
@@ -38,7 +39,7 @@ set(raylib_sources
)

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

@@ -47,6 +48,8 @@ endif ()
# Produces a variable LIBS_PRIVATE that will be used later
include(LibraryConfigurations)

custom_platform_include_if_exists("raylibPlatformCustomLibraryConfig")

if (USE_AUDIO)
MESSAGE(STATUS "Audio Backend: miniaudio")
list(APPEND raylib_sources raudio.c)
@@ -105,6 +108,12 @@ target_include_directories(raylib
${OPENAL_INCLUDE_DIR}
)

if("${PLATFORM}" MATCHES "Custom")
target_include_directories(raylib PUBLIC "${PLATFORM_CUSTOM_ROOT_DIR}/src")
endif()

custom_platform_include_if_exists("raylibPlatformCustomTargetConfig")

# Copy the header files to the build directory for convenience
file(COPY ${raylib_public_headers} DESTINATION "include")

12 changes: 10 additions & 2 deletions src/rcore.c
Original file line number Diff line number Diff line change
@@ -235,7 +235,11 @@ __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int cp, unsigne
#define FLAG_TOGGLE(n, f) ((n) ^= (f))
#define FLAG_CHECK(n, f) ((n) & (f))

#if (defined(__linux__) || defined(PLATFORM_WEB)) && (_POSIX_C_SOURCE < 199309L)
#if defined(PLATFORM_CUSTOM)
#include "raylib_platform_custom.h"
#endif

#if (defined(__linux__) || defined(PLATFORM_WEB) || defined(PLATFORM_CUSTOM_CLOCK_MONOTONIC)) && (_POSIX_C_SOURCE < 199309L)
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 199309L // Required for: CLOCK_MONOTONIC if compiled with c99 without gnu ext.
#endif
@@ -491,6 +495,8 @@ const char *TextFormat(const char *text, ...); // Formatting of tex
#include "platforms/rcore_drm.c"
#elif defined(PLATFORM_ANDROID)
#include "platforms/rcore_android.c"
#elif defined(PLATFORM_CUSTOM)
#include "raylib_platform_custom.c"
#else
// TODO: Include your custom platform backend!
// i.e software rendering backend or console backend!
@@ -559,6 +565,8 @@ void InitWindow(int width, int height, const char *title)
TRACELOG(LOG_INFO, "Platform backend: NATIVE DRM");
#elif defined(PLATFORM_ANDROID)
TRACELOG(LOG_INFO, "Platform backend: ANDROID");
#elif defined(PLATFORM_CUSTOM)
TRACELOG(LOG_INFO, PLATFORM_CUSTOM_BACKEND);
#else
// TODO: Include your custom platform backend!
// i.e software rendering backend or console backend!
@@ -3046,7 +3054,7 @@ void InitTimer(void)
// However, it can also reduce overall system performance, because the thread scheduler switches tasks more often.
// High resolutions can also prevent the CPU power management system from entering power-saving modes.
// Setting a higher resolution does not improve the accuracy of the high-resolution performance counter.
#if defined(_WIN32) && defined(SUPPORT_WINMM_HIGHRES_TIMER) && !defined(SUPPORT_BUSY_WAIT_LOOP) && !defined(PLATFORM_DESKTOP_SDL)
#if defined(_WIN32) && defined(SUPPORT_WINMM_HIGHRES_TIMER) && !defined(SUPPORT_BUSY_WAIT_LOOP) && !defined(PLATFORM_DESKTOP_SDL) && !defined(PLATFORM_CUSTOM_DISABLE_HI_RES_TIMER)
timeBeginPeriod(1); // Setup high-resolution timer to 1ms (granularity of 1-2 ms)
#endif

8 changes: 6 additions & 2 deletions src/rlgl.h
Original file line number Diff line number Diff line change
@@ -324,6 +324,10 @@
//----------------------------------------------------------------------------------
// Types and Structures Definition
//----------------------------------------------------------------------------------
#if defined(PLATFORM_CUSTOM)
#include "raylib_platform_custom.h"
#endif

#if (defined(__STDC__) && __STDC_VERSION__ >= 199901L) || (defined(_MSC_VER) && _MSC_VER >= 1800)
#include <stdbool.h>
#elif !defined(__cplusplus) && !defined(bool) && !defined(RL_BOOL_TYPE)
@@ -818,7 +822,7 @@ RLAPI void rlLoadDrawQuad(void); // Load and draw a quad
#elif defined(GRAPHICS_API_OPENGL_ES2)
// NOTE: OpenGL ES 2.0 can be enabled on PLATFORM_DESKTOP,
// in that case, functions are loaded from a custom glad for OpenGL ES 2.0
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_DESKTOP_SDL)
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_DESKTOP_SDL) || defined(PLATFORM_CUSTOM_ENABLE_OPENGL_ES2)
#define GLAD_GLES2_IMPLEMENTATION
#include "external/glad_gles2.h"
#else
@@ -2281,7 +2285,7 @@ void rlLoadExtensions(void *loader)

#elif defined(GRAPHICS_API_OPENGL_ES2)

#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_DESKTOP_SDL)
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_DESKTOP_SDL) || defined(PLATFORM_CUSTOM_ENABLE_OPENGL_ES2)
// TODO: Support GLAD loader for OpenGL ES 3.0
if (gladLoadGLES2((GLADloadfunc)loader) == 0) TRACELOG(RL_LOG_WARNING, "GLAD: Cannot load OpenGL ES2.0 functions");
else TRACELOG(RL_LOG_INFO, "GLAD: OpenGL ES 2.0 loaded successfully");