zig build -Dcc=tcc
This commit is contained in:
90
build.zig
90
build.zig
@@ -1,4 +1,5 @@
|
||||
const std = @import("std");
|
||||
const builtin = @import("builtin");
|
||||
|
||||
const headers = &[_][]const u8{
|
||||
"common.h",
|
||||
@@ -39,48 +40,37 @@ pub fn build(b: *std.Build) !void {
|
||||
const target = b.standardTargetOptions(.{});
|
||||
const optimize = b.standardOptimizeOption(.{});
|
||||
|
||||
const lib_mod = b.createModule(.{
|
||||
.optimize = optimize,
|
||||
.target = target,
|
||||
.link_libc = true,
|
||||
});
|
||||
const lib = b.addLibrary(.{
|
||||
.name = "tokenizer",
|
||||
.root_module = lib_mod,
|
||||
});
|
||||
|
||||
const cc = b.option([]const u8, "cc", "C compiler") orelse "zig";
|
||||
|
||||
if (std.mem.eql(u8, cc, "zig"))
|
||||
lib.root_module.addCSourceFiles(.{ .files = c_lib_files, .flags = cflags })
|
||||
else for (c_lib_files) |cfile| {
|
||||
const cc1 = b.addSystemCommand(&.{cc});
|
||||
cc1.addArgs(cflags ++ .{"-g"});
|
||||
cc1.addArg(switch (optimize) {
|
||||
.Debug => "-O0",
|
||||
.ReleaseFast, .ReleaseSafe => "-O3", // ubsan?
|
||||
.ReleaseSmall => "-Os",
|
||||
});
|
||||
cc1.addArg("-c");
|
||||
cc1.addFileArg(b.path(cfile));
|
||||
cc1.addArg("-o");
|
||||
lib.root_module.addObjectFile(cc1.addOutputFileArg(try std.fmt.allocPrint(
|
||||
b.allocator,
|
||||
"{s}.o",
|
||||
.{cfile[0 .. cfile.len - 2]},
|
||||
)));
|
||||
}
|
||||
|
||||
const no_exec = b.option(bool, "no-exec", "Compile test binary without running it") orelse false;
|
||||
|
||||
const test_step = b.step("test", "Run unit tests");
|
||||
const test_mod = b.createModule(.{
|
||||
.root_source_file = b.path("test_all.zig"),
|
||||
.optimize = optimize,
|
||||
.target = target,
|
||||
});
|
||||
test_mod.linkLibrary(lib);
|
||||
test_mod.addIncludePath(b.path("."));
|
||||
|
||||
// TODO(zig 0.16+): remove this if block entirely; keep only the addLibrary branch.
|
||||
// Also delete addCObjectsDirectly.
|
||||
// Zig 0.15's ELF archive parser fails on archives containing odd-sized objects
|
||||
// (off-by-one after 2-byte alignment). This is fixed on zig master/0.16.
|
||||
if (comptime builtin.zig_version.order(.{ .major = 0, .minor = 16, .patch = 0 }) == .lt) {
|
||||
addCObjectsDirectly(b, test_mod, cc, optimize);
|
||||
} else {
|
||||
const lib_mod = b.createModule(.{
|
||||
.optimize = optimize,
|
||||
.target = target,
|
||||
.link_libc = true,
|
||||
});
|
||||
const lib = b.addLibrary(.{
|
||||
.name = "tokenizer",
|
||||
.root_module = lib_mod,
|
||||
});
|
||||
addCSources(b, lib.root_module, cc, optimize);
|
||||
test_mod.linkLibrary(lib);
|
||||
}
|
||||
|
||||
const no_exec = b.option(bool, "no-exec", "Compile test binary without running it") orelse false;
|
||||
const test_exe = b.addTest(.{ .root_module = test_mod });
|
||||
if (no_exec) {
|
||||
const install = b.addInstallArtifact(test_exe, .{});
|
||||
@@ -132,3 +122,37 @@ pub fn build(b: *std.Build) !void {
|
||||
for (all_c_files) |cfile| cppcheck.addFileArg(b.path(cfile));
|
||||
lint_step.dependOn(&cppcheck.step);
|
||||
}
|
||||
|
||||
fn addCSources(
|
||||
b: *std.Build,
|
||||
mod: *std.Build.Module,
|
||||
cc: []const u8,
|
||||
optimize: std.builtin.OptimizeMode,
|
||||
) void {
|
||||
if (std.mem.eql(u8, cc, "zig")) {
|
||||
mod.addCSourceFiles(.{ .files = c_lib_files, .flags = cflags });
|
||||
} else for (c_lib_files) |cfile| {
|
||||
const cc1 = b.addSystemCommand(&.{cc});
|
||||
cc1.addArgs(cflags ++ .{"-g"});
|
||||
cc1.addArg(switch (optimize) {
|
||||
.Debug => "-O0",
|
||||
.ReleaseFast, .ReleaseSafe => "-O3",
|
||||
.ReleaseSmall => "-Os",
|
||||
});
|
||||
cc1.addArg("-c");
|
||||
cc1.addFileArg(b.path(cfile));
|
||||
cc1.addArg("-o");
|
||||
mod.addObjectFile(cc1.addOutputFileArg(b.fmt("{s}.o", .{cfile[0 .. cfile.len - 2]})));
|
||||
}
|
||||
}
|
||||
|
||||
// TODO(zig 0.16+): delete this function.
|
||||
fn addCObjectsDirectly(
|
||||
b: *std.Build,
|
||||
mod: *std.Build.Module,
|
||||
cc: []const u8,
|
||||
optimize: std.builtin.OptimizeMode,
|
||||
) void {
|
||||
addCSources(b, mod, cc, optimize);
|
||||
mod.linkSystemLibrary("c", .{});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user