SRCDIR = src LIBSRCS := $(shell find $(SRCDIR)/toolbox -name "*.c") SRCS := $(shell find $(SRCDIR) -name "*.c") OBJDIR = .build LIBOBJS := $(patsubst %.c,%.o,$(subst $(SRCDIR),$(OBJDIR),$(LIBSRCS))) OBJS := $(patsubst %.c,%.o,$(subst $(SRCDIR),$(OBJDIR),$(SRCS))) DYNOBJDIR = .dynbuild DYNLIBOBJS := $(patsubst %.c,%.o,$(subst $(SRCDIR),$(DYNOBJDIR),$(LIBSRCS))) INCS = -I include/ LIBS = -lc CFLAGS += -m64 $(INCS) -pedantic -Wall -Wextra -Wshadow \ -Wcast-align -Wunused -Wconversion -Wmisleading-indentation\ -Wduplicated-cond -Wduplicated-branches -Wlogical-op\ -Wdouble-promotion -Wformat=2 -Wbad-function-cast \ -Wmissing-declarations -Wmissing-parameter-type \ -Wmissing-prototypes -Wnested-externs LDFLAGS += ${LIBS} CC += -std=c99 ifeq ($(DEBUG), 0) CFLAGS += -O3 else CFLAGS += -g -DDEBUG LDFLAGS += -g endif .PHONY: clean install uninstall reinstall all options run test \ lib static_lib dynamic_lib BINDIR = bin TARGET = ToolBox BINARY = $(BINDIR)/$(TARGET) DYNLIB = lib$(shell echo $(TARGET) | tr '[:upper:]' '[:lower:]').so STALIB = lib$(shell echo $(TARGET) | tr '[:upper:]' '[:lower:]').ar PKGCONFILE = $(shell echo $(TARGET) | tr '[:upper:]' '[:lower:]').pc HEADERS = include/toolbox all: lib $(BINARY) lib: static_lib dynamic_lib static_lib: $(STALIB) dynamic_lib: $(DYNLIB) options: @echo $(TARGET) build options: @echo -e "CC = $(CC)" @echo -e "CFLAGS = $(CFLAGS)" @echo -e "LDFLAGS = $(LDFLAGS)" @echo run: $(BINARY) @./$(BINARY) test: ifneq ("$(wildcard compile_commands.json)","") @cppcheck --enable=all --suppress=missingInclude \ --project=compile_commands.json $(INCS) . else @cppcheck --enable=all --suppress=missingInclude $(INCS) . endif bear: @make clean; bear -- make $(OBJS): $(SRCS) | $(OBJDIR) @echo [CC] $@ $(patsubst %.o, %.c, $(subst $(OBJDIR), $(SRCDIR), $@)) @$(CC) -c $(CFLAGS) -o $@ $(patsubst %.o, %.c, $(subst $(OBJDIR), $(SRCDIR), $@)) $(BINARY): $(OBJS) | $(BINDIR) @echo [LD] $@ $^ @$(CC) $(LDFLAGS) -o $@ $^ $(DYNLIBOBJS): $(LIBSRCS) | $(DYNOBJDIR) @echo [CC] -fPIC $@ $(patsubst %.o, %.c, $(subst $(DYNOBJDIR), $(SRCDIR), $@)) @$(CC) -fPIC -c $(CFLAGS) -o $@ $(patsubst %.o, %.c, $(subst $(DYNOBJDIR), $(SRCDIR), $@)) $(DYNLIB): $(DYNLIBOBJS) @echo [LD] $@ $^ @$(CC) -shared $(LDFLAGS) -o $@ $^ $(STALIB): $(LIBOBJS) @echo [AR] $@ $^ @ar rs $@ $^ clean: @rm -rf $(OBJDIR) $(BINDIR) $(DYNOBJDIR) $(DYNLIB) $(STALIB) $(BINDIR): @mkdir -p $@ $(DYNOBJDIR) $(OBJDIR): @mkdir -p $@ @mkdir -p $@/toolbox @mkdir -p $@/toolbox/list reinstall: uninstall install install: $(DYNLIB) $(STALIB) @echo installing dynamic library to /usr/lib @install -Dm755 $(DYNLIB) /usr/lib/$(DYNLIB) @echo installing static library to /usr/lib @install -Dm644 $(STALIB) /usr/lib/$(STALIB) @echo installing pkgconf file to /usr/lib/pkgconfig @install -Dm755 $(PKGCONFILE) /usr/lib/pkgconfig/$(PKGCONFILE) @echo installing headers to /usr/include @cp -rf $(HEADERS) /usr/$(HEADERS) @chmod 755 /usr/$(HEADERS) @find /usr/$(HEADERS) -type f -exec chmod 644 '{}' \; @find /usr/$(HEADERS) -type d -exec chmod 755 '{}' \; uninstall: @echo removing dynamic library from /usr/lib @rm -rf /usr/lib/$(DYNLIB) @echo removing static library from /usr/lib @rm -rf /usr/lib/$(STALIB) @echo removing pkgconf file from /usr/lib/pkgconfig @rm -rf /usr/lib/pkgconfig/$(PKGCONFILE) @echo removing headers from /usr/include @rm -rf /usr/$(HEADERS)