feat: switch to C-based compilation, start of simple event loop

This commit is contained in:
Jef Roosens 2023-05-24 09:03:22 +02:00
parent 01eb5ece55
commit 11cd537759
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
7 changed files with 1238 additions and 94 deletions

143
Makefile
View file

@ -1,69 +1,108 @@
# =====CONFIG=====
BUILD_DIR := ./build
SRC_DIRS := ./src ./trie/src
INCLUDE_DIRS := ./trie/include
TEST_DIR := test
CORES != nproc
# https://spin.atomicobject.com/2016/08/26/makefile-c-projects/ was a great
# base for this Makefile
SRCS := $(shell find $(SRC_DIRS) $(INCLUDE_DIRS) \( -iname '*.cpp' -or -iname '*.c' -or -iname '*.h' \))
BIN_FILENAME ?= lander
BUILD_DIR ?= build
SRC_DIR ?= src
TEST_DIR ?= test
INC_DIRS ?= include
BIN := $(BUILD_DIR)/$(BIN_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
INTERNALCFLAGS := $(INC_FLAGS) $(CFLAGS) -Wall -Wextra
.PHONY: all
all: bin
# =====RECIPES=====
all: build
# =====COMPILATION=====
# Utility used by the CI to lint
.PHONY: objs
objs: $(OBJS)
.PHONY: cmake
cmake: $(BUILD_DIR)/Debug/Makefile
$(BUILD_DIR)/Debug/Makefile: CMakeLists.txt
@ cmake -B'$(BUILD_DIR)/Debug' -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=1 .
@ ln -sf '$(BUILD_DIR)/Debug/compile_commands.json' compile_commands.json
.PHONY: bin
bin: $(BIN)
$(BIN): $(OBJS)
$(CC) $(INTERNALCFLAGS) -o $@ $^
.PHONY: cmake-test
cmake-test: $(BUILD_DIR)/Test/Makefile
$(BUILD_DIR)/Test/Makefile: CMakeLists.txt
@ cmake -B'$(BUILD_DIR)/Test' -DCMAKE_BUILD_TYPE=Test .
$(BUILD_DIR)/$(SRC_DIR)/%.c.o: $(SRC_DIR)/%.c
mkdir -p $(dir $@)
$(CC) $(INTERNALCFLAGS) -c $< -o $@
.PHONY: build
build: cmake
@ make -C '$(BUILD_DIR)/Debug'
# =====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: cmake-test
@ make -C '$(BUILD_DIR)/Test'
build-test: $(BINS_TEST)
.PHONY: cmake-release
cmake-release: $(BUILD_DIR)/Release/Makefile
$(BUILD_DIR)/Release/Makefile: CMakeLists.txt
@ cmake -B'$(BUILD_DIR)/Release' -DCMAKE_BUILD_TYPE=Release .
$(BINS_TEST): %: %.c.o $(BIN)
$(CC) \
$^ -o $@
.PHONY: prod
prod: cmake-release
@ make -C '$(BUILD_DIR)/Release'
# 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 $@
.PHONY: run
run: build
@ LANDER_DATA_DIR=data LANDER_BASE_URL=http://localhost:18080/ LANDER_API_KEY=test ./build/Debug/lander
# =====MAINTENANCE=====
.PHONY: lint
lint:
clang-format -n --Werror $(SRCS) $(SRCS_H) $(SRCS_H_INTERNAL)
.PHONY: valgrind
valgrind: build
@ LANDER_DATA_DIR=data LANDER_BASE_URL=http://localhost:18080/ LANDER_API_KEY=test \
valgrind --tool=memcheck --error-exitcode=1 --track-origins=yes --leak-check=full ./build/Debug/lander
.PHONY: gdb
gdb: build
@ LANDER_DATA_DIR=data LANDER_BASE_URL=http://localhost:18080/ LANDER_API_KEY=test gdb --args ./build/Debug/lander
.PHONY: test
test: build-test
@ $(MAKE) -C '$(BUILD_DIR)/Test' test ARGS=-j$(CORES) CTEST_OUTPUT_ON_FAILURE=1
.PHONY: fmt
fmt:
clang-format -i $(SRCS) $(SRCS_H) $(SRCS_H_INTERNAL)
.PHONY: clean
clean:
@ rm -rf '$(BUILD_DIR)' compile_commands.json
rm -rf $(BUILD_DIR)
.PHONY: lint
lint:
@ clang-format --Werror -n $(SRCS)
.PHONY: format
format:
@ clang-format -i $(SRCS)
.PHONY: bear
bear: clean
bear -- make
bear --append -- make build-test
# Make make aware of the .d files
-include $(DEPS)