2023-11-21 09:36:58 +01:00
|
|
|
# https://spin.atomicobject.com/2016/08/26/makefile-c-projects/ was a great
|
|
|
|
# base for this Makefile
|
|
|
|
|
|
|
|
-include config.mk
|
|
|
|
|
|
|
|
LIB := $(BUILD_DIR)/$(LIB_FILENAME)
|
|
|
|
|
|
|
|
SRCS != find '$(SRC_DIR)' -iname '*.c'
|
2023-11-24 10:39:22 +01:00
|
|
|
SRCS_H != find include -iname '*.h'
|
2023-11-21 09:36:58 +01:00
|
|
|
SRCS_H_INTERNAL != find $(SRC_DIR) -iname '*.h'
|
|
|
|
SRCS_TEST != find '$(TEST_DIR)' -iname '*.c'
|
2023-11-22 15:19:21 +01:00
|
|
|
SRCS_THIRDPARTY != find '$(THIRDPARTY_DIR)/src' -iname '*.c'
|
2023-11-21 09:36:58 +01:00
|
|
|
|
2023-11-22 15:19:21 +01:00
|
|
|
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o) $(SRCS_THIRDPARTY:%=$(BUILD_DIR)/%.o)
|
2023-11-21 09:36:58 +01:00
|
|
|
OBJS_TEST := $(SRCS_TEST:%=$(BUILD_DIR)/%.o)
|
2023-11-22 15:19:21 +01:00
|
|
|
DEPS := $(SRCS:%=$(BUILD_DIR)/%.d) $(SRCS_TEST:%=$(BUILD_DIR)/%.d) $(SRCS_THIRDPARTY:%=$(BUILD_DIR)/%.d)
|
2023-11-21 09:36:58 +01:00
|
|
|
|
|
|
|
BINS_TEST := $(OBJS_TEST:%.c.o=%)
|
|
|
|
TARGETS_TEST := $(BINS_TEST:%=test-%)
|
|
|
|
TARGETS_MEM_TEST := $(BINS_TEST:%=test-mem-%)
|
|
|
|
|
|
|
|
_CFLAGS := $(addprefix -I,$(INC_DIRS)) $(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 $@
|
|
|
|
|
2023-11-22 15:19:21 +01:00
|
|
|
$(BUILD_DIR)/$(THIRDPARTY_DIR)/%.c.o: $(THIRDPARTY_DIR)/%.c
|
|
|
|
mkdir -p $(dir $@)
|
|
|
|
$(CC) $(_CFLAGS) -c $< -o $@
|
2023-11-21 09:36:58 +01:00
|
|
|
|
|
|
|
# =====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 $@
|
|
|
|
|
|
|
|
# =====EXAMPLES=====
|
|
|
|
.PHONY: build-example
|
|
|
|
build-example: $(BINS_EXAMPLE)
|
|
|
|
|
|
|
|
$(BINS_EXAMPLE): %: %.c.o $(LIB)
|
|
|
|
$(CC) \
|
|
|
|
$^ -o $@
|
|
|
|
|
|
|
|
# Example binaries link the resulting library
|
|
|
|
$(BUILD_DIR)/$(EXAMPLE_DIR)/%.c.o: $(EXAMPLE_DIR)/%.c
|
|
|
|
mkdir -p $(dir $@)
|
|
|
|
$(CC) $(_CFLAGS) -I$(PUB_INC_DIR) -c $< -o $@
|
|
|
|
|
|
|
|
# =====MAINTENANCE=====
|
|
|
|
.PHONY: lint
|
|
|
|
lint:
|
2023-11-24 10:39:22 +01:00
|
|
|
clang-format -n --Werror $(SRCS) $(SRCS_H) $(SRCS_H_INTERNAL)
|
2023-11-21 09:36:58 +01:00
|
|
|
|
|
|
|
.PHONY: fmt
|
|
|
|
fmt:
|
2023-11-24 10:39:22 +01:00
|
|
|
clang-format -i $(SRCS) $(SRCS_H) $(SRCS_H_INTERNAL)
|
2023-11-21 09:36:58 +01:00
|
|
|
|
|
|
|
.PHONY: check
|
|
|
|
check:
|
|
|
|
mkdir -p $(BUILD_DIR)/cppcheck
|
|
|
|
cppcheck \
|
|
|
|
$(addprefix -I,$(INC_DIRS)) \
|
|
|
|
--cppcheck-build-dir=$(BUILD_DIR)/cppcheck \
|
|
|
|
--error-exitcode=1 \
|
|
|
|
--enable=warning,style \
|
|
|
|
--inline-suppr \
|
|
|
|
--check-level=exhaustive \
|
|
|
|
--quiet \
|
|
|
|
-j$(shell nproc) \
|
|
|
|
$(SRCS)
|
|
|
|
|
|
|
|
.PHONY: clean
|
|
|
|
clean:
|
|
|
|
rm -rf $(BUILD_DIR)
|
|
|
|
|
|
|
|
|
|
|
|
.PHONY: bear
|
|
|
|
bear: clean
|
|
|
|
bear -- make
|
|
|
|
bear --append -- make build-test
|
|
|
|
bear --append -- make build-example
|
|
|
|
|
|
|
|
|
|
|
|
# Make make aware of the .d files
|
|
|
|
-include $(DEPS)
|