Skip to content

Commit

Permalink
CMake modernization
Browse files Browse the repository at this point in the history
Re-write the CMake project from scratch up to some extent using modern
CMake conventions. Tested with GLFW, OpenGL 3 and rlottie.
Available samples: benchmark, demo, lottie
  • Loading branch information
hobyst committed May 26, 2023
1 parent 39f8193 commit 76032fb
Show file tree
Hide file tree
Showing 23 changed files with 1,100 additions and 123 deletions.
121 changes: 0 additions & 121 deletions CMake/BackendFileList.cmake

This file was deleted.

139 changes: 139 additions & 0 deletions CMake/Backends.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
set(BACKEND_COMMON_HDR_FILES
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Backend.h
)

add_library(rmlui_backend_Win32_GL2 INTERFACE)
target_sources(rmlui_backend_Win32_GL2 INTERFACE
${BACKEND_COMMON_HDR_FILES}
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Platform_Win32.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Renderer_GL2.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Backend_Win32_GL2.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Platform_Win32.h
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Renderer_GL2.h
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Include_Windows.h
)
target_link_libraries(rmlui_backend_Win32_GL2 INTERFACE OpenGL::GL)

add_library(rmlui_backend_Win32_VK INTERFACE)
target_sources(rmlui_backend_Win32_VK INTERFACE
${BACKEND_COMMON_HDR_FILES}
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Platform_Win32.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Renderer_VK.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Backend_Win32_VK.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Platform_Win32.h
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Renderer_VK.h
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Vulkan/ShadersCompiledSPV.h
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Vulkan/vk_mem_alloc.h
)
target_link_libraries(rmlui_backend_Win32_VK INTERFACE Vulkan::Vulkan)

add_library(rmlui_backend_X11_GL2 INTERFACE)
target_sources(rmlui_backend_X11_GL2 INTERFACE
${BACKEND_COMMON_HDR_FILES}
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Platform_X11.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Renderer_GL2.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Backend_X11_GL2.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Platform_X11.h
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Renderer_GL2.h
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Include_Xlib.h
)

# RMLUI_CMAKE_MINIMUM_VERSION_RAISE_NOTICE:
# Once the minimum CMake version is CMake >= 3.14, "${X11_LIBRARIES}" should
# be substituted by "X11:X11" in addition to any of the other imported that might
# be required. More info:
# https://cmake.org/cmake/help/latest/module/FindX11.html
target_link_libraries(rmlui_backend_X11_GL2 INTERFACE OpenGL::GL ${X11_LIBRARIES})


add_library(rmlui_backend_SDL_GL2 INTERFACE)
target_sources(rmlui_backend_SDL_GL2 INTERFACE
${BACKEND_COMMON_HDR_FILES}
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Platform_SDL.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Renderer_GL2.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Backend_SDL_GL2.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Platform_SDL.h
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Renderer_GL2.h
)
target_link_libraries(rmlui_backend_SDL_GL2 INTERFACE OpenGL::GL SDL::SDL)

add_library(rmlui_backend_SDL_GL3 INTERFACE)
target_sources(rmlui_backend_SDL_GL3 INTERFACE
${BACKEND_COMMON_HDR_FILES}
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Platform_SDL.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Renderer_GL3.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Backend_SDL_GL3.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Platform_SDL.h
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Renderer_GL3.h
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Include_GL3.h
)
target_link_libraries(rmlui_backend_SDL_GL3 INTERFACE OpenGL::GL SDL::SDL)

add_library(rmlui_backend_SDL_VK INTERFACE)
target_sources(rmlui_backend_SDL_VK INTERFACE
${BACKEND_COMMON_HDR_FILES}
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Platform_SDL.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Renderer_VK.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Backend_SDL_VK.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Platform_SDL.h
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Renderer_VK.h
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Vulkan/ShadersCompiledSPV.h
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Vulkan/vk_mem_alloc.h
)
target_link_libraries(rmlui_backend_SDL_VK INTERFACE Vulkan::Vulkan SDL::SDL)

add_library(rmlui_backend_SDL_SDLrenderer INTERFACE)
target_sources(rmlui_backend_SDL_SDLrenderer INTERFACE
${BACKEND_COMMON_HDR_FILES}
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Renderer_SDL.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Platform_SDL.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Backend_SDL_SDLrenderer.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Platform_SDL.h
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Renderer_SDL.h
)
target_link_libraries(rmlui_backend_SDL_SDLrenderer INTERFACE SDL::SDL)

add_library(rmlui_backend_SFML_GL2 INTERFACE)
target_sources(rmlui_backend_SFML_GL2 INTERFACE
${BACKEND_COMMON_HDR_FILES}
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Platform_SFML.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Renderer_GL2.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Backend_SFML_GL2.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Platform_SFML.h
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Renderer_GL2.h
)
target_link_libraries(rmlui_backend_SFML_GL2 INTERFACE OpenGL::GL SFML::Graphics SFML::Window SFML::System)

add_library(rmlui_backend_GLFW_GL2 INTERFACE)
target_sources(rmlui_backend_GLFW_GL2 INTERFACE
${BACKEND_COMMON_HDR_FILES}
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Platform_GLFW.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Renderer_GL2.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Backend_GLFW_GL2.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Platform_GLFW.h
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Renderer_GL2.h
)
target_link_libraries(rmlui_backend_GLFW_GL2 INTERFACE OpenGL::GL glfw)

add_library(rmlui_backend_GLFW_GL3 INTERFACE)
target_sources(rmlui_backend_GLFW_GL3 INTERFACE
${BACKEND_COMMON_HDR_FILES}
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Platform_GLFW.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Renderer_GL3.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Backend_GLFW_GL3.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Platform_GLFW.h
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Renderer_GL3.h
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Include_GL3.h
)
target_link_libraries(rmlui_backend_GLFW_GL3 INTERFACE OpenGL::GL glfw)

add_library(rmlui_backend_GLFW_VK INTERFACE)
target_sources(rmlui_backend_GLFW_VK INTERFACE
${BACKEND_COMMON_HDR_FILES}
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Platform_GLFW.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Renderer_VK.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Backend_GLFW_VK.cpp
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Platform_GLFW.h
${PROJECT_SOURCE_DIR}/Backends/RmlUi_Renderer_VK.h
)
target_link_libraries(rmlui_backend_GLFW_VK INTERFACE Vulkan::Vulkan glfw)
48 changes: 48 additions & 0 deletions CMake/Dependencies.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#[[
Function to print a message to the console indicating a dependency hasn't been found
Arguments:
- friendly_name: Friendly name of the target
- target_name: Name of the CMake target the project is supposed to link against
]]
function(report_not_found_dependency friendly_name target_name)
message(FATAL_ERROR
"${friendly_name} has not been found by CMake. If you are consuming RmlUi as a subdirectory "
"inside another CMake project, please ensure that ${friendly_name} can be linked using \"${target_name}\" "
"as its target name. You can create an ALIAS target to offer an alternative name for a CMake target."
)
endfunction()

# Freetype
if(RMLUI_FONT_INTERFACE STREQUAL "freetype")
# Declaring Freetype as a soft dependency so that it doesn't error out if the package
# is declared by other means
find_package("Freetype")

# Instead of relying on the Freetype_NOTFOUND variable, we check directly for the target
if(NOT TARGET Freetype::Freetype)
report_not_found_dependency("Freetype" Freetype::Freetype)
endif()

# Warn about problematic versions of the library with MSVC
if(DEFINED FREETYPE_VERSION_STRING)
if((${FREETYPE_VERSION_STRING} VERSION_GREATER_EQUAL "2.11.0") AND (${CMAKE_CXX_COMPILER_ID} STREQUAL "MSVC"))
message(WARNING "Using Freetype 2.11.0 or greater with MSVC can cause issues.")
endif()
else()
if(${CMAKE_CXX_COMPILER_ID} STREQUAL "MSVC")
message(WARNING "Using Freetype 2.11.0 or greater with MSVC can cause issues.")
endif()
endif()
endif()

# rlottie
if(RMLUI_ENABLE_LOTTIE_PLUGIN)
# Declaring rlottie as a soft dependency so that it doesn't error out if the package
# is declared by other means
find_package("rlottie")

# Instead of relying on the rlottie_NOTFOUND variable, we check directly for the target
if(NOT TARGET rlottie::rlottie)
report_not_found_dependency("rlottie" rlottie::rlottie)
endif()
endif()
29 changes: 29 additions & 0 deletions CMake/SamplesDependencies.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#[[
Function to print a message to the console indicating a dependency hasn't been found
Arguments:
- friendly_name: Friendly name of the target
- target_name: Name of the CMake target the project is supposed to link against
]]
function(report_not_found_dependency friendly_name target_name)
message(FATAL_ERROR
"${friendly_name} has not been found by CMake. If you are consuming RmlUi as a subdirectory "
"inside another CMake project, please ensure that ${friendly_name} can be linked using \"${target_name}\" "
"as its target name. You can create an ALIAS target to offer an alternative name for a CMake target."
)
endfunction()

# GLFW
# Declaring GLFW as a soft dependency so that it doesn't error out if the package
# is declared by other means
find_package("glfw3" "3.3")

# Instead of relying on the <package_name>_NOTFOUND variable, we check directly for the target
if(NOT TARGET glfw)
report_not_found_dependency("GLFW" glfw)
endif()

# OpenGL
find_package("OpenGL")
if(NOT TARGET OpenGL::GL)
report_not_found_dependency("OpenGL" OpenGL::GL)
endif()
38 changes: 38 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Using CMake 3.10.2 as minimum to support all platforms of interest
# https://github.com/mikke89/RmlUi/issues/198#issuecomment-1246957062
cmake_minimum_required(VERSION "3.10.2")

# Define CMake project
project("RmlUi"
VERSION "6.0"
DESCRIPTION "C++ user interface package based on the HTML and CSS standards"
LANGUAGES "C" "CXX"
)

# Disable compiler-specific extensions
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_CXX_EXTENSIONS OFF)

# Declare project-specific options
# "RMLUI_" prefix is included in order to take advantage of the fact that the
# CMake GUI can group variables based on their prefix to make more clear
# which options are specific to this project
set(RMLUI_FONT_INTERFACE "freetype" CACHE STRING "Font engine to build RmlUi with. If left empty, RmlUi won't be built with any of the included font interfaces.")
option(RMLUI_ENABLE_LOTTIE_PLUGIN "Enable plugin for Lottie animations. Requires the rlottie library." OFF)
option(RMLUI_ENABLE_SVG_PLUGIN "Enable plugin for SVG images. Requires the lunasvg library." OFF)
option(RMLUI_BUILD_SAMPLES "Build samples of the library." OFF)
set(RMLUI_SAMPLES_BACKEND "GLFW_GL3" CACHE STRING "Backend to use when building the RmlUi samples. Choose one from ./CMake/BackendFileList.cmake.")

# Add custom CMake modules path for external dependencies
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake/Modules")
list(APPEND CMAKE_PREFIX_PATH "${CMAKE_CURRENT_SOURCE_DIR}/Dependencies")

# Set up external dependencies
include("CMake/Dependencies.cmake")

# Add CMake subdirectories
add_subdirectory("Source")

if(RMLUI_BUILD_SAMPLES)
add_subdirectory("Samples")
endif()
Loading

0 comments on commit 76032fb

Please sign in to comment.