Zig 0.17.0-dev (Split by item)

This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.

findBuildRoot

Maker.findBuildRoot
fn findBuildRoot(arena: Allocator, io: Io, options: FindBuildRootOptions) !BuildRoot

File

lib/compiler/Maker.zig:3353

Code

fn findBuildRoot(arena: Allocator, io: Io, options: FindBuildRootOptions) !BuildRoot {
    const build_zig_basename = if (options.build_file) |bf|
        Dir.path.basename(bf)
    else
        std.zig.build_zig_basename;

    if (options.build_file) |bf| {
        if (Dir.path.dirname(bf)) |dirname| {
            const dir = Io.Dir.cwd().openDir(io, dirname, .{}) catch |err| {
                fatal("failed opening directory containing {q}: {t}", .{ bf, err });
            };
            return .{
                .build_zig_basename = build_zig_basename,
                .directory = .{ .path = dirname, .handle = dir },
                .close_directory = true,
            };
        }

        return .{
            .build_zig_basename = build_zig_basename,
            .directory = .cwd(),
            .close_directory = false,
        };
    }
    // Search up parent directories until we find build.zig.
    var dirname: ?[]const u8 = null;
    while (true) {
        const joined_path = if (dirname) |d|
            try Dir.path.join(arena, &.{ d, build_zig_basename })
        else
            build_zig_basename;
        if (Io.Dir.cwd().access(io, joined_path, .{})) |_| {
            const dir = if (dirname) |d|
                Io.Dir.cwd().openDir(io, d, .{}) catch |err| {
                    fatal("unable to open directory while searching for build.zig file, {q}: {t}", .{ d, err });
                }
            else
                Io.Dir.cwd();
            return .{
                .build_zig_basename = build_zig_basename,
                .directory = .{
                    .path = dirname,
                    .handle = dir,
                },
                .close_directory = dirname != null,
            };
        } else |err| switch (err) {
            error.FileNotFound => {
                dirname = Dir.path.dirname(dirname orelse options.cwd_path) orelse {
                    log.info("initialize {s} template file with \"zig init\"", .{std.zig.build_zig_basename});
                    log.info("see \"zig --help\" for more options", .{});
                    fatal("no build.zig file found, in the current directory or any parent directories", .{});
                };
                continue;
            },
            else => |e| return e,
        }
    }
}