feat(ltm): start library

ltm
Jef Roosens 2023-12-14 09:44:07 +01:00
parent 675c9b78ff
commit 5df1ea8309
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
6 changed files with 2032 additions and 0 deletions

128
ltm/Makefile 100644
View File

@ -0,0 +1,128 @@
# 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'
SRCS_H != find include -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-%)
_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 $@
# =====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:
clang-format -n --Werror \
$(filter-out $(THIRDPARTY),$(SRCS)) \
$(filter-out $(THIRDPARTY),$(SRCS_H)) \
$(filter-out $(THIRDPARTY),$(SRCS_H_INTERNAL))
.PHONY: fmt
fmt:
clang-format -i \
$(filter-out $(THIRDPARTY),$(SRCS)) \
$(filter-out $(THIRDPARTY),$(SRCS_H)) \
$(filter-out $(THIRDPARTY),$(SRCS_H_INTERNAL))
.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) \
$(filter-out $(THIRDPARTY),$(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)

16
ltm/config.mk 100644
View File

@ -0,0 +1,16 @@
LIB_FILENAME = libltm.a
BUILD_DIR = build
SRC_DIR = src
TEST_DIR = test
THIRDPARTY =
PUB_INC_DIR = include
INC_DIRS = $(PUB_INC_DIR) src/_include
# -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

View File

@ -0,0 +1,8 @@
#ifndef LTM_COMMON
#define LTM_COMMON
typedef enum ltm_err {
ltm_err_ok = 0,
} ltm_err;
#endif

View File

@ -0,0 +1,30 @@
#ifndef LTM_TEMPLATE
#define LTM_TEMPLATE
#include <stdlib.h>
#include "ltm/common.h"
/**
* Represents a compiled template
*/
typedef struct lnm_template lnm_template;
/**
* Compile the given template.
*
* @param out where to store pointer to newly allocated `lnm_template`
* @param template nul-terminated string containing the template
*/
ltm_err lnm_template_compile(lnm_template **out, const char *template);
/**
* Compile the given template with a given length.
*
* @param out where to store pointer to newly allocated `lnm_template`
* @param template char buffer containing the template
* @param len length of the char buffer
*/
ltm_err lnm_template_compile_n(lnm_template **out, const char *template, size_t len);
#endif

View File

@ -0,0 +1,11 @@
#include <string.h>
#include "ltm/template.h"
ltm_err lnm_template_compile(lnm_template **out, const char *template) {
return lnm_template_compile_n(out, template, strlen(template));
}
ltm_err lnm_template_compile_n(lnm_template **out, const char *template, size_t len) {
}

1839
ltm/test/test.h 100644

File diff suppressed because it is too large Load Diff