#!/bin/bash # build_config.sh - Build and install configuration for USM and dependencies # Build order - dependencies must be built in this order BUILD_ORDER=("invercargill" "invercargill-json" "usm") # Component definitions declare -A COMPONENTS=( ["invercargill"]="Invercargill|invercargill-1|https://fabrica.unitatem.net/Tilo15/Invercargill.git" ["invercargill-json"]="Invercargill-Json|invercargill-json|https://git.sr.ht/~tilo15/Invercargill-Json" ["usm"]="USM|usm|BUNDLED" ) # Component properties accessor functions get_component_name() { local key="$1" echo "${COMPONENTS[$key]}" | cut -d'|' -f1 } get_component_vapi() { local key="$1" echo "${COMPONENTS[$key]}" | cut -d'|' -f2 } get_component_source() { local key="$1" echo "${COMPONENTS[$key]}" | cut -d'|' -f3 } # Get output redirection based on VERBOSE flag get_output_redirect() { if [[ "$VERBOSE" == "true" ]]; then echo "/dev/stdout" else echo "/dev/null" fi } # Build function for Invercargill build_invercargill() { local src_dir="$1" local prefix="$2" local output_redirect=$(get_output_redirect) pushd "$src_dir" >/dev/null || return 1 # Configure with meson (force libdir=lib for consistency across platforms) if ! meson setup src builddir --prefix="$prefix" --libdir=lib -Ddefault_library=static >"$output_redirect" 2>&1; then log_error "Failed to configure Invercargill" [[ "$VERBOSE" != "true" ]] && log_info "Run with --verbose for details" popd >/dev/null return 1 fi # Build with ninja if ! ninja -C builddir >"$output_redirect" 2>&1; then log_error "Failed to build Invercargill" [[ "$VERBOSE" != "true" ]] && log_info "Run with --verbose for details" popd >/dev/null return 1 fi # Install if ! ninja -C builddir install >"$output_redirect" 2>&1; then log_error "Failed to install Invercargill" [[ "$VERBOSE" != "true" ]] && log_info "Run with --verbose for details" popd >/dev/null return 1 fi popd >/dev/null log_info "Invercargill built and installed successfully" return 0 } # Build function for Invercargill-Json build_invercargill_json() { local src_dir="$1" local prefix="$2" local output_redirect=$(get_output_redirect) pushd "$src_dir" >/dev/null || return 1 # Set PKG_CONFIG_PATH to find bundled Invercargill library export PKG_CONFIG_PATH="$prefix/lib/pkgconfig:$prefix/share/pkgconfig${PKG_CONFIG_PATH:+:$PKG_CONFIG_PATH}" # Set XDG_DATA_DIRS for GObject Introspection and Vala to find .gir and .vapi files # Include default system paths to ensure system vapi files are found export XDG_DATA_DIRS="$prefix/share${XDG_DATA_DIRS:+:$XDG_DATA_DIRS}:/usr/local/share:/usr/share" # Configure with meson (force libdir=lib for consistency across platforms) if ! meson setup src builddir --prefix="$prefix" --libdir=lib -Ddefault_library=static >"$output_redirect" 2>&1; then log_error "Failed to configure Invercargill-Json" [[ "$VERBOSE" != "true" ]] && log_info "Run with --verbose for details" popd >/dev/null return 1 fi # Build with ninja if ! ninja -C builddir >"$output_redirect" 2>&1; then log_error "Failed to build Invercargill-Json" [[ "$VERBOSE" != "true" ]] && log_info "Run with --verbose for details" popd >/dev/null return 1 fi # Install if ! ninja -C builddir install >"$output_redirect" 2>&1; then log_error "Failed to install Invercargill-Json" [[ "$VERBOSE" != "true" ]] && log_info "Run with --verbose for details" popd >/dev/null return 1 fi popd >/dev/null log_info "Invercargill-Json built and installed successfully" return 0 } # Build function for USM build_usm() { local src_dir="$1" local prefix="$2" local output_redirect=$(get_output_redirect) pushd "$src_dir" >/dev/null || return 1 # Set PKG_CONFIG_PATH to find bundled Invercargill libraries export PKG_CONFIG_PATH="$prefix/lib/pkgconfig:$prefix/share/pkgconfig${PKG_CONFIG_PATH:+:$PKG_CONFIG_PATH}" # Set XDG_DATA_DIRS for GObject Introspection and Vala to find .gir and .vapi files # Include default system paths to ensure system vapi files are found export XDG_DATA_DIRS="$prefix/share${XDG_DATA_DIRS:+:$XDG_DATA_DIRS}:/usr/local/share:/usr/share" # Configure with meson (USM's meson.build is at root, force libdir=lib for consistency) if ! meson setup builddir --prefix="$prefix" --libdir=lib >"$output_redirect" 2>&1; then log_error "Failed to configure USM" [[ "$VERBOSE" != "true" ]] && log_info "Run with --verbose for details" popd >/dev/null return 1 fi # Build with ninja if ! ninja -C builddir >"$output_redirect" 2>&1; then log_error "Failed to build USM" [[ "$VERBOSE" != "true" ]] && log_info "Run with --verbose for details" popd >/dev/null return 1 fi # Install if ! ninja -C builddir install >"$output_redirect" 2>&1; then log_error "Failed to install USM" [[ "$VERBOSE" != "true" ]] && log_info "Run with --verbose for details" popd >/dev/null return 1 fi popd >/dev/null log_info "USM built and installed successfully" return 0 } # Generic build dispatcher build_component() { local component="$1" local src_dir="$2" local prefix="$3" case "$component" in "invercargill") build_invercargill "$src_dir" "$prefix" ;; "invercargill-json") build_invercargill_json "$src_dir" "$prefix" ;; "usm") build_usm "$src_dir" "$prefix" ;; *) log_error "Unknown component: $component" return 1 ;; esac } # Install shim to /usr/bin install_shim() { local target_dir="$1" local shim_path="/usr/bin/usm" local sudo="" if ! is_root; then sudo=$(get_sudo) fi log_step "Installing shim to $shim_path..." local shim_content="#!/bin/bash export LD_LIBRARY_PATH=\"${target_dir}/lib\${LD_LIBRARY_PATH:+:\$LD_LIBRARY_PATH}\" exec \"${target_dir}/bin/usm\" \"\$@\" " if [[ -n "$sudo" ]]; then echo "$shim_content" | $sudo tee "$shim_path" >/dev/null $sudo chmod +x "$shim_path" else echo "$shim_content" > "$shim_path" chmod +x "$shim_path" fi log_info "Shim installed to $shim_path" } # Build all components in order build_all() { local src_base="$1" local prefix="$2" local total=${#BUILD_ORDER[@]} local current=0 for component in "${BUILD_ORDER[@]}"; do ((current++)) log_step "[$current/$total] Building $(get_component_name "$component")..." local src_dir if [[ "$(get_component_source "$component")" == "BUNDLED" ]]; then src_dir="$src_base/usm" else src_dir="$src_base/$(get_component_name "$component")" fi if ! build_component "$component" "$src_dir" "$prefix"; then log_error "Failed to build $(get_component_name "$component"). Installation halted." return 1 fi done return 0 }