From 7ce6a4f7ceb6b60c598da9a2cdb01e604ef5190d Mon Sep 17 00:00:00 2001 From: Axy Date: Fri, 15 May 2026 13:10:03 +0200 Subject: [PATCH] Updated makefile a bunch --- .deps | 4 +- Makefile | 96 ++++++++++++++++++++++++++++++++++------------- conf.mk | 5 +++ src/arc.c | 5 ++- src/arc.h | 2 +- src/atomic.h | 37 ++++++++++++++++++ src/ccera.h | 3 +- src/defer.c | 2 +- src/panic.c | 2 +- src/stacktrack.c | 2 +- src/value_types.h | 10 ++--- test.c | 9 +++-- 12 files changed, 132 insertions(+), 45 deletions(-) create mode 100644 conf.mk create mode 100644 src/atomic.h diff --git a/.deps b/.deps index 47f68db..3fe262b 100644 --- a/.deps +++ b/.deps @@ -1,11 +1,11 @@ ${BUILDDIR}/arc.o: src/arc.c src/arc.h src/memutils.h src/panic.h src/jmp.h \ - src/arith.h + src/arith.h src/atomic.h ${BUILDDIR}/defer.o: src/defer.c src/defer.h src/framealloc.h src/align.h \ src/stacktrack.h src/memutils.h src/panic.h src/jmp.h ${BUILDDIR}/eval.o: src/eval.c src/ccera.h src/jmp.h src/defer.h src/framealloc.h \ src/align.h src/stacktrack.h src/memutils.h src/panic.h src/value.h \ src/value_get.h src/value_types.h src/value_new.h src/arith.h \ - src/arc.h + src/arc.h src/atomic.h ${BUILDDIR}/framealloc.o: src/framealloc.c src/framealloc.h src/align.h \ src/stacktrack.h ${BUILDDIR}/panic.o: src/panic.c src/panic.h src/jmp.h src/defer.h src/framealloc.h \ diff --git a/Makefile b/Makefile index f2350d7..bf29c67 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,12 @@ NAME=ccera.a SRCS=src/arc.c src/defer.c src/eval.c src/framealloc.c src/panic.c src/stacktrack.c src/value_lifetime.c src/value_list.c src/value_move.c src/value_new.c -HEADERS=src/align.h src/arc.h src/arith.h src/ccera.h src/defer.h src/framealloc.h src/jmp.h src/memutils.h src/panic.h src/stacktrack.h src/value_get.h src/value.h src/value_new.h src/value_types.h +HEADERS=src/align.h src/arc.h src/arith.h src/atomic.h src/ccera.h src/defer.h src/framealloc.h src/jmp.h src/memutils.h src/panic.h src/stacktrack.h src/value_get.h src/value.h src/value_new.h src/value_types.h + +CC = cc + +CONFFILE ?= conf.mk +include ${CONFFILE} BUILDDIR=.build @@ -12,27 +17,72 @@ DEPSFILE=.deps OBJS=${SRCS:src/%.c=${BUILDDIR}/%.o} -CFLAGS=-Wall -Wextra -Werror -pthread -std=gnu17 -# IMPORTANT: We use this to properly keep track of stack frames at a source level -CFLAGS += -finstrument-functions +CFLAGS ?= + +CFLAGS += -Wall -Wextra -Werror -pthread -finstrument-functions + +CFLAGS += -std=gnu99 + CFLAGS += -g -CFLAGS += -O3 -#CFLAGS += -flto -ffat-lto-objects CFLAGS += -fdiagnostics-color -#CC=clang -#CC=gcc -CC=cc +CFLAGS += -O3 -AR=gcc-ar +LTO ?= 0 + +UBSAN ?= 0 +ifeq (${UBSAN}, 1) +CFLAGS += -fsanitize=undefined +endif + +TSAN ?= 0 +ifeq (${TSAN}, 1) +CFLAGS += -fsanitize=thread +# Tsan incompat with lto +ifeq (${LTO}, 1) +$(info Disabling LTO because TSAN is enabled) +endif +LTO=0 +endif + +ASAN ?= 0 +ifeq (${ASAN}, 1) +CFLAGS += -fsanitize=address +# Tsan incompat with lto +ifeq (${LTO}, 1) +$(info Disabling LTO because ASAN is enabled) +endif +LTO=0 +endif + +ifeq (${CC}, gcc) +ifeq (${LTO}, 1) +$(info Disabling LTO because gcc has SSA corruption bug) +CFLAGS += -flto -ffat-lto-objects +endif +endif + +ifeq (${LTO}, 1) +CFLAGS += -flto -ffat-lto-objects +endif + +AR=ar + +MAKE_CMD=CONFFILE=${CONFFILE} make MAKEFLAGS += --no-print-directory all: ${NAME} +${CONFFILE}: + touch $@ + +${DEPSFILE}: + touch $@ + include ${DEPSFILE} -${BUILDDIR}/%.o: src/%.c ${THIS} ${DEPSFILE} | ${BUILDDIR} +${BUILDDIR}/%.o: src/%.c ${THIS} ${DEPSFILE} ${CONFFILE} | ${BUILDDIR} ${CC} ${CFLAGS} -c -o $@ $< ${BUILDDIR}: @@ -82,26 +132,18 @@ norm: .norm @test ! -e .norm_fail watch-step: - make remakefile - make redeps - make norm - make + ${MAKE_CMD} remakefile + ${MAKE_CMD} redeps + ${MAKE_CMD} norm + ${MAKE_CMD} all watch: - watch -c -n 1 make watch-step + watch -c -n 1 ${MAKE_CMD} watch-step -norm-watch: - watch -c -n 1 make norm - -build-watch: - watch -c -n 1 make - -a.out: test.c ${NAME} ${THIS} - ${CC} ${CFLAGS} test.c ccera.a +a.out: test.c ${NAME} + ${CC} ${CFLAGS} $^ test: a.out - valgrind ./a.out + ./a.out .PHONY: all clean fclean re remakefile redeps norm watch-step watch test - -${BUILDDIR}/arc.o: src/arc.c src/arc.h diff --git a/conf.mk b/conf.mk new file mode 100644 index 0000000..75d7d55 --- /dev/null +++ b/conf.mk @@ -0,0 +1,5 @@ +#LTO=1 +CC=clang +#UBSAN=1 +TSAN=1 +#ASAN=1 diff --git a/src/arc.c b/src/arc.c index 76bbaed..fa34fd1 100644 --- a/src/arc.c +++ b/src/arc.c @@ -6,7 +6,7 @@ /* By: agilliar +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/04/29 16:40:27 by agilliar #+# #+# */ -/* Updated: 2026/05/13 16:42:04 by agilliar ### ########.fr */ +/* Updated: 2026/05/15 11:22:29 by agilliar ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,7 @@ #include "panic.h" #include "memutils.h" #include "arith.h" +#include "atomic.h" t_arc arc_new(size_t size) { @@ -40,7 +41,7 @@ void arc_drop(t_arc arc, t_drop destructor) { if (__atomic_sub_fetch(((size_t *)arc) - 1, 1, __ATOMIC_RELEASE) != 0) return ; - __atomic_thread_fence(__ATOMIC_ACQUIRE); + acquire_fence_on(((size_t *)arc) - 1); ((void (*)(void *))destructor)(arc); free(((char *)arc) - 8); } diff --git a/src/arc.h b/src/arc.h index 5ca8a82..5c5f72e 100644 --- a/src/arc.h +++ b/src/arc.h @@ -6,7 +6,7 @@ /* By: agilliar +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/05/13 08:56:53 by agilliar #+# #+# */ -/* Updated: 2026/05/13 12:55:03 by agilliar ### ########.fr */ +/* Updated: 2026/05/14 15:55:35 by agilliar ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/atomic.h b/src/atomic.h new file mode 100644 index 0000000..c926672 --- /dev/null +++ b/src/atomic.h @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* atomic.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agilliar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/05/15 11:18:31 by agilliar #+# #+# */ +/* Updated: 2026/05/15 11:23:58 by agilliar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ATOMIC_H +# define ATOMIC_H + +# include + +# if __has_feature(thread_sanitizer) + +__attribute__((always_inline)) +static inline void acquire_fence_on(size_t *p) +{ + __atomic_load_n(p, __ATOMIC_ACQUIRE); +} + +# else + +__attribute__((always_inline)) +static inline void acquire_fence_on(size_t *p) +{ + (void) p; + __atomic_thread_fence(__ATOMIC_ACQUIRE); +} + +# endif + +#endif diff --git a/src/ccera.h b/src/ccera.h index 8602e02..e9a3dea 100644 --- a/src/ccera.h +++ b/src/ccera.h @@ -6,7 +6,7 @@ /* By: agilliar +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/04/29 16:08:57 by agilliar #+# #+# */ -/* Updated: 2026/05/14 14:58:03 by agilliar ### ########.fr */ +/* Updated: 2026/05/15 11:21:51 by agilliar ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,7 @@ # include "arith.h" # include "arc.h" # include "stacktrack.h" +# include "atomic.h" // The expression value is a tuple of an evaluatable and the argument to give it // diff --git a/src/defer.c b/src/defer.c index 49778f9..c6b02db 100644 --- a/src/defer.c +++ b/src/defer.c @@ -6,7 +6,7 @@ /* By: agilliar +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/05/05 17:31:36 by agilliar #+# #+# */ -/* Updated: 2026/05/14 14:49:54 by agilliar ### ########.fr */ +/* Updated: 2026/05/15 11:02:57 by agilliar ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/panic.c b/src/panic.c index 46748ec..b06f9da 100644 --- a/src/panic.c +++ b/src/panic.c @@ -6,7 +6,7 @@ /* By: agilliar +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/05/05 15:38:33 by agilliar #+# #+# */ -/* Updated: 2026/05/14 15:27:37 by agilliar ### ########.fr */ +/* Updated: 2026/05/15 11:04:14 by agilliar ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/stacktrack.c b/src/stacktrack.c index 3310c83..65ddc8a 100644 --- a/src/stacktrack.c +++ b/src/stacktrack.c @@ -6,7 +6,7 @@ /* By: agilliar +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/05/14 12:00:36 by agilliar #+# #+# */ -/* Updated: 2026/05/14 15:33:45 by agilliar ### ########.fr */ +/* Updated: 2026/05/15 11:04:26 by agilliar ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/value_types.h b/src/value_types.h index a80c8c7..5fe591e 100644 --- a/src/value_types.h +++ b/src/value_types.h @@ -6,7 +6,7 @@ /* By: agilliar +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/05/13 22:04:36 by agilliar #+# #+# */ -/* Updated: 2026/05/13 22:08:57 by agilliar ### ########.fr */ +/* Updated: 2026/05/15 13:05:11 by agilliar ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,16 +39,16 @@ typedef struct s_value enum e_value tag; } t_value; -typedef struct s_bytes +struct s_bytes { size_t len; char buf[]; -} t_bytes; +}; -typedef struct s_list +struct s_list { size_t len; t_value buf[]; -} t_list; +}; #endif diff --git a/test.c b/test.c index fa9066c..21c539d 100644 --- a/test.c +++ b/test.c @@ -6,7 +6,7 @@ /* By: agilliar +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/04/30 17:19:58 by agilliar #+# #+# */ -/* Updated: 2026/05/14 15:45:14 by agilliar ### ########.fr */ +/* Updated: 2026/05/15 11:08:09 by agilliar ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,11 +15,12 @@ #include #include -void main2(char *orig) +void main2(void *_orig) { - char *s = malloc(strlen(orig) + 1); + char *orig = _orig; + char *s = malloc(strlen(orig) + 1); defer(free, s); - char *s2 = malloc(strlen(orig) + 1); + char *s2 = malloc(strlen(orig) + 1); defer(free, s2); strcpy(s2, orig); printf("%s\n", s2); -- 2.53.0