| Marc Kupietz | a090474 | 2026-08-09 14:29:23 +0200 | [diff] [blame] | 1 | #!/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 | # |
| 20 | set -euo pipefail |
| 21 | |
| 22 | PINNED_VERSION=10.2.1 |
| 23 | PREFIX=$HOME/rocksdb |
| 24 | VERSION= |
| 25 | SOURCE_DIR= |
| 26 | STATIC_ONLY=0 |
| 27 | |
| 28 | usage() { |
| 29 | sed -n '2,17p' "$0" |
| 30 | exit "${1:-0}" |
| 31 | } |
| 32 | |
| 33 | while [ $# -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 |
| 42 | done |
| 43 | |
| 44 | SOURCE_DIR=${SOURCE_DIR:-$PREFIX-src} |
| 45 | |
| 46 | # --- the version ------------------------------------------------------------- |
| 47 | if [ -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 |
| 59 | fi |
| 60 | echo "Building rocksdb $VERSION" |
| 61 | |
| 62 | # --- the prerequisites ------------------------------------------------------- |
| 63 | missing= |
| 64 | for tool in git cmake make; do |
| 65 | command -v $tool > /dev/null || missing="$missing $tool" |
| 66 | done |
| 67 | if ! command -v c++ > /dev/null && ! command -v g++ > /dev/null && ! command -v clang++ > /dev/null; then |
| 68 | missing="$missing c++" |
| 69 | fi |
| 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. |
| 72 | compression_hint= |
| 73 | if 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 |
| 77 | fi |
| 78 | if [ -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 |
| 86 | fi |
| 87 | if [ -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 |
| 90 | fi |
| 91 | |
| 92 | # --- the sources ------------------------------------------------------------- |
| 93 | if [ -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 |
| 97 | else |
| 98 | git clone https://github.com/facebook/rocksdb.git --branch "v$VERSION" --depth 1 "$SOURCE_DIR" |
| 99 | fi |
| 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. |
| 104 | if [ -d /usr/lib64 ] && [ ! -e /etc/debian_version ]; then |
| 105 | LIBDIR=lib64 |
| 106 | else |
| 107 | LIBDIR=lib |
| 108 | fi |
| 109 | |
| Marc Kupietz | 11df785 | 2026-08-09 14:59:09 +0200 | [diff] [blame^] | 110 | # So that the compression libraries of homebrew are found on MacOS |
| 111 | BREW_PREFIX= |
| 112 | if command -v brew > /dev/null; then |
| 113 | BREW_PREFIX="-DCMAKE_PREFIX_PATH=$(brew --prefix)" |
| 114 | fi |
| 115 | |
| Marc Kupietz | a090474 | 2026-08-09 14:29:23 +0200 | [diff] [blame] | 116 | # 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. |
| 123 | cmake -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 Kupietz | 11df785 | 2026-08-09 14:59:09 +0200 | [diff] [blame^] | 133 | -DCMAKE_INSTALL_LIBDIR="$LIBDIR" \ |
| 134 | $BREW_PREFIX |
| 135 | cmake --build "$SOURCE_DIR/build" -j "${CMAKE_BUILD_PARALLEL_LEVEL:-$(getconf _NPROCESSORS_ONLN 2> /dev/null || sysctl -n hw.ncpu)}" |
| Marc Kupietz | a090474 | 2026-08-09 14:29:23 +0200 | [diff] [blame] | 136 | cmake --install "$SOURCE_DIR/build" |
| 137 | |
| 138 | # --- what to do with it ------------------------------------------------------ |
| 139 | echo |
| 140 | echo "Installed rocksdb $VERSION:" |
| 141 | echo " headers: $PREFIX/include" |
| 142 | echo " static: $PREFIX/$LIBDIR/librocksdb.a" |
| 143 | [ $STATIC_ONLY = 1 ] || echo " shared: $PREFIX/$LIBDIR/$( [ "$(uname)" = Darwin ] && echo librocksdb.dylib || echo librocksdb.so )" |
| 144 | echo |
| 145 | echo "Build collocatordb against it with:" |
| 146 | echo " cmake -S . -B build -DCMAKE_PREFIX_PATH=$PREFIX -DCMAKE_INSTALL_PREFIX=/usr/local" |
| 147 | echo |
| 148 | echo "Build dereko2vec against it with:" |
| 149 | echo " cmake -S . -B build -DSTATIC_ROCKSDB=ON -DCMAKE_PREFIX_PATH=$PREFIX" |
| 150 | if [ $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" |
| 154 | fi |