diff --git a/README.md b/README.md index 16632b8..ccaf3ac 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,16 @@ A RocksDB installation that provides `librocksdb.so`. This means that on Debian, See [simple_example](examples/simple_example.nim) +### Static linking + +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 +``` + +(we need the C++ linker profile because it's a C++ library) + ## Future directions In the future, Nim-RocksDB might provide a high-level API that: diff --git a/rocksdb.nimble b/rocksdb.nimble index 8baca02..ee0e2d0 100644 --- a/rocksdb.nimble +++ b/rocksdb.nimble @@ -4,6 +4,7 @@ author = "Status Research & Development GmbH" description = "A wrapper for Facebook's RocksDB, an embeddable, persistent key-value store for fast storage" license = "Apache License 2.0 or GPLv2" skipDirs = @["examples", "tests"] +mode = ScriptMode.Verbose ### Dependencies requires "nim >= 1.2.0", @@ -18,4 +19,7 @@ proc test(args, path: string) = task test, "Run tests": test "", "tests/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/all.nim" diff --git a/rocksdb/librocksdb.nim b/rocksdb/librocksdb.nim index 0b33953..149e8e8 100644 --- a/rocksdb/librocksdb.nim +++ b/rocksdb/librocksdb.nim @@ -1,5 +1,4 @@ -# Nim-RocksDB -# Copyright 2018 Status Research & Development GmbH +# Copyright 2018-2022 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) @@ -48,6 +47,8 @@ proc shouldUseNativeLinking(): bool {.compileTime.} = when defined(linux): return true +const LibrocksbStaticArgs {.strdefine.}: string = "" + template rocksType(T) = type T* = distinct pointer proc isNil*(v: T): bool {.borrow, gcsafe.} @@ -100,11 +101,15 @@ rocksType rocksdb_transaction_t rocksType rocksdb_checkpoint_t ## DB operations -when shouldUseNativeLinking(): +when LibrocksbStaticArgs != "": {.pragma: importrocks, importc, cdecl.} - {.passL: "-lrocksdb".} + {.passL: LibrocksbStaticArgs.} else: - {.pragma: importrocks, importc, cdecl, dynlib: librocksdb.} + when shouldUseNativeLinking(): + {.pragma: importrocks, importc, cdecl.} + {.passL: "-lrocksdb".} + else: + {.pragma: importrocks, importc, cdecl, dynlib: librocksdb.} proc rocksdb_open*(options: rocksdb_options_t; name: cstring; errptr: ptr cstring): rocksdb_t {.importrocks.} proc rocksdb_open_for_read_only*(options: rocksdb_options_t; name: cstring;