Introduction to OpenXR SDK Compilation

For the OpenXR compilation guide, please refer to: https://blog.csdn.net/geyichongchujianghu/article/details/124672713

OpenXR SDK地址为:GitHub - KhronosGroup/OpenXR-SDK-Source: Sources for OpenXR loader, basic API layers, and example code.

Monado runtime address is: Monado / Monado · GitLab 

The system broker address is: Ryan Pavlik / openxr-android-broker · GitLab

1. hello_xr/CMakeLists.txt

file(GLOB LOCAL_HEADERS "*.h")--  The GLOB command is mainly used to match the matching rules to the required files in the specified directory, and the variables are stored in the LOCAL_HEADERS variable 
file(GLOB LOCAL_SOURCE "*.cpp") 
file(GLOB VULKAN_SHADERS "vulkan_shaders/*.glsl") 

# For including compiled shaders 
include_directories(${CMAKE_CURRENT_BINARY_DIR}) --Add the specified directory to the compiler's header file search path, and the specified directory is interpreted as the relative path of the current source code path . CMAKE_CURRENT_BINARY_DIR is the current build directory. hello_xr/build
 
if(ANDROID) 
    add_library(hello_xr MODULE 
        ${LOCAL_SOURCE} 
        ${LOCAL_HEADERS} 
        ${VULKAN_SHADERS} 
        $<TARGET_OBJECTS:android_native_app_glue>) --Add a library named hello_xr, the source file of the library can be specified, or target_sources( ) subsequent designation. The type of library is one of STATIC (static library)/SHARED (dynamic library)/MODULE (module library).
    target_link_libraries(hello_xr ${ANDROID_LIBRARY} ${ANDROID_LOG_LIBRARY}) -- Libraries or flags to use when linking the given target and/or its dependencies 
else() 
    add_executable(hello_xr 
        ${LOCAL_SOURCE} 
        ${LOCAL_HEADERS} 
        ${VULKAN_SHADERS}) --Use the specified source file to generate the target executable file 
endif() 
set_target_properties(hello_xr PROPERTIES FOLDER ${SAMPLES_FOLDER}) --This command is to set the properties of the target, set the property FOLDER to ${SAMPLES_FOLDER}
 
source_group("Headers" FILES ${LOCAL_HEADERS}) -- Any explicitly specified source files will be placed in the group <Headers>. Relative paths are interpreted relative to the current source directory. 
source_group("Shaders" FILES ${VULKAN_SHADERS}) 

compile_glsl(run_hello_xr_glsl_compiles ${VULKAN_SHADERS}) 

add_dependencies(hello_xr 
) 
    generate_openxr_header
    run_hello_xr_glsl_compiles --Add dependencies 
target_include_directories(hello_xr 
    PRIVATE 
    ${PROJECT_SOURCE_DIR}/src 
    ${PROJECT_SOURCE_DIR}/src/common 
    # for helper headers 
    ${PROJECT_SOURCE_DIR}/external/include 
) --Specify target (exe or so file) headers to include file path. 
if(GLSLANG_VALIDATOR AND NOT GLSLC_COMMAND) 
    target_compile_definitions(hello_xr PRIVATE USE_GLSLANGVALIDATOR) --Add compile definition USE_GLSLANGVALIDATOR to target 
endif() 
if(Vulkan_FOUND) 
    target_include_directories(hello_xr 
        PRIVATE 
        ${ Vulkan_INCLUDE_DIRS} 
    ) 
endif() 
target_link_libraries(hello_xr OpenXR::openxr_loader)




 
if(TARGET openxr-gfxwrapper) 
    target_link_libraries(hello_xr openxr-gfxwrapper)
endif()
...

2. hello_xr/build.gradle

externalNativeBuild {
   
   --用于控制Native的编译
    cmake {
        arguments '-DANDROID_STL=c++_shared',
                '-DBUILD_API_LAYERS=OFF',
                '-DBUILD_TESTS=ON',
                '-DBUILD_LOADER=ON',
                '-DBUILD_CONFORMANCE_TESTS=OFF',
                '-DBUILD_ALL_EXTENSIONS=ON'
        targets "openxr_loader", "hello_xr"
    }
}

If you want to compile API_LAYER, you need to modify it as follows:

arguments...

'-DBUILD_API_LAYERS=ON',

targets "openxr_loader", "hello_xr", "XrApiLayer_api_dump"

externalNativeBuild {
    -- used to control Native compilation 
    cmake { 
        path "${project.repoRoot}/CMakeLists.txt" -- specify CMakeLists file 
        version "3.22.1" --cmake version 
    } 
}
   

3. loader/CMakeLists.txt

Because there are too many, only pick the key points

# make cache variables for install destinations
include(GNUInstallDirs)--include文件夹

# List of all files externally generated outside of the loader that the loader
# needs to build with.
set(LOADER_EXTERNAL_GEN_FILES ${COMMON_GENERATED_OUTPUT})--定义变量LOADER_EXTERNAL_GEN_FILES ,并赋值为${COMMON_GENERATED_OUTPUT}
set(LOADER_EXTERNAL_GEN_DEPENDS ${COMMON_GENERATED_DEPENDS})
run_xr_xml_generate(loader_source_generator.py xr_generated_loader.hpp)--
run_xr_xml_generate(loader_source_generator.py xr_generated_loader.cpp)

add_library(openxr_loader ${LIBRARY_TYPE}
    android_utilities.cpp
    android_utilities.h
    api_layer_interface.cpp
    api_layer_interface.hpp
    loader_core.cpp
    loader_instance.cpp
    loader_instance.hpp
    loader_logger.cpp
    loader_logger.hpp
    loader_logger_recorders.cpp
    loader_logger_recorders.hpp
    manifest_file.cpp
    manifest_file.hpp
    runtime_interface.cpp
    runtime_interface.hpp
    ${GENERATED_OUTPUT}
    ${PROJECT_SOURCE_DIR}/src/common/hex_and_handles.h
    ${PROJECT_SOURCE_DIR}/src/common/object_info.cpp
    ${PROJECT_SOURCE_DIR}/src/common/object_info.h
    ${PROJECT_SOURCE_DIR}/src/common/platform_utils.hpp
    ${LOADER_EXTERNAL_GEN_FILES}
    ${openxr_loader_RESOURCE_FILE}
)--生成libopenxr_loader.so,并添加相关编译文件
set_target_properties(openxr_loader PROPERTIES FOLDER ${LOADER_FOLDER})

add_dependencies(openxr_loader generate_openxr_header xr_global_generated_files)--添加依赖
target_include_directories(
    openxr_loader
    # for OpenXR headers
    PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
           $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>

    PRIVATE ${PROJECT_SOURCE_DIR}/src/common

            # for generated dispatch table, common_config.h
            ${CMAKE_CURRENT_SOURCE_DIR}/..
            ${CMAKE_CURRENT_BINARY_DIR}/..

            # for target-specific generated files
            ${CMAKE_CURRENT_SOURCE_DIR}
            ${CMAKE_CURRENT_BINARY_DIR}
    INTERFACE $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> 
)--Specify the header file path that the target (exe or so file) needs to include. INTERFACE, PUBLIC and PRIVATE keywords are required to specify the scope of the following parameters 
target_link_libraries( 
    openxr_loader 
    PRIVATE ${CMAKE_DL_LIBS} 
    PUBLIC Threads::Threads 
) --- Libraries that need to be linked 
target_compile_definitions(openxr_loader PRIVATE ${OPENXR_ALL_SUPPORTED_DEFINES}) --add definitions 
if(ANDROID) 
    target_link_libraries( 
        openxr_loader 
        PRIVATE 
        ${ANDROID_LOG_LIBRARY} 
        ${ANDROID_LIBRARY}) 
end if()
target_compile_definitions(openxr_loader PRIVATE API_NAME="OpenXR")--修改定义
openxr_add_filesystem_utils(openxr_loader)

set_target_properties(openxr_loader PROPERTIES DEBUG_POSTFIX "${OPENXR_DEBUG_POSTFIX}")--设置属性

...
elseif(ANDROID)
    set(JNIPP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../external/jnipp)
    set(JNIWRAPPER_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../external/android-jni-wrappers)
    file(GLOB ANDROID_WRAP_SOURCES ${JNIWRAPPER_ROOT}/wrap/*.cpp)
    set_target_properties(openxr_loader
        PROPERTIES
        CXX_STANDARD 17
        CXX_STANDARD_REQUIRED TRUE)
    target_sources(openxr_loader
        PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}/android_utilities.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/android_utilities.h
        ${ANDROID_WRAP_SOURCES}
        ${JNIPP_ROOT}/jnipp.cpp
    )--将源添加到target
    target_include_directories(openxr_loader
        PRIVATE
        ${JNIPP_ROOT}
        ${JNIWRAPPER_ROOT}
    )
endif()
add_library(headers INTERFACE) --Directly import 
    the generated library, cmake will not add compilation rules to this type 
of 
    library 
    $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
install( 
    TARGETS openxr_loader headers EXPORT openxr_loader_export 
    RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT Loader 
    LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT Loader 
    ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBD IR}" COMPONENT Loader 
) -- will tunrimewe files, library files, header files, etc. Install to the specified directory 
export( 
    EXPORT openxr_loader_export 
    FILE ${CMAKE_CURRENT_BINARY_DIR}/OpenXRTargets.cmake 
    NAMESPACE OpenXR:: 
)
# Create aliases so that it can be used the same whether vendored as source or found with CMake. 
add_library(OpenXR:: openxr_loader ALIAS 
openxr_loader) - - set the library headers alias OpenXR::headers

...

Guess you like

Origin blog.csdn.net/weixin_41028555/article/details/132298460