cmake_minimum_required(VERSION 3.21) if(POLICY CMP0135) cmake_policy(SET CMP0135 NEW) endif() include(FetchContent) # -------------------------------------------------- # Declarations # -------------------------------------------------- # EXCLUDE_FROM_ALL is used to prevent FetchContent from automatically adding # targets to the ALL_BUILD target. We do this for all dependencies except # JsRuntimeHost because JsRuntimeHost has top-level targets (e.g., AppRuntime) # that are part of the Babylon Native contract. When adding dependencies using # FetchContent, add the EXCLUDE_FROM_ALL option except where there are top-level # targets that are part of the Babylon Native contract. FetchContent_Declare(AndroidExtensions GIT_REPOSITORY https://github.com/BabylonJS/AndroidExtensions.git GIT_TAG 2d5af72259cc73e5f249d3c99bee2010be9cb042 EXCLUDE_FROM_ALL) FetchContent_Declare(arcana.cpp GIT_REPOSITORY https://github.com/microsoft/arcana.cpp.git GIT_TAG 53e5b05eaf53154b60dff8ffb45e52a417cf5020 EXCLUDE_FROM_ALL) FetchContent_Declare(arcore-android-sdk GIT_REPOSITORY https://github.com/google-ar/arcore-android-sdk.git GIT_TAG 98cb803de5482fb2b36effe8be3b5a0d3b726976 EXCLUDE_FROM_ALL) FetchContent_Declare(base-n GIT_REPOSITORY https://github.com/azawadzki/base-n.git GIT_TAG 7573e77c0b9b0e8a5fb63d96dbde212c921993b4 EXCLUDE_FROM_ALL) FetchContent_Declare(bgfx.cmake GIT_REPOSITORY https://github.com/BabylonJS/bgfx.cmake.git GIT_TAG 53d884a6e221f96fade72a15e0ad5f92ec7b66b5 EXCLUDE_FROM_ALL) FetchContent_Declare(CMakeExtensions GIT_REPOSITORY https://github.com/BabylonJS/CMakeExtensions.git GIT_TAG 631780e42886e5f12bfd1a5568c7395f1d657f43 EXCLUDE_FROM_ALL) FetchContent_Declare(glslang GIT_REPOSITORY https://github.com/BabylonJS/glslang.git GIT_TAG 39a80699a315cb7f66c4ab3180edd4e2910fab28 EXCLUDE_FROM_ALL) FetchContent_Declare(googletest URL "https://github.com/google/googletest/archive/refs/tags/v1.17.0.tar.gz" EXCLUDE_FROM_ALL) FetchContent_Declare(ios-cmake GIT_REPOSITORY https://github.com/leetal/ios-cmake.git GIT_TAG 4.5.0 EXCLUDE_FROM_ALL) FetchContent_Declare(JsRuntimeHost GIT_REPOSITORY https://github.com/BabylonJS/JsRuntimeHost.git GIT_TAG 598f004457cf7a340a9acbe7138fcc3196b80674) FetchContent_Declare(libwebp GIT_REPOSITORY https://github.com/webmproject/libwebp.git GIT_TAG 57e324e2eb99be46df46d77b65705e34a7ae616c EXCLUDE_FROM_ALL) FetchContent_Declare(metal-cpp GIT_REPOSITORY https://github.com/bkaradzic/metal-cpp.git GIT_TAG metal-cpp_26 EXCLUDE_FROM_ALL) FetchContent_Declare(SPIRV-Cross GIT_REPOSITORY https://github.com/BabylonJS/SPIRV-Cross.git GIT_TAG 0e1654748e9e54b5469fb0fbc1c715b96fa06f8f EXCLUDE_FROM_ALL) # -------------------------------------------------- FetchContent_MakeAvailable(CMakeExtensions) # BABYLON_NATIVE_BUILD_SOURCETREE flag is used by BabylonReactNative NPM build process. # CMake script is run twice. At package creation, BABYLON_NATIVE_BUILD_SOURCETREE is ON # which makes all dependencies to be fetched and packaged. # At build time, cmake is run again on BabylonNative root but FETCHCONTENT_FULLY_DISCONNECTED is ON # and FetchContent_MakeAvailable_With_Message will use provided local folder provided to cmake command line # like '-DFETCHCONTENT_SOURCE_DIR_BGFX.CMAKE=../shared/Babylon/....' if(VISIONOS OR IOS OR BABYLON_NATIVE_BUILD_SOURCETREE) FetchContent_MakeAvailable_With_Message(ios-cmake) if(NOT BABYLON_NATIVE_BUILD_SOURCETREE) set(CMAKE_TOOLCHAIN_FILE "${ios-cmake_SOURCE_DIR}/ios.toolchain.cmake" CACHE PATH "") endif() set(ENABLE_ARC OFF CACHE STRING "Enables or disables ARC support.") endif() if(VISIONOS) set(PLATFORM "VISIONOSCOMBINED" CACHE STRING "") set(DEPLOYMENT_TARGET "1.0" CACHE STRING "") elseif(IOS) set(PLATFORM "OS64COMBINED" CACHE STRING "") set(DEPLOYMENT_TARGET "13" CACHE STRING "") endif() project(BabylonNative) set_property(GLOBAL PROPERTY USE_FOLDERS ON) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) # Suppress "Performing Test ..." output from check_c_compiler_flag() and similar macros. set(CMAKE_REQUIRED_QUIET TRUE) # -------------------------------------------------- # Options # -------------------------------------------------- # General option(BABYLON_NATIVE_BUILD_APPS "Build Babylon Native apps." ${PROJECT_IS_TOP_LEVEL}) option(BABYLON_NATIVE_INSTALL "Include the install target." ${PROJECT_IS_TOP_LEVEL}) option(BABYLON_DEBUG_TRACE "Enable debug trace." OFF) # WARNING: This is experimental. Only use it if you can ensure that your application will properly handle thread affinity. option(BABYLON_NATIVE_CHECK_THREAD_AFFINITY "Checks thread safety in the graphics device calls. It can be removed if hosting application ensures thread coherence." ON) # Plugins option(BABYLON_NATIVE_PLUGIN_EXTERNALTEXTURE "Include Babylon Native Plugin ExternalTexture." ON) option(BABYLON_NATIVE_PLUGIN_NATIVECAMERA "Include Babylon Native Plugin NativeCamera." ON) option(BABYLON_NATIVE_PLUGIN_NATIVECAPTURE "Include Babylon Native Plugin NativeCapture." ON) option(BABYLON_NATIVE_PLUGIN_NATIVEENCODING "Include Babylon Native Plugin NativeEncoding." ON) option(BABYLON_NATIVE_PLUGIN_NATIVEENGINE "Include Babylon Native Plugin NativeEngine." ON) option(BABYLON_NATIVE_PLUGIN_NATIVEENGINE_WEBP "Include Babylon Native Plugin NativeEngine - WebP." ON) option(BABYLON_NATIVE_PLUGIN_NATIVEENGINE_COMPILESHADERS "Include Babylon Native Plugin NativeEngine - Compile Shaders." ON) option(BABYLON_NATIVE_PLUGIN_NATIVEINPUT "Include Babylon Native Plugin NativeInput." ON) option(BABYLON_NATIVE_PLUGIN_NATIVEOPTIMIZATIONS "Include Babylon Native Plugin NativeOptimizations." ON) option(BABYLON_NATIVE_PLUGIN_NATIVETRACING "Include Babylon Native Plugin NativeTracing." ON) option(BABYLON_NATIVE_PLUGIN_NATIVEXR "Include Babylon Native Plugin XR." ON) option(BABYLON_NATIVE_PLUGIN_SHADERCACHE "Include Babylon Native Plugin ShaderCache." ON) option(BABYLON_NATIVE_PLUGIN_SHADERCOMPILER "Include Babylon Native Plugin ShaderCompiler." ON) option(BABYLON_NATIVE_PLUGIN_SHADERTOOL "Include Babylon Native Plugin ShaderTool." ON) option(BABYLON_NATIVE_PLUGIN_TESTUTILS "Include Babylon Native Plugin TestUtils." ON) # Polyfills option(BABYLON_NATIVE_POLYFILL_WINDOW "Include Babylon Native Polyfill Window." ON) option(BABYLON_NATIVE_POLYFILL_CANVAS "Include Babylon Native Polyfill Canvas." ON) # Sanitizers option(ENABLE_SANITIZERS "Enable AddressSanitizer and UBSan" OFF) if(ENABLE_SANITIZERS) set(ENABLE_RTTI ON CACHE BOOL "" FORCE) if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU") set(SANITIZERS "address,undefined") # Check for Clang since vptr and fdsan are Clang-specific if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") # vptr is not supported on Windows. if(NOT WIN32) list(APPEND SANITIZERS "vptr") endif() # FDSan only works on Android builds with Clang if (ANDROID) list(APPEND SANITIZERS "fdsan") endif() endif() string(JOIN "," SANITIZER_FLAGS ${SANITIZERS}) add_compile_options(-fsanitize=${SANITIZER_FLAGS} -fno-omit-frame-pointer) add_link_options(-fsanitize=${SANITIZER_FLAGS}) elseif(MSVC) # MSVC only supports AddressSanitizer add_compile_options(/fsanitize=address /Zi /Od) else() message(FATAL_ERROR "Sanitizers not supported on this compiler.") endif() endif() # -------------------------------------------------- if(ANDROID) set(BABYLON_NATIVE_PLATFORM "Android") elseif(IOS) set(BABYLON_NATIVE_PLATFORM "iOS") elseif (VISIONOS) set(BABYLON_NATIVE_PLATFORM "visionOS") elseif(APPLE) set(BABYLON_NATIVE_PLATFORM "macOS") elseif(WINDOWS_STORE) set(BABYLON_NATIVE_PLATFORM "WinRT") elseif(WIN32) set(BABYLON_NATIVE_PLATFORM "Win32") elseif(UNIX) set(BABYLON_NATIVE_PLATFORM "Unix") else() message(FATAL_ERROR "Unrecognized platform: ${CMAKE_SYSTEM_NAME}") endif() if(APPLE) set(GRAPHICS_API Metal) elseif(UNIX) if(NOT GRAPHICS_API) set(GRAPHICS_API OpenGL) else() if(NOT GRAPHICS_API STREQUAL Vulkan AND NOT GRAPHICS_API STREQUAL OpenGL) message(FATAL_ERROR "Unrecognized/Unsupported render API: ${GRAPHICS_API}") endif() endif() elseif(WIN32) if(NOT GRAPHICS_API) set(GRAPHICS_API D3D11) elseif(GRAPHICS_API STREQUAL "OpenGLWindowsDevOnly") # OpenGLES via ANGLE from a Chromium-based browser (dev-only). set(GRAPHICS_API OpenGL) set(BABYLON_NATIVE_OPENGLES_FROM_BROWSER ON) else() if(NOT GRAPHICS_API STREQUAL Vulkan AND NOT GRAPHICS_API STREQUAL D3D11 AND NOT GRAPHICS_API STREQUAL D3D12) message(FATAL_ERROR "Unrecognized/Unsupported render API: ${GRAPHICS_API}") endif() endif() endif() # Find ANGLE libraries from Edge or Chrome when using OpenGLWindowsDevOnly. if(BABYLON_NATIVE_OPENGLES_FROM_BROWSER) foreach(_browser_exe "msedge.exe" "chrome.exe") if(ANGLE_LIBEGL) break() endif() foreach(_reg_root "HKLM" "HKCU") if(ANGLE_LIBEGL) break() endif() execute_process( COMMAND reg query "${_reg_root}\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\${_browser_exe}" /ve OUTPUT_VARIABLE _reg_output ERROR_QUIET RESULT_VARIABLE _reg_result OUTPUT_STRIP_TRAILING_WHITESPACE ) if(NOT _reg_result EQUAL 0) continue() endif() string(REGEX MATCH "REG_SZ[ \t]+([^\r\n]+)" _ "${_reg_output}") string(STRIP "${CMAKE_MATCH_1}" _browser_path) if(NOT _browser_path) continue() endif() cmake_path(GET _browser_path PARENT_PATH _browser_dir) file(GLOB _version_dirs LIST_DIRECTORIES true "${_browser_dir}/[0-9]*") list(SORT _version_dirs ORDER DESCENDING) foreach(_version_dir ${_version_dirs}) if(IS_DIRECTORY "${_version_dir}" AND EXISTS "${_version_dir}/libEGL.dll") set(ANGLE_LIBEGL "${_version_dir}/libEGL.dll") set(ANGLE_LIBGLESV2 "${_version_dir}/libGLESv2.dll") message(STATUS "Found ANGLE libraries in: ${_version_dir}") break() endif() endforeach() endforeach() endforeach() if(NOT ANGLE_LIBEGL) message(FATAL_ERROR "OpenGLWindowsDevOnly requires ANGLE libraries (libEGL.dll, libGLESv2.dll) from Edge or Chrome, but none were found.") endif() endif() if(APPLE) set(BABYLON_NATIVE_PLATFORM_IMPL_EXT "mm") else() set(BABYLON_NATIVE_PLATFORM_IMPL_EXT "cpp") endif() if(MSVC) # __cplusplus value is not in sync with language version used. MVSC needs this flag to update it accordingly # https://gitlab.kitware.com/cmake/cmake/-/issues/18837 add_compile_options(/Zc:__cplusplus) # https://devblogs.microsoft.com/cppblog/announcing-full-support-for-a-c-c-conformant-preprocessor-in-msvc/ add_compile_options(/Zc:preprocessor) # default to /W3 add_compile_options(/W3) # Enable multiprocessor compilation for faster builds add_compile_options(/MP) endif() if(APPLE) # Create scheme for installation and other targets set(CMAKE_XCODE_GENERATE_SCHEME TRUE) endif() if(BABYLON_DEBUG_TRACE) add_compile_definitions(BABYLON_DEBUG_TRACE) endif() add_subdirectory(Dependencies) add_subdirectory(Core) add_subdirectory(Plugins) add_subdirectory(Polyfills) if(BABYLON_NATIVE_BUILD_APPS) add_subdirectory(Apps) endif() # no install for glslang compiled for shaderc if(BABYLON_NATIVE_INSTALL) include(Install/Install.cmake) endif()