diff --git a/build.zig b/build.zig index 7a4f7bd..b1df2a9 100644 --- a/build.zig +++ b/build.zig @@ -14,6 +14,11 @@ pub fn createLibrary(b: *std.Build, target: std.zig.CrossTarget, optimize: std.b }); libc.addIncludePath(.{ .path = "include" }); + for (header_files) |header_name| + libc.installHeader( + b.fmt("include/{s}", .{header_name}), + header_name, + ); return libc; } @@ -39,13 +44,6 @@ pub fn build(b: *std.Build) void { libc.single_threaded = single_threaded; b.installArtifact(libc); - for (header_files) |header_name| { - b.getInstallStep().dependOn(&b.addInstallHeaderFile( - b.fmt("include/{s}", .{header_name}), - header_name, - ).step); - } - // test suite: { // Compile for huge amount of targets to detect breakage early on: @@ -118,6 +116,7 @@ pub fn build(b: *std.Build) void { } const header_files = [_][]const u8{ + "assert.h", "ctype.h", "errno.h", "inttypes.h", diff --git a/include/assert.h b/include/assert.h new file mode 100644 index 0000000..81cbd9e --- /dev/null +++ b/include/assert.h @@ -0,0 +1,16 @@ +#ifndef _FOUNDATION_LIBC_ASSERT_H_ +#define _FOUNDATION_LIBC_ASSERT_H_ + +#ifndef NDEBUG +#define assert(expr) +#else +extern void __assert(char const * assertion, char const * file, unsigned line) __attribute__((__noreturn__)); + +#define assert(expr) \ + ((expr) \ + ? void(0) \ + : __assert(#expr, __FILE__, __LINE__)) + +#endif + +#endif diff --git a/src/libc.zig b/src/libc.zig index 4475168..fc5c902 100644 --- a/src/libc.zig +++ b/src/libc.zig @@ -2,6 +2,7 @@ const std = @import("std"); const builtin = @import("builtin"); pub const h = @cImport({ + @cInclude("assert.h"); @cInclude("ctype.h"); @cInclude("errno.h"); @cInclude("inttypes.h"); @@ -11,6 +12,7 @@ pub const h = @cImport({ @cInclude("string.h"); @cInclude("tgmath.h"); @cInclude("uchar.h"); + @cInclude("foundation/libc.h"); }); @@ -33,6 +35,7 @@ comptime { comptime { // Drag in all implementations, so they are compiled: + _ = @import("modules/assert.zig"); _ = @import("modules/ctype.zig"); _ = @import("modules/errno.zig"); _ = @import("modules/math.zig"); diff --git a/src/modules/assert.zig b/src/modules/assert.zig new file mode 100644 index 0000000..fc181de --- /dev/null +++ b/src/modules/assert.zig @@ -0,0 +1,14 @@ +const std = @import("std"); + +export fn __assert( + assertion: ?[*:0]const u8, + file: ?[*:0]const u8, + line: c_uint, +) noreturn { + var buf: [256]u8 = undefined; + const str = std.fmt.bufPrint(&buf, "assertion failed: '{?s}' in file {?s} line {}", .{ assertion, file, line }) catch { + @panic("assertion failed"); + }; + + @panic(str); +}