From 19b384c5592694f45aab889c04560cd9411d2535 Mon Sep 17 00:00:00 2001 From: blissful Date: Sun, 12 May 2024 02:41:56 -0400 Subject: [PATCH] help --- Makefile | 5 +- flake.lock | 6 +-- rose-ffi/build.zig | 21 ++++++++ rose-ffi/build.zig.zon | 10 ++++ rose-ffi/default.nix | 12 +++++ rose-zig/src/ffi.zig => rose-ffi/src/root.zig | 2 +- rose-zig/build.zig | 14 +++-- rose-zig/build.zig.zon | 11 ++++ rose-zig/build.zig.zon2json-lock | 7 +++ rose-zig/deps.nix | 52 +++++++++++++++++++ rose-zig/src/root.zig | 24 +++++++++ rose-zig/src/rose.zig | 5 -- 12 files changed, 155 insertions(+), 14 deletions(-) create mode 100644 rose-ffi/build.zig create mode 100644 rose-ffi/build.zig.zon create mode 100644 rose-ffi/default.nix rename rose-zig/src/ffi.zig => rose-ffi/src/root.zig (92%) create mode 100644 rose-zig/build.zig.zon create mode 100644 rose-zig/build.zig.zon2json-lock create mode 100644 rose-zig/deps.nix create mode 100644 rose-zig/src/root.zig delete mode 100644 rose-zig/src/rose.zig diff --git a/Makefile b/Makefile index eb2ca87..536bd61 100644 --- a/Makefile +++ b/Makefile @@ -31,4 +31,7 @@ lint: clean: git clean -xdf -.PHONY: build-zig check test typecheck lintcheck lint clean +nixify-zig-deps: + cd rose-zig && nix run 'github:Cloudef/zig2nix#zon2nix' -- build.zig.zon > 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/rose-ffi/build.zig b/rose-ffi/build.zig new file mode 100644 index 0000000..dfa7a43 --- /dev/null +++ b/rose-ffi/build.zig @@ -0,0 +1,21 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + const rose = b.dependency("rose", .{ + .target = target, + .optimize = optimize, + }); + + const librose = b.addSharedLibrary(.{ + .name = "rose", + .root_source_file = .{ .path = "src/root.zig" }, + .target = target, + .optimize = optimize, + }); + // TODO: Doesn't work. How do I depend? + librose.linkLibrary(rose.artifact("rose")); + b.installArtifact(librose); +} diff --git a/rose-ffi/build.zig.zon b/rose-ffi/build.zig.zon new file mode 100644 index 0000000..3cec32e --- /dev/null +++ b/rose-ffi/build.zig.zon @@ -0,0 +1,10 @@ +.{ + .name = "rose-ffi", + .version = "0.5.0", + .dependencies = .{ + .rose = .{ + .path = "../rose-zig", + }, + }, + .paths = .{""}, +} diff --git a/rose-ffi/default.nix b/rose-ffi/default.nix new file mode 100644 index 0000000..dad233b --- /dev/null +++ b/rose-ffi/default.nix @@ -0,0 +1,12 @@ +{ stdenv +, zig +, version +, rose-zig +}: + +stdenv.mkDerivation { + pname = "rose-ffi"; + version = version; + src = ./.; + nativeBuildInputs = [ zig.hook rose-zig ]; +} diff --git a/rose-zig/src/ffi.zig b/rose-ffi/src/root.zig similarity index 92% rename from rose-zig/src/ffi.zig rename to rose-ffi/src/root.zig index d838260..999221b 100644 --- a/rose-zig/src/ffi.zig +++ b/rose-ffi/src/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/build.zig b/rose-zig/build.zig index 08d4b5b..89a03ce 100644 --- a/rose-zig/build.zig +++ b/rose-zig/build.zig @@ -4,11 +4,17 @@ pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); - const libroseffi = b.addSharedLibrary(.{ - .name = "rose", - .root_source_file = .{ .path = "src/ffi.zig" }, + const sqlite = b.dependency("sqlite", .{ .target = target, .optimize = optimize, }); - b.installArtifact(libroseffi); + + _ = 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") }, + }, + }); } 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/build.zig.zon2json-lock b/rose-zig/build.zig.zon2json-lock new file mode 100644 index 0000000..cfd4667 --- /dev/null +++ b/rose-zig/build.zig.zon2json-lock @@ -0,0 +1,7 @@ +{ + "1220e0961c135c5aa3af77a043dbc5890a18235a157238df0e2882fe84a8c8439c7a": { + "name": "sqlite", + "url": "https://github.com/vrischmann/zig-sqlite/archive/dc339b7cf3bca82a12c2169231dd247587766781.tar.gz", + "hash": "sha256-PbWUedWBuNxA7diVAEh225b6YQA757aSvomQjUgSgRk=" + } +} diff --git a/rose-zig/deps.nix b/rose-zig/deps.nix new file mode 100644 index 0000000..ccb0013 --- /dev/null +++ b/rose-zig/deps.nix @@ -0,0 +1,52 @@ +# generated by zon2nix (https://github.com/Cloudef/zig2nix) + +{ lib, linkFarm, fetchurl, fetchgit, runCommandLocal, zig, name ? "zig-packages" }: + +with builtins; +with lib; + +let + unpackZigArtifact = { name, artifact }: runCommandLocal name { + nativeBuildInputs = [ zig ]; + } '' + hash="$(zig fetch --global-cache-dir "$TMPDIR" ${artifact})" + mv "$TMPDIR/p/$hash" "$out" + chmod 755 "$out" + ''; + + fetchZig = { name, url, hash }: let + artifact = fetchurl { inherit url hash; }; + in unpackZigArtifact { inherit name artifact; }; + + fetchGitZig = { name, url, hash }: let + parts = splitString "#" url; + base = elemAt parts 0; + rev = elemAt parts 1; + in fetchgit { + inherit name rev hash; + url = base; + deepClone = false; + }; + + fetchZigArtifact = { name, url, hash }: let + parts = splitString "://" url; + proto = elemAt parts 0; + path = elemAt parts 1; + fetcher = { + "git+http" = fetchGitZig { inherit name hash; url = "http://${path}"; }; + "git+https" = fetchGitZig { inherit name hash; url = "https://${path}"; }; + http = fetchZig { inherit name hash; url = "http://${path}"; }; + https = fetchZig { inherit name hash; url = "https://${path}"; }; + file = unpackZigArtifact { inherit name; artifact = /. + path; }; + }; + in fetcher.${proto}; +in linkFarm name [ + { + name = "1220e0961c135c5aa3af77a043dbc5890a18235a157238df0e2882fe84a8c8439c7a"; + path = fetchZigArtifact { + name = "sqlite"; + url = "https://github.com/vrischmann/zig-sqlite/archive/dc339b7cf3bca82a12c2169231dd247587766781.tar.gz"; + hash = "sha256-PbWUedWBuNxA7diVAEh225b6YQA757aSvomQjUgSgRk="; + }; + } +] \ No newline at end of file diff --git a/rose-zig/src/root.zig b/rose-zig/src/root.zig new file mode 100644 index 0000000..d31e356 --- /dev/null +++ b/rose-zig/src/root.zig @@ -0,0 +1,24 @@ +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" { + try testing.expect(std.mem.eql(u8, try getRelease(testing.allocator), "hello")); +} 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"); -}