From 0535fa152ae990a28d0b7b9a59e96911074118b8 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Wed, 29 Apr 2026 18:10:33 +0200 Subject: build(zig): include typst module --- build.zig | 24 ++++++++++++++++++++++-- src/root.zig | 1 + src/typst.zig | 11 +++++++++++ typst/typdown_typst.h | 4 ++++ typst/typst.h | 4 ---- 5 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 src/typst.zig create mode 100644 typst/typdown_typst.h delete mode 100644 typst/typst.h diff --git a/build.zig b/build.zig index d41b8d1..8fc9b74 100644 --- a/build.zig +++ b/build.zig @@ -4,17 +4,36 @@ pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); + // build typst module + const build_typst = b.addSystemCommand(&[_][]const u8{ + "cargo", "build", + }); + build_typst.setCwd(b.path("typst/")); + if (optimize != .Debug) build_typst.addArg("--release"); + b.getInstallStep().dependOn(&build_typst.step); + const mod = b.addModule("typdown", .{ .root_source_file = b.path("src/root.zig"), .target = target, .optimize = optimize, }); if (!target.result.isWasiLibC()) mod.link_libc = true; + // link typst module during build + mod.addIncludePath(b.path("typst")); + mod.addLibraryPath( + if (optimize == .Debug) b.path("typst/target/debug/") + else b.path("typst/target/release/") + ); + if (optimize != .Debug) mod.strip = true; + const lib = b.addLibrary(.{ .name = "typdown", - .linkage = .dynamic, + .linkage = .static, .root_module = mod, }); + // link typst module to build + lib.linkSystemLibrary("typdown_typst"); + const installed_lib = b.addInstallArtifact(lib, .{}); // when emitting headers will be fixed //installed_lib.emitted_h = lib.getEmittedH(); @@ -42,10 +61,11 @@ pub fn build(b: *std.Build) void { const run_mod_tests = b.addRunArtifact(mod_tests); const test_step = b.step("test", "Run tests"); + test_step.dependOn(b.getInstallStep()); test_step.dependOn(&run_mod_tests.step); const examples_step = b.step("examples", "Run examples"); - examples_step.dependOn(&installed_lib.step); + examples_step.dependOn(b.getInstallStep()); const example_run = b.addRunArtifact(example); examples_step.dependOn(&example_run.step); diff --git a/src/root.zig b/src/root.zig index 6fd15f5..648fb77 100644 --- a/src/root.zig +++ b/src/root.zig @@ -2,6 +2,7 @@ const std = @import("std"); const builtin = @import("builtin"); const Allocator = std.mem.Allocator; const parser = @import("parser.zig"); +const typst = @import("typst.zig"); pub const Document = parser.Document; pub const Error = parser.Error; /// Parse the content. diff --git a/src/typst.zig b/src/typst.zig new file mode 100644 index 0000000..9608af5 --- /dev/null +++ b/src/typst.zig @@ -0,0 +1,11 @@ +const std = @import("std"); +const typst = @cImport(@cInclude("typdown_typst.h")); + +pub fn generateSVG(alloc: std.mem.Allocator, content: []const u8) ![]const u8 { + const source = try alloc.dupeZ(u8, content); + defer alloc.free(source); + const raw_res = typst.typst_generateSVG(source); + const res = try alloc.dupe(u8, std.mem.span(raw_res)); + defer typst.typst_freeString(raw_res); + return res; +} diff --git a/typst/typdown_typst.h b/typst/typdown_typst.h new file mode 100644 index 0000000..755314a --- /dev/null +++ b/typst/typdown_typst.h @@ -0,0 +1,4 @@ +extern const char *typst_generateSVG(const char*); +extern const char *typst_escapeMath(const char*); + +extern void typst_freeString(const char*); diff --git a/typst/typst.h b/typst/typst.h deleted file mode 100644 index 755314a..0000000 --- a/typst/typst.h +++ /dev/null @@ -1,4 +0,0 @@ -extern const char *typst_generateSVG(const char*); -extern const char *typst_escapeMath(const char*); - -extern void typst_freeString(const char*); -- cgit v1.2.3