Skip to content

Commit

Permalink
Use proper configuration
Browse files Browse the repository at this point in the history
- on macOS, use the proper glfw hint to instantiate the proper Angle metal backend
- on macOS, copies and process the local Google Chrome libraries as security was getting in the way
  • Loading branch information
ypujante committed Jul 25, 2023
1 parent 5344e4b commit 075555d
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 66 deletions.
82 changes: 65 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ set(target "imgui_raylib_cmake")

set(CMAKE_CXX_STANDARD 17)

#######################################################
# Option to turn on/off Google Angle on macOS
#######################################################
option(USE_GOOGLE_ANGLE_MAC_OS "Whether to use Google Angle or not on macOS." ON)

#######################################################
# adding raylib
# Note: raylib is bundled in this project but could
# easily be fetched instead
#######################################################
option(CUSTOMIZE_BUILD "Show options for customizing your Raylib library build." ON)
if (APPLE)
#option(CUSTOMIZE_BUILD "Show options for customizing your Raylib library build." ON)
if (APPLE AND USE_GOOGLE_ANGLE_MAC_OS)
set(OPENGL_VERSION "ES 2.0" CACHE STRING "Forcing ES 2.0 to use angle")
endif()
add_subdirectory("${CMAKE_SOURCE_DIR}/external/raysan5/raylib" raylib EXCLUDE_FROM_ALL)
Expand All @@ -37,21 +44,62 @@ target_link_libraries(${target} PUBLIC raylib imgui rlImGui)
if (APPLE)
set_target_properties(${target} PROPERTIES
BUNDLE True
MACOSX_BUNDLE_GUI_IDENTIFIER com.acme.imgui_raylib_cmake
MACOSX_BUNDLE_BUNDLE_NAME "ImGui RayLib CMake"
MACOSX_BUNDLE_GUI_IDENTIFIER com.acme.raylib_cmake
MACOSX_BUNDLE_BUNDLE_NAME "RayLib CMake"
MACOSX_BUNDLE_BUNDLE_VERSION "1.0.0"
MACOSX_BUNDLE_SHORT_VERSION_STRING "1.0.0"
MACOSX_BUNDLE_ICON_NAME "AppIcon"
MACOSX_BUNDLE_COPYRIGHT "© 2023 acme"
MACOSX_BUNDLE_INFO_PLIST "${CMAKE_SOURCE_DIR}/cmake/MacOSXBundleInfo.plist.in"
INSTALL_RPATH "@executable_path/../Frameworks"
)

target_link_libraries(${target} PRIVATE "-framework IOKit" "-framework Cocoa" "-framework OpenGL")

# link with google angle which implements the old/deprecated macOS OpenGL API to use metal
set(GOOGLE_ANGLE_DIR "${CMAKE_CURRENT_LIST_DIR}/external/google/angle")
list(APPEND google_angle_libs "${GOOGLE_ANGLE_DIR}/macOS/libEGL.dylib" "${GOOGLE_ANGLE_DIR}/macOS/libGLESv2.dylib")
target_link_libraries(${target} PRIVATE "${google_angle_libs}")
if (USE_GOOGLE_ANGLE_MAC_OS)
# Where are the angle libraries copied (under the build folder)
set(GOOGLE_ANGLE_DIR "${CMAKE_CURRENT_BINARY_DIR}/angle/libs")

# The code assumes Google Chrome is installed in the macOS standard location but can be overriden here
set(GOOGLE_CHROME_INSTALL_DIR "/Applications/Google Chrome.app" CACHE PATH "Path to Google Chrome")

macro(find_angle_lib name angle_libs)
# Locate the library
file(GLOB_RECURSE libs "${GOOGLE_CHROME_INSTALL_DIR}/Contents/Frameworks/*/${name}")
if(NOT libs)
message(FATAL_ERROR "Could not locate ${name}. Are you sure that Google Chrome is installed properly? You can set variable GOOGLE_CHROME_INSTALL_DIR to a different location if required.")
endif()
# There may be more than one, pick the latest one
list(GET libs -1 lib)
message(STATUS "Found ${name} under ${lib}")
set(processed_lib "${GOOGLE_ANGLE_DIR}/${name}")
# process the library
add_custom_command(
OUTPUT "${processed_lib}"
COMMAND ${CMAKE_COMMAND} -E make_directory "${GOOGLE_ANGLE_DIR}"
COMMAND ${CMAKE_COMMAND} -E copy "${lib}" "${processed_lib}" # copy the library unde the build folder
COMMAND install_name_tool -id "@rpath/${name}" "${processed_lib}" # change the rpath (note that this invalidates the signature)
COMMAND codesign -s "-" -fv "${processed_lib}" # remove the signature
DEPENDS "${lib}"
)
list(APPEND ${angle_libs} "${processed_lib}")
endmacro()

set(google_angle_libs "")
find_angle_lib(libEGL.dylib google_angle_libs) # libEGL.dylib
find_angle_lib(libGLESv2.dylib google_angle_libs) # libGLESv2.dylib
add_custom_target(process_angle_libs DEPENDS "${google_angle_libs}")
add_dependencies(${target} process_angle_libs)

set_target_properties(${target} PROPERTIES
BUILD_RPATH "${GOOGLE_ANGLE_DIR}" # for debug runs, the rpath is under the build folder
INSTALL_RPATH "@executable_path/../Frameworks" # for install, the rpath is relative to the executable (inside the macOS bundle)
)

target_link_libraries(${target} PRIVATE "${google_angle_libs}")
target_compile_definitions(${target} PRIVATE "USE_GOOGLE_ANGLE_MAC_OS")

endif()

endif()

#######################################################
Expand All @@ -65,12 +113,12 @@ install(
)

if (APPLE)
# installs the libraries under Content/Frameworks
install(
FILES "${google_angle_libs}"
DESTINATION "$<TARGET_BUNDLE_DIR_NAME:${target}>/Contents/Frameworks"
)

# TODO: if using the Xcode generator, the executable needs to be resigned...

if (USE_GOOGLE_ANGLE_MAC_OS)
# installs the libraries under Content/Frameworks
install(
FILES "${google_angle_libs}"
DESTINATION "$<TARGET_BUNDLE_DIR_NAME:${target}>/Contents/Frameworks"
)
# TODO: if using the Xcode generator, the executable needs to be resigned...
endif()
endif()
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
Introduction
------------

This is a 100% self-contained "Hello World" project for an ImGui application that uses raylib for the backend, CMake for the build and Google Angle on macOS (to use the Metal API instead of the deprecated OpenGL API)
This is a "Hello World" project for an ImGui application that uses raylib for the backend, CMake for the build and Google Angle on macOS (to use the Metal API instead of the deprecated OpenGL API)

![macOS](https://github.com/ypujante/imgui-raylib-cmake/releases/download/v1.0.0/macOS.png)
![macOS](https://github.com/ypujante/imgui-raylib-cmake/releases/download/v2.0.0/macOS.png)

Dependencies
------------

On macOS, due to the difficulty in handling security, this project no longer includes the Google Angle libraries but simply depends on Google Chrome being installed on the machine. CMake will automatically find the libraries and applies the changes necessary to make it work (using `install_name_tool` and `codesign`)

Build
-----
Expand All @@ -17,31 +22,33 @@ It is a CMake project, so it builds very simply:
> cmake --build . --target imgui_raylib_cmake
```

> #### Note
> On macOS, CMake automatically copies and modifies the Google Chrome Angle libraries to link with
Run
---

On macOS, you get a bundle that you can simply open
On Windows/Linux, you get an executable that you can simply run
* On macOS, you get a bundle that you can simply open
* On Windows/Linux, you get an executable that you can simply run

Platforms tested
---------------

* macOS (Ventura 13.4): note that OpenGL is deprecated but this project uses Google Angle to provide a very efficient OpenGL wrapper that delegates to Metal (typical 3-5x speed improvement)
* macOS (Ventura 13.4): note that OpenGL is deprecated but this project uses Google Angle to provide a very efficient OpenGL wrapper that delegates to Metal
* Windows 11 (uses native OpenGL)
* Linux Ubuntu (uses native OpenGL)

History
-------

It took me a huge amount of time to set this project up (see [discussion](https://discourse.cmake.org/t/embedding-dylib-in-a-macos-bundle/8465/5?u=fry)) and so I decided to share it on github.
It took me a huge amount of time to set this project up (see [discussion](https://discourse.cmake.org/t/embedding-dylib-in-a-macos-bundle/8465/5?u=fry) as well as this [issue](https://github.com/raysan5/raylib/issues/3179)), and so I decided to share it on github.

Embedded Projects
-----------------

* [ImGui](https://github.com/ocornut/imgui)
* [raylib](https://github.com/raysan5/raylib)
* [rlImGUI](https://github.com/raylib-extras/rlImGui)
* [Google Angle (macOS binaries)](https://chromium.googlesource.com/angle/angle)

Licensing
---------
Expand Down
32 changes: 0 additions & 32 deletions external/google/angle/macOS/LICENSE

This file was deleted.

Binary file removed external/google/angle/macOS/libEGL.dylib
Binary file not shown.
Binary file removed external/google/angle/macOS/libGLESv2.dylib
Binary file not shown.
10 changes: 0 additions & 10 deletions external/google/angle/macOS/version.txt

This file was deleted.

7 changes: 7 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
#include "raylib.h"
#include "rlImGui.h"

#ifdef USE_GOOGLE_ANGLE_MAC_OS
#include "GLFW/glfw3.h"
#endif

int main()
{
#ifdef USE_GOOGLE_ANGLE_MAC_OS
glfwInitHint(GLFW_ANGLE_PLATFORM_TYPE, GLFW_ANGLE_PLATFORM_TYPE_METAL);
#endif

InitWindow(800, 450, "raylib [core] example - basic window");

// SetTargetFPS(60);
Expand Down

0 comments on commit 075555d

Please sign in to comment.