#!/usr/bin/env bash # Makes sure the Orbbec K4A wrapper is built for THIS platform, INCLUDING FTF's # libk4arecord_v2.so, and prints K4A_WRAPPER_DIR=... for build_sdk.sh (the recorder # example needs the headers) and for the recorder's runtime rpath. # # This is the libk4a provider — Orbbec's own, NOT Microsoft's Azure Kinect SDK. It # bundles its own OrbbecSDK, so it builds standalone. # # The recorder does not dlopen the stock libk4a/libk4arecord. It dlopens a single # custom library, libk4arecord_v2.so: the wrapper's own MKV-writing sources linked # to a pure-data shim (k4a_shim.cpp) instead of libk4a — K4A-compatible recording # with no OrbbecSDK dependency. That target is FTF's (wrapper/recordv2/ in this # repo), not part of the stock wrapper, so it is injected and built here. # # Reuses an existing build; only adds the v2 target if it is missing. set -euo pipefail here=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) repo=$(cd "$here/.." && pwd) eval "$("$here/detect_platform.sh")" REPO_URL="${OB_K4A_REPO:-https://github.com/orbbec/OrbbecSDK-K4A-Wrapper}" REF="${OB_K4A_REF:-v1.10.4}" # The wrapper enables the CUDA language (its bundled OrbbecSDK does), so it will # not even CONFIGURE without a working CUDA — regardless of libk4arecord_v2.so not # using it. A non-interactive shell has no cuda/bin on PATH, so point cmake at it # and give the architecture by SoC (cmake needs it explicit here). Empty when no # CUDA is found, letting cmake try its own detection. cuda_flags() { local nvcc for nvcc in /usr/local/cuda/bin/nvcc /usr/local/cuda-*/bin/nvcc; do [[ -x "$nvcc" ]] && break done [[ -x "$nvcc" ]] || return 0 local arch case "$SOC" in tegra210) arch=53 ;; # Nano, TX1 tegra186) arch=62 ;; # TX2 tegra194) arch=72 ;; # Xavier tegra234) arch=87 ;; # Orin tegra264) arch=101 ;; # Thor *) arch=87 ;; # a reasonable modern default esac printf -- '-DCMAKE_CUDA_COMPILER=%s -DCMAKE_CUDA_ARCHITECTURES=%s' "$nvcc" "$arch" } # A usable wrapper base: headers and the stock libk4a. The v2 target is ensured # separately, so a wrapper built before this repo existed still qualifies. find_wrapper() { local d="$1" [[ -f "$d/include/k4a/k4a.h" ]] || return 1 [[ -f "$d/build/lib/libk4a.so" ]] || return 1 echo "$d" } # Injects FTF's recordv2 sources and builds libk4arecord_v2.so if it is not there. # Idempotent: the add_subdirectory line and the copy are guarded. ensure_recordv2() { local d="$1" [[ -f "$d/build/lib/libk4arecord_v2.so" ]] && return 0 echo ">> libk4arecord_v2.so absent — injection de recordv2 et compilation…" >&2 local dst="$d/src/orbbec/recordv2" mkdir -p "$dst" cp "$repo/wrapper/recordv2/CMakeLists.txt" "$dst/" cp "$repo/wrapper/recordv2/k4a_shim.cpp" "$dst/" local ob_cmake="$d/src/orbbec/CMakeLists.txt" if [[ -f "$ob_cmake" ]] && ! grep -q "add_subdirectory(recordv2)" "$ob_cmake"; then echo "add_subdirectory(recordv2)" >> "$ob_cmake" fi ( cd "$d/build" cmake .. -DCMAKE_BUILD_TYPE=Release $(cuda_flags) >&2 make -j"$(nproc)" k4arecord_v2 >&2 ) [[ -f "$d/build/lib/libk4arecord_v2.so" ]] } # 1 & 2: reuse a wrapper base, then make sure the v2 target is present. for candidate in "${K4A_WRAPPER_DIR:-}" \ "$HOME/OrbbecSDK-K4A-Wrapper" \ "$HOME/Downloads/OrbbecSDK-K4A-Wrapper"; do [[ -z "$candidate" ]] && continue if find_wrapper "$candidate" >/dev/null 2>&1; then echo ">> Wrapper K4A trouve: $candidate" >&2 if ensure_recordv2 "$candidate"; then echo "K4A_WRAPPER_DIR=$candidate" exit 0 fi echo "!! libk4arecord_v2.so n'a pas pu etre bati dans $candidate" >&2 exit 1 fi done # 3: clone + build the wrapper base, then the v2 target. dir="${K4A_WRAPPER_DIR:-$HOME/OrbbecSDK-K4A-Wrapper}" echo ">> Wrapper K4A absent — clone et compilation (JetPack ${JETPACK}, ${SOC})" >&2 echo ">> ${REPO_URL} @ ${REF} -> ${dir}" >&2 if [[ ! -d "$dir/.git" ]]; then git clone --recursive --depth 1 --branch "$REF" "$REPO_URL" "$dir" >&2 fi mkdir -p "$dir/build" ( cd "$dir/build" cmake .. -DCMAKE_BUILD_TYPE=Release $(cuda_flags) >&2 make -j"$(nproc)" k4a k4arecord >&2 ) if ! find_wrapper "$dir" >/dev/null 2>&1 || ! ensure_recordv2 "$dir"; then cat >&2 <> Wrapper K4A + libk4arecord_v2.so batis: $dir" >&2 echo "K4A_WRAPPER_DIR=$dir"