Skip to content

Commit

Permalink
Use Zig package manager instead of Git submodules
Browse files Browse the repository at this point in the history
Retrieve TigerBeetle using the Zig package manager instead of using Git
submodules to clone it.
Also remove hardcoded vsr_options struct and use the defaults provided by
TigerBeetle's build.zig (except for release and release_client_min, which are
needed to correctly connect to a cluster)
  • Loading branch information
rbino committed Aug 8, 2024
1 parent 9d4efb8 commit be97388
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 29 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ jobs:
steps:
- name: Clone the repository
uses: actions/checkout@v4
with:
submodules: recursive

- name: Install OTP and Elixir
uses: erlef/setup-beam@v1
Expand Down Expand Up @@ -102,8 +100,6 @@ jobs:
steps:
- name: Clone the repository
uses: actions/checkout@v4
with:
submodules: recursive

- name: Fetch TigerBeetle for Linux
if: runner.os == 'Linux'
Expand Down
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The package can be installed by adding `tigerbeetlex` to your list of dependenci
```elixir
def deps do
[
{:tigerbeetlex, github: "rbino/tigerbeetlex", submodules: true}
{:tigerbeetlex, github: "rbino/tigerbeetlex"}
]
end
```
Expand Down
28 changes: 26 additions & 2 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const std = @import("std");
// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void {
pub fn build(b: *std.Build) !void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
Expand All @@ -16,7 +16,7 @@ pub fn build(b: *std.Build) void {
const optimize = b.standardOptimizeOption(.{});

// Get ERTS_INCLUDE_DIR from env, which should be passed by :build_dot_zig
const erts_include_dir = std.process.getEnvVarOwned(b.allocator, "ERTS_INCLUDE_DIR") catch blk: {
const erts_include_dir = b.graph.env_map.get("ERTS_INCLUDE_DIR") orelse blk: {
// Fallback to extracting it from the erlang shell so we can also execute zig build manually
const argv = [_][]const u8{
"erl",
Expand All @@ -31,6 +31,28 @@ pub fn build(b: *std.Build) void {
break :blk b.run(&argv);
};

// This is passed from mix.exs, which extracts it by "parsing" build.zig.zon
const release = b.option(
[]const u8,
"tigerbeetle_release",
"The release of TigerBeetle targeted by the client",
) orelse {
std.log.err("tigerbeetle_release option is required", .{});
return error.MissingTigerBeetleRelease;
};

// This is hardcoded in TigerBeetle in src/scripts/release.zig
const release_client_min: []const u8 = "0.15.3";

const opts = .{
.target = target,
.@"config-release" = release,
.@"config-release-client-min" = release_client_min,
// The rest of VSR options will use the default value
// TODO: should we expose other VSR build options here?
};
const vsr_mod = b.dependency("tigerbeetle", opts).module("vsr");

const lib = b.addSharedLibrary(.{
.name = "tigerbeetlex",
// In this case the main source file is merely a path, however, in more
Expand All @@ -41,6 +63,8 @@ pub fn build(b: *std.Build) void {
.link_libc = true,
});
lib.addSystemIncludePath(.{ .cwd_relative = erts_include_dir });
// TigerBeetle imports
lib.root_module.addImport("vsr", vsr_mod);
// This is needed to avoid errors on MacOS when loading the NIF
lib.linker_allow_shlib_undefined = true;

Expand Down
16 changes: 16 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.{
// This is currently advisory only; Zig does not yet do anything
// with this value.
.minimum_zig_version = "0.13.0",

// Each dependency must either provide a `url` and `hash`, or a `path`.
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
// Once all dependencies are fetched, `zig build` no longer requires
// internet connectivity.
.dependencies = .{
.tigerbeetle = .{
.url = "https://github.com/tigerbeetle/tigerbeetle/archive/refs/tags/0.15.4.tar.gz",
.hash = "1220df4340b291f24f4bb7346b7ed6fe06109eb79526c56b6ac85738e25519ad2ed7",
},
},
}
7 changes: 7 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
defmodule TigerBeetlex.MixProject do
use Mix.Project

@release_regex ~r{https://github.com/tigerbeetle/tigerbeetle/archive/refs/tags/(?<release>[0-9]+\.[0-9]+\.[0-9]+)\.tar\.gz}

@tigerbeetle_release File.read!("build.zig.zon")
|> then(&Regex.named_captures(@release_regex, &1))
|> Map.fetch!("release")

def project do
[
app: :tigerbeetlex,
version: "0.1.0",
elixir: "~> 1.14",
install_zig: "0.13.0",
zig_build_mode: zig_build_mode(Mix.env()),
zig_extra_options: [tigerbeetle_release: @tigerbeetle_release],
compilers: [:build_dot_zig] ++ Mix.compilers(),
start_permanent: Mix.env() == :prod,
dialyzer: [plt_add_apps: [:zig_parser]],
Expand Down
2 changes: 1 addition & 1 deletion src/account_batch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const batch = @import("batch.zig");
const beam = @import("beam.zig");
const scheduler = beam.scheduler;

const tb = @import("tigerbeetle/src/tigerbeetle.zig");
const tb = @import("vsr").tigerbeetle;
const Account = tb.Account;
const AccountFlags = tb.AccountFlags;
pub const AccountBatch = batch.Batch(Account);
Expand Down
2 changes: 1 addition & 1 deletion src/batch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const RwLock = std.Thread.RwLock;
const beam = @import("beam.zig");
const Resource = beam.resource.Resource;

const tb = @import("tigerbeetle/src/tigerbeetle.zig");
const tb = @import("vsr").tigerbeetle;
const Account = tb.Account;
const Transfer = tb.Transfer;

Expand Down
4 changes: 2 additions & 2 deletions src/client.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const process = beam.process;
const resource = beam.resource;
const Resource = resource.Resource;

const tb = @import("tigerbeetle/src/tigerbeetle.zig");
const tb_client = @import("tigerbeetle/src/clients/c/tb_client.zig");
const tb = @import("vsr").tigerbeetle;
const tb_client = @import("vsr").tb_client;
const Account = tb.Account;
const Transfer = tb.Transfer;

Expand Down
1 change: 0 additions & 1 deletion src/tigerbeetle
Submodule tigerbeetle deleted from 14abae
13 changes: 0 additions & 13 deletions src/tigerbeetlex.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,6 @@ const AccountBatchResource = account_batch.AccountBatchResource;
const IdBatchResource = id_batch.IdBatchResource;
const TransferBatchResource = transfer_batch.TransferBatchResource;

pub const vsr_options = .{
.config_base = .default,
.config_log_level = std.log.Level.info,
.tracer_backend = .none,
.hash_log_mode = .none,
.git_commit = null,
// TODO: take these from a proper place
.release = "0.15.4",
.release_client_min = "0.15.4",
.config_aof_record = false,
.config_aof_recovery = false,
};

pub const std_options = .{
.log_level = .err,
};
Expand Down
2 changes: 1 addition & 1 deletion src/transfer_batch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const batch = @import("batch.zig");
const beam = @import("beam.zig");
const scheduler = beam.scheduler;

const tb = @import("tigerbeetle/src/tigerbeetle.zig");
const tb = @import("vsr").tigerbeetle;
const Transfer = tb.Transfer;
const TransferFlags = tb.TransferFlags;
pub const TransferBatch = batch.Batch(Transfer);
Expand Down

0 comments on commit be97388

Please sign in to comment.