Skip to content

Commit

Permalink
move zig libc command to be lazily built
Browse files Browse the repository at this point in the history
part of #19063

This is a prerequisite for doing the same for Resinator.
  • Loading branch information
andrewrk committed Feb 28, 2024
1 parent 6e07888 commit e5f9d3c
Show file tree
Hide file tree
Showing 20 changed files with 1,499 additions and 1,481 deletions.
10 changes: 5 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -507,16 +507,18 @@ set(ZIG_STAGE2_SOURCES
"${CMAKE_SOURCE_DIR}/lib/std/zig/Ast.zig"
"${CMAKE_SOURCE_DIR}/lib/std/zig/AstGen.zig"
"${CMAKE_SOURCE_DIR}/lib/std/zig/AstRlAnnotate.zig"
"${CMAKE_SOURCE_DIR}/lib/std/zig/c_builtins.zig"
"${CMAKE_SOURCE_DIR}/lib/std/zig/LibCInstallation.zig"
"${CMAKE_SOURCE_DIR}/lib/std/zig/Parse.zig"
"${CMAKE_SOURCE_DIR}/lib/std/zig/render.zig"
"${CMAKE_SOURCE_DIR}/lib/std/zig/Server.zig"
"${CMAKE_SOURCE_DIR}/lib/std/zig/WindowsSdk.zig"
"${CMAKE_SOURCE_DIR}/lib/std/zig/Zir.zig"
"${CMAKE_SOURCE_DIR}/lib/std/zig/c_builtins.zig"
"${CMAKE_SOURCE_DIR}/lib/std/zig/render.zig"
"${CMAKE_SOURCE_DIR}/lib/std/zig/string_literal.zig"
"${CMAKE_SOURCE_DIR}/lib/std/zig/system.zig"
"${CMAKE_SOURCE_DIR}/lib/std/zig/system/NativePaths.zig"
"${CMAKE_SOURCE_DIR}/lib/std/zig/system/x86.zig"
"${CMAKE_SOURCE_DIR}/lib/std/zig/tokenizer.zig"
"${CMAKE_SOURCE_DIR}/lib/std/zig/Zir.zig"
"${CMAKE_SOURCE_DIR}/src/Air.zig"
"${CMAKE_SOURCE_DIR}/src/Compilation.zig"
"${CMAKE_SOURCE_DIR}/src/Compilation/Config.zig"
Expand Down Expand Up @@ -570,7 +572,6 @@ set(ZIG_STAGE2_SOURCES
"${CMAKE_SOURCE_DIR}/src/codegen/llvm/bindings.zig"
"${CMAKE_SOURCE_DIR}/src/glibc.zig"
"${CMAKE_SOURCE_DIR}/src/introspect.zig"
"${CMAKE_SOURCE_DIR}/src/libc_installation.zig"
"${CMAKE_SOURCE_DIR}/src/libcxx.zig"
"${CMAKE_SOURCE_DIR}/src/libtsan.zig"
"${CMAKE_SOURCE_DIR}/src/libunwind.zig"
Expand Down Expand Up @@ -645,7 +646,6 @@ set(ZIG_STAGE2_SOURCES
"${CMAKE_SOURCE_DIR}/src/translate_c/ast.zig"
"${CMAKE_SOURCE_DIR}/src/type.zig"
"${CMAKE_SOURCE_DIR}/src/wasi_libc.zig"
"${CMAKE_SOURCE_DIR}/src/windows_sdk.zig"
"${CMAKE_SOURCE_DIR}/src/stubs/aro_builtins.zig"
"${CMAKE_SOURCE_DIR}/src/stubs/aro_names.zig"
)
Expand Down
137 changes: 137 additions & 0 deletions lib/compiler/libc.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
const std = @import("std");
const mem = std.mem;
const io = std.io;
const LibCInstallation = std.zig.LibCInstallation;

const usage_libc =
\\Usage: zig libc
\\
\\ Detect the native libc installation and print the resulting
\\ paths to stdout. You can save this into a file and then edit
\\ the paths to create a cross compilation libc kit. Then you
\\ can pass `--libc [file]` for Zig to use it.
\\
\\Usage: zig libc [paths_file]
\\
\\ Parse a libc installation text file and validate it.
\\
\\Options:
\\ -h, --help Print this help and exit
\\ -target [name] <arch><sub>-<os>-<abi> see the targets command
\\ -includes Print the libc include directories for the target
\\
;

pub fn main() !void {
var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena_instance.deinit();
const arena = arena_instance.allocator();
const gpa = arena;

const args = try std.process.argsAlloc(arena);
const zig_lib_directory = args[1];

var input_file: ?[]const u8 = null;
var target_arch_os_abi: []const u8 = "native";
var print_includes: bool = false;
{
var i: usize = 2;
while (i < args.len) : (i += 1) {
const arg = args[i];
if (mem.startsWith(u8, arg, "-")) {
if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {
const stdout = std.io.getStdOut().writer();
try stdout.writeAll(usage_libc);
return std.process.cleanExit();
} else if (mem.eql(u8, arg, "-target")) {
if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
i += 1;
target_arch_os_abi = args[i];
} else if (mem.eql(u8, arg, "-includes")) {
print_includes = true;
} else {
fatal("unrecognized parameter: '{s}'", .{arg});
}
} else if (input_file != null) {
fatal("unexpected extra parameter: '{s}'", .{arg});
} else {
input_file = arg;
}
}
}

const target_query = std.zig.parseTargetQueryOrReportFatalError(gpa, .{
.arch_os_abi = target_arch_os_abi,
});
const target = std.zig.resolveTargetQueryOrFatal(target_query);

if (print_includes) {
const libc_installation: ?*LibCInstallation = libc: {
if (input_file) |libc_file| {
const libc = try arena.create(LibCInstallation);
libc.* = LibCInstallation.parse(arena, libc_file, target) catch |err| {
fatal("unable to parse libc file at path {s}: {s}", .{ libc_file, @errorName(err) });
};
break :libc libc;
} else {
break :libc null;
}
};

const is_native_abi = target_query.isNativeAbi();

const libc_dirs = std.zig.LibCDirs.detect(
arena,
zig_lib_directory,
target,
is_native_abi,
true,
libc_installation,
) catch |err| {
const zig_target = try target.zigTriple(arena);
fatal("unable to detect libc for target {s}: {s}", .{ zig_target, @errorName(err) });
};

if (libc_dirs.libc_include_dir_list.len == 0) {
const zig_target = try target.zigTriple(arena);
fatal("no include dirs detected for target {s}", .{zig_target});
}

var bw = std.io.bufferedWriter(std.io.getStdOut().writer());
var writer = bw.writer();
for (libc_dirs.libc_include_dir_list) |include_dir| {
try writer.writeAll(include_dir);
try writer.writeByte('\n');
}
try bw.flush();
return std.process.cleanExit();
}

if (input_file) |libc_file| {
var libc = LibCInstallation.parse(gpa, libc_file, target) catch |err| {
fatal("unable to parse libc file at path {s}: {s}", .{ libc_file, @errorName(err) });
};
defer libc.deinit(gpa);
} else {
if (!target_query.isNative()) {
fatal("unable to detect libc for non-native target", .{});
}
var libc = LibCInstallation.findNative(.{
.allocator = gpa,
.verbose = true,
.target = target,
}) catch |err| {
fatal("unable to detect native libc: {s}", .{@errorName(err)});
};
defer libc.deinit(gpa);

var bw = std.io.bufferedWriter(std.io.getStdOut().writer());
try libc.render(bw.writer());
try bw.flush();
}
}

fn fatal(comptime format: []const u8, args: anytype) noreturn {
std.log.err(format, args);
std.process.exit(1);
}
16 changes: 16 additions & 0 deletions lib/std/Target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2755,6 +2755,22 @@ fn eqlIgnoreCase(ignore_case: bool, a: []const u8, b: []const u8) bool {
}
}

pub fn osArchName(target: std.Target) [:0]const u8 {
return switch (target.os.tag) {
.linux => switch (target.cpu.arch) {
.arm, .armeb, .thumb, .thumbeb => "arm",
.aarch64, .aarch64_be, .aarch64_32 => "aarch64",
.mips, .mipsel, .mips64, .mips64el => "mips",
.powerpc, .powerpcle, .powerpc64, .powerpc64le => "powerpc",
.riscv32, .riscv64 => "riscv",
.sparc, .sparcel, .sparc64 => "sparc",
.x86, .x86_64 => "x86",
else => @tagName(target.cpu.arch),
},
else => @tagName(target.cpu.arch),
};
}

const Target = @This();
const std = @import("std.zig");
const builtin = @import("builtin");
Expand Down
Loading

0 comments on commit e5f9d3c

Please sign in to comment.