Skip to content

Commit

Permalink
get sqlite set up in zig; split ffi off from core rose zig module (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
azuline authored May 12, 2024
1 parent 882f248 commit ad36e48
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 21 deletions.
16 changes: 10 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ check: typecheck test lintcheck
build-zig:
cd rose-zig && zig build -Doptimize=Debug

typecheck:
typecheck: build-zig
mypy .

test: build-zig
test-py: build-zig
pytest -n logical .
coverage html

test-seq: build-zig
pytest .
coverage html
test-zig:
cd rose-zig && zig build test --summary all

test: test-zig test-py

snapshot: build-zig
pytest --snapshot-update .
Expand All @@ -31,4 +32,7 @@ lint:
clean:
git clean -xdf

.PHONY: build-zig check test typecheck lintcheck lint clean
nixify-zig-deps:
cd rose-zig && zon2nix > deps.nix

.PHONY: build-zig check test typecheck lintcheck lint clean nixify-zig-deps
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
nodePackages.prettier
zig
zls
zon2nix
];
})
];
Expand Down
4 changes: 2 additions & 2 deletions rose-cli/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
, rose-watch
}:

python-pin.pkgs.buildPythonPackage {
pname = "rose-cli";
python-pin.pkgs.buildPythonApplication {
pname = "rose";
version = version;
src = ./.;
propagatedBuildInputs = [
Expand Down
1 change: 1 addition & 0 deletions rose-py/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ python-pin.pkgs.buildPythonPackage {
propagatedBuildInputs = [
rose-zig
py-deps.appdirs
py-deps.cffi
py-deps.click
py-deps.jinja2
py-deps.mutagen
Expand Down
33 changes: 30 additions & 3 deletions rose-zig/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,38 @@ pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const libroseffi = b.addSharedLibrary(.{
// Dependencies.
const sqlite = b.dependency("sqlite", .{
.target = target,
.optimize = optimize,
});

// Specify the core library module.
const rose = b.addModule("rose", .{
.root_source_file = b.path("rose/root.zig"),
.target = target,
.optimize = optimize,
.imports = &[_]std.Build.Module.Import{
.{ .name = "sqlite", .module = sqlite.module("sqlite") },
},
});

// Tests for the core library module.
const test_step = b.step("test", "Run unit tests");
const unit_tests = b.addTest(.{
.root_source_file = .{ .path = "rose/root.zig" },
.target = target,
});
const run_unit_tests = b.addRunArtifact(unit_tests);
test_step.dependOn(&run_unit_tests.step);

// Shared library for compatibility with other languages.
const librose = b.addSharedLibrary(.{
.name = "rose",
.root_source_file = .{ .path = "src/ffi.zig" },
.root_source_file = .{ .path = "ffi/root.zig" },
.target = target,
.optimize = optimize,
});
b.installArtifact(libroseffi);
librose.root_module.addImport("rose", rose);
b.installArtifact(librose);
}
11 changes: 11 additions & 0 deletions rose-zig/build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.{
.name = "rose",
.version = "0.5.0",
.dependencies = .{
.sqlite = .{
.url = "https://github.com/vrischmann/zig-sqlite/archive/dc339b7cf3bca82a12c2169231dd247587766781.tar.gz",
.hash = "1220e0961c135c5aa3af77a043dbc5890a18235a157238df0e2882fe84a8c8439c7a",
},
},
.paths = .{""},
}
6 changes: 5 additions & 1 deletion rose-zig/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{ stdenv
{ callPackage
, stdenv
, zig
, version
}:
Expand All @@ -8,4 +9,7 @@ stdenv.mkDerivation {
version = version;
src = ./.;
nativeBuildInputs = [ zig.hook ];
postPatch = ''
ln -s ${callPackage ./deps.nix { }} $ZIG_GLOBAL_CACHE_DIR/p
'';
}
13 changes: 13 additions & 0 deletions rose-zig/deps.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# generated by zon2nix (https://github.com/nix-community/zon2nix)

{ linkFarm, fetchzip }:

linkFarm "zig-packages" [
{
name = "1220e0961c135c5aa3af77a043dbc5890a18235a157238df0e2882fe84a8c8439c7a";
path = fetchzip {
url = "https://github.com/vrischmann/zig-sqlite/archive/dc339b7cf3bca82a12c2169231dd247587766781.tar.gz";
hash = "sha256-YouCVidJqhI5+joTSe1aSbnjVC+qVG2aIz6MyibRmQk=";
};
}
]
2 changes: 1 addition & 1 deletion rose-zig/src/ffi.zig → rose-zig/ffi/root.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const std = @import("std");
const rose = @import("rose.zig");
const rose = @import("rose");

var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
Expand Down
26 changes: 26 additions & 0 deletions rose-zig/rose/root.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const std = @import("std");
const sqlite = @import("sqlite");
const testing = std.testing;

pub fn getRelease(allocator: std.mem.Allocator) ![:0]const u8 {
return try allocator.dupeZ(u8, "hello");
}

pub fn getTrack(allocator: std.mem.Allocator) void {
const db = try sqlite.Db.init(.{
.mode = sqlite.Db.Mode{ .File = "/home/blissful/.cache/rose/cache.sqlite3" },
.open_flags = .{
.write = true,
.create = true,
},
.threading_mode = .MultiThread,
});
_ = db;
_ = allocator;
}

test "basic add functionality" {
const message = try getRelease(testing.allocator);
try testing.expect(std.mem.eql(u8, message, "hello"));
testing.allocator.free(message);
}
5 changes: 0 additions & 5 deletions rose-zig/src/rose.zig

This file was deleted.

0 comments on commit ad36e48

Please sign in to comment.