Skip to content

Commit 0dd541e

Browse files
committed
Fix dependencies build.
Activate Linux builds.
1 parent 63c50a4 commit 0dd541e

File tree

5 files changed

+116
-108
lines changed

5 files changed

+116
-108
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ storage of data on disk. RocksDB is widely used in many industries for various a
88
systems, big data systems, and storage engines for other distributed systems.
99

1010
The aim of this project is to provide a multi-platform RocksDB implementation that can be used across different platforms,
11-
including JVM, Android, and native macOS/iOS. This allows developers to write applications that can run on different platforms
11+
including JVM, Android, and native Linux/macOS/iOS. This allows developers to write applications that can run on different platforms
1212
without having to rewrite the codebase.
1313

1414
This project is useful for developers who want to build multi-platform applications that require high-performance disk-based
@@ -22,7 +22,9 @@ This common library includes most common operations, but if you need additional
2222
them by creating an issue or submitting a merge request.
2323

2424
## Native platforms
25-
Currently, only iOS and macOS (+arm64 simulators) are supported for native compilations. Since the implementation is based on the RocksDB C API,
25+
Currently, only Linux, iOS and macOS (+arm64 simulators) are supported for native compilations.
26+
27+
Since the implementation is based on the RocksDB C API,
2628
other native platforms should be able to be added in the future if RocksDB can be compiled to that platform.
2729
Unfortunately tvOS and watchOS cannot be supported. No tvOS because it blocks certain calls RocksDB needs, and no watchOS
2830
because it needs specific arm64_32 support.
@@ -32,6 +34,8 @@ Kotlin Native targets:
3234
- macosX64
3335
- iosArm64
3436
- iosSimulatorArm64
37+
- linuxX64
38+
- linuxArm64
3539

3640
## Reference
3741

build.gradle.kts

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,6 @@ val rocksDBAndroidVersion = "9.6.1"
2727

2828
val rocksdbBuildPath = "./rocksdb/build"
2929

30-
val buildMacOS by tasks.creating(Exec::class) {
31-
workingDir = projectDir
32-
commandLine("./buildRocksdbMac.sh")
33-
}
34-
35-
val buildLinux by tasks.creating(Exec::class) {
36-
workingDir = projectDir
37-
commandLine("./buildRocksdbLinux.sh")
38-
}
39-
40-
val buildIOS by tasks.creating(Exec::class) {
41-
workingDir = projectDir
42-
commandLine("./buildRocksdbiOS.sh")
43-
}
44-
45-
val buildIOSSimulator by tasks.creating(Exec::class) {
46-
workingDir = projectDir
47-
commandLine("./buildRocksdbiOS.sh")
48-
}
49-
5030
android {
5131
namespace = "io.maryk.rocksdb"
5232
buildToolsVersion = "34.0.0"
@@ -132,7 +112,7 @@ kotlin {
132112
)
133113
}
134114

135-
val task = tasks.create("buildDependencies_$buildName", Exec::class) {
115+
val dependencyTask = tasks.create("buildDependencies_$buildName", Exec::class) {
136116
workingDir = projectDir
137117
val options = buildList<String> {
138118
if (buildName.startsWith("linux") && isMacOs) {
@@ -155,7 +135,7 @@ kotlin {
155135
defFile("src/nativeInterop/cinterop/rocksdb.def")
156136
includeDirs("rocksdb/include/rocksdb")
157137
tasks[interopProcessingTaskName].dependsOn(buildTask)
158-
tasks[interopProcessingTaskName].dependsOn(task)
138+
tasks[interopProcessingTaskName].dependsOn(dependencyTask)
159139
}
160140
}
161141
}
@@ -174,7 +154,7 @@ kotlin {
174154
workingDir = projectDir
175155
commandLine("./buildRocksdbApple.sh", "--platform=ios", "--arch=arm64")
176156
}
177-
setupTarget("ios_arm64", buildTask, "-arch arm64 -target arm64-apple-ios13.0-simulator -isysroot $sdkPath", "-DPLATFORM=OS64")
157+
setupTarget("ios_arm64", buildTask, "-arch arm64 -target arm64-apple-ios13.0 -isysroot $sdkPath", "-DPLATFORM=OS64")
178158
}
179159
iosSimulatorArm64 {
180160
val sdkPathProvider = providers.exec {
@@ -205,9 +185,20 @@ kotlin {
205185
}
206186
setupTarget("macos_arm64", buildTask, "-arch arm64 -target arm64-apple-macos11.0", "-DPLATFORM=MAC")
207187
}
208-
// linuxX64 {
209-
// setupTarget("linux_x86_64", buildLinux, "-march=x86-64")
210-
// }
188+
linuxX64 {
189+
val buildTask = tasks.create("buildLib-"+this.name, Exec::class) {
190+
workingDir = projectDir
191+
commandLine("./buildRocksdbLinux.sh", "--arch=x86-64")
192+
}
193+
setupTarget("linux_x86_64", buildTask, "-march=x86-64")
194+
}
195+
linuxArm64 {
196+
val buildTask = tasks.create("buildLib-"+this.name, Exec::class) {
197+
workingDir = projectDir
198+
commandLine("./buildRocksdbLinux.sh", "--arch=arm64")
199+
}
200+
setupTarget("linux_arm64", buildTask, "-march=armv8-a")
201+
}
211202
}
212203

213204
// Creates the folders for the database

buildDependencies.sh

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ set -euo pipefail
2323
# Default Values
2424
# ---------------------------------------------------------
2525
DOWNLOAD_DIR="lib"
26+
TOOLCHAIN_FILE=null
2627

2728
# iOS Toolchain
2829
IOS_TOOLCHAIN_URL="https://github.com/leetal/ios-cmake/archive/refs/tags/4.5.0.tar.gz"
@@ -92,6 +93,7 @@ while [[ $# -gt 0 ]]; do
9293
case "$1" in
9394
--extra-cflags)
9495
EXTRA_CFLAGS="$2"
96+
EXTRA_CXXFLAGS="$2"
9597
shift 2
9698
;;
9799
--extra-cmakeflags)
@@ -115,6 +117,16 @@ done
115117
# Set OUTPUT_DIR to DEFAULT_OUTPUT_DIR if not provided
116118
OUTPUT_DIR="${OUTPUT_DIR:-$DEFAULT_OUTPUT_DIR}"
117119

120+
set +u
121+
if [[ "$OUTPUT_DIR" == *linux_x86_64* ]]; then
122+
export CC="$HOME/.konan/dependencies/x86_64-unknown-linux-gnu-gcc-8.3.0-glibc-2.19-kernel-4.9-2/bin/x86_64-unknown-linux-gnu-gcc"
123+
export CXX="$HOME/.konan/dependencies/x86_64-unknown-linux-gnu-gcc-8.3.0-glibc-2.19-kernel-4.9-2/bin/x86_64-unknown-linux-gnu-g++"
124+
elif [[ "$OUTPUT_DIR" == *linux_arm64* ]]; then
125+
export CC="$HOME/.konan/dependencies/aarch64-unknown-linux-gnu-gcc-8.3.0-glibc-2.25-kernel-4.9-2/bin/aarch64-unknown-linux-gnu-gcc"
126+
export CXX="$HOME/.konan/dependencies/aarch64-unknown-linux-gnu-gcc-8.3.0-glibc-2.25-kernel-4.9-2/bin/aarch64-unknown-linux-gnu-g++"
127+
fi
128+
set -u
129+
118130
# Create the output directory if it doesn't exist
119131
mkdir -p "$OUTPUT_DIR"
120132
mkdir -p "$DOWNLOAD_DIR/include"
@@ -163,7 +175,7 @@ download_ios_toolchain() {
163175
rm -rf "${DOWNLOAD_DIR}/ios-cmake-4.5.0"
164176
}
165177

166-
if [[ "${EXTRA_CFLAGS}" == *"-isysroot"* ]]; then
178+
if [[ "${EXTRA_CFLAGS}" == *"-isysroot"* || "${EXTRA_CFLAGS}" == *"apple-macos"* ]]; then
167179
download_ios_toolchain
168180
TOOLCHAIN_FILE="${IOS_TOOLCHAIN_DIR}/ios.toolchain.cmake"
169181
fi
@@ -244,7 +256,7 @@ build_zstd() {
244256
local src_dir="${DOWNLOAD_DIR}/zstd-${ZSTD_VER}"
245257
tar xzf "${tarball}" -C "${DOWNLOAD_DIR}" > /dev/null
246258
pushd "${src_dir}/lib" > /dev/null
247-
CFLAGS="${EXTRA_CFLAGS} -fPIC -O2" make clean > /dev/null
259+
make clean > /dev/null
248260
CFLAGS="${EXTRA_CFLAGS} -fPIC -O2" make libzstd.a > /dev/null
249261
popd > /dev/null
250262
cp "${src_dir}/lib/zstd.h" "${src_dir}/lib/zdict.h" "${DOWNLOAD_DIR}/include/"
@@ -264,8 +276,16 @@ build_snappy() {
264276
tar xzf "${tarball}" -C "${DOWNLOAD_DIR}" > /dev/null
265277
pushd "${src_dir}" > /dev/null
266278

267-
if [ -n "${TOOLCHAIN_FILE}" ]; then
279+
if [ "${TOOLCHAIN_FILE}" != null ]; then
268280
EXTRA_CMAKEFLAGS+=""" -DCMAKE_TOOLCHAIN_FILE="../../${TOOLCHAIN_FILE}""""
281+
else
282+
local toolchain_file="toolchain.cmake"
283+
echo "set(CMAKE_C_FLAGS \"\${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS}\")" > "${toolchain_file}"
284+
echo "set(CMAKE_CXX_FLAGS \"\${CMAKE_CXX_FLAGS} ${EXTRA_CXXFLAGS}\")" >> "${toolchain_file}"
285+
echo "set(CMAKE_POSITION_INDEPENDENT_CODE ON)" >> "${toolchain_file}"
286+
echo "set(CMAKE_BUILD_TYPE Release)" >> "${toolchain_file}"
287+
288+
EXTRA_CMAKEFLAGS+=""" -DCMAKE_C_COMPILER=$CC -DCMAKE_CXX_COMPILER=$CXX -DCMAKE_TOOLCHAIN_FILE=$toolchain_file"""
269289
fi
270290

271291
cmake -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
@@ -295,7 +315,7 @@ build_lz4() {
295315
echo "Building LZ4 version ${LZ4_VER}..."
296316
tar xzf "${tarball}" -C "${DOWNLOAD_DIR}" > /dev/null
297317
pushd "${src_dir}/lib" > /dev/null
298-
CFLAGS="${EXTRA_CFLAGS} -fPIC -O2" LDFLAGS="${EXTRA_LDFLAGS}" make clean > /dev/null
318+
make clean > /dev/null
299319
CFLAGS="${EXTRA_CFLAGS} -fPIC -O2" LDFLAGS="${EXTRA_LDFLAGS}" make > /dev/null
300320
cp "lz4.h" "lz4hc.h" "../../../${DOWNLOAD_DIR}/include"
301321
cp "liblz4.a" "../../../${OUTPUT_DIR}/"

buildRocksdbApple.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ check_build() {
159159
# Build (make)
160160
###############################################################################
161161
echo "Starting build for: $BUILD_DIR"
162+
163+
if [ -f "${BUILD_DIR}/librocksdb.a" ]; then
164+
echo "** BUILD SKIPPED: ${BUILD_DIR}/librocksdb.a already exists **"
165+
exit 0
166+
fi
167+
162168
BUILD_OUTPUT=$(
163169
make -j"$(sysctl -n hw.ncpu)" \
164170
LIB_MODE=static \

buildRocksdbLinux.sh

Lines changed: 63 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,84 @@
11
#!/usr/bin/env bash
22

3-
echo "Building RocksDB..."
3+
ARCH="" # e.g. arm64, x86_64, etc.
4+
5+
for arg in "$@"; do
6+
case $arg in
7+
--arch=*)
8+
ARCH="${arg#*=}"
9+
;;
10+
*)
11+
echo "Unknown option: $arg"
12+
exit 1
13+
;;
14+
esac
15+
done
16+
17+
# Validate
18+
if [ -z "$ARCH" ]; then
19+
echo "Usage: $0 --arch=<arm64|x86_64>"
20+
exit 1
21+
fi
22+
23+
echo "Building RocksDB for $ARCH..."
424

525
# Optional: navigate to the rocksdb directory
626
cd "rocksdb" || { echo "Failed to navigate to rocksdb"; exit 1; }
727

828
# Simple function to check build output
929
check_build() {
1030
local output="$1"
11-
local arch="$2"
31+
local folder="$2"
1232

13-
if echo "$output" | grep -q "AR build/linux_${arch}/librocksdb.a"; then
14-
echo "** BUILD SUCCEEDED for $arch **"
33+
if echo "$output" | grep -q "AR build/$folder/librocksdb.a"; then
34+
echo "** BUILD SUCCEEDED for $ARCH **"
1535
elif echo "$output" | grep -q "make: Nothing to be done for 'static_lib'."; then
16-
echo "** BUILD NOT NEEDED for $arch (Already up to date) **"
36+
echo "** BUILD NOT NEEDED for $ARCH (Already up to date) **"
1737
else
18-
echo "** BUILD FAILED for $arch **"
38+
echo "** BUILD FAILED for $ARCH **"
1939
echo "$output"
2040
exit 1
2141
fi
2242
}
2343

24-
build_for_arch() {
25-
local arch="$1"
44+
EXTRA_FLAGS="-I../lib/include -DZLIB -DBZIP2 -DSNAPPY -DLZ4 -DZSTD "
2645

27-
# Set up arch-specific variables
28-
local pkgs
29-
local cc
30-
local cxx
31-
local extra_cflags
32-
local extra_cxxflags
46+
if [[ "$ARCH" == "arm64" ]]; then
47+
folder="linux_arm64"
48+
cc="$HOME/.konan/dependencies/aarch64-unknown-linux-gnu-gcc-8.3.0-glibc-2.25-kernel-4.9-2/bin/aarch64-unknown-linux-gnu-gcc"
49+
cxx="$HOME/.konan/dependencies/aarch64-unknown-linux-gnu-gcc-8.3.0-glibc-2.25-kernel-4.9-2/bin/aarch64-unknown-linux-gnu-g++"
50+
EXTRA_FLAGS+="-march=armv8-a"
51+
else
52+
folder="linux_x86_64"
53+
cc="$HOME/.konan/dependencies/x86_64-unknown-linux-gnu-gcc-8.3.0-glibc-2.19-kernel-4.9-2/bin/x86_64-unknown-linux-gnu-gcc"
54+
cxx="$HOME/.konan/dependencies/x86_64-unknown-linux-gnu-gcc-8.3.0-glibc-2.19-kernel-4.9-2/bin/x86_64-unknown-linux-gnu-g++"
55+
EXTRA_FLAGS+="-march=x86-64"
56+
fi
3357

34-
if [[ "$arch" == "arm64" ]]; then
35-
pkgs="gcc-aarch64-linux-gnu g++-aarch64-linux-gnu"
36-
cc="aarch64-linux-gnu-gcc"
37-
cxx="aarch64-linux-gnu-g++"
38-
extra_cflags="-march=armv8-a"
39-
extra_cxxflags="-march=armv8-a"
40-
else
41-
pkgs="gcc-x86-64-linux-gnu g++-x86-64-linux-gnu"
42-
cc="x86_64-linux-gnu-gcc"
43-
cxx="x86_64-linux-gnu-g++"
44-
extra_cflags="-march=x86-64"
45-
extra_cxxflags="-march=x86-64"
46-
fi
58+
# Check if the build output already exists
59+
if [ -f "build/$folder/librocksdb.a" ]; then
60+
echo "** BUILD SKIPPED: build/$folder/librocksdb.a already exists **"
61+
exit 0
62+
fi
4763

48-
echo "Building for $arch..."
49-
50-
# Run either in Docker (non-Linux) or natively (Linux)
51-
local output
52-
if [[ "$(uname -s)" == "Linux" ]]; then
53-
echo "Detected Linux — building $arch directly..."
54-
output=$(
55-
apt-get update &&
56-
apt-get install -y $pkgs &&
57-
make -j"$(nproc)" \
58-
LIB_MODE=static \
59-
LIBNAME="build/linux_${arch}/librocksdb" \
60-
DEBUG_LEVEL=0 \
61-
OBJ_DIR="build/linux_${arch}" \
62-
EXTRA_CXXFLAGS="$extra_cxxflags" \
63-
EXTRA_CFLAGS="$extra_cflags" \
64-
LD_FLAGS="-lbz2 -lz" \
65-
static_lib
66-
)
67-
else
68-
echo "Detected non-Linux system — building $arch via Docker..."
69-
output=$(
70-
docker run --rm \
71-
-v "$PWD":/rocks \
72-
-w /rocks \
73-
buildpack-deps \
74-
bash -c "
75-
apt-get update &&
76-
apt-get install -y $pkgs &&
77-
make -j\$(nproc) \
78-
LIB_MODE=static \
79-
LIBNAME=build/linux_${arch}/librocksdb \
80-
DEBUG_LEVEL=0 \
81-
OBJ_DIR=build/linux_${arch} \
82-
EXTRA_CXXFLAGS='$extra_cxxflags' \
83-
EXTRA_CFLAGS='$extra_cflags' \
84-
LD_FLAGS='-lbz2 -lz' \
85-
CC=$cc \
86-
CXX=$cxx \
87-
static_lib
88-
"
89-
)
90-
fi
91-
92-
# Check build results
93-
check_build "$output" "$arch"
94-
}
64+
if [[ "$(uname -s)" == "Linux" ]]; then
65+
output=$(
66+
make -j"$(nproc)" \
67+
LIB_MODE=static \
68+
LIBNAME="build/$folder/librocksdb" \
69+
DEBUG_LEVEL=0 \
70+
CC=$cc \
71+
CXX=$cxx \
72+
OBJ_DIR="build/$folder" \
73+
EXTRA_CXXFLAGS="$EXTRA_FLAGS" \
74+
EXTRA_CFLAGS="$EXTRA_FLAGS" \
75+
PORTABLE=1 \
76+
LD_FLAGS="-lbz2 -lz -lz4 -lsnappy" \
77+
static_lib
78+
)
9579

96-
build_for_arch "x86_64"
97-
build_for_arch "aarch64"
80+
check_build "$output" "$folder"
81+
else
82+
echo "Should only build on linux"
83+
exit 1
84+
fi

0 commit comments

Comments
 (0)