From ad36e4887285ebd179053af4479106375b19b8fd Mon Sep 17 00:00:00 2001 From: azuline Date: Sun, 12 May 2024 12:04:52 -0500 Subject: [PATCH] get sqlite set up in zig; split ffi off from core rose zig module (#127) --- Makefile | 16 ++++++++----- flake.lock | 6 ++--- flake.nix | 1 + rose-cli/default.nix | 4 ++-- rose-py/default.nix | 1 + rose-zig/build.zig | 33 +++++++++++++++++++++++--- rose-zig/build.zig.zon | 11 +++++++++ rose-zig/default.nix | 6 ++++- rose-zig/deps.nix | 13 ++++++++++ rose-zig/{src/ffi.zig => ffi/root.zig} | 2 +- rose-zig/rose/root.zig | 26 ++++++++++++++++++++ rose-zig/src/rose.zig | 5 ---- 12 files changed, 103 insertions(+), 21 deletions(-) create mode 100644 rose-zig/build.zig.zon create mode 100644 rose-zig/deps.nix rename rose-zig/{src/ffi.zig => ffi/root.zig} (92%) create mode 100644 rose-zig/rose/root.zig delete mode 100644 rose-zig/src/rose.zig diff --git a/Makefile b/Makefile index eb2ca87..2f17f51 100644 --- a/Makefile +++ b/Makefile @@ -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 . @@ -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 diff --git a/flake.lock b/flake.lock index c235b3d..488bea8 100644 --- a/flake.lock +++ b/flake.lock @@ -20,11 +20,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1712608508, - "narHash": "sha256-vMZ5603yU0wxgyQeHJryOI+O61yrX2AHwY6LOFyV1gM=", + "lastModified": 1715266358, + "narHash": "sha256-doPgfj+7FFe9rfzWo1siAV2mVCasW+Bh8I1cToAXEE4=", "owner": "nixos", "repo": "nixpkgs", - "rev": "4cba8b53da471aea2ab2b0c1f30a81e7c451f4b6", + "rev": "f1010e0469db743d14519a1efd37e23f8513d714", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index fb1209e..2fd5f7b 100644 --- a/flake.nix +++ b/flake.nix @@ -80,6 +80,7 @@ nodePackages.prettier zig zls + zon2nix ]; }) ]; diff --git a/rose-cli/default.nix b/rose-cli/default.nix index 49261c0..a50ed5e 100644 --- a/rose-cli/default.nix +++ b/rose-cli/default.nix @@ -6,8 +6,8 @@ , rose-watch }: -python-pin.pkgs.buildPythonPackage { - pname = "rose-cli"; +python-pin.pkgs.buildPythonApplication { + pname = "rose"; version = version; src = ./.; propagatedBuildInputs = [ diff --git a/rose-py/default.nix b/rose-py/default.nix index 2c56594..9a36267 100644 --- a/rose-py/default.nix +++ b/rose-py/default.nix @@ -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 diff --git a/rose-zig/build.zig b/rose-zig/build.zig index 08d4b5b..614c298 100644 --- a/rose-zig/build.zig +++ b/rose-zig/build.zig @@ -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); } diff --git a/rose-zig/build.zig.zon b/rose-zig/build.zig.zon new file mode 100644 index 0000000..04a6fe7 --- /dev/null +++ b/rose-zig/build.zig.zon @@ -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 = .{""}, +} diff --git a/rose-zig/default.nix b/rose-zig/default.nix index c6b648a..235af1d 100644 --- a/rose-zig/default.nix +++ b/rose-zig/default.nix @@ -1,4 +1,5 @@ -{ stdenv +{ callPackage +, stdenv , zig , version }: @@ -8,4 +9,7 @@ stdenv.mkDerivation { version = version; src = ./.; nativeBuildInputs = [ zig.hook ]; + postPatch = '' + ln -s ${callPackage ./deps.nix { }} $ZIG_GLOBAL_CACHE_DIR/p + ''; } diff --git a/rose-zig/deps.nix b/rose-zig/deps.nix new file mode 100644 index 0000000..a6d7ce3 --- /dev/null +++ b/rose-zig/deps.nix @@ -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="; + }; + } +] diff --git a/rose-zig/src/ffi.zig b/rose-zig/ffi/root.zig similarity index 92% rename from rose-zig/src/ffi.zig rename to rose-zig/ffi/root.zig index d838260..999221b 100644 --- a/rose-zig/src/ffi.zig +++ b/rose-zig/ffi/root.zig @@ -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(); diff --git a/rose-zig/rose/root.zig b/rose-zig/rose/root.zig new file mode 100644 index 0000000..46ed947 --- /dev/null +++ b/rose-zig/rose/root.zig @@ -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); +} diff --git a/rose-zig/src/rose.zig b/rose-zig/src/rose.zig deleted file mode 100644 index 9a0b023..0000000 --- a/rose-zig/src/rose.zig +++ /dev/null @@ -1,5 +0,0 @@ -const std = @import("std"); - -pub fn getRelease(allocator: std.mem.Allocator) ![:0]const u8 { - return try allocator.dupeZ(u8, "hello"); -}