#!/usr/bin/env bash # Qt 5 / CMVR setup using the target machine's configured distribution repositories. set -euo pipefail export LC_ALL=C log() { printf '\n[CMVR] %s\n' "$*"; } fail() { printf '\n[ERROR] %s\n' "$*" >&2; return 1; } have() { command -v "$1" >/dev/null 2>&1; } usage() { cat <<'HELP' CMVR setup for Kylin Linux x86 (Qt 5.12+, CMake 3.16+). bash setup_kylin.sh Install Qt and development/runtime dependencies bash setup_kylin.sh --source /path/qt_client Install, configure and build Release bash setup_kylin.sh --app /path/CmvrControlStation Install and check an existing Linux application bash setup_kylin.sh --check Check installed tools/dependencies only bash setup_kylin.sh --dry-run Print repository package plan without installing Options: --source DIR Full qt_client source tree, not the Windows ZIP --app FILE Existing native Linux ELF application --jobs N Parallel build jobs (default: 4) --with-qtcreator Also install the optional Qt Creator IDE --no-update Use current package metadata (e.g. prepared offline repositories) --check Do not install, build, or generate a launcher --dry-run Do not refresh repositories, install, build, or generate files -h, --help Show this help Run as the desktop user; the script invokes sudo only for package management. No robot connections, motor commands or service restarts are performed. HELP } detect_platform() { [[ $(uname -s) == Linux ]] || { fail 'Run this script on the Kylin Linux computer.'; return 1; } ARCH=$(uname -m) case "$ARCH" in x86_64|i386|i486|i586|i686) ;; *) fail "Expected Linux x86, detected $ARCH"; return 1 ;; esac OS_NAME=Linux if [[ -r /etc/os-release ]]; then # Standard distribution-owned identification file. OS_NAME=$(. /etc/os-release; printf '%s' "${PRETTY_NAME:-Linux}") fi if have apt-get && have dpkg; then PM=apt elif have dnf; then PM=dnf elif have yum; then PM=yum else fail 'No supported package manager: apt-get, dnf or yum.'; return 1 fi log "System: $OS_NAME; architecture: $ARCH; package manager: $PM" } package_available() { local pkg=$1 candidate if [[ $PM == apt ]]; then candidate=$(apt-cache policy "$pkg" 2>/dev/null | awk '/Candidate:/ {print $2; exit}') [[ -n $candidate && $candidate != '(none)' ]] else # rpm -q handles packages already installed but absent from current repository metadata. rpm -q "$pkg" >/dev/null 2>&1 || "$PM" -q list "$pkg" >/dev/null 2>&1 fi } select_package() { local required=$1 label=$2 pkg shift 2 for pkg in "$@"; do if package_available "$pkg"; then PACKAGES+=("$pkg") return 0 fi done if [[ $required == required ]]; then MISSING+=("$label: $*") else log "Optional package unavailable ($label): $*" fi } resolve_packages() { PACKAGES=(); MISSING=() if [[ $PM == apt ]]; then select_package required 'C/C++ compiler' build-essential select_package required CMake cmake select_package required 'build tool' ninja-build make select_package required pkg-config pkg-config pkgconf select_package required 'Qt5 base development' qtbase5-dev select_package required 'Qt5 development tools' qtbase5-dev-tools select_package required 'Qt5 qmake' qt5-qmake select_package required 'Qt5 multimedia' qtmultimedia5-dev select_package required 'Qt5 3D modules' qt3d5-dev select_package required 'Qt3D mesh plugin' qt3d-assimpsceneimport-plugin select_package required 'Protobuf development' libprotobuf-dev select_package required protoc protobuf-compiler select_package required 'gRPC C++ development' libgrpc++-dev select_package required 'gRPC code generator' protobuf-compiler-grpc select_package required 'FFmpeg codec' libavcodec-dev select_package required 'FFmpeg utility' libavutil-dev select_package required 'FFmpeg scaling' libswscale-dev select_package required urdfdom liburdfdom-dev select_package required TinyXML libtinyxml-dev select_package required Assimp libassimp-dev select_package required OpenGL libgl1-mesa-dev libgl-dev select_package required 'XCB integration' libxcb-xinerama0 select_package optional 'XCB cursor' libxcb-cursor0 select_package optional 'Qt Wayland integration' qtwayland5 select_package optional 'Qt multimedia backend' libqt5multimedia5-plugins select_package optional 'GStreamer base codecs' gstreamer1.0-plugins-base select_package optional 'GStreamer additional codecs' gstreamer1.0-plugins-good select_package optional 'Mesa diagnostics' mesa-utils select_package optional 'Chinese fonts' fonts-noto-cjk fonts-wqy-microhei select_package required 'ELF inspection' file select_package required 'Python examples' python3 if (( WITH_CREATOR )); then select_package required QtCreator qtcreator; fi else select_package required 'C compiler' gcc select_package required 'C++ compiler' gcc-c++ select_package required CMake cmake cmake3 select_package required 'build tool' ninja-build make select_package required pkg-config pkgconf-pkg-config pkgconfig select_package required 'Qt5 base development' qt5-qtbase-devel select_package required 'Qt5 multimedia' qt5-qtmultimedia-devel select_package required 'Qt5 3D modules' qt5-qt3d-devel select_package required 'Qt3D runtime/plugins' qt5-qt3d select_package required 'Protobuf development' protobuf-devel select_package required protoc protobuf-compiler select_package required 'gRPC development' grpc-devel select_package optional 'gRPC C++ split package' grpc-c++-devel grpc-c++ select_package required 'gRPC code generator' grpc-plugins select_package required FFmpeg ffmpeg-devel select_package required urdfdom urdfdom-devel select_package required TinyXML tinyxml-devel select_package required Assimp assimp-devel select_package required OpenGL mesa-libGL-devel libglvnd-devel select_package required 'XCB integration' xcb-util-wm select_package optional 'XCB image support' xcb-util-image select_package optional 'XCB key support' xcb-util-keysyms select_package optional 'XCB render support' xcb-util-renderutil select_package optional 'XCB cursor' xcb-util-cursor select_package optional 'Qt5 graphics integration' qt5-qtbase-gui select_package optional 'Qt Wayland integration' qt5-qtwayland select_package optional 'GStreamer base codecs' gstreamer1-plugins-base select_package optional 'GStreamer additional codecs' gstreamer1-plugins-good select_package optional 'Mesa diagnostics' glx-utils select_package optional 'Chinese fonts' google-noto-sans-cjk-fonts wqy-microhei-fonts select_package required 'ELF inspection' file select_package required 'Python examples' python3 if (( WITH_CREATOR )); then select_package required QtCreator qt-creator qtcreator; fi fi if ((${#MISSING[@]})); then printf '\nRequired packages unavailable in this system\047s repositories:\n' >&2 printf ' %s\n' "${MISSING[@]}" >&2 fail 'Ask the system administrator to enable the matching Kylin development/media repositories, then rerun. No foreign repositories were added.' return 1 fi } install_packages() { local -a privilege=() if (( EUID != 0 )); then have sudo || { fail 'Package installation requires root or sudo.'; return 1; } privilege=(sudo) fi if (( UPDATE_METADATA )); then log 'Refreshing configured repository metadata...' if [[ $PM == apt ]]; then "${privilege[@]}" apt-get update else "${privilege[@]}" "$PM" makecache fi fi resolve_packages log 'Installing Qt5 and CMVR dependencies...' if [[ $PM == apt ]]; then "${privilege[@]}" apt-get install -y --no-install-recommends "${PACKAGES[@]}" else "${privilege[@]}" "$PM" install -y "${PACKAGES[@]}" fi } version_at_least() { [[ $(printf '%s\n%s\n' "$2" "$1" | sort -V | head -n 1) == "$2" ]] } probe_dependencies() { local tool qt_version cmake_version tmp status CMAKE_TOOL='' for tool in cmake cmake3; do if have "$tool"; then cmake_version=$("$tool" --version | awk 'NR==1 {print $3}') if version_at_least "$cmake_version" 3.16; then CMAKE_TOOL=$(command -v "$tool"); break; fi fi done [[ -n $CMAKE_TOOL ]] || { fail 'CMake >= 3.16 is required; repository package is too old or missing.'; return 1; } QMAKE_TOOL='' for tool in qmake-qt5 qmake /usr/lib64/qt5/bin/qmake /usr/lib/qt5/bin/qmake; do if have "$tool"; then qt_version=$("$tool" -query QT_VERSION 2>/dev/null) || continue if [[ $qt_version == 5.* ]] && version_at_least "$qt_version" 5.12; then QMAKE_TOOL=$(command -v "$tool"); break fi fi done [[ -n $QMAKE_TOOL ]] || { fail 'Qt 5.12+ qmake is required. Qt6 alone cannot build this Qt5 project.'; return 1; } log "Using Qt $qt_version ($QMAKE_TOOL); CMake $cmake_version" for tool in c++ pkg-config protoc grpc_cpp_plugin file python3; do have "$tool" || { fail "Missing executable: $tool"; return 1; } done if ! python3 -c 'import sys; sys.exit(0 if sys.version_info >= (3,9) else 1)'; then log 'Python is older than 3.9: Qt UI builds can continue, but the classroom example requires Python 3.9+.' fi tmp=$(mktemp -d "${TMPDIR:-/tmp}/cmvr-dependency-probe.XXXXXXXX") cat > "$tmp/CMakeLists.txt" <<'CMAKE' cmake_minimum_required(VERSION 3.16) project(CmvrDependencyProbe LANGUAGES CXX) set(CMAKE_CXX_STANDARD 17) find_package(Qt5 5.12 REQUIRED COMPONENTS Core Gui Widgets Network Multimedia 3DCore 3DRender 3DExtras 3DInput 3DAnimation) find_package(Protobuf CONFIG QUIET) if(NOT Protobuf_FOUND) find_package(Protobuf REQUIRED) endif() find_package(gRPC CONFIG REQUIRED) if(NOT TARGET protobuf::protoc OR NOT TARGET gRPC::grpc_cpp_plugin) message(FATAL_ERROR "Protobuf/gRPC compiler CMake targets are missing") endif() find_package(PkgConfig REQUIRED) pkg_check_modules(FFMPEG REQUIRED libavcodec libavutil libswscale) find_path(URDF_INCLUDE urdf_parser/urdf_parser.h PATH_SUFFIXES include include/urdfdom) find_path(TINYXML_INCLUDE tinyxml.h PATH_SUFFIXES include) find_library(URDF_LIBRARY urdfdom_model) if(NOT URDF_INCLUDE OR NOT TINYXML_INCLUDE OR NOT URDF_LIBRARY) message(FATAL_ERROR "urdfdom or TinyXML development files are missing") endif() execute_process(COMMAND "${PROBE_QMAKE}" -query QT_INSTALL_PLUGINS OUTPUT_VARIABLE PLUGINS OUTPUT_STRIP_TRAILING_WHITESPACE) if(NOT EXISTS "${PLUGINS}/platforms/libqxcb.so") message(FATAL_ERROR "Qt xcb platform plugin is missing") endif() if(NOT EXISTS "${PLUGINS}/sceneparsers/libassimpsceneimport.so") message(FATAL_ERROR "Qt3D Assimp scene parser is missing; install the distribution's matching Qt3D mesh plugin") endif() message(STATUS "CMVR dependency probe passed") CMAKE status=0 "$CMAKE_TOOL" -S "$tmp" -B "$tmp/build" "-DPROBE_QMAKE=$QMAKE_TOOL" || status=$? # Remove only the private directory just created by mktemp. if [[ $tmp == "${TMPDIR:-/tmp}"/cmvr-dependency-probe.* ]]; then rm -rf -- "$tmp"; fi (( status == 0 )) || { fail 'Dependency probe failed; review the CMake error above. Installation is not declared complete.'; return 1; } check_plugin "$($QMAKE_TOOL -query QT_INSTALL_PLUGINS)/platforms/libqxcb.so" check_plugin "$($QMAKE_TOOL -query QT_INSTALL_PLUGINS)/sceneparsers/libassimpsceneimport.so" } check_plugin() { local file=$1 output [[ -f $file ]] || { fail "File not found: $file"; return 1; } if ! output=$(ldd "$file" 2>&1); then printf '%s\n' "$output" >&2; fail "Cannot resolve ELF dependencies: $file"; return 1 fi if [[ $output == *'not found'* ]]; then printf '%s\n' "$output" >&2; fail "Missing shared libraries: $file"; return 1 fi } check_app() { local app=$1 description [[ -f $app ]] || { fail "Application not found: $app"; return 1; } description=$(file -Lb -- "$app") [[ $description == ELF* ]] || { fail 'Provide a native Linux ELF application, not the Windows .exe.'; return 1; } if [[ $ARCH != x86_64 && $description == *64-bit* ]]; then fail 'A 64-bit application cannot run on this 32-bit OS.'; return 1; fi check_plugin "$app" log "ELF loader dependencies resolved: $app" log 'If symbol/GLIBC/Qt ABI errors remain, rebuild from source on this machine. File presence alone does not prove ABI compatibility.' } build_source() { local source=$1 output_dir build_dir [[ -f $source/CMakeLists.txt && -f $source/src/main.cpp && -d $source/protos/cmvr ]] || { fail '--source must point to the complete qt_client source tree.'; return 1; } build_dir=$source/build/kylin-release log "Configuring Release in $build_dir" "$CMAKE_TOOL" -S "$source" -B "$build_dir" -DCMAKE_BUILD_TYPE=Release -DCMVR_WITH_MSQUIC=OFF "$CMAKE_TOOL" --build "$build_dir" --parallel "$JOBS" APP=$source/bin/release/CmvrControlStation check_app "$APP" output_dir=$(dirname -- "$APP") cat > "$output_dir/run_cmvr.sh" <<'LAUNCHER' #!/usr/bin/env bash set -euo pipefail cd -- "$(dirname -- "$0")" if [[ ${CMVR_SOFTWARE_OPENGL:-0} == 1 ]]; then export QT_OPENGL=software LIBGL_ALWAYS_SOFTWARE=1 fi exec ./CmvrControlStation "$@" LAUNCHER chmod u+x -- "$output_dir/run_cmvr.sh" log "Ready: bash \"$output_dir/run_cmvr.sh\"" log 'For offline class: select Demo mode before connecting. This script does not launch the GUI or connect to devices.' } main() { local source='' arg APP=''; JOBS=4; WITH_CREATOR=0; UPDATE_METADATA=1; CHECK_ONLY=0; DRY_RUN=0 while (($#)); do arg=$1; shift case "$arg" in --source|--app|--jobs) (($#)) || { fail "Missing value for $arg"; return 2; } case "$arg" in --source) source=$1 ;; --app) APP=$1 ;; --jobs) JOBS=$1 ;; esac shift ;; --with-qtcreator) WITH_CREATOR=1 ;; --no-update) UPDATE_METADATA=0 ;; --check) CHECK_ONLY=1 ;; --dry-run) DRY_RUN=1 ;; -h|--help) usage; return 0 ;; *) fail "Unknown option: $arg"; usage; return 2 ;; esac done [[ $JOBS =~ ^[1-9][0-9]*$ ]] || { fail '--jobs must be a positive integer.'; return 2; } (( !(CHECK_ONLY && DRY_RUN) )) || { fail 'Choose either --check or --dry-run.'; return 2; } if [[ -n $source ]]; then [[ -d $source ]] || { fail "Source directory does not exist: $source"; return 2; } source=$(cd -- "$source" && pwd -P) [[ -f $source/CMakeLists.txt && -f $source/src/main.cpp ]] || { fail 'Source tree is incomplete.'; return 2; } fi if [[ -n $APP ]]; then [[ -f $APP ]] || { fail "Application does not exist: $APP"; return 2; } APP=$(cd -- "$(dirname -- "$APP")" && printf '%s/%s' "$PWD" "$(basename -- "$APP")") fi detect_platform if (( DRY_RUN )); then resolve_packages log 'Dry run: required/available package plan (current metadata only)' printf ' %s\n' "${PACKAGES[@]}" log 'No sudo, installation, build or GUI launch was performed.' return 0 fi if (( ! CHECK_ONLY )); then install_packages; fi probe_dependencies if [[ -n $APP ]]; then check_app "$APP"; fi if [[ -n $source ]] && (( ! CHECK_ONLY )); then build_source "$source"; fi log 'Dependency checks passed. No devices were controlled.' if [[ -z $source && -z $APP ]]; then log 'To build: bash setup_kylin.sh --source /path/to/qt_client' log 'To check a Linux release: bash setup_kylin.sh --check --app /path/to/CmvrControlStation' fi } if [[ ${BASH_SOURCE[0]} == "$0" ]]; then main "$@"; fi