Embed binary data at compile time

Well i tried to change some of the suggested things, but i didn’t manage to get it working. Nonetheless i thank you all again for taking your time trying to help me :slight_smile:

After playing with it some, I realized I misread your original message. My bad. I apologize for that.

This is the guts of my minimal build.zig implementation:

// setup, loops, file reading, populating "names" variable
// ...

var src: std.ArrayList(u8) = .empty;
try src.appendSlice(b.allocator,
    \\pub const Icons = struct {
    \\
);
for (names.items) |name| {
    try src.appendSlice(b.allocator, b.fmt("    @\"{s}\": []const u8,\n", .{name}));
}
try src.appendSlice(b.allocator,
    \\};
    \\
    \\pub const icons: Icons = blk: {
    \\    var f: Icons = undefined;
    \\    for (@typeInfo(Icons).@"struct".fields) |field| {
    \\        @field(f, field.name) = @embedFile(field.name ++ ".tvg");
    \\    }
    \\    break :blk f;
    \\};
    \\
);

...

I can post it on a gist if you are interested.

(I do stand behind my other comments on the thread.)

1 Like

That would be nice! :slight_smile:

the part im struggling with is: how do i get the arraylist hooked to my main file from there?
And also in your example are the tvgs generated on disk or in cache and than just passed as lazyPath ?

https://codeberg.org/xsawyerx/tvg-svg-poc/

They’re generated on disk. I tried the cache, which worked great except that ZLS didn’t read from there, so the benefit you were going for was gone.

1 Like

Wow thank you for the effort.
This does indeed look clean!

In the meantime (took me measly 4 hours), i managed to come up with a solution! :smiley:
Now, i don’t know if it’s better, BUT it caches every SVG separately.
Also with no permanent disk write and the LSP is still happy it seems.
Compile time is the same as my hexdump version tho…

Can be viewed here and is open for critique.

Is there anything with my solution, i came up with, that is worse than yours?

Again ty i learned some things which is great :smiley:

1 Like