Skip to content

Commit

Permalink
Remove dependency: leveldb-sys. Use deps (git-modules) instead.
Browse files Browse the repository at this point in the history
Google Snappy, version: 1.2.1
Google Leveldb, commit ID: 23e35d7
  • Loading branch information
rim99 committed Sep 22, 2024
1 parent 8e6d1af commit 59df310
Show file tree
Hide file tree
Showing 22 changed files with 359 additions and 49 deletions.
22 changes: 0 additions & 22 deletions .github/workflows/cargo-test.yml

This file was deleted.

41 changes: 36 additions & 5 deletions .github/workflows/suite.yml → .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,46 +1,77 @@
on: [push, pull_request]
on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

name: Continuous integration
name: Continuous Integration

env:
CARGO_TERM_COLOR: always

jobs:
test-x86_64-unknown-linux-gnu:
name: Test Suite (x86_64-unknown-linux-gnu)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
submodules: recursive
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
components: rustfmt, clippy
- uses: actions-rs/cargo@v1
with:
command: build
args: --verbose
- uses: actions-rs/cargo@v1
with:
command: test
args: --verbose

test-x86_64-unknown-windows-msvc:
name: Test Suite (x86_64-unknown-windows-msvc)
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
submodules: recursive
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
components: rustfmt, clippy
- uses: actions-rs/cargo@v1
with:
command: build
args: --verbose
- uses: actions-rs/cargo@v1
with:
command: test
args: --verbose

test-x86_64-unknown-darwin:
name: Test Suite (x86_64-unknown-darwin)
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
submodules: recursive
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
components: rustfmt, clippy
- uses: actions-rs/cargo@v1
with:
command: build
args: --verbose
- uses: actions-rs/cargo@v1
with:
command: test
args: --verbose
7 changes: 7 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

[submodule "deps/google-leveldb"]
path = deps/google-leveldb
url = https://github.com/google/leveldb.git
[submodule "deps/google-snappy"]
path = deps/google-snappy
url = https://github.com/google/snappy.git
19 changes: 12 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[package]
name = "leveldb"
version = "0.8.6"
name = "leveldb-rs-binding"
version = "0.8.7"
edition = "2021"
authors = [
"Florian Gilcher <[email protected]>",
"Zhang Xin <zx3306@126.com>"
"Zhang Xin <zxin3306@126.com>",
]

description = "An interface for leveldb"
Expand All @@ -16,19 +16,24 @@ readme = "README.md"
homepage = "https://github.com/rim99/leveldb-rs-binding"
repository = "https://github.com/rim99/leveldb-rs-binding"

build = "src/sys/build.rs"
links = "leveldb"
autotests = false

[features]
default = ["leveldb-sys/snappy"]
default = ["snappy"]
snappy = []

[lib]
name = "leveldb"

[dependencies]
libc = "0.2.158"
libc = "0.2.*"
ffi-opaque = "2"

[dependencies.leveldb-sys]
version = "2.0.0"
[build-dependencies]
cmake = "0.1"
num_cpus = "1.10"

[dev-dependencies]
tempdir = "0.3.4"
Expand Down
1 change: 1 addition & 0 deletions deps/google-leveldb
Submodule google-leveldb added at 23e35d
1 change: 1 addition & 0 deletions deps/google-snappy
Submodule google-snappy added at 2c94e1
2 changes: 1 addition & 1 deletion src/database/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::Database;
use crate::database::key::from_u8;
use crate::database::key::Key;
use crate::options::{c_writeoptions, WriteOptions};
use leveldb_sys::*;
use crate::sys::leveldb::*;
use libc::{c_char, c_void, size_t};
use std::marker::PhantomData;
use std::ptr;
Expand Down
2 changes: 1 addition & 1 deletion src/database/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Drop for Bytes {
unsafe {
use libc::c_void;

::leveldb_sys::leveldb_free(self.bytes as *mut u8 as *mut c_void);
crate::sys::leveldb::leveldb_free(self.bytes as *mut u8 as *mut c_void);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/database/cache.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Structs and traits to work with the leveldb cache.
use leveldb_sys::{leveldb_cache_create_lru, leveldb_cache_destroy, leveldb_cache_t};
use crate::sys::leveldb::{leveldb_cache_create_lru, leveldb_cache_destroy, leveldb_cache_t};
use libc::size_t;

#[allow(missing_docs)]
Expand Down
2 changes: 1 addition & 1 deletion src/database/compaction.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Compaction
use super::key::Key;
use super::Database;
use leveldb_sys::leveldb_compact_range;
use crate::sys::leveldb::leveldb_compact_range;
use libc::{c_char, size_t};

pub trait Compaction<'a, K: Key + 'a> {
Expand Down
2 changes: 1 addition & 1 deletion src/database/comparator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! Databases written with one Comparator cannot be opened with another.
use crate::database::key::from_u8;
use crate::database::key::Key;
use leveldb_sys::*;
use crate::sys::leveldb::*;
use libc::{c_char, c_void, size_t};
use std::cmp::Ordering;
use std::marker::PhantomData;
Expand Down
2 changes: 1 addition & 1 deletion src/database/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! The module defining custom leveldb error type.
use leveldb_sys::leveldb_free;
use crate::sys::leveldb::leveldb_free;
use libc::c_char;
use libc::c_void;
use std;
Expand Down
2 changes: 1 addition & 1 deletion src/database/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use super::key::{from_u8, Key};
use super::options::{c_readoptions, ReadOptions};
use super::Database;
use leveldb_sys::{
use crate::sys::leveldb::{
leveldb_create_iterator, leveldb_iter_destroy, leveldb_iter_key, leveldb_iter_next,
leveldb_iter_prev, leveldb_iter_seek, leveldb_iter_seek_to_first, leveldb_iter_seek_to_last,
leveldb_iter_valid, leveldb_iter_value, leveldb_iterator_t, leveldb_readoptions_destroy,
Expand Down
2 changes: 1 addition & 1 deletion src/database/kv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::bytes::Bytes;
use super::error::Error;
use crate::database::key::Key;
use crate::options::{c_readoptions, c_writeoptions, ReadOptions, WriteOptions};
use leveldb_sys::*;
use crate::sys::leveldb::*;
use libc::{c_char, size_t};
use std::borrow::Borrow;
use std::ptr;
Expand Down
2 changes: 1 addition & 1 deletion src/database/management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::ffi::CString;
use std::path::Path;
use std::ptr;

use leveldb_sys::{leveldb_destroy_db, leveldb_repair_db};
use crate::sys::leveldb::{leveldb_destroy_db, leveldb_repair_db};

/// destroy a database. You shouldn't hold a handle on the database anywhere at that time.
pub fn destroy(name: &Path, options: Options) -> Result<(), Error> {
Expand Down
2 changes: 1 addition & 1 deletion src/database/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! The main database module, allowing to interface with leveldb on
//! a key-value basis.
use leveldb_sys::*;
use crate::sys::leveldb::*;

use self::error::Error;
use self::options::{c_options, Options};
Expand Down
2 changes: 1 addition & 1 deletion src/database/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! * `Options`: used when opening a database
//! * `ReadOptions`: used when reading from leveldb
//! * `WriteOptions`: used when writng to leveldb
use leveldb_sys::*;
use crate::sys::leveldb::*;

use crate::database::cache::Cache;
use crate::database::key::Key;
Expand Down
4 changes: 2 additions & 2 deletions src/database/snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
//!
//! Snapshots give you a reference to the database at a certain
//! point in time and won't change while you work with them.
use leveldb_sys::{leveldb_create_snapshot, leveldb_release_snapshot};
use leveldb_sys::{leveldb_snapshot_t, leveldb_t};
use crate::sys::leveldb::{leveldb_create_snapshot, leveldb_release_snapshot};
use crate::sys::leveldb::{leveldb_snapshot_t, leveldb_t};

use crate::database::key::Key;
use crate::database::kv::KV;
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@
#![crate_name = "leveldb"]
#![deny(missing_docs)]

extern crate leveldb_sys;
extern crate libc;
#[macro_use]
extern crate ffi_opaque;

pub use crate::database::batch;
pub use crate::database::compaction;
Expand All @@ -55,10 +56,11 @@ pub use crate::database::kv;
pub use crate::database::management;
pub use crate::database::options;
pub use crate::database::snapshots;
use leveldb_sys::{leveldb_major_version, leveldb_minor_version};
pub use crate::sys::leveldb::{leveldb_major_version, leveldb_minor_version};

#[allow(missing_docs)]
pub mod database;
pub mod sys;

/// Library version information
///
Expand Down
2 changes: 2 additions & 0 deletions src/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod database;
pub mod sys;
104 changes: 104 additions & 0 deletions src/sys/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use std::{
env,
path::{Path, PathBuf},
};

/// Directory name within `$OUT_DIR` where the static libraries should be built.
const LIBDIR: &'static str = "lib";

#[cfg(feature = "snappy")]
fn build_snappy() -> PathBuf {
println!("[snappy] Building");

let outdir = env::var("OUT_DIR").unwrap();
let libdir = Path::new(&outdir).join(LIBDIR);

env::set_var("NUM_JOBS", num_cpus::get().to_string());
let dest_prefix =
cmake::Config::new(Path::new("deps").join("google-snappy"))
.define("BUILD_SHARED_LIBS", "OFF")
.define("SNAPPY_BUILD_TESTS", "OFF")
.define("SNAPPY_BUILD_BENCHMARKS", "OFF")
.define("HAVE_LIBZ", "OFF")
.define("CMAKE_INSTALL_LIBDIR", &libdir)
.build();

assert_eq!(
dest_prefix.join(LIBDIR),
libdir,
"CMake should build Snappy in provided LIBDIR"
);
println!("cargo:rustc-link-search=native={}", libdir.display());
println!("cargo:rustc-link-lib=static=snappy");

dest_prefix
}

fn build_leveldb(snappy_prefix: Option<PathBuf>) {
println!("[leveldb] Building");

let outdir = env::var("OUT_DIR").unwrap();
let libdir = Path::new(&outdir).join(LIBDIR);

env::set_var("NUM_JOBS", num_cpus::get().to_string());
let mut config =
cmake::Config::new(Path::new("deps").join("google-leveldb"));
config
.define("LEVELDB_BUILD_TESTS", "OFF")
.define("LEVELDB_BUILD_BENCHMARKS", "OFF")
.define("CMAKE_INSTALL_LIBDIR", &libdir);
if let Some(snappy_prefix) = snappy_prefix {
#[cfg(target_env = "msvc")]
let ldflags = format!("/LIBPATH:{}", snappy_prefix.join(LIBDIR).display());
#[cfg(not(target_env = "msvc"))]
let ldflags = format!("-L{}", snappy_prefix.join(LIBDIR).display());

env::set_var(
"LDFLAGS",
ldflags
);

config
.define("HAVE_SNAPPY", "ON")
.cflag(format!("-I{}", snappy_prefix.join("include").display()))
.cxxflag(format!("-I{}", snappy_prefix.join("include").display()));
} else {
config.define("HAVE_SNAPPY", "OFF");
}
let dest_prefix = config.build();

assert_eq!(
dest_prefix.join(LIBDIR),
libdir,
"CMake should build LevelDB in provided LIBDIR"
);
println!("cargo:rustc-link-search=native={}", libdir.display());
println!("cargo:rustc-link-lib=static=leveldb");
}

fn main() {
println!("[build] Started");

// If we have the appropriate feature, then we build snappy.
#[cfg(feature = "snappy")]
let snappy_prefix = Some(build_snappy());
#[cfg(not(feature = "snappy"))]
let snappy_prefix: Option<PathBuf> = None;

// Build LevelDB
build_leveldb(snappy_prefix);

// Link to the standard C++ library
let target = env::var("TARGET").unwrap();
if target.contains("apple") || target.contains("freebsd") {
println!("cargo:rustc-link-lib=c++");
} else if target.contains("gnu") || target.contains("netbsd") || target.contains("openbsd") {
println!("cargo:rustc-link-lib=stdc++");
} else if target.contains("musl") {
// We want to link to libstdc++ *statically*. This requires that the user passes the right
// search path to rustc via `-Lstatic=/path/to/libstdc++`.
println!("cargo:rustc-link-lib=static=stdc++");
}

println!("[build] Finished");
}
Loading

0 comments on commit 59df310

Please sign in to comment.