Compare commits

...

7 Commits

4 changed files with 123 additions and 42 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

100
Makefile
View File

@@ -44,30 +44,88 @@ BIN := $(addprefix $(BINDIR)/,$(TEST_FN:.c=))
CCLSCMDS := compile_commands.json CCLSCMDS := compile_commands.json
##################################### Check for compiler and requested build
BUILDS := release dev perf debug
# last compilation build
BUILDFILE := .lastbuild
lastbuild := $(file < $(BUILDFILE))
$(info brlib last:$(lastbuild))
# default to gcc
CC ?= cc
ifeq ($(CC),cc)
CC = gcc
endif
# if no build specified, use last one
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 ##################################### pre-processor flags
CPPFLAGS := -I $(INCDIR) override CPPFLAGS += -I $(INCDIR)
#CPPFLAGS += -DDEBUG # global
#CPPFLAGS += -DDEBUG_DEBUG_C # log() funcs debug ifeq ($(BUILD),release)
CPPFLAGS += -DDEBUG_DEBUG # activate logs funcs CPPFLAGS += -DNDEBUG # assert (unused)
CPPFLAGS += -DDEBUG_POOL # mem pools 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 += -O2
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))
@@ -77,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)
@@ -178,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
@@ -248,7 +314,7 @@ cleanccls:
# maybe run cleanobj cleanlibobj in commands ? # maybe run cleanobj cleanlibobj in commands ?
$(CCLSCMDS): cleanobj $(SRC) | $(CCLSROOT) $(CCLSCMDS): cleanobj $(SRC) | $(CCLSROOT)
@echo "Generating ccls compile commands file ($@)." @echo "Generating ccls compile commands file ($@)."
@$(BEAR) -- make test @$(BEAR) -- $(MAKE) test
##################################### valgrind (mem check) ##################################### valgrind (mem check)
.PHONY: memcheck .PHONY: memcheck

View File

@@ -18,6 +18,7 @@
#ifndef _BRLIB_H #ifndef _BRLIB_H
#define _BRLIB_H #define _BRLIB_H
#include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <bits/wordsize.h> /* defines __WORDSIZE: 32 or 64 */ #include <bits/wordsize.h> /* defines __WORDSIZE: 32 or 64 */

View File

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