blob: cc2cd1fcd328ce09655c4b59d334828a320fb01a [file] [log] [blame]
Marc Kupietza0904742026-08-09 14:29:23 +02001#!/usr/bin/env bash
2#
3# Builds rocksdb once, with the shared and the static library, and installs it
4# into a prefix of its own (default: $HOME/rocksdb). Works on Fedora, Rocky
5# Linux, RHEL, Debian, Ubuntu and MacOS.
6#
7# The version to build is, in this order:
8# 1. the one given with -v
9# 2. the one of the rocksdb package of the distribution, so that the static
10# library matches the headers and the shared library collocatordb was
11# built against
12# 3. a pinned one that is known to compile with the compilers of Rocky Linux
13# 9 (gcc 11) up to Fedora (gcc 16) and MacOS (clang)
14#
15# Examples:
16# scripts/build-rocksdb.sh # build the version of the distribution
17# scripts/build-rocksdb.sh -v 10.2.1 # build a specific version
18# scripts/build-rocksdb.sh -p /opt/rocksdb # install somewhere else
19#
20set -euo pipefail
21
22PINNED_VERSION=10.2.1
23PREFIX=$HOME/rocksdb
24VERSION=
25SOURCE_DIR=
26STATIC_ONLY=0
27
28usage() {
29 sed -n '2,17p' "$0"
30 exit "${1:-0}"
31}
32
33while [ $# -gt 0 ]; do
34 case $1 in
35 -p|--prefix) PREFIX=$2; shift 2 ;;
36 -v|--version) VERSION=$2; shift 2 ;;
37 -s|--source) SOURCE_DIR=$2; shift 2 ;;
38 --static-only) STATIC_ONLY=1; shift ;;
39 -h|--help) usage 0 ;;
40 *) echo "Unknown option: $1" >&2; usage 1 ;;
41 esac
42done
43
44SOURCE_DIR=${SOURCE_DIR:-$PREFIX-src}
45
46# --- the version -------------------------------------------------------------
47if [ -z "$VERSION" ]; then
48 if command -v rpm > /dev/null && rpm -q rocksdb > /dev/null 2>&1; then
49 VERSION=$(rpm -q --qf '%{VERSION}' rocksdb)
50 elif command -v dpkg-query > /dev/null && dpkg-query -W librocksdb-dev > /dev/null 2>&1; then
51 VERSION=$(dpkg-query -W -f '${Version}' librocksdb-dev | cut -d- -f1)
52 elif command -v brew > /dev/null && brew list --versions rocksdb > /dev/null 2>&1; then
53 VERSION=$(brew list --versions rocksdb | awk '{print $2}' | cut -d' ' -f1)
54 else
55 VERSION=$PINNED_VERSION
56 echo "No rocksdb package found, building the pinned version $VERSION."
57 echo "Give another one with -v, see https://github.com/facebook/rocksdb/tags"
58 fi
59fi
60echo "Building rocksdb $VERSION"
61
62# --- the prerequisites -------------------------------------------------------
63missing=
64for tool in git cmake make; do
65 command -v $tool > /dev/null || missing="$missing $tool"
66done
67if ! command -v c++ > /dev/null && ! command -v g++ > /dev/null && ! command -v clang++ > /dev/null; then
68 missing="$missing c++"
69fi
70# The compression libraries rocksdb is built with below. Where they are
71# missing the build still succeeds, just without them, so this is a hint.
72compression_hint=
73if command -v pkg-config > /dev/null; then
74 for lib in snappy zlib bzip2 liblz4 libzstd; do
75 pkg-config --exists $lib 2> /dev/null || compression_hint="$compression_hint $lib"
76 done
77fi
78if [ -n "$missing" ]; then
79 echo "Missing build tools:$missing" >&2
80 echo >&2
81 echo "Install them and the compression libraries:" >&2
82 echo " Fedora, Rocky, RHEL: sudo dnf install cmake gcc-c++ git snappy-devel zlib-devel bzip2-devel lz4-devel libzstd-devel" >&2
83 echo " Debian, Ubuntu: sudo apt-get install cmake g++ git libsnappy-dev zlib1g-dev libbz2-dev liblz4-dev libzstd-dev" >&2
84 echo " MacOS: brew install cmake snappy zlib bzip2 lz4 zstd" >&2
85 exit 1
86fi
87if [ -n "$compression_hint" ]; then
88 echo "Warning: pkg-config does not find:$compression_hint" >&2
89 echo "rocksdb will be built without them. The devel packages named above provide them." >&2
90fi
91
92# --- the sources -------------------------------------------------------------
93if [ -d "$SOURCE_DIR/.git" ]; then
94 echo "Reusing the checkout in $SOURCE_DIR"
95 git -C "$SOURCE_DIR" fetch --depth 1 origin "refs/tags/v$VERSION"
96 git -C "$SOURCE_DIR" checkout -q FETCH_HEAD
97else
98 git clone https://github.com/facebook/rocksdb.git --branch "v$VERSION" --depth 1 "$SOURCE_DIR"
99fi
100
101# --- the build ---------------------------------------------------------------
102# lib64 where the distribution has one (Fedora, Rocky, RHEL, openSUSE), lib
103# elsewhere. Both are searched for a prefix by cmake, this is for the user.
104if [ -d /usr/lib64 ] && [ ! -e /etc/debian_version ]; then
105 LIBDIR=lib64
106else
107 LIBDIR=lib
108fi
109
Marc Kupietz11df7852026-08-09 14:59:09 +0200110# So that the compression libraries of homebrew are found on MacOS
111BREW_PREFIX=
112if command -v brew > /dev/null; then
113 BREW_PREFIX="-DCMAKE_PREFIX_PATH=$(brew --prefix)"
114fi
115
Marc Kupietza0904742026-08-09 14:29:23 +0200116# WITH_GFLAGS and WITH_LIBURING are off so that the static library does not
117# drag them into everything that links it. FAIL_ON_WARNINGS off so that a
118# newer compiler does not turn rocksdb's own warnings into errors. And
119# -include cstdint because older rocksdb versions declare uint64_t and friends
120# without including it, which newer compilers no longer tolerate - Fedora 44
121# packages 10.2.1 and builds it with gcc 16, for instance. Where it is already
122# included, the flag changes nothing.
123cmake -S "$SOURCE_DIR" -B "$SOURCE_DIR/build" \
124 -DCMAKE_BUILD_TYPE=Release \
125 -DCMAKE_CXX_FLAGS="-include cstdint" \
126 -DFAIL_ON_WARNINGS=OFF \
127 -DWITH_TESTS=OFF -DWITH_ALL_TESTS=OFF \
128 -DWITH_BENCHMARK_TOOLS=OFF -DWITH_TOOLS=OFF -DWITH_CORE_TOOLS=OFF \
129 -DWITH_GFLAGS=OFF -DWITH_LIBURING=OFF \
130 -DWITH_SNAPPY=ON -DWITH_LZ4=ON -DWITH_ZLIB=ON -DWITH_ZSTD=ON -DWITH_BZ2=ON \
131 -DROCKSDB_BUILD_SHARED=$([ $STATIC_ONLY = 1 ] && echo OFF || echo ON) \
132 -DCMAKE_INSTALL_PREFIX="$PREFIX" \
Marc Kupietz11df7852026-08-09 14:59:09 +0200133 -DCMAKE_INSTALL_LIBDIR="$LIBDIR" \
134 $BREW_PREFIX
135cmake --build "$SOURCE_DIR/build" -j "${CMAKE_BUILD_PARALLEL_LEVEL:-$(getconf _NPROCESSORS_ONLN 2> /dev/null || sysctl -n hw.ncpu)}"
Marc Kupietza0904742026-08-09 14:29:23 +0200136cmake --install "$SOURCE_DIR/build"
137
138# --- what to do with it ------------------------------------------------------
139echo
140echo "Installed rocksdb $VERSION:"
141echo " headers: $PREFIX/include"
142echo " static: $PREFIX/$LIBDIR/librocksdb.a"
143[ $STATIC_ONLY = 1 ] || echo " shared: $PREFIX/$LIBDIR/$( [ "$(uname)" = Darwin ] && echo librocksdb.dylib || echo librocksdb.so )"
144echo
145echo "Build collocatordb against it with:"
146echo " cmake -S . -B build -DCMAKE_PREFIX_PATH=$PREFIX -DCMAKE_INSTALL_PREFIX=/usr/local"
147echo
148echo "Build dereko2vec against it with:"
149echo " cmake -S . -B build -DSTATIC_ROCKSDB=ON -DCMAKE_PREFIX_PATH=$PREFIX"
150if [ $STATIC_ONLY = 0 ] && [ "$(uname)" = Linux ]; then
151 echo
152 echo "Programs that use the shared library find it after:"
153 echo " echo $PREFIX/$LIBDIR | sudo tee /etc/ld.so.conf.d/rocksdb.conf && sudo ldconfig"
154fi