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

Build liblmdb, associated tools and tests with zig build system #1

Merged
merged 16 commits into from
Sep 30, 2024
Merged
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
61 changes: 61 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: CI

on:
push:
branches: [main]

pull_request:
branches: [main]

workflow_dispatch:

jobs:
build:
strategy:
fail-fast: false
matrix:
zig-version: ["master", "0.13.0"]
os: [ubuntu-latest, macos-latest, windows-latest]
optimize: [ReleaseSafe, ReleaseFast]
build-options: ["-Dlto"]

runs-on: ${{ matrix.os }}

steps:
- name: Check out repository
uses: actions/checkout@v4
with:
submodules: false

- name: Set up Zig
uses: mlugg/setup-zig@v1
with:
version: ${{ matrix.zig-version }}
use-cache: false

- name: Run `build`
run: zig build ${{ matrix.build-options }} -Doptimize=${{ matrix.optimize }} --summary all

test:
strategy:
fail-fast: false
matrix:
zig-version: ["master", "0.13.0"]
os: [ubuntu-latest, macos-latest, windows-latest]

runs-on: ${{ matrix.os }}

steps:
- name: Check out repository
uses: actions/checkout@v4
with:
submodules: false

- name: Setup Zig
uses: mlugg/setup-zig@v1
with:
version: ${{ matrix.zig-version }}
use-cache: false

- name: Install `Test`
run: zig build install-test --summary all
201 changes: 201 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
const std = @import("std");
const builtin = @import("builtin");
const mem = std.mem;

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const lmdb_upstream = b.dependency(
"lmdb",
.{ .target = target, .optimize = optimize },
);
const lmdb_root = "libraries/liblmdb";

const strip = b.option(bool, "strip", "Strip debug information") orelse false;
const lto = b.option(bool, "lto", "Enable link time optimization") orelse false;

// writing WritingLibFiles isn't implemented on windows
// and zld the only linker suppored on macos
const is_macos = builtin.os.tag == .macos;
const is_windows = builtin.os.tag == .windows;
const use_lld = if (is_macos) false else if (is_windows) true else switch (optimize) {
.Debug => false,
else => true,
};
const liblmdb = b.addStaticLibrary(.{
.name = "lmdb",
.target = target,
.optimize = optimize,
.link_libc = true,
.strip = strip,
.use_llvm = switch (optimize) {
.Debug => if (is_windows) true else false,
else => true,
},
.use_lld = use_lld,
});
liblmdb.want_lto = if (is_macos) false else lto;
liblmdb.root_module.sanitize_c = false;

const liblmdb_src = .{
"mdb.c",
"midl.c",
};
const lmdb_includes = .{
"lmdb.h",
"midl.h",
};
const cflags = .{
"-pthread",
"-std=c23",
};

liblmdb.addCSourceFiles(.{
.root = lmdb_upstream.path(lmdb_root),
.files = &liblmdb_src,
.flags = &cflags,
});
liblmdb.addIncludePath(lmdb_upstream.path(lmdb_root));
liblmdb.root_module.addCMacro("_XOPEN_SOURCE", "600");
if (is_macos) {
liblmdb.root_module.addCMacro("_DARWIN_C_SOURCE", "");
}

liblmdb.installHeadersDirectory(
lmdb_upstream.path(lmdb_root),
"",
.{ .include_extensions = &lmdb_includes },
);

b.installArtifact(liblmdb);

const lmdb_tools = [_][]const u8{
"mdb_copy.c",
"mdb_drop.c",
"mdb_dump.c",
"mdb_load.c",
"mdb_stat.c",
"mplay.c",
};

const tools_step = b.step("tools", "Install lmdb tools");

for (lmdb_tools) |tool_file| {
const bin_name = tool_file[0..mem.indexOfScalar(u8, tool_file, '.').?];
const tool = b.addExecutable(.{
.name = bin_name,
.target = target,
.optimize = optimize,
.link_libc = true,
.strip = strip,
.use_llvm = switch (optimize) {
.Debug => false,
else => true,
},
.use_lld = use_lld,
});
tool.root_module.sanitize_c = false;

tool.addCSourceFiles(.{
.root = lmdb_upstream.path(lmdb_root),
.files = &.{tool_file},
.flags = &cflags,
});
tool.addIncludePath(lmdb_upstream.path(lmdb_root));
tool.root_module.addCMacro("_XOPEN_SOURCE", "600");
if (is_macos) {
tool.root_module.addCMacro("_DARWIN_C_SOURCE", "");
}
tool.linkLibrary(liblmdb);

const install_tool = b.addInstallArtifact(tool, .{});
tools_step.dependOn(&install_tool.step);
}

const lmdb_api = b.addTranslateC(.{
.root_source_file = b.path("include/c.h"),
.target = b.graph.host,
.optimize = .Debug,
});

if (@hasDecl(std.Build.Step.TranslateC, "addIncludeDir")) {
const path = lmdb_upstream.path(lmdb_root);
const absolute_include = path.getPath2(b, null);
lmdb_api.addIncludeDir(absolute_include);
} else {
lmdb_api.addIncludePath(lmdb_upstream.path(lmdb_root));
}

_ = b.addModule("lmdb", .{
.root_source_file = lmdb_api.getOutput(),
.target = target,
.optimize = optimize,
});

const cflags_test = .{
"-pthread",
"-std=c17", //c23 forbids function use without prototype
"-Wno-format",
"-Wno-implicit-function-declaration",
};

const lmdb_test = [_][]const u8{
"mtest.c",
"mtest2.c",
"mtest3.c",
"mtest4.c",
"mtest5.c",
// "mtest6.c", // disabled as it requires building liblmdb with MDB_DEBUG
};

const test_step = b.step("install-test", "Install lmdb unit tests");
const test_subpath = "test/";

for (lmdb_test) |test_file| {
const test_name = test_file[0..mem.indexOfScalar(u8, test_file, '.').?];
const test_exe = b.addExecutable(.{
.name = test_name,
.target = target,
.optimize = .Debug,
.link_libc = true,
.use_lld = use_lld,
});
test_exe.root_module.sanitize_c = false;

test_exe.addCSourceFiles(.{
.root = lmdb_upstream.path(lmdb_root),
.files = &.{test_file},
.flags = &cflags_test,
});
test_exe.addIncludePath(lmdb_upstream.path(lmdb_root));

test_exe.linkLibrary(liblmdb);

const install_test_exe = b.addInstallArtifact(test_exe, .{ .dest_dir = .{ .override = .{
.custom = test_subpath,
} } });
test_step.dependOn(&install_test_exe.step);
}

const create_testdb = struct {
fn make(step: *std.Build.Step, options: blk: {
if (@hasDecl(std.Build.Step, "MakeOptions")) {
break :blk std.Build.Step.MakeOptions;
} else {
break :blk std.Progress.Node;
}
}) !void {
_ = options;
const step_build = step.owner;
std.fs.cwd().makeDir(step_build.fmt(
"{s}/{s}/testdb/",
.{ step_build.install_prefix, test_subpath },
)) catch |err| switch (err) {
error.PathAlreadyExists => {},
else => unreachable,
};
}
}.make;
test_step.makeFn = create_testdb;
}
19 changes: 19 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.{
.name = "lmdb",
.version = "0.9.31",
.minimum_zig_version = "0.13.0",
.dependencies = .{
.lmdb = .{
.url = "https://github.com/LMDB/lmdb/archive/9c9d34558cc438f99aebd1ab58f83fd7faeabc0a.tar.gz",
.hash = "12205c0f7d4ad44b671cf89a7793aafa31ba4739f01b706313e4eadc944b734bd224",
.lazy = false,
},
},
.paths = .{
"build.zig",
"build.zig.zon",
"include",
"LICENSE",
"README.md",
},
}
2 changes: 2 additions & 0 deletions include/c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include "lmdb.h"
#include "midl.h"
Loading