From 3a9d19d3682f0105681eed1a6d068ae3c400960f Mon Sep 17 00:00:00 2001 From: Rylie Pavlik Date: Fri, 22 Nov 2024 11:32:08 -0600 Subject: [PATCH 1/8] Use cardboard_api.lds to limit exports --- sdk/CMakeLists.txt | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/sdk/CMakeLists.txt b/sdk/CMakeLists.txt index c70925bb..8552abd8 100644 --- a/sdk/CMakeLists.txt +++ b/sdk/CMakeLists.txt @@ -112,3 +112,16 @@ target_link_libraries(GfxPluginCardboard # is not needed dl ) + +set_property( + TARGET GfxPluginCardboard + APPEND_STRING + PROPERTY + LINK_FLAGS + " -Wl,--version-script=\"${CMAKE_CURRENT_SOURCE_DIR}/cardboard_api.lds\"" +) +set_property( + TARGET GfxPluginCardboard + APPEND + PROPERTY LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/cardboard_api.lds" +) From 65b9c6b2a51ef26c3f1d87e28ad50a6f9a2bf91a Mon Sep 17 00:00:00 2001 From: Rylie Pavlik Date: Fri, 22 Nov 2024 11:59:10 -0600 Subject: [PATCH 2/8] Slightly fix CMake to be able to consume things right. --- hellocardboard-android/CMakeLists.txt | 3 ++- sdk/CMakeLists.txt | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/hellocardboard-android/CMakeLists.txt b/hellocardboard-android/CMakeLists.txt index 8a199954..e216c913 100644 --- a/hellocardboard-android/CMakeLists.txt +++ b/hellocardboard-android/CMakeLists.txt @@ -12,7 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -cmake_minimum_required(VERSION 3.4.1) +cmake_minimum_required(VERSION 3.22.1) +project(hellocardboard_android VERSION 1.0.0 LANGUAGES CXX) # C++ flags. set(CMAKE_CXX_STANDARD 17) diff --git a/sdk/CMakeLists.txt b/sdk/CMakeLists.txt index c70925bb..85b907f9 100644 --- a/sdk/CMakeLists.txt +++ b/sdk/CMakeLists.txt @@ -12,7 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -cmake_minimum_required(VERSION 3.4.1) +cmake_minimum_required(VERSION 3.22.1) +project(cardboard_sdk VERSION 1.0.0 LANGUAGES CXX) # C++ flags. set(CMAKE_CXX_STANDARD 17) @@ -101,6 +102,7 @@ add_definitions(-DVK_USE_PLATFORM_ANDROID_KHR=1) # Build target_link_libraries(GfxPluginCardboard + PUBLIC ${android-lib} ${GLESv2-lib} # #gles3 - Library is only needed if OpenGL ES 3.0 support is desired. From f61f652f3252624cba1453d116b051abd6b26f4b Mon Sep 17 00:00:00 2001 From: Rylie Pavlik Date: Fri, 22 Nov 2024 12:18:15 -0600 Subject: [PATCH 3/8] Clean up CMake files to fit modern conventions a bit better. e.g. dashes almost never appear in variable names, and this tends to break syntax highlighters at least. --- hellocardboard-android/CMakeLists.txt | 16 ++++++++-------- sdk/CMakeLists.txt | 24 ++++++++++++------------ 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/hellocardboard-android/CMakeLists.txt b/hellocardboard-android/CMakeLists.txt index e216c913..44128f2f 100644 --- a/hellocardboard-android/CMakeLists.txt +++ b/hellocardboard-android/CMakeLists.txt @@ -21,10 +21,10 @@ set(CMAKE_CXX_STANDARD_REQUIRED True) add_compile_options(-Wall -Wextra) # Standard Android dependencies -find_library(android-lib android) -find_library(GLESv2-lib GLESv2) -find_library(GLESv3-lib GLESv3) -find_library(log-lib log) +find_library(ANDROID_LIB android) +find_library(GLESv2_LIB GLESv2) +find_library(GLESv3_LIB GLESv3) +find_library(LOG_LIB log) set(libs_dir ${CMAKE_CURRENT_SOURCE_DIR}/libraries) @@ -37,8 +37,8 @@ add_library(cardboard_jni SHARED ${native_srcs}) target_include_directories(cardboard_jni PRIVATE ${libs_dir}) # Build target_link_libraries(cardboard_jni - ${android-lib} - ${GLESv2-lib} - ${GLESv3-lib} - ${log-lib} + ${ANDROID_LIB} + ${GLESv2_LIB} + ${GLESv3_LIB} + ${LOG_LIB} ${libs_dir}/jni/${ANDROID_ABI}/libGfxPluginCardboard.so) diff --git a/sdk/CMakeLists.txt b/sdk/CMakeLists.txt index 85b907f9..d6482d8b 100644 --- a/sdk/CMakeLists.txt +++ b/sdk/CMakeLists.txt @@ -21,13 +21,13 @@ set(CMAKE_CXX_STANDARD_REQUIRED True) add_compile_options(-Wall -Wextra) # Standard Android dependencies -find_library(android-lib android) -find_library(GLESv2-lib GLESv2) -find_library(log-lib log) +find_library(ANDROID_LIB android) +find_library(GLESv2_LIB GLESv2) +find_library(LOG_LIB log) # #gles3 - Library is only needed if OpenGL ES 3.0 support is desired. Remove # the following line if OpenGL ES 3.0 support is not needed. -find_library(GLESv3-lib GLESv3) +find_library(GLESv3_LIB GLESv3) include_directories(.) @@ -51,8 +51,8 @@ file(GLOB screen_params_srcs "screen_params/android/*.cc") file(GLOB device_params_srcs "device_params/android/*.cc") # Rendering Sources file(GLOB rendering_opengl_srcs "rendering/opengl_*.cc") -# #vulkan This is required for Vulkan rendering. Remove the following two lines -# if Vulkan rendering is not needed. +# #vulkan This is required for Vulkan rendering. Remove the following two lines +# if Vulkan rendering is not needed. file(GLOB rendering_vulkan_srcs "rendering/android/*.cc") file(GLOB rendering_vulkan_wrapper_srcs "rendering/android/vulkan/*.cc") @@ -97,20 +97,20 @@ target_include_directories(GfxPluginCardboard PRIVATE ../third_party/unity_plugin_api) # #vulkan This is required for Vulkan rendering. Remove the following line if -# Vulkan rendering is not needed. +# Vulkan rendering is not needed. add_definitions(-DVK_USE_PLATFORM_ANDROID_KHR=1) # Build target_link_libraries(GfxPluginCardboard PUBLIC - ${android-lib} - ${GLESv2-lib} + ${ANDROID_LIB} + ${GLESv2_LIB} # #gles3 - Library is only needed if OpenGL ES 3.0 support is desired. # Remove the following line if OpenGL ES 3.0 support is not needed. - ${GLESv3-lib} - ${log-lib} + ${GLESv3_LIB} + ${LOG_LIB} # #vulkan - This is required for Vulkan rendering (it is required to load # libvulkan.so at runtime). Remove the following line if Vulkan rendering - # is not needed + # is not needed dl ) From 310943d97d067c7671569b48e5f456003952ebc7 Mon Sep 17 00:00:00 2001 From: Rylie Pavlik Date: Fri, 22 Nov 2024 12:23:57 -0600 Subject: [PATCH 4/8] Add options to the SDK cmake --- sdk/CMakeLists.txt | 86 ++++++++++++++++++++++++++++++---------------- 1 file changed, 57 insertions(+), 29 deletions(-) diff --git a/sdk/CMakeLists.txt b/sdk/CMakeLists.txt index d6482d8b..758843db 100644 --- a/sdk/CMakeLists.txt +++ b/sdk/CMakeLists.txt @@ -20,6 +20,11 @@ set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED True) add_compile_options(-Wall -Wextra) +# If these are already defined, the existing value is used, rather then the default here. +option(CARDBOARDSDK_RENDERING_GLESv3 "Enable rendering with GLESv3" ON) +option(CARDBOARDSDK_RENDERING_VULKAN "Enable rendering with Vulkan" ON) +option(CARDBOARDSDK_UNITY_PLUGIN "Enable use as a Unity plugin" ON) + # Standard Android dependencies find_library(ANDROID_LIB android) find_library(GLESv2_LIB GLESv2) @@ -27,7 +32,9 @@ find_library(LOG_LIB log) # #gles3 - Library is only needed if OpenGL ES 3.0 support is desired. Remove # the following line if OpenGL ES 3.0 support is not needed. -find_library(GLESv3_LIB GLESv3) +if(CARDBOARDSDK_RENDERING_GLESv3) + find_library(GLESv3_LIB GLESv3) +endif() include_directories(.) @@ -51,22 +58,27 @@ file(GLOB screen_params_srcs "screen_params/android/*.cc") file(GLOB device_params_srcs "device_params/android/*.cc") # Rendering Sources file(GLOB rendering_opengl_srcs "rendering/opengl_*.cc") -# #vulkan This is required for Vulkan rendering. Remove the following two lines -# if Vulkan rendering is not needed. -file(GLOB rendering_vulkan_srcs "rendering/android/*.cc") -file(GLOB rendering_vulkan_wrapper_srcs "rendering/android/vulkan/*.cc") - -# === Cardboard Unity JNI === -file(GLOB cardboard_unity_jni_srcs "unity/android/*.cc") - -# === Cardboard Unity Wrapper === -file(GLOB cardboard_xr_unity_srcs - "unity/xr_unity_plugin/*.cc" - "unity/xr_unity_plugin/vulkan/*.cc" -) -# === Cardboard XR Provider for Unity === -file(GLOB cardboard_xr_provider_srcs "unity/xr_provider/*.cc") +if(CARDBOARDSDK_RENDERING_VULKAN) + # #vulkan This is required for Vulkan rendering. Remove the following two lines + # if Vulkan rendering is not needed. + file(GLOB rendering_vulkan_srcs "rendering/android/*.cc") + file(GLOB rendering_vulkan_wrapper_srcs "rendering/android/vulkan/*.cc") +endif() + +if(CARDBOARDSDK_UNITY_PLUGIN) + # === Cardboard Unity JNI === + file(GLOB cardboard_unity_jni_srcs "unity/android/*.cc") + + # === Cardboard Unity Wrapper === + file(GLOB cardboard_xr_unity_srcs + "unity/xr_unity_plugin/*.cc" + "unity/xr_unity_plugin/vulkan/*.cc" + ) + + # === Cardboard XR Provider for Unity === + file(GLOB cardboard_xr_provider_srcs "unity/xr_provider/*.cc") +endif() # Output binary add_library(GfxPluginCardboard SHARED @@ -92,25 +104,41 @@ add_library(GfxPluginCardboard SHARED # Cardboard XR Provider for Unity sources ${cardboard_xr_provider_srcs}) -# Includes -target_include_directories(GfxPluginCardboard - PRIVATE ../third_party/unity_plugin_api) +if(CARDBOARDSDK_UNITY_PLUGIN) + # Includes + target_include_directories(GfxPluginCardboard + PRIVATE ../third_party/unity_plugin_api) +endif() -# #vulkan This is required for Vulkan rendering. Remove the following line if -# Vulkan rendering is not needed. -add_definitions(-DVK_USE_PLATFORM_ANDROID_KHR=1) +if(CARDBOARDSDK_RENDERING_VULKAN) + # #vulkan This is required for Vulkan rendering. Remove the following line if + # Vulkan rendering is not needed. + target_compile_definitions(GfxPluginCardboard PUBLIC VK_USE_PLATFORM_ANDROID_KHR=1) +endif() # Build target_link_libraries(GfxPluginCardboard PUBLIC ${ANDROID_LIB} ${GLESv2_LIB} - # #gles3 - Library is only needed if OpenGL ES 3.0 support is desired. - # Remove the following line if OpenGL ES 3.0 support is not needed. - ${GLESv3_LIB} ${LOG_LIB} - # #vulkan - This is required for Vulkan rendering (it is required to load - # libvulkan.so at runtime). Remove the following line if Vulkan rendering - # is not needed - dl ) + +if(CARDBOARDSDK_RENDERING_VULKAN) + target_link_libraries(GfxPluginCardboard + PUBLIC + # #vulkan - This is required for Vulkan rendering (it is required to load + # libvulkan.so at runtime). Remove the following line if Vulkan rendering + # is not needed + dl + ) +endif() + +if(CARDBOARDSDK_RENDERING_GLESv3) + target_link_libraries(GfxPluginCardboard + PUBLIC + # #gles3 - Library is only needed if OpenGL ES 3.0 support is desired. + # Remove the following line if OpenGL ES 3.0 support is not needed. + ${GLESv3_LIB} + ) +endif() From b3bc2db6243c932fc3886e82e721ca6d34cf0dda Mon Sep 17 00:00:00 2001 From: Rylie Pavlik Date: Fri, 22 Nov 2024 12:58:28 -0600 Subject: [PATCH 5/8] Minor CMake adjustment. Would be better to use find_library et al, but they inexplicably failed. --- hellocardboard-android/CMakeLists.txt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/hellocardboard-android/CMakeLists.txt b/hellocardboard-android/CMakeLists.txt index 44128f2f..ce183446 100644 --- a/hellocardboard-android/CMakeLists.txt +++ b/hellocardboard-android/CMakeLists.txt @@ -28,17 +28,20 @@ find_library(LOG_LIB log) set(libs_dir ${CMAKE_CURRENT_SOURCE_DIR}/libraries) +set(CARDBOARD_LIB ${libs_dir}/jni/${ANDROID_ABI}/libGfxPluginCardboard.so) +set(CARDBOARD_INCLUDE_DIR ${libs_dir}) + # === Cardboard Sample === # Sources file(GLOB native_srcs "src/main/jni/*.cc") # Output binary add_library(cardboard_jni SHARED ${native_srcs}) # Includes -target_include_directories(cardboard_jni PRIVATE ${libs_dir}) +target_include_directories(cardboard_jni PRIVATE ${CARDBOARD_INCLUDE_DIR}) # Build target_link_libraries(cardboard_jni ${ANDROID_LIB} ${GLESv2_LIB} ${GLESv3_LIB} ${LOG_LIB} - ${libs_dir}/jni/${ANDROID_ABI}/libGfxPluginCardboard.so) + ${CARDBOARD_LIB}) From 0df4eb6a05d18c80b3dbbc1a3314e8d347668cb3 Mon Sep 17 00:00:00 2001 From: Rylie Pavlik Date: Fri, 22 Nov 2024 11:59:32 -0600 Subject: [PATCH 6/8] Shared STL for lib --- sdk/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/build.gradle b/sdk/build.gradle index 4955ae74..ce03850b 100644 --- a/sdk/build.gradle +++ b/sdk/build.gradle @@ -22,7 +22,7 @@ android { abiFilters 'armeabi-v7a', 'arm64-v8a' } externalNativeBuild.cmake { - arguments "-DANDROID_STL=c++_static" + arguments "-DANDROID_STL=c++_shared" } defaultConfig { consumerProguardFiles 'proguard-rules.pro' From d7fda77f9b73928e28981fcca9fa8df94e7c7d22 Mon Sep 17 00:00:00 2001 From: Rylie Pavlik Date: Fri, 22 Nov 2024 11:59:38 -0600 Subject: [PATCH 7/8] No ABI filter for lib --- sdk/build.gradle | 3 --- 1 file changed, 3 deletions(-) diff --git a/sdk/build.gradle b/sdk/build.gradle index ce03850b..1cf23086 100644 --- a/sdk/build.gradle +++ b/sdk/build.gradle @@ -18,9 +18,6 @@ android { minSdkVersion 26 targetSdkVersion 33 testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' - ndk { - abiFilters 'armeabi-v7a', 'arm64-v8a' - } externalNativeBuild.cmake { arguments "-DANDROID_STL=c++_shared" } From c28e437e5590b584a246589de14fa19f703aaf30 Mon Sep 17 00:00:00 2001 From: Rylie Pavlik Date: Fri, 22 Nov 2024 11:19:58 -0600 Subject: [PATCH 8/8] Switch to using "prefab" to consume the Cardboard SDK. Remove gradle code for extracting non-prefab AAR, too --- hellocardboard-android/CMakeLists.txt | 10 ++++------ hellocardboard-android/build.gradle | 27 +++------------------------ sdk/build.gradle | 8 ++++++++ 3 files changed, 15 insertions(+), 30 deletions(-) diff --git a/hellocardboard-android/CMakeLists.txt b/hellocardboard-android/CMakeLists.txt index ce183446..8c02f8cb 100644 --- a/hellocardboard-android/CMakeLists.txt +++ b/hellocardboard-android/CMakeLists.txt @@ -26,22 +26,20 @@ find_library(GLESv2_LIB GLESv2) find_library(GLESv3_LIB GLESv3) find_library(LOG_LIB log) -set(libs_dir ${CMAKE_CURRENT_SOURCE_DIR}/libraries) +# Cardboard SDK prefab +find_package(sdk REQUIRED CONFIG) -set(CARDBOARD_LIB ${libs_dir}/jni/${ANDROID_ABI}/libGfxPluginCardboard.so) -set(CARDBOARD_INCLUDE_DIR ${libs_dir}) # === Cardboard Sample === # Sources file(GLOB native_srcs "src/main/jni/*.cc") # Output binary add_library(cardboard_jni SHARED ${native_srcs}) -# Includes -target_include_directories(cardboard_jni PRIVATE ${CARDBOARD_INCLUDE_DIR}) # Build target_link_libraries(cardboard_jni ${ANDROID_LIB} ${GLESv2_LIB} ${GLESv3_LIB} ${LOG_LIB} - ${CARDBOARD_LIB}) + # This also adds include directories + sdk::GfxPluginCardboard) diff --git a/hellocardboard-android/build.gradle b/hellocardboard-android/build.gradle index 882ea920..061e6c2c 100644 --- a/hellocardboard-android/build.gradle +++ b/hellocardboard-android/build.gradle @@ -36,6 +36,9 @@ android { externalNativeBuild.cmake { path "CMakeLists.txt" } + buildFeatures { + prefab true + } namespace 'com.google.cardboard' } @@ -49,27 +52,3 @@ dependencies { implementation 'com.google.protobuf:protobuf-javalite:3.19.4' implementation project(":sdk") } - -// The dependencies for NDK builds live inside the .aar files so they need to -// be extracted before NDK targets can link against. -task extractNdk(type: Copy) { - if (file("${project.rootDir}/sdk/build/outputs/aar/sdk-release.aar").exists()) { - copy { - from zipTree("${project.rootDir}/sdk/build/outputs/aar/sdk-release.aar") - into "libraries/" - include "jni/**/libGfxPluginCardboard.so" - } - copy { - from "${project.rootDir}/sdk/include/cardboard.h" - into "libraries/" - } - } -} - -task deleteNdk(type: Delete) { - delete "libraries/jni" - delete "libraries/cardboard.h" -} - -build.dependsOn(extractNdk) -clean.dependsOn(deleteNdk) diff --git a/sdk/build.gradle b/sdk/build.gradle index 1cf23086..2c540e41 100644 --- a/sdk/build.gradle +++ b/sdk/build.gradle @@ -46,6 +46,14 @@ android { main.proto.srcDirs = ["${project.rootDir}/proto"] main.proto.includes = ["cardboard_device.proto"] } + buildFeatures { + prefabPublishing true + } + prefab { + GfxPluginCardboard { + headers "include" + } + } namespace 'com.google.cardboard.sdk' }