Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for static linking RocksDb. #40

Merged
merged 30 commits into from Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
e1e10f7
Added rocksdb as a submodule in vendor directory.
web3-developer Mar 18, 2024
a66265a
Add support for static linking using nimble.
web3-developer Mar 18, 2024
44a4a33
Add script to build static library dependencies.
web3-developer Mar 18, 2024
c66b341
Refactor / cleanup.
web3-developer Mar 19, 2024
3a8c60d
Try static linking compression deps.
web3-developer Mar 19, 2024
d430138
Try fixing deps build on i386 linux.
web3-developer Mar 19, 2024
53bd890
Disable warnings as error in deps build.
web3-developer Mar 19, 2024
7b09117
Try force using gcc as compiler.
web3-developer Mar 19, 2024
4b39c1c
Set recommended compression options in build and in library.
web3-developer Mar 20, 2024
e5804f3
Move static linking into wrapper code.
web3-developer Mar 20, 2024
6484f7e
Try changing compiler errors/warnings for rocksdb build.
web3-developer Mar 20, 2024
b41cb96
Print rocksdb make config.
web3-developer Mar 21, 2024
a92de96
Try use clang for macos.
web3-developer Mar 21, 2024
f22c6e2
Try linking using clang.
web3-developer Mar 21, 2024
834d8dd
Just build MacOS for now. Use clang++.
web3-developer Mar 21, 2024
c5b068d
Try again.
web3-developer Mar 21, 2024
3de2f77
Conditionally set linker using when defined(macosx).
web3-developer Mar 21, 2024
7b8ad05
Add support for linking windows static libraries.
web3-developer Mar 22, 2024
d6c58f6
Test windows CI.
web3-developer Mar 22, 2024
f3206c2
Try again.
web3-developer Mar 22, 2024
9231166
Build rocksdb static libs using vcpkg.
web3-developer Mar 22, 2024
4816d95
Try copy static libraries into build dir. Set build type to release.
web3-developer Mar 22, 2024
60a4ca0
Don't run build script on windows.
web3-developer Mar 22, 2024
3ac06cd
Debug copy libs.
web3-developer Mar 22, 2024
8fc0f8f
Remove lib prefix from library names for windows.
web3-developer Mar 22, 2024
5c800b9
Try using latest windows builder.
web3-developer Mar 22, 2024
5b20f27
Try mingw static build.
web3-developer Mar 22, 2024
a66be85
Try again.
web3-developer Mar 22, 2024
d1cb20f
Re-enable linux and macos in CI.
web3-developer Mar 25, 2024
a5fb2d3
Static linking is not supported on windows. Update documentation and CI.
web3-developer Mar 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions .github/workflows/ci.yml
Expand Up @@ -14,8 +14,8 @@ jobs:
target:
- os: linux
cpu: amd64
- os: linux
cpu: i386
# - os: linux
# cpu: i386
- os: macos
cpu: amd64
- os: windows
Expand All @@ -34,7 +34,7 @@ jobs:
shell: bash
- target:
os: windows
builder: windows-2019
builder: windows-latest
shell: msys2 {0}

defaults:
Expand Down Expand Up @@ -188,7 +188,13 @@ jobs:
# https://github.com/status-im/nimbus-eth2/issues/3121
export NIMFLAGS="-d:nimRawSetjmp"
fi

nim --version
nimble --version
nimble install -y --depsOnly
nimble test

# static linking is not supported on windows
if [[ "${{ matrix.target.os }}" != "windows" ]]; then
nimble test_static
fi
5 changes: 5 additions & 0 deletions .gitmodules
@@ -0,0 +1,5 @@
[submodule "vendor/rocksdb"]
path = vendor/rocksdb
url = https://github.com/facebook/rocksdb
ignore = dirty
branch = master
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -25,10 +25,10 @@ See [simple_example](examples/simple_example.nim)
To statically link librocksdb, you would do something like:

```nim
nim c -d:LibrocksbStaticArgs='-l:librocksdb.a' --gcc.linkerexe=g++ --threads:on your_program.nim
nim c -d:rocksdb_static_linking --threads:on your_program.nim
```

(we need the C++ linker profile because it's a C++ library)
See the config.nims file which contains the static linking configuration which is switched on with the `rocksdb_static_linking` flag. Note that static linking is currently not supported on windows.

### Contribution

Expand Down
11 changes: 11 additions & 0 deletions config.nims
Expand Up @@ -11,3 +11,14 @@
when fileExists("nimble.paths"):
include "nimble.paths"
# end Nimble config

when defined(rocksdb_static_linking):
# use the C++ linker profile because it's a C++ library
when defined(macosx):
switch("clang.linkerexe", "clang++")
else:
switch("gcc.linkerexe", "g++")

switch("dynlibOverride", "rocksdb")
switch("dynlibOverride", "lz4")
switch("dynlibOverride", "zstd")
5 changes: 3 additions & 2 deletions nim.cfg
@@ -1,2 +1,3 @@
-p:"src"

--threads:on
--outdir:build
--hints:off
9 changes: 7 additions & 2 deletions rocksdb.nim
Expand Up @@ -8,8 +8,13 @@
# at your option. This file may not be copied, modified, or distributed except according to those terms.

import
./rocksdb/[backup, columnfamily, rocksdb, rocksiterator],
./rocksdb/[sstfilewriter, transactiondb, writebatch]
./rocksdb/[backup,
columnfamily,
rocksdb,
rocksiterator,
sstfilewriter,
transactiondb,
writebatch]

export
backup,
Expand Down
19 changes: 9 additions & 10 deletions rocksdb.nimble
Expand Up @@ -8,19 +8,18 @@ mode = ScriptMode.Verbose

### Dependencies
requires "nim >= 1.6",
"stew",
"results",
"tempfile",
"unittest2"

proc test(args, path: string) =
if not dirExists "build":
mkDir "build"
exec "nim " & getEnv("TEST_LANG", "c") & " " & getEnv("NIMFLAGS") & " " & args &
" --outdir:build -r --hints:off --threads:on --skipParentCfg " & path
task clean, "Remove temporary files":
exec "rm -rf build"
exec "make -C vendor/rocksdb clean"

task test, "Run tests":
test "", "tests/test_all.nim"
# Too troublesome to install "librocksdb.a" in CI, but this is how we would
# test it (we need the C++ linker profile because it's a C++ library):
# test "-d:LibrocksbStaticArgs='-l:librocksdb.a' --gcc.linkerexe=g++", "tests/test_all.nim"
exec "nim c -r --threads:on tests/test_all.nim"

task test_static, "Run tests after static linking dependencies":
when not defined(windows):
exec "scripts/build_static_deps.sh"
exec "nim c -d:rocksdb_static_linking -r --threads:on tests/test_all.nim"
4 changes: 4 additions & 0 deletions rocksdb/columnfamily/cfopts.nim
Expand Up @@ -34,6 +34,10 @@ proc setCreateMissingColumnFamilies*(cfOpts: ColFamilyOptionsRef, flag: bool) =

proc defaultColFamilyOptions*(): ColFamilyOptionsRef =
let opts = newColFamilyOptions()

rocksdb_options_set_compression(opts.cPtr, rocksdb_lz4_compression)
# rocksdb_options_set_bottommost_compression(opts.cPtr, rocksdb_zstd_compression)

# Enable creating column families if they do not exist
opts.setCreateMissingColumnFamilies(true)
return opts
Expand Down
19 changes: 15 additions & 4 deletions rocksdb/lib/librocksdb.nim
Expand Up @@ -47,8 +47,6 @@ proc shouldUseNativeLinking(): bool {.compileTime.} =
when defined(linux):
return true

const LibrocksbStaticArgs {.strdefine.}: string = ""

type
rocksdb_t* = object
rocksdb_backup_engine_t* = object
Expand Down Expand Up @@ -114,11 +112,24 @@ type

## DB operations

when LibrocksbStaticArgs != "":
when defined(rocksdb_static_linking):
{.pragma: importrocks, importc, cdecl.}
{.passL: LibrocksbStaticArgs.}

import std/[os, strutils]
const
topLevelPath = currentSourcePath.parentDir().parentDir().parentDir()
libsDir = topLevelPath.replace('\\', '/') & "/build/lib"

when defined(windows):
{.passL: "-lshlwapi -lrpcrt4".}
{.passL: libsDir & "/rocksdb.lib".}
{.passL: libsDir & "/lz4.lib".}
{.passL: libsDir & "/zstd.lib".}
else:
{.passL: libsDir & "/librocksdb.a".}
{.passL: libsDir & "/liblz4.a".}
{.passL: libsDir & "/libzstd.a".}

else:
when shouldUseNativeLinking():
{.pragma: importrocks, importc, cdecl.}
Expand Down
7 changes: 5 additions & 2 deletions rocksdb/options/backupopts.nim
Expand Up @@ -31,8 +31,11 @@ proc cPtr*(engineOpts: BackupEngineOptionsRef): BackupEngineOptionsPtr =
# TODO: Add setters and getters for backup options properties.

proc defaultBackupEngineOptions*(): BackupEngineOptionsRef {.inline.} =
newBackupEngineOptions()
# TODO: set prefered defaults
let opts = newBackupEngineOptions()
rocksdb_options_set_compression(opts.cPtr, rocksdb_lz4_compression)
# rocksdb_options_set_bottommost_compression(opts.cPtr, rocksdb_zstd_compression)
opts


proc close*(engineOpts: BackupEngineOptionsRef) =
if not engineOpts.isClosed():
Expand Down
6 changes: 5 additions & 1 deletion rocksdb/options/dbopts.nim
Expand Up @@ -48,7 +48,11 @@ proc setCreateMissingColumnFamilies*(dbOpts: DbOptionsRef, flag: bool) =
rocksdb_options_set_create_missing_column_families(dbOpts.cPtr, flag.uint8)

proc defaultDbOptions*(): DbOptionsRef =
let opts = newDbOptions()
let opts: DbOptionsRef = newDbOptions()

rocksdb_options_set_compression(opts.cPtr, rocksdb_lz4_compression)
# rocksdb_options_set_bottommost_compression(opts.cPtr, rocksdb_zstd_compression)

# Optimize RocksDB. This is the easiest way to get RocksDB to perform well:
opts.setIncreaseParallelism(countProcessors())
# This requires snappy - disabled because rocksdb is not always compiled with
Expand Down
48 changes: 48 additions & 0 deletions scripts/build_static_deps.sh
@@ -0,0 +1,48 @@
#!/usr/bin/env bash

# Nim-RocksDB
# Copyright 2018-2024 Status Research & Development GmbH
# Licensed under either of
#
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
# * GPL license, version 2.0, ([LICENSE-GPLv2](LICENSE-GPLv2) or https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html)
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.

set -e

cd "$(dirname "${BASH_SOURCE[0]}")"/..

REPO_DIR="${PWD}"
ROCKSDB_LIB_DIR="${REPO_DIR}/vendor/rocksdb"
BUILD_DEST="${REPO_DIR}/build/lib"



[[ -z "$NPROC" ]] && NPROC=2 # number of CPU cores available

git submodule update --init

export DISABLE_WARNING_AS_ERROR=1

export ROCKSDB_DISABLE_SNAPPY=1
export ROCKSDB_DISABLE_ZLIB=1
export ROCKSDB_DISABLE_BZIP=1

export PORTABLE=1
export DEBUG_LEVEL=0

make -C "${ROCKSDB_LIB_DIR}" -j${NPROC} liblz4.a libzstd.a --no-print-directory > /dev/null

export EXTRA_CFLAGS="-fpermissive -Wno-error -w -I${ROCKSDB_LIB_DIR}/lz4-1.9.4/lib -I${ROCKSDB_LIB_DIR}/zstd-1.5.5/lib -DLZ4 -DZSTD"
export EXTRA_CXXFLAGS="-fpermissive -Wno-error -w -I${ROCKSDB_LIB_DIR}/lz4-1.9.4/lib -I${ROCKSDB_LIB_DIR}/zstd-1.5.5/lib -DLZ4 -DZSTD"

make -C "${ROCKSDB_LIB_DIR}" -j${NPROC} static_lib --no-print-directory > /dev/null

#cat "${REPO_DIR}/vendor/rocksdb/make_config.mk"

mkdir -p "${BUILD_DEST}"

cp "${ROCKSDB_LIB_DIR}/liblz4.a" "${BUILD_DEST}/"
cp "${ROCKSDB_LIB_DIR}/libzstd.a" "${BUILD_DEST}/"
cp "${ROCKSDB_LIB_DIR}/librocksdb.a" "${BUILD_DEST}/"
1 change: 1 addition & 0 deletions vendor/rocksdb
Submodule rocksdb added at a66dae