From 5556e46cc453c024defa833b964648465ddc9e39 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Sat, 2 May 2026 16:41:33 +0200 Subject: build(zig): link statically typst --- go/build.zig | 14 ++++++-------- go/typdown.go | 25 +++++++++++++++++++++++-- go/typdown_test.go | 9 +++++++-- 3 files changed, 36 insertions(+), 12 deletions(-) (limited to 'go') diff --git a/go/build.zig b/go/build.zig index 6517786..6399042 100644 --- a/go/build.zig +++ b/go/build.zig @@ -4,6 +4,8 @@ pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); + const install_step = b.getInstallStep(); + const typdown = b.dependency("typdown", .{ .optimize = optimize, .target = target, @@ -11,15 +13,10 @@ pub fn build(b: *std.Build) !void { const lib = b.addLibrary(.{ .name = "typdown", .root_module = typdown, - .linkage = .static, }); //lib.bundle_compiler_rt = true; //lib.pie = true; - const install = b.addInstallArtifact(lib, .{}); - // when emitting headers will be fixed - // currently, we have to use a symlink/copy to get it - //installed.emitted_h = lib.getEmittedH(); - b.getInstallStep().dependOn(&install.step); + const install_lib = b.addInstallArtifact(lib, .{}); var flags = try std.ArrayList(u8).initCapacity(b.allocator, 2); try flags.appendSlice(b.allocator, "-linkmode external -extldflags -static"); @@ -29,7 +26,8 @@ pub fn build(b: *std.Build) !void { "-ldflags", flags.items, ".", }); - b.getInstallStep().dependOn(&go_build.step); + go_build.step.dependOn(&install_lib.step); + install_step.dependOn(&go_build.step); const test_step = b.step("test", "Run tests"); test_step.dependOn(b.getInstallStep()); @@ -42,5 +40,5 @@ pub fn build(b: *std.Build) !void { if (race) go_test.addArg("-race"); go_test.addArg("./..."); - test_step.dependOn(&go_test.step); + test_step.dependOn(install_step); } diff --git a/go/typdown.go b/go/typdown.go index 386b779..d4bf569 100644 --- a/go/typdown.go +++ b/go/typdown.go @@ -28,7 +28,11 @@ var ( ErrInvalidLink = errors.New("invalid link") ) -func Parse(content string) (template.HTML, error) { +type Document struct { + ptr unsafe.Pointer +} + +func Parse(content string) (*Document, error) { code := C.uchar(0) conv := C.CString(content) raw := C.typdown_parse(conv, &code) @@ -38,8 +42,25 @@ func Parse(content string) (template.HTML, error) { if code == 1 { panic(err) } - return "", err + return nil, err } + return &Document{raw}, nil +} + +func (d *Document) Deinit() { + C.typdown_free(d.ptr) +} + +func (d *Document) RenderHTML() (template.HTML, error) { + code := C.uchar(0) + raw := C.typdown_renderHTML(d.ptr, &code) defer C.free(unsafe.Pointer(raw)) + if code > 0 { + err := codeErrors[uint8(code)] + if code == 1 { + panic(err) + } + return "", err + } return template.HTML(C.GoString(raw)), nil } diff --git a/go/typdown_test.go b/go/typdown_test.go index c1d8985..974f661 100644 --- a/go/typdown_test.go +++ b/go/typdown_test.go @@ -7,7 +7,12 @@ func TestParse(t *testing.T) { if err != nil { t.Fatal(err) } - if res != `

hello world

` { - t.Errorf("invalid result: %s", res) + defer res.Deinit() + got, err := res.RenderHTML() + if err != nil { + t.Fatal(err) + } + if got != `

hello world

` { + t.Errorf("invalid result: %s", got) } } -- cgit v1.2.3