Really simple example:
Project structure:
build.zig:
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe_mod = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{
.name = "spec.xml",
.module = b.createModule(.{
.root_source_file = b.path("assets/spec.xml"),
}),
},
},
});
const exe_compile = b.addExecutable(.{
.name = "foo",
.root_module = exe_mod,
});
b.installArtifact(exe_compile);
const run_step = b.step("run", "Run the app");
const run_cmd = b.addRunArtifact(exe_compile);
run_step.dependOn(&run_cmd.step);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
}
src/main.zig
const std = @import("std");
const xml = @embedFile("spec.xml");
pub fn main() !void {
std.debug.print("{s}\n", .{xml});
}
This compiles, runs and prints out everything correctly.
However I thought I was supposed to use use b.addEmbedPath, but was unable to get it working.
I managed to make it work by putting assets folder is into src and changing path to xml file in build.xml to src/assets/spec.xml, otherwise zig build run fails with:
src\main.zig:3:24: error: unable to open 'assets/spec.xml': FileNotFound
const xml = @embedFile("assets/spec.xml");
And since since assets folder is in src, there is actually no need for use of b.addEmbedPath since everything in src is already “embeddable“ by default.
My question is, is the way it’s currently written a “correct” way to embed files outside of src folder? If so, what is the purpose of b.addEmbedPath since, like I figured out, everything inside src is already “embeddable” by default?
Edit: @Sze That was a typo. I meant to use name of imported module, not path inside main.zig.
