update zig to 0.11 and update build script

Also update memcpy to avoid recursive memcpy due to optimizations.
This commit is contained in:
Brendan Hansknecht 2023-09-20 14:42:11 -07:00
parent 0aaf8ebf8a
commit 2e2e609547
No known key found for this signature in database
GPG key ID: 0EA784685083E75B
18 changed files with 535 additions and 507 deletions

View file

@ -1,25 +1,23 @@
const std = @import("std");
const mem = std.mem;
const Builder = std.build.Builder;
const Build = std.Build;
const LazyPath = Build.LazyPath;
const CrossTarget = std.zig.CrossTarget;
const Arch = std.Target.Cpu.Arch;
pub fn build(b: *Builder) void {
// b.setPreferredReleaseMode(.Debug);
b.setPreferredReleaseMode(.ReleaseFast);
const mode = b.standardReleaseOptions();
pub fn build(b: *Build) void {
// const mode = b.standardOptimizeOption(.{ .preferred_optimize_mode = .ReleaseFast });
const mode = b.standardOptimizeOption(.{ .preferred_optimize_mode = .ReleaseFast });
// Options
const fallback_main_path = "./src/main.zig";
const main_path_desc = b.fmt("Override path to main.zig. Used by \"ir\" and \"test\". Defaults to \"{s}\". ", .{fallback_main_path});
const main_path = b.option([]const u8, "main-path", main_path_desc) orelse fallback_main_path;
const main_path = .{ .path = b.option([]const u8, "main-path", main_path_desc) orelse fallback_main_path };
// Tests
var main_tests = b.addTest(main_path);
main_tests.setBuildMode(mode);
main_tests.linkLibC();
const main_tests = b.addTest(.{ .root_source_file = main_path, .link_libc = true });
const test_step = b.step("test", "Run tests");
test_step.dependOn(&main_tests.step);
test_step.dependOn(&b.addRunArtifact(main_tests).step);
// Targets
const host_target = b.standardTargetOptions(.{
@ -35,7 +33,7 @@ pub fn build(b: *Builder) void {
// LLVM IR
generateLlvmIrFile(b, mode, host_target, main_path, "ir", "builtins-host");
generateLlvmIrFile(b, mode, linux32_target, main_path, "ir-i386", "builtins-i386");
generateLlvmIrFile(b, mode, linux32_target, main_path, "ir-x86", "builtins-x86");
generateLlvmIrFile(b, mode, linux_x64_target, main_path, "ir-x86_64", "builtins-x86_64");
generateLlvmIrFile(b, mode, linux_aarch64_target, main_path, "ir-aarch64", "builtins-aarch64");
generateLlvmIrFile(b, mode, windows64_target, main_path, "ir-windows-x86_64", "builtins-windows-x86_64");
@ -45,29 +43,31 @@ pub fn build(b: *Builder) void {
generateObjectFile(b, mode, host_target, main_path, "object", "builtins-host");
generateObjectFile(b, mode, windows64_target, main_path, "windows-x86_64-object", "builtins-windows-x86_64");
generateObjectFile(b, mode, wasm32_target, main_path, "wasm32-object", "builtins-wasm32");
removeInstallSteps(b);
}
// TODO zig 0.9 can generate .bc directly, switch to that when it is released!
fn generateLlvmIrFile(
b: *Builder,
b: *Build,
mode: std.builtin.Mode,
target: CrossTarget,
main_path: []const u8,
main_path: LazyPath,
step_name: []const u8,
object_name: []const u8,
) void {
const obj = b.addObject(object_name, main_path);
obj.setBuildMode(mode);
const obj = b.addObject(.{ .name = object_name, .root_source_file = main_path, .optimize = mode, .target = target, .use_llvm = true });
obj.strip = true;
obj.emit_llvm_ir = .emit;
obj.emit_llvm_bc = .emit;
obj.emit_bin = .no_emit;
obj.target = target;
// Generating the bin seems required to get zig to generate the llvm ir.
_ = obj.getEmittedBin();
const ir_file = obj.getEmittedLlvmIr();
const bc_file = obj.getEmittedLlvmBc();
const install_ir = b.addInstallFile(ir_file, b.fmt("{s}.ll", .{object_name}));
const install_bc = b.addInstallFile(bc_file, b.fmt("{s}.bc", .{object_name}));
const ir = b.step(step_name, "Build LLVM ir");
ir.dependOn(&obj.step);
ir.dependOn(&install_ir.step);
ir.dependOn(&install_bc.step);
b.getInstallStep().dependOn(ir);
}
// Generate Object File
@ -76,29 +76,37 @@ fn generateLlvmIrFile(
// is never called. I think it could theoretically be called by a dynamic lib that links to the executable
// or something similar.
fn generateObjectFile(
b: *Builder,
b: *Build,
mode: std.builtin.Mode,
target: CrossTarget,
main_path: []const u8,
main_path: LazyPath,
step_name: []const u8,
object_name: []const u8,
) void {
const obj = b.addObject(object_name, main_path);
obj.setBuildMode(mode);
obj.linkSystemLibrary("c");
obj.setOutputDir(".");
const obj = b.addObject(.{ .name = object_name, .root_source_file = main_path, .optimize = mode, .target = target, .use_llvm = true });
obj.strip = true;
obj.target = target;
obj.link_function_sections = true;
obj.force_pic = true;
const obj_file = obj.getEmittedBin();
var suffix =
if (target.os_tag == .windows)
"obj"
else
"o";
const install = b.addInstallFile(obj_file, b.fmt("{s}.{s}", .{ object_name, suffix }));
const obj_step = b.step(step_name, "Build object file for linking");
obj_step.dependOn(&obj.step);
obj_step.dependOn(&install.step);
b.getInstallStep().dependOn(obj_step);
}
fn makeLinux32Target() CrossTarget {
var target = CrossTarget.parse(.{}) catch unreachable;
target.cpu_arch = std.Target.Cpu.Arch.i386;
target.cpu_arch = std.Target.Cpu.Arch.x86;
target.os_tag = std.Target.Os.Tag.linux;
target.abi = std.Target.Abi.musl;
@ -145,12 +153,3 @@ fn makeWasm32Target() CrossTarget {
return target;
}
fn removeInstallSteps(b: *Builder) void {
for (b.top_level_steps.items) |top_level_step, i| {
const name = top_level_step.step.name;
if (mem.eql(u8, name, "install") or mem.eql(u8, name, "uninstall")) {
_ = b.top_level_steps.swapRemove(i);
}
}
}