| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413 |
- #!/usr/bin/env bash
- #
- # packaging/build.sh — Top-level build orchestrator for smartbotic-database
- #
- # Supports two modes:
- # Default: Docker build targeting Debian 13 (production .debs)
- # --local: Native build on current system (development/testing)
- #
- set -euo pipefail
- # ---------------------------------------------------------------------------
- # Paths
- # ---------------------------------------------------------------------------
- SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
- PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
- BASE_IMAGE_NAME="smartbotic-db-build-base:debian13"
- BUILD_JOBS="${BUILD_JOBS:-$(nproc)}"
- BUILD_DIR="$PROJECT_DIR/build"
- # Where the smartbotics-deb-repo checkout lives. Only --repo / --sync need it.
- #
- # No absolute default: the old one (/data/smartbotics-deb-repo) was one
- # machine's layout, and when that checkout moved, a publish failed only AFTER
- # a full package build. Resolution order: $DEB_REPO_DIR, then a checkout next
- # to this one. Resolved and validated before anything is built.
- DEB_REPO_DIR="${DEB_REPO_DIR:-}"
- # Output directories — separate to prevent local/Docker overwriting each other
- # dist/local/ — native build for current system
- # dist/debian13/ — Docker build for Debian 13 production
- DIST_BASE="${PROJECT_DIR}/dist"
- VERSION="$(cat "$PROJECT_DIR/VERSION")"
- GIT_COMMIT="$(git -C "$PROJECT_DIR" rev-parse --short HEAD 2>/dev/null || echo "unknown")"
- # ---------------------------------------------------------------------------
- # Defaults
- # ---------------------------------------------------------------------------
- LOCAL_MODE=0
- INSTALL_AFTER=0
- SKIP_TESTS=0
- NO_PACKAGE=0
- DEB_REVISION=1
- REBUILD_BASE=0
- NO_CACHE=0
- DO_REPO=0
- SUITE=""
- DO_SYNC=0
- # ---------------------------------------------------------------------------
- # Colored logging
- # ---------------------------------------------------------------------------
- log_info() { printf '\033[1;34m[INFO]\033[0m %s\n' "$*"; }
- log_success() { printf '\033[1;32m[OK]\033[0m %s\n' "$*"; }
- log_warn() { printf '\033[1;33m[WARN]\033[0m %s\n' "$*"; }
- log_error() { printf '\033[1;31m[ERROR]\033[0m %s\n' "$*"; }
- # ---------------------------------------------------------------------------
- # Usage
- # ---------------------------------------------------------------------------
- usage() {
- cat <<EOF
- Usage: packaging/build.sh [OPTIONS]
- Build modes:
- (default) Docker build targeting Debian 13 (production)
- --local Native build for current system (development)
- Options:
- --install Install .debs locally after build (--local only)
- --no-package Stop after the build: no .debs (--local only)
- --skip-tests Skip running tests
- --deb-revision N Debian revision suffix (default: 1)
- --rebuild-base Force rebuild Docker base image
- --no-cache Build without Docker caches
- --repo Add packages to local APT repository
- --suite NAME Distribution suite (e.g., trixie)
- --sync Add to repo and upload to repository.smartbotics.ai
- Environment variables:
- BUILD_JOBS Parallel build jobs (default: nproc)
- DEB_REPO_DIR Path to a smartbotics-deb-repo checkout, for --repo/--sync
- (default: ../smartbotics-deb-repo next to this checkout)
- If this host cannot build natively (missing yyjson/lmdb, a different protobuf),
- use packaging/dev.sh, which runs the same --local build inside the Debian 13
- build image.
- EOF
- exit 0
- }
- # ---------------------------------------------------------------------------
- # Argument parsing
- # ---------------------------------------------------------------------------
- while [[ $# -gt 0 ]]; do
- case "$1" in
- --local) LOCAL_MODE=1; shift ;;
- --install) INSTALL_AFTER=1; shift ;;
- --skip-tests) SKIP_TESTS=1; shift ;;
- --no-package) NO_PACKAGE=1; shift ;;
- --deb-revision) DEB_REVISION="$2"; shift 2 ;;
- --rebuild-base) REBUILD_BASE=1; shift ;;
- --no-cache) NO_CACHE=1; shift ;;
- --repo) DO_REPO=1; shift ;;
- --suite) SUITE="$2"; shift 2 ;;
- --sync) DO_SYNC=1; shift ;;
- -h|--help) usage ;;
- *)
- log_error "Unknown option: $1"
- usage
- ;;
- esac
- done
- # Validate option combinations
- if [[ $INSTALL_AFTER -eq 1 && $LOCAL_MODE -eq 0 ]]; then
- log_error "--install is only supported with --local"
- exit 1
- fi
- if [[ $NO_PACKAGE -eq 1 && $LOCAL_MODE -eq 0 ]]; then
- log_error "--no-package is only supported with --local"
- exit 1
- fi
- if [[ $NO_PACKAGE -eq 1 && $INSTALL_AFTER -eq 1 ]]; then
- log_error "--no-package and --install contradict each other"
- exit 1
- fi
- if [[ $DO_SYNC -eq 1 && $DO_REPO -eq 0 ]]; then
- DO_REPO=1
- fi
- if [[ $DO_REPO -eq 1 && $NO_PACKAGE -eq 1 ]]; then
- log_error "--repo/--sync need packages; drop --no-package"
- exit 1
- fi
- if [[ $DO_REPO -eq 1 && -z "$SUITE" ]]; then
- log_error "--repo requires --suite NAME"
- exit 1
- fi
- # Find the apt repository tooling NOW, not after the build. A publish that
- # cannot possibly succeed should cost seconds, not a full package build.
- if [[ $DO_REPO -eq 1 ]]; then
- if [[ -z "$DEB_REPO_DIR" ]]; then
- DEB_REPO_DIR="$(dirname "$PROJECT_DIR")/smartbotics-deb-repo"
- fi
- for tool in add-packages.sh create-repo.sh sync-repo.sh; do
- if [[ ! -x "$DEB_REPO_DIR/scripts/$tool" ]]; then
- log_error "apt repository tooling not found: $DEB_REPO_DIR/scripts/$tool"
- log_error "Set DEB_REPO_DIR to your smartbotics-deb-repo checkout, e.g."
- log_error " DEB_REPO_DIR=/path/to/smartbotics-deb-repo $0 --repo --suite $SUITE"
- exit 1
- fi
- done
- DEB_REPO_DIR="$(cd "$DEB_REPO_DIR" && pwd)"
- fi
- # ---------------------------------------------------------------------------
- # Set output directory based on mode
- # ---------------------------------------------------------------------------
- if [[ $LOCAL_MODE -eq 1 ]]; then
- OUTPUT_DIR="${DIST_BASE}/local"
- else
- OUTPUT_DIR="${DIST_BASE}/debian13"
- fi
- if [[ $NO_PACKAGE -eq 0 ]]; then
- mkdir -p "$OUTPUT_DIR"
- fi
- # ---------------------------------------------------------------------------
- # Banner
- # ---------------------------------------------------------------------------
- log_info "smartbotic-database build v${VERSION} (${GIT_COMMIT})"
- log_info "Build jobs: ${BUILD_JOBS}"
- if [[ $LOCAL_MODE -eq 1 ]]; then
- log_info "Mode: local (native)"
- else
- log_info "Mode: Docker (Debian 13)"
- fi
- if [[ $NO_PACKAGE -eq 0 ]]; then
- log_info "Output: ${OUTPUT_DIR}"
- fi
- # ---------------------------------------------------------------------------
- # Local build
- # ---------------------------------------------------------------------------
- # A CMake build tree belongs to the place and the toolchain that configured it.
- # It records absolute source and binary paths and refuses to be used from
- # anywhere else, and it caches every library path it found. Both go stale for
- # ordinary reasons:
- #
- # - the checkout was moved or renamed, or the tree was built in a container
- # that mounted the repository at a different path;
- # - the tree was built in the build image (packaging/dev.sh) and someone then
- # ran cmake against it on the host, or the other way round. The paths match
- # but the cache now holds another system's libraries.
- #
- # CMake's own errors for these do not say what to do. Dropping the cache makes
- # the next configure start clean; nothing else needs removing, because every
- # compile command changes and Ninja rebuilds what is affected.
- #
- # The second case is detected by the CMake version that last wrote the cache.
- # Two environments sharing one tree almost never share a CMake version, and a
- # cache written by a different CMake is worth a clean configure regardless.
- forget_foreign_build_tree() {
- local cache="$BUILD_DIR/CMakeCache.txt"
- [[ -f "$cache" ]] || return 0
- cached() { sed -n "s/^$1:[A-Z]*=//p" "$cache" | head -n1; }
- local why=""
- local configured_src configured_bin
- configured_src="$(cached CMAKE_HOME_DIRECTORY)"
- configured_bin="$(cached CMAKE_CACHEFILE_DIR)"
- if [[ "$configured_src" != "$PROJECT_DIR" || "$configured_bin" != "$BUILD_DIR" ]]; then
- why="it was configured for ${configured_src:-an unknown location}, not $PROJECT_DIR"
- else
- local wrote running
- wrote="$(cached CMAKE_CACHE_MAJOR_VERSION).$(cached CMAKE_CACHE_MINOR_VERSION).$(cached CMAKE_CACHE_PATCH_VERSION)"
- running="$(cmake --version | sed -n '1s/^cmake version //p')"
- if [[ "$wrote" != "$running" ]]; then
- why="it was last configured by CMake $wrote and this is CMake $running (a different system)"
- fi
- fi
- [[ -n "$why" ]] || return 0
- log_warn "build/ cannot be reused: $why."
- log_warn "Reconfiguring from scratch; anything whose build command changed is rebuilt."
- rm -f -- "$cache"
- }
- build_local() {
- forget_foreign_build_tree
- log_info "Configuring CMake (Release)..."
- cmake -B "$BUILD_DIR" -G Ninja \
- -DCMAKE_BUILD_TYPE=Release \
- -DSMARTBOTIC_DB_BUILD_SERVICE=ON \
- -DSMARTBOTIC_DB_BUILD_CLIENT=ON \
- -DSMARTBOTIC_DB_BUILD_CLI=ON \
- -DSMARTBOTIC_DB_BUILD_TESTS=ON \
- -DBUILD_SHARED_LIBS=ON \
- -S "$PROJECT_DIR"
- log_info "Building with ${BUILD_JOBS} jobs..."
- cmake --build "$BUILD_DIR" -j"$BUILD_JOBS"
- log_success "Build completed"
- if [[ $SKIP_TESTS -eq 0 ]]; then
- log_info "Running tests..."
- LD_LIBRARY_PATH="$BUILD_DIR/client" \
- "$BUILD_DIR/tests/test_vector_storage"
- log_success "Tests passed"
- else
- log_warn "Tests skipped (--skip-tests)"
- fi
- if [[ $NO_PACKAGE -eq 1 ]]; then
- log_info "Packaging skipped (--no-package)"
- return 0
- fi
- log_info "Creating .deb packages..."
- OUTPUT_DIR="$OUTPUT_DIR" BUILD_DEB_REVISION="$DEB_REVISION" "$PROJECT_DIR/packaging/deb/create-debs.sh"
- log_success "Packages created"
- if [[ $INSTALL_AFTER -eq 1 ]]; then
- log_info "Installing .deb packages..."
- sudo dpkg -i \
- "$OUTPUT_DIR"/libsmartbotic-db-client_*.deb \
- "$OUTPUT_DIR"/libsmartbotic-db-client-dev_*.deb \
- "$OUTPUT_DIR"/smartbotic-db-cli_*.deb \
- "$OUTPUT_DIR"/smartbotic-database_*.deb \
- 2>/dev/null || sudo apt-get install -f -y
- log_success "Packages installed"
- fi
- }
- # ---------------------------------------------------------------------------
- # Docker build
- # ---------------------------------------------------------------------------
- build_docker() {
- if ! command -v docker &>/dev/null; then
- log_error "Docker is not installed or not in PATH"
- exit 1
- fi
- # Check / build base image
- if [[ $REBUILD_BASE -eq 1 ]] || ! docker image inspect "$BASE_IMAGE_NAME" &>/dev/null; then
- if [[ $REBUILD_BASE -eq 1 ]]; then
- log_info "Rebuilding base image (--rebuild-base)..."
- else
- log_info "Base image not found, building ${BASE_IMAGE_NAME}..."
- fi
- local base_cache_args=()
- if [[ $NO_CACHE -eq 1 ]]; then
- base_cache_args+=("--no-cache")
- fi
- docker buildx build \
- -f "$PROJECT_DIR/packaging/Dockerfile.base" \
- -t "$BASE_IMAGE_NAME" \
- "${base_cache_args[@]+"${base_cache_args[@]}"}" \
- "$PROJECT_DIR"
- log_success "Base image built: ${BASE_IMAGE_NAME}"
- else
- log_info "Using existing base image: ${BASE_IMAGE_NAME}"
- fi
- # Build packages
- log_info "Building packages in Docker..."
- local build_cache_args=()
- if [[ $NO_CACHE -eq 1 ]]; then
- build_cache_args+=("--no-cache")
- fi
- docker buildx build \
- -f "$PROJECT_DIR/packaging/Dockerfile.build" \
- --build-arg "BASE_IMAGE=${BASE_IMAGE_NAME}" \
- --build-arg "BUILD_VERSION=${VERSION}" \
- --build-arg "BUILD_DEB_REVISION=${DEB_REVISION}" \
- --build-arg "BUILD_JOBS=${BUILD_JOBS}" \
- --build-arg "BUILD_GIT_COMMIT=${GIT_COMMIT}" \
- --target packages \
- --output "type=local,dest=$OUTPUT_DIR" \
- "${build_cache_args[@]+"${build_cache_args[@]}"}" \
- "$PROJECT_DIR"
- log_success "Docker build completed"
- }
- # ---------------------------------------------------------------------------
- # Main
- # ---------------------------------------------------------------------------
- if [[ $LOCAL_MODE -eq 1 ]]; then
- build_local
- else
- build_docker
- fi
- if [[ $NO_PACKAGE -eq 1 ]]; then
- echo ""
- log_success "Build complete: $BUILD_DIR"
- exit 0
- fi
- # ---------------------------------------------------------------------------
- # List created packages
- # ---------------------------------------------------------------------------
- # Only what THIS run produced. dist/ accumulates every version ever built, and
- # both the listing and the publish step used to take the whole directory - so a
- # publish re-staged every historical package, and anything else sitting in
- # dist/ went to the repository with it.
- shopt -s nullglob
- BUILT_DEBS=("$OUTPUT_DIR"/*_"${VERSION}-${DEB_REVISION}"_*.deb)
- shopt -u nullglob
- if [[ ${#BUILT_DEBS[@]} -eq 0 ]]; then
- log_error "No ${VERSION}-${DEB_REVISION} .deb files found in $OUTPUT_DIR"
- exit 1
- fi
- echo ""
- log_info "Created packages:"
- ls -lh "${BUILT_DEBS[@]}"
- # ---------------------------------------------------------------------------
- # Repository operations
- # ---------------------------------------------------------------------------
- if [[ $DO_REPO -eq 1 ]]; then
- echo ""
- log_info "Adding packages to repository (suite: ${SUITE})..."
- "$DEB_REPO_DIR/scripts/add-packages.sh" "${BUILT_DEBS[@]}"
- log_success "Packages added to staging"
- "$DEB_REPO_DIR/scripts/create-repo.sh" --suite "$SUITE"
- log_success "Repository created for suite: ${SUITE}"
- if [[ $DO_SYNC -eq 1 ]]; then
- log_info "Syncing repository to repository.smartbotics.ai..."
- "$DEB_REPO_DIR/scripts/sync-repo.sh"
- log_success "Repository synced"
- fi
- fi
- # ---------------------------------------------------------------------------
- # Next steps
- # ---------------------------------------------------------------------------
- echo ""
- log_success "Build complete! (v${VERSION}-${DEB_REVISION})"
- echo ""
- if [[ $LOCAL_MODE -eq 1 && $INSTALL_AFTER -eq 0 ]]; then
- log_info "To install locally:"
- echo " sudo dpkg -i ${OUTPUT_DIR}/*.deb || sudo apt-get install -f -y"
- echo ""
- fi
- if [[ $DO_REPO -eq 0 ]]; then
- log_info "To publish to repository:"
- echo " packaging/build.sh --repo --suite trixie"
- echo ""
- fi
- if [[ $DO_REPO -eq 1 && $DO_SYNC -eq 0 ]]; then
- log_info "To sync repository to remote:"
- echo " packaging/build.sh --repo --suite ${SUITE} --sync"
- echo ""
- fi
|