build.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. #!/usr/bin/env bash
  2. #
  3. # packaging/build.sh — Top-level build orchestrator for smartbotic-database
  4. #
  5. # Supports two modes:
  6. # Default: Docker build targeting Debian 13 (production .debs)
  7. # --local: Native build on current system (development/testing)
  8. #
  9. set -euo pipefail
  10. # ---------------------------------------------------------------------------
  11. # Paths
  12. # ---------------------------------------------------------------------------
  13. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  14. PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
  15. BASE_IMAGE_NAME="smartbotic-db-build-base:debian13"
  16. BUILD_JOBS="${BUILD_JOBS:-$(nproc)}"
  17. BUILD_DIR="$PROJECT_DIR/build"
  18. # Where the smartbotics-deb-repo checkout lives. Only --repo / --sync need it.
  19. #
  20. # No absolute default: the old one (/data/smartbotics-deb-repo) was one
  21. # machine's layout, and when that checkout moved, a publish failed only AFTER
  22. # a full package build. Resolution order: $DEB_REPO_DIR, then a checkout next
  23. # to this one. Resolved and validated before anything is built.
  24. DEB_REPO_DIR="${DEB_REPO_DIR:-}"
  25. # Output directories — separate to prevent local/Docker overwriting each other
  26. # dist/local/ — native build for current system
  27. # dist/debian13/ — Docker build for Debian 13 production
  28. DIST_BASE="${PROJECT_DIR}/dist"
  29. VERSION="$(cat "$PROJECT_DIR/VERSION")"
  30. GIT_COMMIT="$(git -C "$PROJECT_DIR" rev-parse --short HEAD 2>/dev/null || echo "unknown")"
  31. # ---------------------------------------------------------------------------
  32. # Defaults
  33. # ---------------------------------------------------------------------------
  34. LOCAL_MODE=0
  35. INSTALL_AFTER=0
  36. SKIP_TESTS=0
  37. NO_PACKAGE=0
  38. DEB_REVISION=1
  39. REBUILD_BASE=0
  40. NO_CACHE=0
  41. DO_REPO=0
  42. SUITE=""
  43. DO_SYNC=0
  44. # ---------------------------------------------------------------------------
  45. # Colored logging
  46. # ---------------------------------------------------------------------------
  47. log_info() { printf '\033[1;34m[INFO]\033[0m %s\n' "$*"; }
  48. log_success() { printf '\033[1;32m[OK]\033[0m %s\n' "$*"; }
  49. log_warn() { printf '\033[1;33m[WARN]\033[0m %s\n' "$*"; }
  50. log_error() { printf '\033[1;31m[ERROR]\033[0m %s\n' "$*"; }
  51. # ---------------------------------------------------------------------------
  52. # Usage
  53. # ---------------------------------------------------------------------------
  54. usage() {
  55. cat <<EOF
  56. Usage: packaging/build.sh [OPTIONS]
  57. Build modes:
  58. (default) Docker build targeting Debian 13 (production)
  59. --local Native build for current system (development)
  60. Options:
  61. --install Install .debs locally after build (--local only)
  62. --no-package Stop after the build: no .debs (--local only)
  63. --skip-tests Skip running tests
  64. --deb-revision N Debian revision suffix (default: 1)
  65. --rebuild-base Force rebuild Docker base image
  66. --no-cache Build without Docker caches
  67. --repo Add packages to local APT repository
  68. --suite NAME Distribution suite (e.g., trixie)
  69. --sync Add to repo and upload to repository.smartbotics.ai
  70. Environment variables:
  71. BUILD_JOBS Parallel build jobs (default: nproc)
  72. DEB_REPO_DIR Path to a smartbotics-deb-repo checkout, for --repo/--sync
  73. (default: ../smartbotics-deb-repo next to this checkout)
  74. If this host cannot build natively (missing yyjson/lmdb, a different protobuf),
  75. use packaging/dev.sh, which runs the same --local build inside the Debian 13
  76. build image.
  77. EOF
  78. exit 0
  79. }
  80. # ---------------------------------------------------------------------------
  81. # Argument parsing
  82. # ---------------------------------------------------------------------------
  83. while [[ $# -gt 0 ]]; do
  84. case "$1" in
  85. --local) LOCAL_MODE=1; shift ;;
  86. --install) INSTALL_AFTER=1; shift ;;
  87. --skip-tests) SKIP_TESTS=1; shift ;;
  88. --no-package) NO_PACKAGE=1; shift ;;
  89. --deb-revision) DEB_REVISION="$2"; shift 2 ;;
  90. --rebuild-base) REBUILD_BASE=1; shift ;;
  91. --no-cache) NO_CACHE=1; shift ;;
  92. --repo) DO_REPO=1; shift ;;
  93. --suite) SUITE="$2"; shift 2 ;;
  94. --sync) DO_SYNC=1; shift ;;
  95. -h|--help) usage ;;
  96. *)
  97. log_error "Unknown option: $1"
  98. usage
  99. ;;
  100. esac
  101. done
  102. # Validate option combinations
  103. if [[ $INSTALL_AFTER -eq 1 && $LOCAL_MODE -eq 0 ]]; then
  104. log_error "--install is only supported with --local"
  105. exit 1
  106. fi
  107. if [[ $NO_PACKAGE -eq 1 && $LOCAL_MODE -eq 0 ]]; then
  108. log_error "--no-package is only supported with --local"
  109. exit 1
  110. fi
  111. if [[ $NO_PACKAGE -eq 1 && $INSTALL_AFTER -eq 1 ]]; then
  112. log_error "--no-package and --install contradict each other"
  113. exit 1
  114. fi
  115. if [[ $DO_SYNC -eq 1 && $DO_REPO -eq 0 ]]; then
  116. DO_REPO=1
  117. fi
  118. if [[ $DO_REPO -eq 1 && $NO_PACKAGE -eq 1 ]]; then
  119. log_error "--repo/--sync need packages; drop --no-package"
  120. exit 1
  121. fi
  122. if [[ $DO_REPO -eq 1 && -z "$SUITE" ]]; then
  123. log_error "--repo requires --suite NAME"
  124. exit 1
  125. fi
  126. # Find the apt repository tooling NOW, not after the build. A publish that
  127. # cannot possibly succeed should cost seconds, not a full package build.
  128. if [[ $DO_REPO -eq 1 ]]; then
  129. if [[ -z "$DEB_REPO_DIR" ]]; then
  130. DEB_REPO_DIR="$(dirname "$PROJECT_DIR")/smartbotics-deb-repo"
  131. fi
  132. for tool in add-packages.sh create-repo.sh sync-repo.sh; do
  133. if [[ ! -x "$DEB_REPO_DIR/scripts/$tool" ]]; then
  134. log_error "apt repository tooling not found: $DEB_REPO_DIR/scripts/$tool"
  135. log_error "Set DEB_REPO_DIR to your smartbotics-deb-repo checkout, e.g."
  136. log_error " DEB_REPO_DIR=/path/to/smartbotics-deb-repo $0 --repo --suite $SUITE"
  137. exit 1
  138. fi
  139. done
  140. DEB_REPO_DIR="$(cd "$DEB_REPO_DIR" && pwd)"
  141. fi
  142. # ---------------------------------------------------------------------------
  143. # Set output directory based on mode
  144. # ---------------------------------------------------------------------------
  145. if [[ $LOCAL_MODE -eq 1 ]]; then
  146. OUTPUT_DIR="${DIST_BASE}/local"
  147. else
  148. OUTPUT_DIR="${DIST_BASE}/debian13"
  149. fi
  150. if [[ $NO_PACKAGE -eq 0 ]]; then
  151. mkdir -p "$OUTPUT_DIR"
  152. fi
  153. # ---------------------------------------------------------------------------
  154. # Banner
  155. # ---------------------------------------------------------------------------
  156. log_info "smartbotic-database build v${VERSION} (${GIT_COMMIT})"
  157. log_info "Build jobs: ${BUILD_JOBS}"
  158. if [[ $LOCAL_MODE -eq 1 ]]; then
  159. log_info "Mode: local (native)"
  160. else
  161. log_info "Mode: Docker (Debian 13)"
  162. fi
  163. if [[ $NO_PACKAGE -eq 0 ]]; then
  164. log_info "Output: ${OUTPUT_DIR}"
  165. fi
  166. # ---------------------------------------------------------------------------
  167. # Local build
  168. # ---------------------------------------------------------------------------
  169. # A CMake build tree belongs to the place and the toolchain that configured it.
  170. # It records absolute source and binary paths and refuses to be used from
  171. # anywhere else, and it caches every library path it found. Both go stale for
  172. # ordinary reasons:
  173. #
  174. # - the checkout was moved or renamed, or the tree was built in a container
  175. # that mounted the repository at a different path;
  176. # - the tree was built in the build image (packaging/dev.sh) and someone then
  177. # ran cmake against it on the host, or the other way round. The paths match
  178. # but the cache now holds another system's libraries.
  179. #
  180. # CMake's own errors for these do not say what to do. Dropping the cache makes
  181. # the next configure start clean; nothing else needs removing, because every
  182. # compile command changes and Ninja rebuilds what is affected.
  183. #
  184. # The second case is detected by the CMake version that last wrote the cache.
  185. # Two environments sharing one tree almost never share a CMake version, and a
  186. # cache written by a different CMake is worth a clean configure regardless.
  187. forget_foreign_build_tree() {
  188. local cache="$BUILD_DIR/CMakeCache.txt"
  189. [[ -f "$cache" ]] || return 0
  190. cached() { sed -n "s/^$1:[A-Z]*=//p" "$cache" | head -n1; }
  191. local why=""
  192. local configured_src configured_bin
  193. configured_src="$(cached CMAKE_HOME_DIRECTORY)"
  194. configured_bin="$(cached CMAKE_CACHEFILE_DIR)"
  195. if [[ "$configured_src" != "$PROJECT_DIR" || "$configured_bin" != "$BUILD_DIR" ]]; then
  196. why="it was configured for ${configured_src:-an unknown location}, not $PROJECT_DIR"
  197. else
  198. local wrote running
  199. wrote="$(cached CMAKE_CACHE_MAJOR_VERSION).$(cached CMAKE_CACHE_MINOR_VERSION).$(cached CMAKE_CACHE_PATCH_VERSION)"
  200. running="$(cmake --version | sed -n '1s/^cmake version //p')"
  201. if [[ "$wrote" != "$running" ]]; then
  202. why="it was last configured by CMake $wrote and this is CMake $running (a different system)"
  203. fi
  204. fi
  205. [[ -n "$why" ]] || return 0
  206. log_warn "build/ cannot be reused: $why."
  207. log_warn "Reconfiguring from scratch; anything whose build command changed is rebuilt."
  208. rm -f -- "$cache"
  209. }
  210. build_local() {
  211. forget_foreign_build_tree
  212. log_info "Configuring CMake (Release)..."
  213. cmake -B "$BUILD_DIR" -G Ninja \
  214. -DCMAKE_BUILD_TYPE=Release \
  215. -DSMARTBOTIC_DB_BUILD_SERVICE=ON \
  216. -DSMARTBOTIC_DB_BUILD_CLIENT=ON \
  217. -DSMARTBOTIC_DB_BUILD_CLI=ON \
  218. -DSMARTBOTIC_DB_BUILD_TESTS=ON \
  219. -DBUILD_SHARED_LIBS=ON \
  220. -S "$PROJECT_DIR"
  221. log_info "Building with ${BUILD_JOBS} jobs..."
  222. cmake --build "$BUILD_DIR" -j"$BUILD_JOBS"
  223. log_success "Build completed"
  224. if [[ $SKIP_TESTS -eq 0 ]]; then
  225. log_info "Running tests..."
  226. LD_LIBRARY_PATH="$BUILD_DIR/client" \
  227. "$BUILD_DIR/tests/test_vector_storage"
  228. log_success "Tests passed"
  229. else
  230. log_warn "Tests skipped (--skip-tests)"
  231. fi
  232. if [[ $NO_PACKAGE -eq 1 ]]; then
  233. log_info "Packaging skipped (--no-package)"
  234. return 0
  235. fi
  236. log_info "Creating .deb packages..."
  237. OUTPUT_DIR="$OUTPUT_DIR" BUILD_DEB_REVISION="$DEB_REVISION" "$PROJECT_DIR/packaging/deb/create-debs.sh"
  238. log_success "Packages created"
  239. if [[ $INSTALL_AFTER -eq 1 ]]; then
  240. log_info "Installing .deb packages..."
  241. sudo dpkg -i \
  242. "$OUTPUT_DIR"/libsmartbotic-db-client_*.deb \
  243. "$OUTPUT_DIR"/libsmartbotic-db-client-dev_*.deb \
  244. "$OUTPUT_DIR"/smartbotic-db-cli_*.deb \
  245. "$OUTPUT_DIR"/smartbotic-database_*.deb \
  246. 2>/dev/null || sudo apt-get install -f -y
  247. log_success "Packages installed"
  248. fi
  249. }
  250. # ---------------------------------------------------------------------------
  251. # Docker build
  252. # ---------------------------------------------------------------------------
  253. build_docker() {
  254. if ! command -v docker &>/dev/null; then
  255. log_error "Docker is not installed or not in PATH"
  256. exit 1
  257. fi
  258. # Check / build base image
  259. if [[ $REBUILD_BASE -eq 1 ]] || ! docker image inspect "$BASE_IMAGE_NAME" &>/dev/null; then
  260. if [[ $REBUILD_BASE -eq 1 ]]; then
  261. log_info "Rebuilding base image (--rebuild-base)..."
  262. else
  263. log_info "Base image not found, building ${BASE_IMAGE_NAME}..."
  264. fi
  265. local base_cache_args=()
  266. if [[ $NO_CACHE -eq 1 ]]; then
  267. base_cache_args+=("--no-cache")
  268. fi
  269. docker buildx build \
  270. -f "$PROJECT_DIR/packaging/Dockerfile.base" \
  271. -t "$BASE_IMAGE_NAME" \
  272. "${base_cache_args[@]+"${base_cache_args[@]}"}" \
  273. "$PROJECT_DIR"
  274. log_success "Base image built: ${BASE_IMAGE_NAME}"
  275. else
  276. log_info "Using existing base image: ${BASE_IMAGE_NAME}"
  277. fi
  278. # Build packages
  279. log_info "Building packages in Docker..."
  280. local build_cache_args=()
  281. if [[ $NO_CACHE -eq 1 ]]; then
  282. build_cache_args+=("--no-cache")
  283. fi
  284. docker buildx build \
  285. -f "$PROJECT_DIR/packaging/Dockerfile.build" \
  286. --build-arg "BASE_IMAGE=${BASE_IMAGE_NAME}" \
  287. --build-arg "BUILD_VERSION=${VERSION}" \
  288. --build-arg "BUILD_DEB_REVISION=${DEB_REVISION}" \
  289. --build-arg "BUILD_JOBS=${BUILD_JOBS}" \
  290. --build-arg "BUILD_GIT_COMMIT=${GIT_COMMIT}" \
  291. --target packages \
  292. --output "type=local,dest=$OUTPUT_DIR" \
  293. "${build_cache_args[@]+"${build_cache_args[@]}"}" \
  294. "$PROJECT_DIR"
  295. log_success "Docker build completed"
  296. }
  297. # ---------------------------------------------------------------------------
  298. # Main
  299. # ---------------------------------------------------------------------------
  300. if [[ $LOCAL_MODE -eq 1 ]]; then
  301. build_local
  302. else
  303. build_docker
  304. fi
  305. if [[ $NO_PACKAGE -eq 1 ]]; then
  306. echo ""
  307. log_success "Build complete: $BUILD_DIR"
  308. exit 0
  309. fi
  310. # ---------------------------------------------------------------------------
  311. # List created packages
  312. # ---------------------------------------------------------------------------
  313. # Only what THIS run produced. dist/ accumulates every version ever built, and
  314. # both the listing and the publish step used to take the whole directory - so a
  315. # publish re-staged every historical package, and anything else sitting in
  316. # dist/ went to the repository with it.
  317. shopt -s nullglob
  318. BUILT_DEBS=("$OUTPUT_DIR"/*_"${VERSION}-${DEB_REVISION}"_*.deb)
  319. shopt -u nullglob
  320. if [[ ${#BUILT_DEBS[@]} -eq 0 ]]; then
  321. log_error "No ${VERSION}-${DEB_REVISION} .deb files found in $OUTPUT_DIR"
  322. exit 1
  323. fi
  324. echo ""
  325. log_info "Created packages:"
  326. ls -lh "${BUILT_DEBS[@]}"
  327. # ---------------------------------------------------------------------------
  328. # Repository operations
  329. # ---------------------------------------------------------------------------
  330. if [[ $DO_REPO -eq 1 ]]; then
  331. echo ""
  332. log_info "Adding packages to repository (suite: ${SUITE})..."
  333. "$DEB_REPO_DIR/scripts/add-packages.sh" "${BUILT_DEBS[@]}"
  334. log_success "Packages added to staging"
  335. "$DEB_REPO_DIR/scripts/create-repo.sh" --suite "$SUITE"
  336. log_success "Repository created for suite: ${SUITE}"
  337. if [[ $DO_SYNC -eq 1 ]]; then
  338. log_info "Syncing repository to repository.smartbotics.ai..."
  339. "$DEB_REPO_DIR/scripts/sync-repo.sh"
  340. log_success "Repository synced"
  341. fi
  342. fi
  343. # ---------------------------------------------------------------------------
  344. # Next steps
  345. # ---------------------------------------------------------------------------
  346. echo ""
  347. log_success "Build complete! (v${VERSION}-${DEB_REVISION})"
  348. echo ""
  349. if [[ $LOCAL_MODE -eq 1 && $INSTALL_AFTER -eq 0 ]]; then
  350. log_info "To install locally:"
  351. echo " sudo dpkg -i ${OUTPUT_DIR}/*.deb || sudo apt-get install -f -y"
  352. echo ""
  353. fi
  354. if [[ $DO_REPO -eq 0 ]]; then
  355. log_info "To publish to repository:"
  356. echo " packaging/build.sh --repo --suite trixie"
  357. echo ""
  358. fi
  359. if [[ $DO_REPO -eq 1 && $DO_SYNC -eq 0 ]]; then
  360. log_info "To sync repository to remote:"
  361. echo " packaging/build.sh --repo --suite ${SUITE} --sync"
  362. echo ""
  363. fi