allo system compiler

This commit is contained in:
2024-12-19 20:13:20 +02:00
parent 7361b6058d
commit 69e90b6b9f

View File

@@ -1,9 +1,28 @@
const std = @import("std");
const c_lib = &[_][]const u8{ "tokenizer.c", "ast.c", "zig1.c" };
const all_c_files = c_lib ++ &[_][]const u8{"main.c"};
const c_lib_files = &[_][]const u8{ "tokenizer.c", "ast.c", "zig1.c" };
const all_c_files = c_lib_files ++ &[_][]const u8{"main.c"};
const cflags = &[_][]const u8{
"-std=c11",
"-Wall",
"-Wvla",
"-Wextra",
"-Werror",
"-Wshadow",
"-Wswitch",
"-Walloca",
"-Wformat=2",
"-fno-common",
"-Wconversion",
"-Wswitch-enum",
"-Wuninitialized",
"-Wdouble-promotion",
"-fstack-protector-all",
"-Wimplicit-fallthrough",
//"-D_FORTIFY_SOURCE=2", // consider when optimization flags are enabled
};
pub fn build(b: *std.Build) void {
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
@@ -12,29 +31,31 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
.target = target,
});
lib.addCSourceFiles(.{
.files = c_lib,
.flags = &[_][]const u8{
"-std=c11",
"-Wall",
"-Wvla",
"-Wextra",
"-Werror",
"-Wshadow",
"-Wswitch",
"-Walloca",
"-Wformat=2",
"-fno-common",
"-Wconversion",
"-Wswitch-enum",
"-Wuninitialized",
"-Wdouble-promotion",
"-fstack-protector-all",
"-Wimplicit-fallthrough",
//"-D_FORTIFY_SOURCE=2", // consider when optimization flags are enabled
},
});
lib.addIncludePath(b.path("."));
const cc = b.option([]const u8, "cc", "C compiler") orelse "zig";
if (std.mem.eql(u8, cc, "zig"))
lib.addCSourceFiles(.{ .files = c_lib_files, .flags = cflags })
else for (c_lib_files) |cfile| {
const objfile = try std.fmt.allocPrint(
b.allocator,
"{s}.o",
.{cfile[0 .. cfile.len - 2]},
);
const cc1 = b.addSystemCommand(&.{cc});
cc1.addArgs(cflags);
cc1.addArg("-g");
cc1.addArgs(switch (optimize) {
.Debug => &.{"-O0"},
.ReleaseFast, .ReleaseSafe => &.{"-O3"}, // TODO ubsan?
.ReleaseSmall => &.{"-Os"},
});
cc1.addArg("-c");
cc1.addFileArg(b.path(cfile));
cc1.addArg("-o");
const obj = cc1.addOutputFileArg(objfile);
lib.addObjectFile(obj);
}
lib.linkLibC();
const test_step = b.step("test", "Run unit tests");