diff --git a/.gitmodules b/.gitmodules index fcee35578..b1120c041 100644 --- a/.gitmodules +++ b/.gitmodules @@ -19,3 +19,6 @@ [submodule "vendor/libpng"] path = vendor/libpng url = https://github.com/glennrp/libpng.git +[submodule "vendor/utf8proc"] + path = vendor/utf8proc + url = https://github.com/JuliaStrings/utf8proc.git diff --git a/build.zig b/build.zig index 48cb6d5a5..7a60fcfc9 100644 --- a/build.zig +++ b/build.zig @@ -6,6 +6,7 @@ const glfw = @import("vendor/mach/glfw/build.zig"); const freetype = @import("pkg/freetype/build.zig"); const libuv = @import("pkg/libuv/build.zig"); const libpng = @import("pkg/libpng/build.zig"); +const utf8proc = @import("pkg/utf8proc/build.zig"); const zlib = @import("pkg/zlib/build.zig"); const tracylib = @import("pkg/tracy/build.zig"); const system_sdk = @import("vendor/mach/glfw/system_sdk.zig"); @@ -182,6 +183,10 @@ fn addDeps( var libuv_step = try libuv.link(b, step); system_sdk.include(b, libuv_step, .{}); + // utf8proc + step.addPackage(utf8proc.pkg); + _ = try utf8proc.link(b, step); + // Tracy step.addPackage(tracylib.pkg); if (tracy) { diff --git a/pkg/utf8proc/build.zig b/pkg/utf8proc/build.zig new file mode 100644 index 000000000..6fe9dc3ac --- /dev/null +++ b/pkg/utf8proc/build.zig @@ -0,0 +1,51 @@ +const std = @import("std"); + +/// Directories with our includes. +const root = thisDir() ++ "../../../vendor/utf8proc/"; +const include_path = root; + +pub const include_paths = .{include_path}; + +pub const pkg = std.build.Pkg{ + .name = "utf8proc", + .source = .{ .path = thisDir() ++ "/main.zig" }, +}; + +fn thisDir() []const u8 { + return std.fs.path.dirname(@src().file) orelse "."; +} + +pub fn link(b: *std.build.Builder, step: *std.build.LibExeObjStep) !*std.build.LibExeObjStep { + const lib = try buildLib(b, step); + step.linkLibrary(lib); + step.addIncludePath(include_path); + return lib; +} + +pub fn buildLib( + b: *std.build.Builder, + step: *std.build.LibExeObjStep, +) !*std.build.LibExeObjStep { + const lib = b.addStaticLibrary("utf8proc", null); + lib.setTarget(step.target); + lib.setBuildMode(step.build_mode); + + // Include + lib.addIncludePath(include_path); + + // Link + lib.linkLibC(); + + // Compile + var flags = std.ArrayList([]const u8).init(b.allocator); + defer flags.deinit(); + + // C files + lib.addCSourceFiles(srcs, flags.items); + + return lib; +} + +const srcs = &.{ + root ++ "utf8proc.c", +}; diff --git a/pkg/utf8proc/c.zig b/pkg/utf8proc/c.zig new file mode 100644 index 000000000..adeb226b0 --- /dev/null +++ b/pkg/utf8proc/c.zig @@ -0,0 +1,3 @@ +pub usingnamespace @cImport({ + @cInclude("utf8proc.h"); +}); diff --git a/pkg/utf8proc/main.zig b/pkg/utf8proc/main.zig new file mode 100644 index 000000000..31c54b545 --- /dev/null +++ b/pkg/utf8proc/main.zig @@ -0,0 +1,8 @@ +pub const c = @import("c.zig"); + +/// Given a codepoint, return a character width analogous to `wcwidth(codepoint)`, +/// except that a width of 0 is returned for non-printable codepoints +/// instead of -1 as in `wcwidth`. +pub fn charwidth(codepoint: u21) u8 { + return @intCast(u8, c.utf8proc_charwidth(@intCast(i32, codepoint))); +} diff --git a/vendor/utf8proc b/vendor/utf8proc new file mode 160000 index 000000000..63f31c908 --- /dev/null +++ b/vendor/utf8proc @@ -0,0 +1 @@ +Subproject commit 63f31c908ef7656415f73d6c178f08181239f74c