diff options
author | Olivier Chafik <ochafik@users.noreply.github.com> | 2024-04-21 18:48:53 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-21 18:48:53 +0100 |
commit | 5cf5e7d490dfdd2e70bface2d35dfd14aa44b4fb (patch) | |
tree | 9af269d3fa30667bbaefc6978db94013d68b3f22 /build.zig | |
parent | 40f74e4d739e9250431cf339ae7588b28d8d0663 (diff) |
`build`: generate hex dump of server assets during build (#6661)
* `build`: generate hex dumps of server assets on the fly
* build: workaround lack of -n on gnu xxd
* build: don't use xxd in cmake
* build: don't call xxd from build.zig
* build: more idiomatic hexing
* build: don't use xxd in Makefile (od hackery instead)
* build: avoid exceeding max cmd line limit in makefile hex dump
* build: hex dump assets at cmake build time (not config time)
Diffstat (limited to 'build.zig')
-rw-r--r-- | build.zig | 29 |
1 files changed, 29 insertions, 0 deletions
@@ -140,4 +140,33 @@ pub fn build(b: *std.build.Builder) !void { if (server.target.isWindows()) { server.linkSystemLibrary("ws2_32"); } + + const server_assets = [_][]const u8{ "index.html", "index.js", "completion.js", "json-schema-to-grammar.mjs" }; + for (server_assets) |asset| { + const input_path = b.fmt("examples/server/public/{s}", .{asset}); + const output_path = b.fmt("examples/server/{s}.hpp", .{asset}); + + // Portable equivalent of `b.addSystemCommand(&.{ "xxd", "-n", asset, "-i", input_path, output_path }) })`: + + const input = try std.fs.cwd().readFileAlloc(b.allocator, input_path, std.math.maxInt(usize)); + defer b.allocator.free(input); + + var buf = std.ArrayList(u8).init(b.allocator); + defer buf.deinit(); + + for (input) |byte| { + try std.fmt.format(buf.writer(), "0x{X:0>2}, ", .{byte}); + } + + var name = try std.mem.replaceOwned(u8, b.allocator, asset, "-", "_"); + defer b.allocator.free(name); + std.mem.replaceScalar(u8, name, '.', '_'); + + try std.fs.cwd().writeFile(output_path, b.fmt( + "unsigned char {s}[] = {{{s}}};\nunsigned int {s}_len = {d};\n", + .{ name, buf.items, name, input.len }, + )); + + std.debug.print("Dumped hex of \"{s}\" ({s}) to {s}\n", .{ input_path, name, output_path }); + } } |