nucleo-l053r8-benches/crates/algorithms/Makefile

97 lines
2.8 KiB
Makefile
Executable file

include project.conf
ifndef PROJECT_NAME
$(error Missing PROJECT_NAME. Put variables at project.conf file)
endif
ifndef TEST_BIN
$(error Missing TEST_BIN. Put variables at project.conf file)
endif
OS := $(shell uname -s)
SHELL := bash
COLOR_PREFIX := e
ifeq ($(OS),Darwin)
COLOR_PREFIX := 033
endif
BROWN=\$(COLOR_PREFIX)[0;33m
BLUE=\$(COLOR_PREFIX)[1;34m
END_COLOR=\$(COLOR_PREFIX)[0m
BUILDDIR := target/c/build
BINDIR := target/c/bin
SRCDIR := src/c
SRCEXT := c
CC := gcc
# Defines the language standards for GCC
STD := -std=gnu99 # See man gcc for more options
# Protection for stack-smashing attack
STACK := -fstack-protector-all -Wstack-protector
# Specifies to GCC the required warnings
WARNS := -Wall -Wextra -pedantic # -pedantic warns on language standards
# Flags for compiling
CFLAGS := -O3 $(STD) $(STACK) $(WARNS)
# Debug options
DEBUG := -g3 -DDEBUG=1
# %.o file names
NAMES := $(notdir $(basename $(wildcard $(SRCDIR)/*.$(SRCEXT))))
NAMES += $(notdir $(basename $(wildcard $(SRCDIR)/crc/*.$(SRCEXT))))
NAMES_TEST := $(notdir $(basename $(wildcard $(SRCDIR)/test/*.$(SRCEXT))))
OBJECTS :=$(patsubst %,$(BUILDDIR)/%.o,$(NAMES))
OBJECTS_TEST :=$(patsubst %,$(BUILDDIR)/test/%.o,$(NAMES_TEST))
.PHONY: help all prepare run build
default: prepare all
help:
@echo "C Project Template"
@echo "Target rules:"
@echo " all - Compiles and generates static library and tests"
@echo " build - Compiles and generates static library"
@echo " test - Compile and run tests"
@echo " prepare - Prepare the environment"
@echo " clean - Clean the project by removing build artifacts"
@echo " help - Prints a help message with target rules"
prepare:
mkdir -p $(BUILDDIR) $(BUILDDIR)/test $(BINDIR)
all: $(BINDIR)/$(TEST_BIN) $(BINDIR)/$(LIBRARY)
build: prepare $(BINDIR)/$(LIBRARY)
$(BINDIR)/$(LIBRARY): $(OBJECTS)
@echo OBJECTS: $(OBJECTS)
@echo -en "$(BROWN)LD $(END_COLOR)";
gcc-ar rcs $(BINDIR)/$(LIBRARY) $(OBJECTS)
@echo -en "\n--\nLibrary file placed at" \
"$(BROWN)$(BINDIR)/$(LIBRARY)$(END_COLOR)\n";
$(BINDIR)/$(TEST_BIN): $(OBJECTS) $(OBJECTS_TEST)
@echo OBJECTS: $(OBJECTS) $(OBJECTS_TEST)
@echo -en "$(BROWN)LD $(END_COLOR)";
$(CC) -o $(BINDIR)/$(TEST_BIN) $+ $(DEBUG) $(CFLAGS) $(BUILD)
@echo -en "\n--\nTest binary file placed at" \
"$(BROWN)$(BINDIR)/$(TEST_BIN)$(END_COLOR)\n";
test: prepare $(BINDIR)/$(TEST_BIN)
@echo "Running the tests"
@echo "========================================================================="
@$(BINDIR)/$(TEST_BIN)
# Rule for object binaries compilation
$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
@echo -en "$(BROWN)CC $(END_COLOR)";
$(CC) -c $^ -o $@ $(DEBUG) $(CFLAGS) $(BUILDDIR)
$(BUILDDIR)/test/%.o: $(SRCDIR)/test/%.$(SRCEXT)
@echo -en "$(BROWN)CC $(END_COLOR)";
$(CC) -c $^ -o $@ $(DEBUG) $(CFLAGS) $(BUILDDIR)
clean:
rm -rvf $(BUILDDIR) $(BINDIR) $(LOGDIR)