build.sh 16 KB

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