-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.zig
101 lines (87 loc) · 3.2 KB
/
build.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const std = @import("std");
// These need to match the upstream filenames
const models = .{
"ja",
"ja_knbc",
"th",
"zh-hans",
"zh-hant",
};
fn compressModels(b: *std.Build) ![]const u8 {
const tmp = b.makeTempPath();
var tmp_dir = try std.fs.openDirAbsolute(tmp, .{});
defer tmp_dir.close();
const budoux = b.dependency("budoux", .{});
inline for (models) |name| {
var arena = std.heap.ArenaAllocator.init(b.allocator);
defer arena.deinit();
const compressed_model = blk: {
const model_path = std.fmt.comptimePrint("budoux/models/{s}.json", .{name});
const path = budoux.path(model_path).getPath(b);
var file = try std.fs.openFileAbsolute(path, .{ .mode = .read_only });
defer file.close();
var buf = std.ArrayList(u8).init(arena.allocator());
try std.compress.zlib.compress(file.reader(), buf.writer(), .{
.level = .best,
});
break :blk buf.items;
};
var file = try tmp_dir.createFile(name ++ ".z", .{});
defer file.close();
try file.writeAll(compressed_model);
}
return tmp;
}
fn addModels(b: *std.Build, mod: *std.Build.Module, dir: []const u8) !void {
inline for (models) |name| {
const path = b.fmt("{s}/{s}.z", .{ dir, name });
var import: [("budoux-" ++ name).len]u8 = ("budoux-" ++ name).*;
std.mem.replaceScalar(u8, &import, '_', '-');
mod.addAnonymousImport(import[0..], .{
.root_source_file = .{ .cwd_relative = path },
});
}
}
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const dir = try compressModels(b);
const mod = b.addModule("zig-budoux", .{
.root_source_file = b.path("src/budoux.zig"),
});
try addModels(b, mod, dir);
const lib = b.addStaticLibrary(.{
.name = "budoux",
.root_source_file = b.path("src/c.zig"),
.target = target,
.optimize = .ReleaseFast,
.pic = true, // to stop clang from complaining
});
lib.addIncludePath(b.path("include"));
lib.installHeader(b.path("include/budoux.h"), "budoux.h");
try addModels(b, &lib.root_module, dir);
b.installArtifact(lib);
const exe_test = b.addTest(.{
.root_source_file = b.path("src/budoux.zig"),
.target = target,
.optimize = optimize,
});
try addModels(b, &exe_test.root_module, dir);
const run_test_exe = b.addRunArtifact(exe_test);
const run_test = b.step("test", "Run unit tests");
run_test.dependOn(&run_test_exe.step);
const docs_step = b.step("docs", "Build the project documentation");
const doc_obj = b.addObject(.{
.name = "docs",
.root_source_file = b.path("src/budoux.zig"),
.target = target,
.optimize = optimize,
});
try addModels(b, &doc_obj.root_module, dir);
const install_docs = b.addInstallDirectory(.{
.source_dir = doc_obj.getEmittedDocs(),
.install_dir = .prefix,
.install_subdir = std.fmt.comptimePrint("docs/{s}", .{"zig-budoux"}),
});
docs_step.dependOn(&install_docs.step);
}