Skip to content

Commit

Permalink
poc
Browse files Browse the repository at this point in the history
  • Loading branch information
azuline committed May 12, 2024
1 parent db5a822 commit 60c8a5c
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 21 deletions.
14 changes: 12 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
# Testing
db.sqlite3

# Nix
result
# Python
.direnv
__pycache__
.ruff_cache
.mypy_cache
.coverage
.coverage.*
result
db.sqlite3
htmlcov

# Zig
*.so
*.so.o
zig-cache
zig-out
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ lint:
ruff check --fix .
prettier --write .

.PHONY: check test typecheck lintcheck lint
clean:
git clean -xdf

.PHONY: check test typecheck lintcheck lint clean
42 changes: 24 additions & 18 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
};
doCheck = false;
};
prod-deps = with python.pkgs; [
prod-py-deps = with python.pkgs; [
appdirs
cffi
click
jinja2
llfuse
Expand All @@ -38,22 +39,15 @@
uuid6-python
watchdog
];
dev-deps = with python.pkgs; [
dev-py-deps = with python.pkgs; [
mypy
pytest
pytest-timeout
pytest-cov
pytest-xdist
snapshottest
];
zig-pkgs = with pkgs; [
zig
zls
];
dev-cli = pkgs.writeShellScriptBin "rose" ''
cd $ROSE_ROOT
python -m rose_cli "$@"
'';
version = nixpkgs.lib.strings.removeSuffix "\n" (builtins.readFile ./rose/.version);
in
{
devShells.default = pkgs.mkShell {
Expand All @@ -70,25 +64,37 @@
buildInputs = [
(pkgs.buildEnv {
name = "rose-devshell";
paths = with pkgs; ([
(python.withPackages (_: prod-deps ++ dev-deps))
paths = with pkgs; [
(python.withPackages (_: prod-py-deps ++ dev-py-deps))
ruff
dev-cli
nodePackages.pyright
nodePackages.prettier
] ++ zig-pkgs);
zig
zls
];
})
];
};
packages = rec {
rose = python.pkgs.buildPythonPackage {
rose-zig = pkgs.stdenv.mkDerivation {
pname = "rose";
version = version;
src = ./rose_zig;
nativeBuildInputs = [ pkgs.zig.hook ];
};
# TODO: Split up into multiple packages.
rose-py = python.pkgs.buildPythonPackage {
pname = "rose";
version = nixpkgs.lib.strings.removeSuffix "\n" (builtins.readFile ./rose/.version);
version = version;
src = ./.;
propagatedBuildInputs = prod-deps;
propagatedBuildInputs = prod-py-deps ++ [ rose-zig ];
nativeBuildInputs = [ pkgs.makeWrapper ];
postInstall = ''
wrapProgram $out/bin/rose \
--prefix LD_LIBRARY_PATH : "${pkgs.lib.makeLibraryPath [ rose-zig ]}"
'';
doCheck = false;
};
default = rose;
};
});
}
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ ignore_missing_imports = true
[[tool.mypy.overrides]]
module = "snapshottest"
ignore_missing_imports = true
[[tool.mypy.overrides]]
module = "cffi"
ignore_missing_imports = true

[tool.pytest.ini_options]
addopts = [
Expand Down
16 changes: 16 additions & 0 deletions rose/ffi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# type: ignore
from cffi import FFI

ffi = FFI()

lib = ffi.dlopen("rose")

ffi.cdef("""
void free_str(void *str);
char *getRelease();
""")


def get_release() -> str:
return ffi.string(ffi.gc(lib.getRelease(), lib.free_str)).decode("utf-8")
4 changes: 4 additions & 0 deletions rose_cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

def main() -> None:
from rose import RoseExpectedError
from rose.ffi import get_release

print(get_release())
exit(0)

try:
cli()
Expand Down
14 changes: 14 additions & 0 deletions rose_zig/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const std = @import("std");

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" },
.target = target,
.optimize = optimize,
});
b.installArtifact(libroseffi);
}
17 changes: 17 additions & 0 deletions rose_zig/src/ffi.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const std = @import("std");
const rose = @import("rose.zig");

var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();

export fn getRelease() [*:0]const u8 {
const message = rose.getRelease(allocator) catch |err| switch (err) {
error.OutOfMemory => @panic("Out of memory"),
};
return message.ptr;
}

export fn free_str(str: [*:0]const u8) void {
const len = std.mem.len(str);
allocator.free(str[0 .. len + 1]);
}
5 changes: 5 additions & 0 deletions rose_zig/src/rose.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const std = @import("std");

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

0 comments on commit 60c8a5c

Please sign in to comment.