chore(lsm): add example binary support to Makefile

lsm
Jef Roosens 2023-10-29 13:58:42 +01:00
parent 1c421c1e67
commit 1461956d98
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 29 additions and 4 deletions

View File

@ -9,13 +9,17 @@ 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'
SRCS_EXAMPLE != find '$(EXAMPLE_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)
OBJS_EXAMPLE := $(SRCS_EXAMPLE:%=$(BUILD_DIR)/%.o)
DEPS := $(SRCS:%=$(BUILD_DIR)/%.d) $(SRCS_TEST:%=$(BUILD_DIR)/%.d) $(SRCS_EXAMPLE:%=$(BUILD_DIR)/%.d)
BINS_TEST := $(OBJS_TEST:%.c.o=%)
BINS_EXAMPLE := $(OBJS_EXAMPLE:%.c.o=%)
TARGETS_TEST := $(BINS_TEST:%=test-%)
TARGETS_EXAMPLE := $(BINS_EXAMPLE:%=test-%)
TARGETS_MEM_TEST := $(BINS_TEST:%=test-mem-%)
_CFLAGS := $(addprefix -I,$(INC_DIRS)) $(CFLAGS) -Wall -Wextra
@ -71,14 +75,27 @@ $(BUILD_DIR)/$(TEST_DIR)/%.c.o: $(TEST_DIR)/%.c
-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 $(SRCS) $(SRCS_H) $(SRCS_H_INTERNAL)
clang-format -n --Werror $(SRCS) $(SRCS_H) $(SRCS_H_INTERNAL) $(SRCS_EXAMPLE)
.PHONY: fmt
fmt:
clang-format -i $(SRCS) $(SRCS_H) $(SRCS_H_INTERNAL)
clang-format -i $(SRCS) $(SRCS_H) $(SRCS_H_INTERNAL) $(SRCS_EXAMPLE)
.PHONY: clean
clean:
@ -89,6 +106,7 @@ clean:
bear: clean
bear -- make
bear --append -- make build-test
bear --append -- make build-example
# Make make aware of the .d files

View File

@ -3,7 +3,9 @@ LIB_FILENAME = liblsm.a
BUILD_DIR = build
SRC_DIR = src
TEST_DIR = test
INC_DIRS = include src/_include
EXAMPLE_DIR = example
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

View File

@ -0,0 +1,5 @@
#include <stdio.h>
#include "lsm.h"
int main() { printf("yuh"); }