diff --git a/CMakeLists.txt b/CMakeLists.txt index 447e134..ca3a356 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) @@ -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() ####################################################### @@ -65,12 +113,12 @@ install( ) if (APPLE) - # installs the libraries under Content/Frameworks - install( - FILES "${google_angle_libs}" - DESTINATION "$/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 "$/Contents/Frameworks" + ) + # TODO: if using the Xcode generator, the executable needs to be resigned... + endif() endif() diff --git a/README.md b/README.md index 6e054cf..ff0903e 100644 --- a/README.md +++ b/README.md @@ -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 ----- @@ -17,23 +22,26 @@ 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 ----------------- @@ -41,7 +49,6 @@ 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 --------- diff --git a/external/google/angle/macOS/LICENSE b/external/google/angle/macOS/LICENSE deleted file mode 100644 index 0f65fd6..0000000 --- a/external/google/angle/macOS/LICENSE +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright 2018 The ANGLE Project Authors. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions -// are met: -// -// Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// -// Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following -// disclaimer in the documentation and/or other materials provided -// with the distribution. -// -// Neither the name of TransGaming Inc., Google Inc., 3DLabs Inc. -// Ltd., nor the names of their contributors may be used to endorse -// or promote products derived from this software without specific -// prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS -// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, -// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN -// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -// POSSIBILITY OF SUCH DAMAGE. diff --git a/external/google/angle/macOS/libEGL.dylib b/external/google/angle/macOS/libEGL.dylib deleted file mode 100755 index 88d74d6..0000000 Binary files a/external/google/angle/macOS/libEGL.dylib and /dev/null differ diff --git a/external/google/angle/macOS/libGLESv2.dylib b/external/google/angle/macOS/libGLESv2.dylib deleted file mode 100755 index 7904b48..0000000 Binary files a/external/google/angle/macOS/libGLESv2.dylib and /dev/null differ diff --git a/external/google/angle/macOS/version.txt b/external/google/angle/macOS/version.txt deleted file mode 100644 index 7db9bbf..0000000 --- a/external/google/angle/macOS/version.txt +++ /dev/null @@ -1,10 +0,0 @@ -Copied from Google Chrome.app version 114.0.5735.198 - -Modified to fit environment following Apple's instructions at -https://developer.apple.com/documentation/xcode/embedding-nonstandard-code-structures-in-a-bundle - -install_name_tool -id "@rpath/libEGL.dylib" libEGL.dylib -codesign -s "-" -fv libEGL.dylib - -install_name_tool -id "@rpath/libGLESv2.dylib" libGLESv2.dylib -codesign -s "-" -fv libGLESv2.dylib diff --git a/main.cpp b/main.cpp index 1f0bdd9..059f086 100644 --- a/main.cpp +++ b/main.cpp @@ -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);