Skip to content

make @embedFile support module-mapped names the same way as @import #14553

@andrewrk

Description

@andrewrk

Concrete example:

bootloader.zig

pub export fn _start() void {}

main.zig

const std = @import("std");
const blah = @embedFile("bootloader.elf");

pub fn main() !void {
    std.debug.print("first 3 bytes: " ++ blah[0..3] ++ "\n", .{});
}

build.zig

const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const bootloader = b.addExecutable(.{
        .name = "bootloader",
        .root_source_file = .{ .path = "bootloader.zig" },
        .target = .{
            .cpu_arch = .x86,
            .os_tag = .freestanding,
        },
        .optimize = .ReleaseSmall,
    });

    const exe = b.addExecutable(.{
        .name = "embed-generated-file",
        .root_source_file = .{ .path = "main.zig" },
        .target = target,
        .optimize = optimize,
    });
    exe.addAnonymousModule("bootloader.elf", .{
        .source_file = bootloader.getOutputSource(),
    });
    exe.install();
}

This fully works according to the build system, but compilation of main.zig fails like this:

andy@ark ~/m/embed-generated-file> zig build
main.zig:2:25: error: unable to open 'bootloader.elf': FileNotFound
const blah = @embedFile("bootloader.elf");
                        ^~~~~~~~~~~~~~~~
referenced by:
    main: main.zig:5:42
    callMain: /home/andy/Downloads/zig/lib/std/start.zig:616:32
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

error: embed-generated-file...
error: The following command exited with error code 1:
/home/andy/Downloads/zig/build-release/stage3/bin/zig build-exe /home/andy/misc/embed-generated-file/main.zig --cache-dir /home/andy/misc/embed-generated-file/zig-cache --global-cache-dir /home/andy/.cache/zig --name embed-generated-file --pkg-begin bootloader.elf /home/andy/misc/embed-generated-file/zig-cache/o/4fbab5d6b4239cb2e694a118ffff2202/bootloader --pkg-end --enable-cache 

You can see that it did, indeed, pass these flags:

--pkg-begin
bootloader.elf
/home/andy/misc/embed-generated-file/zig-cache/o/4fbab5d6b4239cb2e694a118ffff2202/bootloader
--pkg-end

This issue is to make zig honor those flags, and thereby allow @embedFile("bootloader.elf") to work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementSolving this issue will likely involve adding new logic or components to the codebase.frontendTokenization, parsing, AstGen, Sema, and Liveness.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions