refactor: decouple trie into static library

lsm
Jef Roosens 2023-07-28 18:48:44 +02:00
parent 17dcc1db06
commit fb4a9a3b2e
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
12 changed files with 1967 additions and 7 deletions

View File

@ -9,7 +9,10 @@ BUILD_DIR ?= build
SRC_DIR ?= src
TEST_DIR ?= test
THIRDPARTY_DIR ?= thirdparty
INC_DIRS ?= include $(THIRDPARTY_DIR)/include
INC_DIRS ?= include $(THIRDPARTY_DIR)/include trie/include
LIBS ?= trie m
LIB_DIRS ?= ./trie/build
BIN := $(BUILD_DIR)/$(BIN_FILENAME)
@ -36,7 +39,12 @@ INC_FLAGS := $(addprefix -I,$(INC_DIRS))
# -MP: generate a dummy target for every header file (according to the docs it
# prevents some errors when removing header files)
CFLAGS ?= -MMD -MP -g
INTERNALCFLAGS := $(INC_FLAGS) $(CFLAGS) -Wall -Wextra -DLANDER_VERSION=\"$(VERSION)\"
_CFLAGS := $(INC_FLAGS) $(CFLAGS) -Wall -Wextra -DLANDER_VERSION=\"$(VERSION)\"
_LDFLAGS := $(addprefix -L,$(LIB_DIRS)) $(addprefix -l,$(LIBS)) $(LDFLAGS)
export CFLAGS
export LDFLAGS
.PHONY: all
all: bin
@ -47,18 +55,22 @@ all: bin
.PHONY: objs
objs: $(OBJS)
.PHONY: libtrie
libtrie:
$(MAKE) -C trie
.PHONY: bin
bin: $(BIN)
$(BIN): $(OBJS)
$(CC) $(INTERNALCFLAGS) $(LDFLAGS) -lm -o $@ $^
$(BIN): $(OBJS) libtrie
$(CC) -o $@ $(OBJS) $(_LDFLAGS)
$(BUILD_DIR)/$(SRC_DIR)/%.c.o: $(SRC_DIR)/%.c
mkdir -p $(dir $@)
$(CC) $(INTERNALCFLAGS) -c $< -o $@
$(CC) $(_CFLAGS) -c $< -o $@
$(BUILD_DIR)/$(THIRDPARTY_DIR)/%.c.o: $(THIRDPARTY_DIR)/%.c
mkdir -p $(dir $@)
$(CC) $(INTERNALCFLAGS) -c $< -o $@
$(CC) $(_CFLAGS) -c $< -o $@
# =====TESTING=====
@ -95,7 +107,7 @@ $(BINS_TEST): %: %.c.o
# exposed.
$(BUILD_DIR)/$(TEST_DIR)/%.c.o: $(TEST_DIR)/%.c
mkdir -p $(dir $@)
$(CC) $(INTERNALCFLAGS) -I$(TEST_DIR) \
$(CC) $(_CFLAGS) -I$(TEST_DIR) \
-I$(dir $(@:$(BUILD_DIR)/$(TEST_DIR)/%=$(SRC_DIR)/%)) \
-c $< -o $@
@ -111,6 +123,7 @@ fmt:
.PHONY: clean
clean:
rm -rf $(BUILD_DIR)
$(MAKE) -C trie clean
.PHONY: bear

108
trie/Makefile 100644
View File

@ -0,0 +1,108 @@
# https://spin.atomicobject.com/2016/08/26/makefile-c-projects/ was a great
# base for this Makefile
LIB_FILENAME ?= libtrie.a
BUILD_DIR ?= build
SRC_DIR ?= src
TEST_DIR ?= test
INC_DIRS ?= include
LIB := $(BUILD_DIR)/$(LIB_FILENAME)
SRCS != find '$(SRC_DIR)' -iname '*.c'
SRCS_H != find $(INC_DIRS) -iname '*.h'
SRCS_H_INTERNAL != find $(SRC_DIR) -iname '*.h'
SRCS_TEST != find '$(TEST_DIR)' -iname '*.c'
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
OBJS_TEST := $(SRCS_TEST:%=$(BUILD_DIR)/%.o)
DEPS := $(SRCS:%=$(BUILD_DIR)/%.d) $(SRCS_TEST:%=$(BUILD_DIR)/%.d)
BINS_TEST := $(OBJS_TEST:%.c.o=%)
TARGETS_TEST := $(BINS_TEST:%=test-%)
TARGETS_MEM_TEST := $(BINS_TEST:%=test-mem-%)
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
# -MMD: generate a .d file for every source file. This file can be imported by
# make and makes make aware that a header file has been changed, ensuring an
# object file is also recompiled if only a header is changed.
# -MP: generate a dummy target for every header file (according to the docs it
# prevents some errors when removing header files)
CFLAGS ?= -MMD -MP -g
_CFLAGS := $(INC_FLAGS) $(CFLAGS) -Wall -Wextra
.PHONY: all
all: lib
# =====COMPILATION=====
# Utility used by the CI to lint
.PHONY: objs
objs: $(OBJS)
.PHONY: lib
lib: $(LIB)
$(LIB): $(OBJS)
ar -rcs $@ $(OBJS)
$(BUILD_DIR)/$(SRC_DIR)/%.c.o: $(SRC_DIR)/%.c
mkdir -p $(dir $@)
$(CC) -c $(_CFLAGS) $< -o $@
# =====TESTING=====
.PHONY: test
test: $(TARGETS_TEST)
.PHONY: test-mem
test-mem: $(TARGETS_MEM_TEST)
.PHONY: $(TARGETS_TEST)
$(TARGETS_TEST): test-%: %
./$^
.PHONY: $(TARGETS_MEM_TEST)
$(TARGETS_MEM_TEST): test-mem-%: %
valgrind --tool=memcheck --error-exitcode=1 --track-origins=yes --leak-check=full ./$^
.PHONY: build-test
build-test: $(BINS_TEST)
$(BINS_TEST): %: %.c.o $(LIB)
$(CC) \
$^ -o $@
# Along with the include directory, each test includes $(TEST_DIR) (which
# contains the acutest.h header file), and the src directory of the module it's
# testing. This allows tests to access internal methods, which aren't publicly
# exposed.
$(BUILD_DIR)/$(TEST_DIR)/%.c.o: $(TEST_DIR)/%.c
mkdir -p $(dir $@)
$(CC) $(_CFLAGS) -I$(TEST_DIR) \
-I$(dir $(@:$(BUILD_DIR)/$(TEST_DIR)/%=$(SRC_DIR)/%)) \
-c $< -o $@
# =====MAINTENANCE=====
.PHONY: lint
lint:
clang-format -n --Werror $(SRCS) $(SRCS_H) $(SRCS_H_INTERNAL)
.PHONY: fmt
fmt:
clang-format -i $(SRCS) $(SRCS_H) $(SRCS_H_INTERNAL)
.PHONY: clean
clean:
rm -rf $(BUILD_DIR)
.PHONY: bear
bear: clean
bear -- make
bear --append -- make build-test
# Make make aware of the .d files
-include $(DEPS)

1839
trie/test/test.h 100644

File diff suppressed because it is too large Load Diff