Skip to content

Commit

Permalink
Add assert.h, fix installation of headers (#1)
Browse files Browse the repository at this point in the history
* initial implementation of assert

* fix use of return type

* fix formatting

* install header

* change install headers

* small fix

* small fix
  • Loading branch information
mattnite authored Jan 21, 2024
1 parent b7e5cab commit 0c228a7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
13 changes: 6 additions & 7 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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:
Expand Down Expand Up @@ -118,6 +116,7 @@ pub fn build(b: *std.Build) void {
}

const header_files = [_][]const u8{
"assert.h",
"ctype.h",
"errno.h",
"inttypes.h",
Expand Down
16 changes: 16 additions & 0 deletions include/assert.h
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions src/libc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -11,6 +12,7 @@ pub const h = @cImport({
@cInclude("string.h");
@cInclude("tgmath.h");
@cInclude("uchar.h");

@cInclude("foundation/libc.h");
});

Expand All @@ -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");
Expand Down
14 changes: 14 additions & 0 deletions src/modules/assert.zig
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 0c228a7

Please sign in to comment.