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