]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
tools/nolibc: add assert() and assert.h
authorThomas Weißschuh <linux@weissschuh.net>
Thu, 9 Apr 2026 16:22:08 +0000 (18:22 +0200)
committerThomas Weißschuh <linux@weissschuh.net>
Mon, 27 Apr 2026 07:00:49 +0000 (09:00 +0200)
Add the standard assert() macro from the assert.h header.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
Acked-by: Willy Tarreau <w@1wt.eu>
Link: https://patch.msgid.link/20260409-nolibc-assert-v1-1-42da8b367e23@weissschuh.net
tools/include/nolibc/Makefile
tools/include/nolibc/assert.h [new file with mode: 0644]
tools/include/nolibc/nolibc.h

index 7455097cff693f964134f1f35a40073bdc079b68..f0e6e71e83355f3ef46fdd8587420329b2cd4675 100644 (file)
@@ -20,6 +20,7 @@ OUTPUT ?= $(CURDIR)/
 architectures := arm arm64 loongarch m68k mips powerpc riscv s390 sh sparc x86
 arch_files := arch.h $(addsuffix .h, $(addprefix arch-, $(architectures)))
 all_files := \
+               assert.h \
                byteswap.h \
                compiler.h \
                crt.h \
diff --git a/tools/include/nolibc/assert.h b/tools/include/nolibc/assert.h
new file mode 100644 (file)
index 0000000..84ff8ad
--- /dev/null
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
+/*
+ * Assert for NOLIBC
+ * Copyright (C) 2026 Thomas Weißschuh <linux@weissschuh.net>
+ */
+
+/* make sure to include all global symbols */
+#include "nolibc.h"
+
+#ifndef _NOLIBC_ASSERT_H
+#define _NOLIBC_ASSERT_H
+
+#include "errno.h"
+#include "stdio.h"
+#include "stdlib.h"
+
+#endif /* _NOLIBC_ASSERT_H */
+
+/* NDEBUG needs to be evaluated on *each* inclusion */
+#ifdef assert
+#undef assert
+#endif
+
+#ifndef NDEBUG
+#define assert(expr)                                                                   \
+({                                                                                     \
+       if (!(expr)) {                                                                  \
+               fprintf(stderr, "%s: %s:%d: %s: Assertion `%s' failed.\n",              \
+                       program_invocation_short_name, __FILE__, __LINE__, __func__,    \
+                       #expr);                                                         \
+               abort();                                                                \
+       }                                                                               \
+})
+#else
+#define assert(expr) ((void)0)
+#endif
index f4120f65fe794c28e98e2e28f254afdafa4fc344..4b99795d7a65c2fc1666b49d6ed3241de873c6c3 100644 (file)
 #include "err.h"
 #include "byteswap.h"
 #include "endian.h"
+#include "assert.h"
 
 /* Used by programs to avoid std includes */
 #define NOLIBC