Skip to content

Commit

Permalink
Added method to let CMake find raylib without a raylib-config.cmake file
Browse files Browse the repository at this point in the history
  • Loading branch information
texus committed Sep 29, 2024
1 parent 167502a commit 8d92b0b
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 28 deletions.
20 changes: 4 additions & 16 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -569,28 +569,15 @@ jobs:
C:\msys64\usr\bin\wget.exe -nv https://github.com/libsdl-org/SDL/releases/download/release-${env:SDL_VERSION}/SDL2-devel-${env:SDL_VERSION}-VC.zip
C:\msys64\usr\bin\wget.exe -nv https://github.com/libsdl-org/SDL_ttf/releases/download/release-${env:SDL_TTF_VERSION}/SDL2_ttf-devel-${env:SDL_TTF_VERSION}-VC.zip
C:\msys64\usr\bin\wget.exe -nv https://github.com/glfw/glfw/releases/download/${env:GLFW_VERSION}/glfw-${env:GLFW_VERSION}.bin.WIN32.zip
C:\msys64\usr\bin\wget.exe -nv https://github.com/raysan5/raylib/releases/download/${env:RAYLIB_VERSION}/raylib-${env:RAYLIB_VERSION}_win32_msvc16.zip
C:\msys64\usr\bin\wget.exe -nv https://github.com/ubawurinna/freetype-windows-binaries/archive/refs/tags/v${env:FREETYPE_VERSION}.zip
7z x SFML-${env:SFML_VERSION}-windows-vc17-32-bit.zip
7z x SDL2-devel-${env:SDL_VERSION}-VC.zip
7z x SDL2_ttf-devel-${env:SDL_TTF_VERSION}-VC.zip
7z x glfw-${env:GLFW_VERSION}.bin.WIN32.zip
7z x raylib-${env:RAYLIB_VERSION}_win32_msvc16.zip
7z x v${env:FREETYPE_VERSION}.zip
- name: Cache raylib
uses: actions/cache@v4
id: cache-raylib
with:
path: RAYLIB_INSTALL
key: CACHE_WINDOWS_RAYLIB_${{env.RAYLIB_VERSION}}

- name: Build raylib
if: steps.cache-raylib.outputs.cache-hit != 'true'
run: |
C:\msys64\usr\bin\wget.exe -nv "https://github.com/raysan5/raylib/archive/refs/tags/${env:RAYLIB_VERSION}.zip"
7z x "${env:RAYLIB_VERSION}.zip"
cmake -DCMAKE_INSTALL_PREFIX=RAYLIB_INSTALL -DBUILD_SHARED_LIBS=ON -DBUILD_EXAMPLES=OFF -S raylib-${env:RAYLIB_VERSION} -B raylib-build -T v${env:MSVC_TOOLSET_VERSION} -A Win32
cmake --build raylib-build --config Release --target install
- name: Build TGUI
run: >
cmake -B TGUI-build -T v${env:MSVC_TOOLSET_VERSION} -A Win32
Expand All @@ -599,8 +586,9 @@ jobs:
-DSDL2_ttf_DIR="${env:GITHUB_WORKSPACE}/SDL2_ttf-${env:SDL_TTF_VERSION}/cmake/"
-DGLFW_INCLUDE_DIR="${env:GITHUB_WORKSPACE}/glfw-${env:GLFW_VERSION}.bin.WIN32/include"
-DGLFW_LIBRARY="${env:GITHUB_WORKSPACE}/glfw-${env:GLFW_VERSION}.bin.WIN32/lib-vc2022/glfw3dll.lib"
-Draylib_INCLUDE_DIR="${env:GITHUB_WORKSPACE}/raylib-${env:RAYLIB_VERSION}_win32_msvc16/include"
-Draylib_LIBRARY="${env:GITHUB_WORKSPACE}/raylib-${env:RAYLIB_VERSION}_win32_msvc16/lib/raylibdll.lib"
-DFREETYPE_WINDOWS_BINARIES_PATH="${env:GITHUB_WORKSPACE}/freetype-windows-binaries-${env:FREETYPE_VERSION}"
-Draylib_ROOT="${env:GITHUB_WORKSPACE}/RAYLIB_INSTALL/"
-DCMAKE_INSTALL_PREFIX=TGUI-build/install
-DCMAKE_UNITY_BUILD=OFF
-DBUILD_SHARED_LIBS=ON
Expand Down
57 changes: 47 additions & 10 deletions cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ macro(tgui_add_dependency_sdl)
endmacro()


# Check if we can find GLFW with a config file, or whether we need to use our FindGLFW3.cmake file
# Check if we can find GLFW with a config file, or whether we need to use our Findglfw3.cmake file
function(tgui_try_find_glfw_config)
find_package(glfw3 CONFIG QUIET)

Expand All @@ -344,14 +344,12 @@ macro(tgui_add_dependency_glfw)
tgui_try_find_glfw_config()

if(TGUI_FOUND_GLFW_CONFIG)
find_package(glfw3 CONFIG REQUIRED)
# In case we used our find module earlier, we will remove our custom GLFW_INCLUDE_DIR and GLFW_LIBRARY variables to avoid confusion as they are no longer used
unset(GLFW_INCLUDE_DIR CACHE)
unset(GLFW_LIBRARY CACHE)

if(NOT TGUI_OS_LINUX)
# In case we used our find module earlier, we will remove our custom GLFW_INCLUDE_DIR and GLFW_LIBRARY variables to avoid confusion as they are no longer used
unset(GLFW_INCLUDE_DIR CACHE)
unset(GLFW_LIBRARY CACHE)
endif()
else() # If it fails then use the FindGLFW3.cmake file that ships with TGUI
find_package(glfw3 CONFIG REQUIRED)
else() # If it fails then use the Findglfw3.cmake file that ships with TGUI
find_package(glfw3)

if(NOT glfw3_FOUND)
Expand Down Expand Up @@ -594,16 +592,55 @@ macro(tgui_add_dependency_freetype)
endmacro()


# Find OpenGL and add it as a dependency
# Check if we can find raylib with a config file, or whether we need to use our Findraylib.cmake file
function(tgui_try_find_raylib_config)
find_package(raylib CONFIG QUIET)

if(raylib_FOUND AND TARGET raylib)
set(TGUI_FOUND_RAYLIB_CONFIG TRUE PARENT_SCOPE)
else()
set(TGUI_FOUND_RAYLIB_CONFIG FALSE PARENT_SCOPE)
endif()
endfunction()

# Find raylib and add it as a dependency
macro(tgui_add_dependency_raylib)
if(NOT TARGET raylib)
find_package(raylib CONFIG REQUIRED)
# First try looking for an raylib config file
tgui_try_find_raylib_config()

if(TGUI_FOUND_RAYLIB_CONFIG)
find_package(raylib CONFIG REQUIRED)
else() # If it fails then use the Findraylib.cmake file that ships with TGUI
find_package(raylib)

if(NOT raylib_FOUND)
message(FATAL_ERROR
"CMake couldn't find raylib.\n"
"If raylib was build with CMake then set the raylib_DIR variable to the directory containing raylib-config.cmake (i.e. RAYLIB_ROOT/lib/cmake/raylib)\n"
"Alternatively you can manually set raylib_INCLUDE_DIR to the 'include' directory and raylib_LIBRARY to the correct library file. You are responsible for making sure the selected library is compatible.\n")
endif()

# Remove the empty raylib_DIR variable if we found raylib via the alternative way
if(NOT raylib_DIR)
unset(raylib_DIR CACHE)
endif()
endif()

if (raylib_VERSION VERSION_LESS "4")
message(FATAL_ERROR "raylib 4 or higher is required")
endif()
endif()

if(TGUI_SHARED_LIBS)
get_target_property(raylib_target_type raylib TYPE)
if (raylib_target_type STREQUAL "STATIC_LIBRARY")
# The user has to link raylib in his own program, which would conflict with the one already inside the TGUI dll.
# Note that we might not always detect this, raylib_target_type could be unknown, e.g. when manually specifying the library via raylib_LIBRARY.
message(FATAL_ERROR "Linking statically to raylib isn't allowed when linking TGUI dynamically. Either set TGUI_SHARED_LIBS to FALSE to link TGUI statically or use a dynamic raylib library.")
endif()
endif()

target_link_libraries(tgui PUBLIC raylib)
endmacro()

Expand Down
2 changes: 1 addition & 1 deletion cmake/Macros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ function(copy_dlls_to_exe post_build_destination install_destination target)
endif()
endif()

# TODO: SFML, SDL, SDL_ttf and GLFW
# TODO: SFML, SDL, SDL_ttf, GLFW and raylib

# Previously we were just listing the files to copy, now we will actually give the commands for the copying.
# We are merely setting triggers here, the actual copying only happens after building or when installing.
Expand Down
76 changes: 76 additions & 0 deletions cmake/Modules/Findraylib.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
####################################################################################################
# TGUI - Texus' Graphical User Interface
# Copyright (C) 2012-2024 Bruno Van de Velde (vdv_b@tgui.eu)
#
# This software is provided 'as-is', without any express or implied warranty.
# In no event will the authors be held liable for any damages arising from the use of this software.
#
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it freely,
# subject to the following restrictions:
#
# 1. The origin of this software must not be misrepresented;
# you must not claim that you wrote the original software.
# If you use this software in a product, an acknowledgment
# in the product documentation would be appreciated but is not required.
#
# 2. Altered source versions must be plainly marked as such,
# and must not be misrepresented as being the original software.
#
# 3. This notice may not be removed or altered from any source distribution.
####################################################################################################

#[=======================================================================[.rst:
Findraylib
----------
Defines an import target "raylib" based on the raylib_INCLUDE_DIR and raylib_LIBRARY variables.
The raylib_VERSION variable will be set to the found raylib version.
TGUI first searches for a config file in raylib_DIR, which is the recommended way to find raylib.
If this fails then this find script is used so that you can specify the include directory and library manually.
This script is intended for when you download the precompiled raylib release which lacks a cmake config file.
#]=======================================================================]

set(raylib_INCLUDE_DIR "" CACHE PATH "Include directory of raylib")
set(raylib_LIBRARY "" CACHE FILEPATH "raylib library to link to")

# Read raylib version
if(raylib_INCLUDE_DIR AND EXISTS "${raylib_INCLUDE_DIR}/raylib.h")
file(STRINGS "${raylib_INCLUDE_DIR}/raylib.h" raylib_VERSION_MAJOR_LINE REGEX "^#define[ \t]+RAYLIB_VERSION_MAJOR[ \t]+[0-9]+$")
file(STRINGS "${raylib_INCLUDE_DIR}/raylib.h" raylib_VERSION_MINOR_LINE REGEX "^#define[ \t]+RAYLIB_VERSION_MINOR[ \t]+[0-9]+$")
file(STRINGS "${raylib_INCLUDE_DIR}/raylib.h" raylib_VERSION_PATCH_LINE REGEX "^#define[ \t]+RAYLIB_VERSION_PATCH[ \t]+[0-9]+$")
string(REGEX REPLACE "^#define[ \t]+RAYLIB_VERSION_MAJOR[ \t]+([0-9]+)$" "\\1" raylib_VERSION_MAJOR "${raylib_VERSION_MAJOR_LINE}")
string(REGEX REPLACE "^#define[ \t]+RAYLIB_VERSION_MINOR[ \t]+([0-9]+)$" "\\1" raylib_VERSION_MINOR "${raylib_VERSION_MINOR_LINE}")
string(REGEX REPLACE "^#define[ \t]+RAYLIB_VERSION_PATCH[ \t]+([0-9]+)$" "\\1" raylib_VERSION_PATCH "${raylib_VERSION_PATCH_LINE}")
set(raylib_VERSION ${raylib_VERSION_MAJOR}.${raylib_VERSION_MINOR}.${raylib_VERSION_PATCH})
unset(raylib_VERSION_MAJOR_LINE)
unset(raylib_VERSION_MINOR_LINE)
unset(raylib_VERSION_PATCH_LINE)
unset(raylib_VERSION_MAJOR)
unset(raylib_VERSION_MINOR)
unset(raylib_VERSION_PATCH)
endif()

include(FindPackageHandleStandardArgs)

FIND_PACKAGE_HANDLE_STANDARD_ARGS(raylib
REQUIRED_VARS raylib_INCLUDE_DIR raylib_LIBRARY
VERSION_VAR raylib_VERSION)

if(raylib_FOUND)
if(raylib_LIBRARY AND NOT TARGET raylib)
add_library(raylib UNKNOWN IMPORTED)
set_target_properties(raylib PROPERTIES
IMPORTED_LOCATION "${raylib_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${raylib_INCLUDE_DIR}")

get_filename_component(raylib_LIBRARY_FILENAME "${raylib_LIBRARY}" NAME)
if(raylib_LIBRARY_FILENAME STREQUAL "raylibdll.lib" OR raylib_LIBRARY_FILENAME STREQUAL "rayliblibdll.a")
set_target_properties(raylib PROPERTIES
INTERFACE_COMPILE_DEFINITIONS "USE_LIBTYPE_SHARED")
endif()
endif()
endif()
6 changes: 5 additions & 1 deletion cmake/TGUIConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ endif()

# Search for raylib when a backend requires it
if(NOT TARGET raylib AND (@TGUI_HAS_WINDOW_BACKEND_RAYLIB@ OR @TGUI_HAS_FONT_BACKEND_RAYLIB@ OR @TGUI_HAS_RENDERER_BACKEND_RAYLIB@))
find_dependency(raylib CONFIG)
if(@TGUI_FOUND_RAYLIB_CONFIG@)
find_dependency(raylib CONFIG)
else()
find_dependency(raylib)
endif()
endif()

# Search for SDL when a backend requires it
Expand Down

0 comments on commit 8d92b0b

Please sign in to comment.