mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 05:44:20 +00:00
Functions like isMinGW() and isGnuLibC() have a good reason to exist: They look at multiple components of the target. But functions like isWasm(), isDarwin(), isGnu(), etc only exist to save 4-8 characters. I don't think this is a good enough reason to keep them, especially given that: * It's not immediately obvious to a reader whether target.isDarwin() means the same thing as target.os.tag.isDarwin() precisely because isMinGW() and similar functions *do* look at multiple components. * It's not clear where we would draw the line. The logical conclusion before this commit would be to also wrap Arch.isX86(), Os.Tag.isSolarish(), Abi.isOpenHarmony(), etc... this obviously quickly gets out of hand. * It's nice to just have a single correct way of doing something.
151 lines
5 KiB
Zig
151 lines
5 KiB
Zig
const std = @import("std");
|
|
const builtin = @import("builtin");
|
|
|
|
pub fn build(b: *std.Build) void {
|
|
const test_step = b.step("test", "Test it");
|
|
b.default_step = test_step;
|
|
|
|
const target = b.standardTargetOptions(.{});
|
|
const optimize = b.standardOptimizeOption(.{});
|
|
|
|
// Unwinding with a frame pointer
|
|
//
|
|
// getcontext version: zig std
|
|
//
|
|
// Unwind info type:
|
|
// - ELF: DWARF .debug_frame
|
|
// - MachO: __unwind_info encodings:
|
|
// - x86_64: RBP_FRAME
|
|
// - aarch64: FRAME, DWARF
|
|
{
|
|
const exe = b.addExecutable(.{
|
|
.name = "unwind_fp",
|
|
.root_module = b.createModule(.{
|
|
.root_source_file = b.path("unwind.zig"),
|
|
.target = target,
|
|
.optimize = optimize,
|
|
.unwind_tables = if (target.result.os.tag.isDarwin()) .@"async" else null,
|
|
.omit_frame_pointer = false,
|
|
}),
|
|
});
|
|
|
|
const run_cmd = b.addRunArtifact(exe);
|
|
test_step.dependOn(&run_cmd.step);
|
|
}
|
|
|
|
// Unwinding without a frame pointer
|
|
//
|
|
// getcontext version: zig std
|
|
//
|
|
// Unwind info type:
|
|
// - ELF: DWARF .eh_frame_hdr + .eh_frame
|
|
// - MachO: __unwind_info encodings:
|
|
// - x86_64: STACK_IMMD, STACK_IND
|
|
// - aarch64: FRAMELESS, DWARF
|
|
{
|
|
const exe = b.addExecutable(.{
|
|
.name = "unwind_nofp",
|
|
.root_module = b.createModule(.{
|
|
.root_source_file = b.path("unwind.zig"),
|
|
.target = target,
|
|
.optimize = optimize,
|
|
.unwind_tables = .@"async",
|
|
.omit_frame_pointer = true,
|
|
}),
|
|
});
|
|
|
|
const run_cmd = b.addRunArtifact(exe);
|
|
test_step.dependOn(&run_cmd.step);
|
|
}
|
|
|
|
// Unwinding through a C shared library without a frame pointer (libc)
|
|
//
|
|
// getcontext version: libc
|
|
//
|
|
// Unwind info type:
|
|
// - ELF: DWARF .eh_frame + .debug_frame
|
|
// - MachO: __unwind_info encodings:
|
|
// - x86_64: STACK_IMMD, STACK_IND
|
|
// - aarch64: FRAMELESS, DWARF
|
|
{
|
|
const c_shared_lib = b.addLibrary(.{
|
|
.linkage = .dynamic,
|
|
.name = "c_shared_lib",
|
|
.root_module = b.createModule(.{
|
|
.root_source_file = null,
|
|
.target = target,
|
|
.optimize = optimize,
|
|
.link_libc = true,
|
|
.strip = false,
|
|
}),
|
|
});
|
|
|
|
if (target.result.os.tag == .windows)
|
|
c_shared_lib.root_module.addCMacro("LIB_API", "__declspec(dllexport)");
|
|
|
|
c_shared_lib.root_module.addCSourceFile(.{
|
|
.file = b.path("shared_lib.c"),
|
|
.flags = &.{"-fomit-frame-pointer"},
|
|
});
|
|
|
|
const exe = b.addExecutable(.{
|
|
.name = "shared_lib_unwind",
|
|
.root_module = b.createModule(.{
|
|
.root_source_file = b.path("shared_lib_unwind.zig"),
|
|
.target = target,
|
|
.optimize = optimize,
|
|
.unwind_tables = if (target.result.os.tag.isDarwin()) .@"async" else null,
|
|
.omit_frame_pointer = true,
|
|
}),
|
|
});
|
|
|
|
exe.linkLibrary(c_shared_lib);
|
|
|
|
const run_cmd = b.addRunArtifact(exe);
|
|
test_step.dependOn(&run_cmd.step);
|
|
|
|
// Separate debug info ELF file
|
|
if (target.result.ofmt == .elf) {
|
|
const filename = b.fmt("{s}_stripped", .{exe.out_filename});
|
|
const stripped_exe = b.addObjCopy(exe.getEmittedBin(), .{
|
|
.basename = filename, // set the name for the debuglink
|
|
.compress_debug = true,
|
|
.strip = .debug,
|
|
.extract_to_separate_file = true,
|
|
});
|
|
|
|
const run_stripped = std.Build.Step.Run.create(b, b.fmt("run {s}", .{filename}));
|
|
run_stripped.addFileArg(stripped_exe.getOutput());
|
|
test_step.dependOn(&run_stripped.step);
|
|
}
|
|
}
|
|
|
|
// Unwinding without libc/posix
|
|
//
|
|
// No "getcontext" or "ucontext_t"
|
|
const no_os_targets = [_]std.Target.Os.Tag{ .freestanding, .other };
|
|
inline for (no_os_targets) |os_tag| {
|
|
const exe = b.addExecutable(.{
|
|
.name = "unwind_freestanding",
|
|
.root_module = b.createModule(.{
|
|
.root_source_file = b.path("unwind_freestanding.zig"),
|
|
.target = b.resolveTargetQuery(.{
|
|
.cpu_arch = .x86_64,
|
|
.os_tag = os_tag,
|
|
}),
|
|
.optimize = optimize,
|
|
.unwind_tables = null,
|
|
.omit_frame_pointer = false,
|
|
}),
|
|
});
|
|
|
|
// This "freestanding" binary is runnable because it invokes the
|
|
// Linux exit syscall directly.
|
|
if (builtin.os.tag == .linux and builtin.cpu.arch == .x86_64) {
|
|
const run_cmd = b.addRunArtifact(exe);
|
|
test_step.dependOn(&run_cmd.step);
|
|
} else {
|
|
test_step.dependOn(&exe.step);
|
|
}
|
|
}
|
|
}
|