2023-01-18 14:12:02 +01:00
|
|
|
# https://spin.atomicobject.com/2016/08/26/makefile-c-projects/ was a great
|
|
|
|
# base for this Makefile
|
|
|
|
|
2023-01-18 11:29:16 +01:00
|
|
|
LIB_FILENAME ?= libvieter.a
|
|
|
|
|
|
|
|
BUILD_DIR ?= build
|
|
|
|
SRC_DIR ?= src
|
2023-01-18 13:08:09 +01:00
|
|
|
TEST_DIR ?= test
|
2023-01-18 11:29:16 +01:00
|
|
|
INC_DIRS ?= include
|
|
|
|
|
2023-01-27 20:48:51 +01:00
|
|
|
LIB := $(BUILD_DIR)/$(LIB_FILENAME)
|
|
|
|
|
2023-01-18 11:29:16 +01:00
|
|
|
SRCS != find '$(SRC_DIR)' -iname '*.c'
|
2023-01-18 11:38:34 +01:00
|
|
|
SRCS_H != find $(INC_DIRS) -iname '*.h'
|
2023-01-18 13:08:09 +01:00
|
|
|
SRCS_TEST != find '$(TEST_DIR)' -iname '*.c'
|
|
|
|
|
2023-01-18 11:29:16 +01:00
|
|
|
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
|
2023-01-18 13:08:09 +01:00
|
|
|
OBJS_TEST := $(SRCS_TEST:%=$(BUILD_DIR)/%.o)
|
|
|
|
DEPS := $(SRCS:%=$(BUILD_DIR)/%.d) $(SRCS_TEST:%=$(BUILD_DIR)/%.d)
|
|
|
|
|
|
|
|
BINS_TEST := $(OBJS_TEST:%.c.o=%)
|
2023-01-24 14:06:46 +01:00
|
|
|
TARGETS_TEST := $(BINS_TEST:%=test-%)
|
|
|
|
TARGETS_MEM_TEST := $(BINS_TEST:%=test-mem-%)
|
2023-01-18 11:29:16 +01:00
|
|
|
|
|
|
|
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)
|
2023-01-22 20:34:05 +01:00
|
|
|
CFLAGS ?= -MMD -MP -g
|
2023-01-22 12:46:09 +01:00
|
|
|
VIETERCFLAGS := $(INC_FLAGS) $(CFLAGS) -Wall -Wextra
|
2023-01-18 11:29:16 +01:00
|
|
|
|
|
|
|
.PHONY: all
|
|
|
|
all: vieter
|
|
|
|
|
|
|
|
|
|
|
|
# =====COMPILATION=====
|
2023-01-22 13:02:11 +01:00
|
|
|
# Utility used by the CI to lint
|
|
|
|
.PHONY: objs
|
|
|
|
objs: $(OBJS)
|
|
|
|
|
2023-01-18 11:29:16 +01:00
|
|
|
.PHONY: vieter
|
2023-01-27 20:48:51 +01:00
|
|
|
vieter: $(LIB)
|
2023-01-19 22:45:22 +01:00
|
|
|
$(LIB): $(OBJS)
|
2023-01-18 11:29:16 +01:00
|
|
|
ar -rcs $@ $(OBJS)
|
|
|
|
|
2023-01-18 13:08:09 +01:00
|
|
|
$(BUILD_DIR)/$(SRC_DIR)/%.c.o: $(SRC_DIR)/%.c
|
2023-01-18 11:29:16 +01:00
|
|
|
mkdir -p $(dir $@)
|
2023-01-22 12:46:09 +01:00
|
|
|
$(CC) $(VIETERCFLAGS) -c $< -o $@
|
2023-01-18 11:29:16 +01:00
|
|
|
|
|
|
|
|
2023-01-18 13:08:09 +01:00
|
|
|
# =====TESTING=====
|
|
|
|
.PHONY: test
|
2023-01-24 14:06:46 +01:00
|
|
|
test: $(TARGETS_TEST)
|
2023-01-18 13:08:09 +01:00
|
|
|
|
2023-01-22 12:17:56 +01:00
|
|
|
.PHONY: test-mem
|
2023-01-24 14:06:46 +01:00
|
|
|
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 ./$^
|
2023-01-22 12:17:56 +01:00
|
|
|
|
2023-01-19 22:23:19 +01:00
|
|
|
test-mem: build-test
|
|
|
|
@ $(foreach bin,$(BINS_TEST),valgrind --tool=memcheck --error-exitcode=1 \
|
|
|
|
--track-origins=yes --leak-check=full ./$(bin);)
|
|
|
|
|
2023-01-18 13:08:09 +01:00
|
|
|
.PHONY: build-test
|
|
|
|
build-test: $(BINS_TEST)
|
|
|
|
|
|
|
|
# For simplicity, we link every object file to each of the test files. This
|
|
|
|
# might be changed later if this starts to become too slow.
|
2023-01-27 20:48:51 +01:00
|
|
|
$(BINS_TEST): %: %.c.o $(LIB)
|
|
|
|
$(CC) \
|
|
|
|
$^ -o $@
|
2023-01-18 13:08:09 +01:00
|
|
|
|
2023-01-27 20:48:51 +01:00
|
|
|
# Along with the include directory, each test includes $(TEST_DIR) (which
|
2023-01-22 10:08:15 +01:00
|
|
|
# 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.
|
2023-01-18 13:08:09 +01:00
|
|
|
$(BUILD_DIR)/$(TEST_DIR)/%.c.o: $(TEST_DIR)/%.c
|
|
|
|
mkdir -p $(dir $@)
|
2023-01-22 12:46:09 +01:00
|
|
|
$(CC) $(VIETERCFLAGS) -I$(TEST_DIR) \
|
2023-01-22 10:08:15 +01:00
|
|
|
-I$(dir $(@:$(BUILD_DIR)/$(TEST_DIR)/%=$(SRC_DIR)/%)) \
|
|
|
|
-c $< -o $@
|
2023-01-18 16:58:33 +01:00
|
|
|
|
2023-01-18 11:38:34 +01:00
|
|
|
# =====MAINTENANCE=====
|
|
|
|
.PHONY: lint
|
|
|
|
lint:
|
2023-01-18 13:53:50 +01:00
|
|
|
clang-format -n --Werror $(SRCS) $(SRCS_H)
|
2023-01-18 11:38:34 +01:00
|
|
|
|
|
|
|
.PHONY: fmt
|
|
|
|
fmt:
|
2023-01-18 13:53:50 +01:00
|
|
|
clang-format -i $(SRCS) $(SRCS_H)
|
2023-01-18 11:38:34 +01:00
|
|
|
|
2023-01-18 11:29:16 +01:00
|
|
|
.PHONY: clean
|
|
|
|
clean:
|
|
|
|
rm -rf $(BUILD_DIR)
|
|
|
|
|
|
|
|
|
|
|
|
# Make make aware of the .d files
|
|
|
|
-include $(DEPS)
|