Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[alias]
build-static-ffi = "build --frozen --profile maxperf --package firewood-ffi --features ethhash,logger"
build-profile-ffi = "build --frozen --profile profiling --package firewood-ffi --features ethhash,logger"
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ lto = "fat"
debug = false
inherits = "release"

[profile.profiling]
inherits = "release"
debug = true
strip = false
lto = "fat"
panic = "unwind"

[workspace.lints.rust]
unsafe_code = "deny"
missing_debug_implementations = "warn"
Expand Down
2 changes: 1 addition & 1 deletion ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ parking_lot.workspace = true
# Regular dependencies
chrono = "0.4.42"
oxhttp = "0.3.1"
tikv-jemallocator = "0.6.1"
tikv-jemallocator = { version = "0.6.1", features = ["profiling"] }
# Optional dependencies
env_logger = { workspace = true, optional = true }
derive-where = "1.6.0"
Expand Down
87 changes: 51 additions & 36 deletions ffi/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@
(craneLib.filterCargoSources path type);
};

# Map Nix platform to Rust target triple (Go/Rust use aarch64, Nix uses arm64)
rustTarget = {
"aarch64-darwin" = "aarch64-apple-darwin";
"x86_64-darwin" = "x86_64-apple-darwin";
"aarch64-linux" = "aarch64-unknown-linux-gnu";
"x86_64-linux" = "x86_64-unknown-linux-gnu";
}.${pkgs.stdenv.hostPlatform.system} or pkgs.stdenv.hostPlatform.config;

commonArgs = {
inherit src;
strictDeps = true;
Expand All @@ -63,47 +71,48 @@
MACOSX_DEPLOYMENT_TARGET = "15.0";
};

cargoArtifacts = craneLib.buildDepsOnly (commonArgs // {
# Use cargo alias defined in .cargo/config.toml
cargoBuildCommand = "cargo build-static-ffi";
});

firewood-ffi = craneLib.buildPackage (commonArgs // {
inherit cargoArtifacts;
# Use cargo alias defined in .cargo/config.toml
cargoBuildCommand = "cargo build-static-ffi";

# Disable tests - we only need to build the static library
doCheck = false;

# Install the static library and header
postInstall = ''
# Create a package structure compatible with FIREWOOD_LD_MODE=STATIC_LIBS
mkdir -p $out/ffi
cp -R ./ffi/* $out/ffi/
mkdir -p $out/ffi/libs/${pkgs.stdenv.hostPlatform.config}
cp target/maxperf/libfirewood_ffi.a $out/ffi/libs/${pkgs.stdenv.hostPlatform.config}/

# Run go generate to switch CGO directives to STATIC_LIBS mode
cd $out/ffi
HOME=$TMPDIR GOTOOLCHAIN=local FIREWOOD_LD_MODE=STATIC_LIBS ${go}/bin/go generate
'';

meta = with lib; {
description = "C FFI bindings for Firewood, an embedded key-value store";
homepage = "https://github.com/ava-labs/firewood";
license = {
fullName = "Ava Labs Ecosystem License 1.1";
url = "https://github.com/ava-labs/firewood/blob/main/LICENSE.md";
mkFfi = { cargoCmd, targetProfile, extraArgs ? {} }:
craneLib.buildPackage (commonArgs // {
cargoArtifacts = craneLib.buildDepsOnly (commonArgs // {
cargoBuildCommand = cargoCmd;
} // extraArgs);
cargoBuildCommand = cargoCmd;
doCheck = false;
postInstall = ''
mkdir -p $out/ffi
cp -R ./ffi/* $out/ffi/
mkdir -p $out/ffi/libs/${rustTarget}
cp target/${targetProfile}/libfirewood_ffi.a $out/ffi/libs/${rustTarget}/
cd $out/ffi
HOME=$TMPDIR GOTOOLCHAIN=local FIREWOOD_LD_MODE=STATIC_LIBS ${go}/bin/go generate
'';
meta = with lib; {
description = "C FFI bindings for Firewood, an embedded key-value store";
homepage = "https://github.com/ava-labs/firewood";
license = {
fullName = "Ava Labs Ecosystem License 1.1";
url = "https://github.com/ava-labs/firewood/blob/main/LICENSE.md";
};
platforms = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
};
platforms = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
};
});
} // extraArgs);

firewood-ffi = mkFfi {
cargoCmd = "cargo build-static-ffi";
targetProfile = "maxperf";
};

firewood-ffi-profile = mkFfi {
cargoCmd = "cargo build-profile-ffi";
targetProfile = "profiling";
extraArgs.RUSTFLAGS = "-C force-frame-pointers=yes";
};
in
{
packages = {
inherit firewood-ffi;
inherit firewood-ffi firewood-ffi-profile;
default = firewood-ffi;
profile = firewood-ffi-profile;
};

apps.go = {
Expand All @@ -130,6 +139,12 @@
jq
just
rustToolchain
# Profiling tools
rustfilt
inferno
jemalloc
graphviz
perl
];

shellHook = ''
Expand Down
Loading