diff --git a/code/assert.c b/code/assert.c new file mode 100644 index 0000000..146e326 --- /dev/null +++ b/code/assert.c @@ -0,0 +1,46 @@ + +#include +#include +#include <_platform.h> + +#if defined(_os_win) + #define WIN32_LEAN_AND_MEAN + #include + + // TODO: make it work via printf + + static int _assert_strlen(char const *str) + { + int len = 0; + while(*str++!=0) ++len; + return len; + } + + static void _assert_win32_prints(HANDLE conout, char const *str) + { + DWORD written = 0; + WriteConsole(conout, str, _assert_strlen(str), &written, NULL); + } + + extern void _assert_print(char *cond, char const *func, char const *file, char const *line) + { + HANDLE conout = GetStdHandle(STD_OUTPUT_HANDLE); + if(conout != INVALID_HANDLE_VALUE) { + _assert_win32_prints(conout, "Assertion Failed: "); + _assert_win32_prints(conout, cond); + _assert_win32_prints(conout, "\n\tFile: "); + _assert_win32_prints(conout, file); + _assert_win32_prints(conout, "\n\tFunc: "); + _assert_win32_prints(conout, func); + _assert_win32_prints(conout, "\n\tLine: "); + _assert_win32_prints(conout, line); + } + else { + // TODO: + MessageBoxA(NULL, + "Assertion Failed Somewhere, good luck finding it!\n", + "Error", + MB_OK); + } + } +#endif \ No newline at end of file diff --git a/code/entry_windows.c b/code/entry_windows.c new file mode 100644 index 0000000..257bda4 --- /dev/null +++ b/code/entry_windows.c @@ -0,0 +1,9 @@ + +#include<_platform.h> + +extern int main(); + +int mainCRTStartup() +{ + return main(); +} diff --git a/inc/_platform.h b/inc/_platform.h new file mode 100644 index 0000000..d55bd1d --- /dev/null +++ b/inc/_platform.h @@ -0,0 +1,47 @@ +#if !defined(_platform_h) +#define _platform_h + +// Compiler Identification + +#if defined(_MSC_VER) && !defined(__clang__) + #define _compiler_msvc +#endif + +#if defined(__GNUC__) && !defined(__clang__) + #define _compiler_gnu +#endif + +#if defined(__clang__) + #define _compiler_clang +#endif + +#if !(defined(_compiler_msvc) \ + || defined(_compiler_gnu) \ + || defined(_compiler_clang)) + #error "Unsupported Compiler" +#endif + +#if !defined(_func) + #if defined(_compiler_msvc) + #define _func __FUNCTION__ + #else + #define _func __func__ + #endif +#endif + +// OS Identification + +#if defined(_WIN32) + #define _os_win +#endif + +#if defined(__linux__) && !defined(__ANDROID__) + #define _os_linux +#endif + +#if !(defined(_os_win) \ + || defined(_os_linux)) + #error "Unsupported OS" +#endif + +#endif \ No newline at end of file diff --git a/inc/assert.h b/inc/assert.h new file mode 100644 index 0000000..11f2361 --- /dev/null +++ b/inc/assert.h @@ -0,0 +1,32 @@ +#if !defined(_assert_h) +#define _assert_h + +#include "_platform.h" + +extern void _assert_print(char *cond, char const *func, char const *file, char const *line); + +// TODO: use abort() to break +#if defined(_compiler_msvc) + #include + #define _compiler_brk __debugbreak +#else + #define _compiler_brk __builtin_trap +#endif + +#define _str_(x) #x +#define _str(x) _str_(x) + +#if defined(NDEBUG) + #define assert(ignore) ((void)0) +#else + #define _static_assert _Static_assert + #define assert(condition) \ + do { \ + if(!(condition)) { \ + _assert_print(#condition, _func, __FILE__, _str(__LINE__)); \ + _compiler_brk(); \ + } \ + } while(0) +#endif + +#endif \ No newline at end of file diff --git a/inc/complex.h b/inc/complex.h new file mode 100644 index 0000000..05e77a7 --- /dev/null +++ b/inc/complex.h @@ -0,0 +1,2 @@ + +#error "Complex Numbers aren't implemented" diff --git a/inc/ctype.h b/inc/ctype.h new file mode 100644 index 0000000..e69de29 diff --git a/inc/errno.h b/inc/errno.h new file mode 100644 index 0000000..e69de29 diff --git a/inc/fenv.h b/inc/fenv.h new file mode 100644 index 0000000..e69de29 diff --git a/inc/inttypes.h b/inc/inttypes.h new file mode 100644 index 0000000..e69de29 diff --git a/inc/locale.h b/inc/locale.h new file mode 100644 index 0000000..e69de29 diff --git a/inc/math.h b/inc/math.h new file mode 100644 index 0000000..e69de29 diff --git a/inc/signal.h b/inc/signal.h new file mode 100644 index 0000000..e69de29 diff --git a/inc/stdatomic.h b/inc/stdatomic.h new file mode 100644 index 0000000..e69de29 diff --git a/inc/stdio.h b/inc/stdio.h new file mode 100644 index 0000000..e980e51 --- /dev/null +++ b/inc/stdio.h @@ -0,0 +1,4 @@ + +#define EOF -1 + + diff --git a/inc/stdlib.h b/inc/stdlib.h new file mode 100644 index 0000000..c3e55b4 --- /dev/null +++ b/inc/stdlib.h @@ -0,0 +1,2 @@ + +#define NULL ((void *)0) \ No newline at end of file diff --git a/inc/string.h b/inc/string.h new file mode 100644 index 0000000..e69de29 diff --git a/inc/tgmath.h b/inc/tgmath.h new file mode 100644 index 0000000..e69de29 diff --git a/inc/threads.h b/inc/threads.h new file mode 100644 index 0000000..e69de29 diff --git a/inc/time.h b/inc/time.h new file mode 100644 index 0000000..e69de29 diff --git a/inc/uchar.h b/inc/uchar.h new file mode 100644 index 0000000..e69de29 diff --git a/inc/wchar.h b/inc/wchar.h new file mode 100644 index 0000000..e69de29 diff --git a/inc/wctype.h b/inc/wctype.h new file mode 100644 index 0000000..e69de29 diff --git a/makefile b/makefile new file mode 100644 index 0000000..74d0193 --- /dev/null +++ b/makefile @@ -0,0 +1,20 @@ + +GNUFLAGS=-Iinc -Icode +CLFLAGS=/link /incremental:no /subsystem:windows /nodefaultlib kernel32.lib + +CC=clang +CFLAGS=$(GNUFLAGS) +LDFLAGS=/nologo /nodefaultlib /entry:mainCRTStartup + +SRC_DIR := code +OBJ_DIR := build +SRC_FILES := $(wildcard $(SRC_DIR)/*.c) +OBJ_FILES := $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SRC_FILES)) + +ciabatta.lib: $(OBJ_FILES) + lib $(LDFLAGS) /out:lib/$@ $^ + +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c + $(CC) $(CFLAGS) -c -o $@ $< + +.PHONY: ciabatta.lib diff --git a/test/bake.bat b/test/bake.bat new file mode 100644 index 0000000..dd919ca --- /dev/null +++ b/test/bake.bat @@ -0,0 +1,6 @@ +@echo off +cl /c /I ..\inc /nologo /Gm- /GR- /EHa- /Oi /W4 /Z7 test.c +clang -c -Wall test.c -otest1.obj + +link /nologo test.obj /nodefaultlib user32.lib kernel32.lib ..\lib\ciabatta.lib -subsystem:console +link /nologo test1.obj /nodefaultlib user32.lib kernel32.lib ..\lib\ciabatta.lib -subsystem:console \ No newline at end of file diff --git a/test/test.c b/test/test.c new file mode 100644 index 0000000..1cd2011 --- /dev/null +++ b/test/test.c @@ -0,0 +1,8 @@ +// MSVC won't use my unless i provide path explicitly +// idk +#include"..\inc\assert.h" +int main() +{ + assert(0); + return 0; +} \ No newline at end of file