Skip to content

Commit

Permalink
help
Browse files Browse the repository at this point in the history
  • Loading branch information
azuline committed May 12, 2024
1 parent 882f248 commit 19b384c
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 14 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
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.

21 changes: 21 additions & 0 deletions rose-ffi/build.zig
Original file line number Diff line number Diff line change
@@ -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);
}
10 changes: 10 additions & 0 deletions rose-ffi/build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.{
.name = "rose-ffi",
.version = "0.5.0",
.dependencies = .{
.rose = .{
.path = "../rose-zig",
},
},
.paths = .{""},
}
12 changes: 12 additions & 0 deletions rose-ffi/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{ stdenv
, zig
, version
, rose-zig
}:

stdenv.mkDerivation {
pname = "rose-ffi";
version = version;
src = ./.;
nativeBuildInputs = [ zig.hook rose-zig ];
}
2 changes: 1 addition & 1 deletion rose-zig/src/ffi.zig → rose-ffi/src/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
14 changes: 10 additions & 4 deletions rose-zig/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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") },
},
});
}
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 = .{""},
}
7 changes: 7 additions & 0 deletions rose-zig/build.zig.zon2json-lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"1220e0961c135c5aa3af77a043dbc5890a18235a157238df0e2882fe84a8c8439c7a": {
"name": "sqlite",
"url": "https://github.com/vrischmann/zig-sqlite/archive/dc339b7cf3bca82a12c2169231dd247587766781.tar.gz",
"hash": "sha256-PbWUedWBuNxA7diVAEh225b6YQA757aSvomQjUgSgRk="
}
}
52 changes: 52 additions & 0 deletions rose-zig/deps.nix
Original file line number Diff line number Diff line change
@@ -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=";
};
}
]
24 changes: 24 additions & 0 deletions rose-zig/src/root.zig
Original file line number Diff line number Diff line change
@@ -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"));
}
5 changes: 0 additions & 5 deletions rose-zig/src/rose.zig

This file was deleted.

0 comments on commit 19b384c

Please sign in to comment.