Compare commits

..

4 Commits

3 changed files with 121 additions and 52 deletions

1
.gitignore vendored
View File

@@ -2,6 +2,7 @@ compile_commands.json
core core
/.ccls-cache/ /.ccls-cache/
/test/test/ /test/test/
.lastbuild
# /test/cutest/ # /test/cutest/
/tmp/ /tmp/
# created when building # created when building

101
Makefile
View File

@@ -44,33 +44,88 @@ BIN := $(addprefix $(BINDIR)/,$(TEST_FN:.c=))
CCLSCMDS := compile_commands.json CCLSCMDS := compile_commands.json
##################################### pre-processor flags ##################################### Check for compiler and requested build
CPPFLAGS := -I $(INCDIR) BUILDS := release dev perf debug
#CPPFLAGS += -DDEBUG # global # last compilation build
#CPPFLAGS += -DDEBUG_DEBUG_C # log() funcs debug BUILDFILE := .lastbuild
CPPFLAGS += -DDEBUG_DEBUG # activate logs funcs lastbuild := $(file < $(BUILDFILE))
CPPFLAGS += -DDEBUG_POOL # mem pools $(info brlib last:$(lastbuild))
# default to gcc
CC ?= cc
ifeq ($(CC),cc)
CC = gcc
endif
CPPFLAGS += -DBUG_ON # bug_on in bug.h # if no build specified, use last one
CPPFLAGS += -DWARN_ON # warn_on in bug.h ifeq ($(build),)
build := $(lastbuild)
endif
# if build is still undefined, set a default
ifeq ($(build),)
build := release
endif
$(info brlib build=$(build))
# check for valid build
ifeq ($(filter $(build),$(BUILDS)),)
$(error Error: Unknown build=`$(build)`. Possible builds are: $(BUILDS))
endif
# if new build, rewrite BUILDFILE
ifneq ($(build),$(lastbuild))
$(info New build:`$(build)` (was:$(lastbuild)))
$(file >$(BUILDFILE),$(build))
endif
##################################### pre-processor flags
override CPPFLAGS += -I $(INCDIR)
ifeq ($(BUILD),release)
CPPFLAGS += -DNDEBUG # assert (unused)
CPPFLAGS += -DBUG_ON=0 CPPFLAGS += -DWARN_ON=0 else # ifeq ($(BUILD),dev)
#CPPFLAGS += -DDEBUG #CPPFLAGS += -DDEBUG_DEBUG_C CPPFLAGS += -DDEBUG_DEBUG CPPFLAGS += -DDEBUG_POOL CPPFLAGS += -DBUG_ON=1 CPPFLAGS += -DWARN_ON=1 # warn_on in bug.h
endif
# remove extraneous spaces (due to spaces before comments) # remove extraneous spaces (due to spaces before comments)
CPPFLAGS := $(strip $(CPPFLAGS)) CPPFLAGS := $(strip $(CPPFLAGS))
##################################### compiler flags ##################################### compiler flags
CFLAGS := -std=gnu11 CFLAGS := -std=gnu11
CFLAGS += -O3
CFLAGS += -g
CFLAGS += -Wall CFLAGS += -Wall
CFLAGS += -Wextra CFLAGS += -Wextra
CFLAGS += -march=native CFLAGS += -march=native
CFLAGS += -Wmissing-declarations CFLAGS += -Wmissing-declarations
CFLAGS += -Wno-unused-result CFLAGS += -Wno-unused-result
# TODO: specific to dynamic
CFLAGS += -fPIC CFLAGS += -fPIC
# for gprof
#CFLAGS += -pg ### dev OR release
# Next one may be useful for valgrind (some invalid instructions) ifeq ($(BUILD),release)
# CFLAGS += -mno-tbm CFLAGS += -O3
CFLAGS += -funroll-loops
CFLAGS += -flto
else ifeq ($(BUILD),dev)
CFLAGS += -Og
CFLAGS += -g
CFLAGS += -ginline-points # inlined funcs debug info
# for gprof
#CFLAGS += -pg
# Next one may be useful for valgrind (some invalid instructions)
# CFLAGS += -mno-tbm
else ifeq ($(BUILD),perf)
CFLAGS += -O3
CFLAGS += -g # symbols (gdb, perf, etc.)
CFLAGS += -ginline-points # inlined funcs debug info
CFLAGS += -funroll-loops
else ifeq ($(BUILD),debug)
CFLAGS += -O0
CFLAGS += -g # symbols (gdb, perf, etc.)
# for gprof
#CFLAGS += -pg
# Next one may be useful for valgrind (when invalid instructions)
#CFLAGS += -mno-tbm
endif
CFLAGS := $(strip $(CFLAGS)) CFLAGS := $(strip $(CFLAGS))
@@ -80,14 +135,21 @@ LDFLAGS := -L$(LIBDIR)
LIBS := -l$(LIB) LIBS := -l$(LIB)
DEPFLAGS = -MMD -MP -MF $(DEPDIR)/$*.d DEPFLAGS = -MMD -MP -MF $(DEPDIR)/$*.d
ifeq ($(BUILD),release)
LDFLAGS += -flto
endif
##################################### General targets ##################################### General targets
.PHONY: all libs compile test emacs ccls bear clean cleanall cleanallall .PHONY: all libs lib-static lib-dynamic compile test emacs ccls bear clean \
cleanall cleanallall
# default: build libraries # default: build libraries
all: libs all: libs
# default: build libraries # libraries
libs: $(DLIB) $(SLIB) libs: lib-static lib-dynamic
lib-static: $(SLIB)
lib-dynamic: $(DLIB)
# build objects # build objects
compile: $(OBJ) compile: $(OBJ)
@@ -181,9 +243,10 @@ cleanobj:
cleanobjdir: cleanobjdir:
$(call rmdir,$(OBJDIR),brlib objects) $(call rmdir,$(OBJDIR),brlib objects)
$(OBJDIR)/%.o: $(SRCDIR)/%.c | $(OBJDIR) $(DEPDIR) # $(OBJDIR)/%.o: $(BUILDFILE)
$(OBJDIR)/%.o: $(SRCDIR)/%.c $(BUILDFILE) | $(OBJDIR) $(DEPDIR)
@echo compiling $< "->" $@. @echo compiling $< "->" $@.
@$(CC) -c $(DEPFLAGS) $(CPPFLAGS) $(CFLAGS) $< -o $@ $(CC) -c $(DEPFLAGS) $(CPPFLAGS) $(CFLAGS) $< -o $@
##################################### brlib libraries ##################################### brlib libraries
.PHONY: cleanlib cleanlibdir .PHONY: cleanlib cleanlibdir

View File

@@ -11,46 +11,51 @@
* *
*/ */
#ifndef _BUG_H #ifndef BUG_H
#define _BUG_H #define BUG_H
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdarg.h> #include <stdarg.h>
#include "likely.h" #define bug_on_always(expr) do { \
if (expr) { \
fprintf(stderr, \
"** BUG ON %s[%s:%d]: assertion '%s' failed.\n", \
__func__, __FILE__,__LINE__, #expr); \
abort(); \
/* not reached */ \
} \
} while (0)
#define warn(expr, args...) ({ \
int _ret = !!(expr); \
if (_ret) \
fprintf(stderr, ##args); \
_ret; \
})
#define warn_on_always(expr) ({ \
warn(expr, \
"** WARN ON %s[%s:%d]: assertion '%s' failed.\n", \
__func__, __FILE__,__LINE__, #expr); \
})
#ifdef BUG_ON #ifdef BUG_ON
#define bug_on(expr) do { \
if (unlikely(expr)) { \
fprintf(stderr, \
"** BUG IN %s[%s:%d]: assertion \"" #expr "\" failed.\n", \
__func__, __FILE__,__LINE__); \
abort(); \
} \
} while (0)
#else
#define bug_on(expr) ({ 0; })
#endif
#ifdef WARN_ON #define bug_on(expr) bug_on_always(expr)
#define warn_on(expr) ({ \
int _ret = !!(expr); \
if (unlikely(_ret)) \
fprintf(stderr, \
"** WARN ON %s[%s:%d]: assertion \"" #expr "\" failed.\n", \
__func__, __FILE__,__LINE__); \
unlikely(_ret); \
})
#else
#define warn_on(expr) ({ 0; })
#endif
#define warn(expr, args...) ({ \
int _ret = !!(expr); \
if (unlikely(_ret)) \
fprintf(stderr, ##args); \
unlikely(_ret); \
})
#endif /* _BUG_H */ #define warn_on(expr) warn_on_always(expr)
#define warn_on_or_eval(expr) warn_on(expr)
#else
#define bug_on(expr)
#define warn_on(expr)
#define warn_on_or_eval(expr) (expr)
#endif /* BUG_ON */
#endif /* BUG_H */