Repository navigation
Expand file tree
/
Copy pathbuild.zig
More file actions
190 lines (160 loc) · 6.27 KB
/
Copy pathbuild.zig
File metadata and controls
190 lines (160 loc) · 6.27 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
const std = @import("std");
const builtin = @import("builtin");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const polystate = b.addModule("root", .{
.root_source_file = b.path("src/polystate.zig"),
.target = target,
.optimize = optimize,
});
const mod_tests = b.addTest(.{
.root_module = polystate,
});
const run_mod_tests = b.addRunArtifact(mod_tests);
const test_step = b.step("test", "Run tests");
test_step.dependOn(&run_mod_tests.step);
addExampleGraphsStep(b, target, polystate);
addBenchStep(b, target, polystate);
}
fn addBenchStep(
b: *std.Build,
target: std.Build.ResolvedTarget,
polystate: *std.Build.Module,
) void {
const bench_exe = b.addExecutable(.{
.name = "polystate_bench",
.root_module = b.createModule(.{
.root_source_file = b.path("bench/highState.zig"),
.target = target,
.imports = &.{
.{ .name = "polystate", .module = polystate },
},
}),
});
const bench_run = b.addRunArtifact(bench_exe);
const bench_step = b.step("bench", "polystate bench");
bench_step.dependOn(&bench_run.step);
}
fn addExampleGraphsStep(
b: *std.Build,
target: std.Build.ResolvedTarget,
polystate_mod: *std.Build.Module,
) void {
const graph_step = b.step("example-graphs", "Generate SVG graphs for the README examples");
const examples_dir_name = "examples";
const io = b.graph.io;
const graph_install_path = b.build_root.handle.realPathFileAlloc(io, examples_dir_name, b.allocator) catch |err| std.debug.panic("{}", .{err});
const graph_install_path_relative = std.fs.path.relative(b.allocator, "", &b.graph.environ_map, b.install_path, graph_install_path) catch |err| std.debug.panic("{}", .{err});
const graph_install_dir: std.Build.InstallDir = .{ .custom = graph_install_path_relative };
var examples_dir = b.build_root.handle.openDir(io, examples_dir_name, .{ .iterate = true }) catch |err| std.debug.panic("{}", .{err});
defer examples_dir.close(io);
var iterator = examples_dir.iterate();
while (iterator.next(io) catch |err| std.debug.panic("{}", .{err})) |entry| {
if (entry.kind == .directory) {
const example_name = b.allocator.dupe(u8, entry.name) catch @panic("OOM");
const mod = b.addModule(
example_name,
.{
.root_source_file = b.path(b.pathJoin(&.{ examples_dir_name, example_name, "main.zig" })),
.target = target,
.imports = &.{
.{
.name = "polystate",
.module = polystate_mod,
},
},
},
);
addGraphToStep(b, graph_step, mod, target, polystate_mod, graph_install_dir, example_name);
}
}
}
fn addGraphToStep(
b: *std.Build,
step: *std.Build.Step,
mod: *std.Build.Module,
target: std.Build.ResolvedTarget,
polystate: *std.Build.Module,
install_dir: std.Build.InstallDir,
dst_rel_path: []const u8,
) void {
const graph_file = addGraphFile(b, "graph", mod, .graphviz, polystate, target);
const dot_cmd = b.addSystemCommand(&.{"dot"});
dot_cmd.addArg("-Tsvg");
dot_cmd.addFileArg(graph_file);
const graph_svg = dot_cmd.captureStdOut(.{});
const install_graph_svg = b.addInstallFileWithDir(graph_svg, install_dir, b.pathJoin(&.{ dst_rel_path, "graph.svg" }));
step.dependOn(&install_graph_svg.step);
}
pub const GraphMode = enum {
graphviz,
mermaid,
json,
};
pub fn addGraphFile(
b: *std.Build,
module_name: []const u8,
module: *std.Build.Module,
graph_mode: GraphMode,
polystate: *std.Build.Module,
target: std.Build.ResolvedTarget,
) std.Build.LazyPath {
var allocating: std.Io.Writer.Allocating = .init(b.graph.arena);
const writer = &allocating.writer;
writer.print(
\\const std = @import("std");
\\const ps = @import("polystate");
\\const Target = @import("{s}");
\\pub fn main(init: std.process.Init) !void {{
\\ const io = init.io;
\\ var gpa_instance = std.heap.DebugAllocator(.{{}}){{}};
\\ const gpa = gpa_instance.allocator();
\\ var graph = try ps.Graph.initWithFsm(gpa, Target.EnterFsmState);
\\ defer graph.deinit();
\\ var stdout_buffer: [1024]u8 = undefined;
\\ var stdout_writer = std.Io.File.stdout().writer(io, &stdout_buffer);
\\ const writer = &stdout_writer.interface;
\\ defer writer.flush() catch @panic("Failed to flush");
\\ try graph.{s}(writer);
\\}}
, .{ module_name, switch (graph_mode) {
.graphviz => "generateDot",
.mermaid => "generateMermaid",
.json => "generateJson",
} }) catch @panic("OOM");
const options = b.addOptions();
options.contents = writer.toArrayList();
const opt_mod = b.createModule(.{
.root_source_file = options.getOutput(),
.target = target,
.imports = &.{
.{ .name = "polystate", .module = polystate },
.{ .name = b.allocator.dupe(u8, module_name) catch @panic("OOM"), .module = module },
},
});
const gen_exe_name = std.mem.concat(b.allocator, u8, &.{ "_generate_graph_for_", module_name }) catch @panic("OOM");
const opt_exe = b.addExecutable(.{
.name = gen_exe_name,
.root_module = opt_mod,
});
const run = b.addRunArtifact(opt_exe);
return run.captureStdOut(.{});
}
pub fn addInstallGraphFile(
b: *std.Build,
module_name: []const u8,
module: *std.Build.Module,
graph_mode: GraphMode,
polystate: *std.Build.Module,
target: std.Build.ResolvedTarget,
install_dir: std.Build.InstallDir,
) *std.Build.Step.InstallFile {
const dot_file = addGraphFile(b, module_name, module, graph_mode, polystate, target);
const output_name = std.mem.concat(b.allocator, u8, &.{ module_name, switch (graph_mode) {
.graphviz => ".dot",
.mermaid => ".mmd",
.json => ".json",
} }) catch @panic("OOM");
return b.addInstallFileWithDir(dot_file, install_dir, output_name);
}