Commit a643ba6

kravantokh <kravantokh@noreply.codeberg.org>
2026-03-13 13:21:48
Add Makefile. Will abandon make later.
1 parent 8eeda17
include/kc_entry.h
@@ -0,0 +1,17 @@
+#ifndef KC_HEADER_ENTRY_INCLUDED
+#define KC_HEADER_ENTRY_INCLUDED
+
+#include "../platform/info.h"
+
+#if defined(KC_PLATFORM_LINUX64) && defined(KC_PLATFORM_ARCH_x86_64)
+	// void _start(void) {
+	// 	__asm__ volatile(
+	// 	
+	// 		"call main\n"
+	// 	);
+	// }
+#else
+	#error No entrypoint defined for current platform.
+#endif
+
+#endif
include/testing.h
@@ -0,0 +1,22 @@
+#ifndef KC_HEADER_TESTING_INCLUDED
+#define KC_HEADER_TESTING_INCLUDED
+
+#include "../macros/macros.h"
+
+#define KC_TESTFUNC( NAME, ... ) \
+	IN_CUSTOM_SECTION( kc_tests, void,  _kc_test_##NAME(void)) { \
+	__VA_ARGS__ \
+	}
+
+#if defined(KC_IMPLEMENT) && defined(KC_USE_PLATFORM_LIBC)
+#include <stdio.h>
+
+IN_CUSTOM_SECTION( kc_tests, void, kc_test_runner(void) {
+
+	printf("Welcome to the test runner.");
+
+})
+
+#endif
+
+#endif
macros/macros.h
@@ -21,12 +21,20 @@
 #include "../platform/info.h"
 
 #if defined(KC_COMPILER_GCC) || defined(KC_COMPILER_CLANG) || defined(KC_COMPILER_TCC)
+
 	#define PACK( definition ) definition __attribute__( ( __packed__ ) )
+	#define IN_CUSTOM_SECTION( section_name, type, ... ) type __attribute__((section(#section_name))) __VA_ARGS__ 
+
 #elif defined(KC_COMPILER_MSVC)
+
 	#define PACK( DECL ) \
 		__pragma( pack( push, 1 ) ) \
 			DECL \
 		__pragma( pack( pop ) )
+
+	#define IN_CUSTOM_SECTION( section_name, ... ) __declspec(allocate(#section_name)) __VA_ARGS__ 
+
+
 #else
 	#error Unsupported compiler.
 #endif
platform/info.h
@@ -20,9 +20,11 @@
 	#if __x86_64__
 		#define KC_PLATFORM_BITNESS 64
 		#define KC_PLATFORM_LINUX64 1
+		#define KC_PLATFORM_ARCH_x86_64
 	#elif __i386__
 		#define KC_PLATFORM_BITNESS 32
 		#define KC_PLATFORM_LINUX32 1
+		#define KC_PLATFORM_ARCH_i386
 	#else
 		#error Unknown linux-supported architecture.
 	#endif
src/test_runner.c
@@ -0,0 +1,16 @@
+#include <stdio.h>
+typedef void (*test_fn)(void);
+
+#include "../include/types.h"
+#include "./testfunction_list.h"
+
+static const u64 kc_test_function_count = sizeof(kc_test_functions)/sizeof(kc_test_functions[0]);"
+
+
+void kc_test_runner(void) {
+	for( u64 i = 0; i < test_function_count; ++i ) {
+		
+	}
+	exit(STATUS_SUCCESS);
+}
+
tests/a.out
Binary file
tests/main.c
@@ -8,6 +8,12 @@
 #include "../include/types.h"
 #include "../platform/assert.h"
 
+#include "../include/testing.h"
+
+KC_TESTFUNC( test0, 
+	printf("Good riddance!\n");
+	*((volatile u8*)0) = 'd';
+)
 
 int main(void) {
 	MemArena a;
make.mk
@@ -0,0 +1,145 @@
+# This requires GNU Make
+.SUFFIXES: # disable built-in rules
+
+BUILD_DIR ?= ./build
+CC ?= gcc
+LD ?= ld
+RM ?= rm
+MAKE ?= make
+
+KC_USE_PLATFORM_LIBC ?= TRUE
+KC_MEMORY_ZEROING ?= LOOSE
+PROGNAME ?= main
+LDFLAGS ?=
+CFLAGS += -std=c99
+CFLAGS += -I$(KC_DIR)/include
+
+ifndef PROJ_SRC_DIR
+    $(error PROJ_SRC_DIR is not defined. Where should source files be taken from?)
+endif
+
+ifndef KC_DIR 
+    $(error KC_DIR is not defined. The library can not be found without it.)
+endif
+
+OBJS := $(wildcard $(PROJ_SRC_DIR)/*.c)
+OBJS := $(OBJS:$(PROJ_SRC_DIR)/%.c=$(BUILD_DIR)/%.o)
+$(info $(OBJS))
+HEADER_DEPS := $(OBJS:.o=.d)
+
+ifeq ( $(CC), cc )
+    $(warning If you actually want to do anything besides compilation, please set CC to your actual compiler's name (like gcc or clang).)
+endif
+
+$(info )
+$(info ** Identifying system configuration **)
+$(info Compiler: $(CC))
+
+ifeq ($(CC), gcc)
+    ANALYZE_FLAGS += -fanalyzer -Wanalyzer-symbol-too-complex -Wanalyzer-too-complex
+    DEBUG_FLAGS += -Og -g -Wall -Wextra -pedantic -Wpedantic
+    SANITIZE_FLAGS += -fsanitize=address,pointer-compare
+    PROFILE_FLAGS +=
+else ifeq ( $(CC), clang )
+    ANALYZE_FLAGS += -Xanalyze
+    DEBUG_FLAGS += -Og -g -Wall -Wextra -pedantic -Wpedantic
+    SANITIZE_FLAGS +=
+    PROFILE_FLAGS +=
+else
+    $(warning Current compiler is not supported for analysis.)
+endif
+
+
+# Cross-platform mess
+ifdef OS
+    $(info OS: windows)
+
+    MAIN_TARGET=$(BUILD_DIR)/$(PROGNAME).exe
+    TEST_TARGET=$(BUILD_DIR)/test_runner.exe
+    CP=cp
+else ifeq ($(shell uname),Linux)
+    $(info OS: linux)
+
+    MAIN_TARGET=$(BUILD_DIR)/$(PROGNAME)
+    TEST_TARGET=$(BUILD_DIR)/test_runner
+    CP=cp
+else
+    $(error The current platform is unsupported)
+endif
+
+$(info )
+$(info ** Outputs ** )
+
+$(info name $(PROGNAME) )
+$(info main $(MAIN_TARGET) )
+$(info test $(TEST_TARGET) )
+
+$(info )
+$(info ** Identifying library options **)
+
+ifeq ($(KC_MEMORY_ZEROING), LOOSE)
+    CFLAGS += -DKC_ZERO_MEMORY_LOOSE 
+    $(info Memory zeroing: LOOSE)
+
+else ifeq ($(KC_MEMORY_ZEROING), STRICT)
+    CFLAGS += -DKC_ZERO_MEMORY_STRICT
+    $(info Memory zeroing: STRICT)
+else
+    $(error Invalid value '$(KC_MEMORY_ZEROING)' passed for KC_MEMORY_ZEROING option. Allowed are: LOOSE, STRICT)
+endif
+
+ifeq ($(KC_USE_PLATFORM_LIBC), TRUE)
+    $(info Platform libc ON)
+    CFLAGS += -DKC_USE_PLATFORM_LIBC
+    BUILD_COMMAND := $(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) -o $(MAIN_TARGET)
+else
+    CFLAGS += 0 -nostdlib -nolibc -nodefaultlibs
+    $(info Platform libc OFF)
+    BUILD_COMMAND := $(LD) $(LDFLAGS) $(OBJS) -o $(MAIN_TARGET)
+endif
+
+-include $(HEADER_DEPS)
+
+.PHONY:
+help:
+	$(info )
+	$(info ** Possible Targets **)
+	$(info -- release - release buikd)
+	$(info -- debug - debug build)
+	$(info -- test - build and run tests)
+	$(info -- analyze - debug build with static analyzer)
+	$(info )
+
+.PHONY: release
+release: $(MAIN_TARGET)
+
+.PHONY: debug
+debug: CFLAGS += $(DEBUG_FLAGS) $(SANITIZE_FLAGS)
+debug: $(MAIN_TARGET)
+	@echo Building debug
+	@echo $(DEBUG_FLAGS)
+
+.PHONY: test
+test: $(TEST_TARGET)
+	$(TEST_TARGET)
+
+$(MAIN_TARGET): $(OBJS)
+	$(BUILD_COMMAND)
+
+$(TEST_TARGET): $(BUILD_DIR)/test_runner.c $(OBJS) $(BUILD_DIR)/testfunction_list.h
+	
+
+$(KC_DIR)/test_runner.c: $(KC_DIR)/src/test_runner.c
+	$(CP) $< $@ 
+
+$(BUILD_DIR)/testfunction_list.h: $(OBJS)
+	
+
+$(BUILD_DIR)/%.o : $(PROJ_SRC_DIR)/%.c
+	@mkdir -p $(@D)
+	$(CC) $(CFLAGS) -MMD $< -c -o $@
+
+.PHONY: clean
+clean:
+	$(RM) -rf ./build/
+
README.md
@@ -9,11 +9,13 @@ All headers in subdirectories are not supposed to be consumed. Only headers in t
 
 # Usage
 
-This repo is meant to be used as a git submodule in any project you want to use it it. The include paths are convieved so that they are reasonable if you add this repo's `include` directory to the include paths of your compiler.
+This repo is meant to be used as a git submodule in any project you want to use it it. The include paths are convieved so that they are reasonable if you add this repo's `include` directory to the include paths of your compiler. You should include the `Makefile` from this project into your own `Makefile`, adding any additional directories you need to the `C_FILES_DIRS`. Do not name any of your C files starting with `kc` or `kc_`. These are reserved for the library.
 
-# Macros
+# Macros and Makefile options
+
+These can all be either declared with -D for the compiler or given to the makefile via CLI or env, as usual with makefiles. In the makefile's case set them to 1.
 
 - KC_IMPLEMENT - actually generate the implementation from the headers and not just declare everything. This is great for unity builds or grouping different implementations into different `.c` files.
 - KC_USE_PLATFORM_LIBC - use the current platform's libc where this is an option.
-- KC_ZERO_MEMORY_STRICT, KC_ZERO_MEMORY_LOOSE, KC_ZERO_MEMORY_NONE - zero out buffers, released memory etc. even in places where it would not be strictly necessary from a functional standpoint. Strict zeroes out the most, loose zeroes out the most critical ones and none does not zero out any places that are not strictly necessary for functionality.
+- KC_ZERO_MEMORY_STRICT, KC_ZERO_MEMORY_LOOSE - zero out buffers, released memory etc. even in places where it would not be strictly necessary from a functional standpoint. Strict zeroes out the most, loose zeroes out the most critical ones and none does not zero out any places that are not strictly necessary for functionality.