Skip to content
This repository has been archived by the owner on Jan 24, 2025. It is now read-only.

Add assert.h, fix installation of headers #1

Merged
merged 7 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
Loading