lander/Makefile

118 lines
2.9 KiB
Makefile
Raw Normal View History

# https://spin.atomicobject.com/2016/08/26/makefile-c-projects/ was a great
# base for this Makefile
2022-11-15 16:05:47 +01:00
2023-05-29 14:48:50 +02:00
VERSION := 0.1.0
BIN_FILENAME ?= lander
2022-11-15 16:05:47 +01:00
BUILD_DIR ?= build
SRC_DIR ?= src
TEST_DIR ?= test
THIRDPARTY_DIR ?= thirdparty
INC_DIRS ?= include $(THIRDPARTY_DIR)/include
2022-11-15 16:05:47 +01:00
BIN := $(BUILD_DIR)/$(BIN_FILENAME)
2022-11-15 16:05:47 +01:00
SRCS != find '$(SRC_DIR)' -iname '*.c'
SRCS_TEST != find '$(TEST_DIR)' -iname '*.c'
SRCS_THIRDPARTY != find '$(THIRDPARTY_DIR)/src' -iname '*.c'
SRCS_H != find include -iname '*.h'
SRCS_H_INTERNAL != find $(SRC_DIR) -iname '*.h'
2022-11-15 16:05:47 +01:00
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o) $(SRCS_THIRDPARTY:%=$(BUILD_DIR)/%.o)
OBJS_TEST := $(SRCS_TEST:%=$(BUILD_DIR)/%.o)
DEPS := $(SRCS:%=$(BUILD_DIR)/%.d) $(SRCS_THIRDPARTY:%=$(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-%)
2022-11-15 16:05:47 +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)
CFLAGS ?= -MMD -MP -g
2023-05-29 14:48:50 +02:00
INTERNALCFLAGS := $(INC_FLAGS) $(CFLAGS) -Wall -Wextra -DLANDER_VERSION=\"$(VERSION)\"
.PHONY: all
all: bin
2022-11-15 16:21:27 +01:00
# =====COMPILATION=====
# Utility used by the CI to lint
.PHONY: objs
objs: $(OBJS)
2022-11-15 16:05:47 +01:00
.PHONY: bin
bin: $(BIN)
$(BIN): $(OBJS)
$(CC) $(INTERNALCFLAGS) $(LDFLAGS) -o $@ $^
2022-12-07 13:29:21 +01:00
$(BUILD_DIR)/$(SRC_DIR)/%.c.o: $(SRC_DIR)/%.c
mkdir -p $(dir $@)
$(CC) $(INTERNALCFLAGS) -c $< -o $@
$(BUILD_DIR)/$(THIRDPARTY_DIR)/%.c.o: $(THIRDPARTY_DIR)/%.c
mkdir -p $(dir $@)
$(CC) $(INTERNALCFLAGS) -c $< -o $@
# =====TESTING=====
.PHONY: test
test: $(TARGETS_TEST)
.PHONY: test-mem
test-mem: $(TARGETS_MEM_TEST)
.PHONY: $(TARGETS_TEST)
$(TARGETS_TEST): test-%: %
./$^
2022-11-15 16:05:47 +01:00
.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 $(BIN)
$(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) $(INTERNALCFLAGS) -I$(TEST_DIR) \
-I$(dir $(@:$(BUILD_DIR)/$(TEST_DIR)/%=$(SRC_DIR)/%)) \
-c $< -o $@
# =====MAINTENANCE=====
2022-11-15 16:05:47 +01:00
.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
2022-11-15 16:05:47 +01:00
# Make make aware of the .d files
-include $(DEPS)