summaryrefslogtreecommitdiff
path: root/Makefile
blob: 97a099026564dfd3893c53a3b7edcdeb822f2aea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
CXX ?= g++
CPPFLAGS ?= -pipe -O2 -Wall -Wextra -fPIC -std=c++14

OK_STRING    = "[OK]"
ERROR_STRING = "[ERROR]"
WARN_STRING  = "[WARNING]"
COM_STRING   = "Compiling"

SRC_DIR    := src
HEADER_DIR := include
BIN_DIR    := bin
OBJ_DIR    := $(BIN_DIR)/obj
MAIN       := $(SRC_DIR)/main.cc
SOURCES    := $(filter-out $(SRC_DIR)/main.cc, $(wildcard $(SRC_DIR)/*.cc))
OBJECTS    := $(SOURCES:$(SRC_DIR)/%.cc=$(OBJ_DIR)/%.o)
EXECUTABLE := $(BIN_DIR)/hmacsha256

clean: COUNT=$(words $(wildcard $(OBJ_DIR)/*.o))

all: $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS) $(MAIN)
	@script -q -e -c '$(CXX) -I$(HEADER_DIR) -o $@ $(OBJECTS) $(MAIN)' $@.log > /dev/null; \
	RESULT=$$?; \
	sed -i '1d;$$d' $@.log; \
	sed -i '/^[[:space:]]*$$/d' $@.log; \
	if [ $$RESULT -ne 0 ]; then \
		printf "%-60b%b" "Building $@" "$(ERROR_STRING)\n"; \
	elif [ -s $@.log ]; then \
		printf "%-60b%b" "Building $@" "$(WARN_STRING)\n"; \
	else  \
		printf "%-60b%b" "Building $@" "$(OK_STRING)\n"; \
		printf "%b" "Build success\n"; \
	fi; \
	cat $@.log; \
	rm -f $@.log; \

$(OBJECTS): $(OBJ_DIR)/%.o : $(SRC_DIR)/%.cc
	@script -q -e -c '$(CXX) $(CPPFLAGS) -c -I$(HEADER_DIR) $< -o $@' $@.log > /dev/null; \
	RESULT=$$?; \
	sed -i '1d;$$d' $@.log; \
	sed -i '/^[[:space:]]*$$/d' $@.log; \
	if [ $$RESULT -ne 0 ]; then \
		printf "%-60b%b" "$(COM_STRING) $@" "$(ERROR_STRING)\n"; \
	elif [ -s $@.log ]; then \
		printf "%-60b%b" "$(COM_STRING) $@" "$(WARN_STRING)\n"; \
	else  \
		printf "%-60b%b" "$(COM_STRING) $@" "$(OK_STRING)\n"; \
	fi; \
	cat $@.log; \
	rm -f $@.log; \

clean:
	@if [ $(COUNT) -ne 0 ] || [ -f  $(EXECUTABLE) ]; then \
	if [ $(COUNT) -ne 0 ]; then \
		printf "%b" "Cleaning objects\n"; \
		rm -f $(OBJ_DIR)/*.o; \
	fi; \
	if [ -f  $(EXECUTABLE) ]; then \
		printf "%b" "Removing executable\n"; \
		rm -f  $(EXECUTABLE); \
	fi; \
	printf "%b" "Clean complete\n"; \
	else \
	printf "%b" "Nothing to clean\n"; \
	fi;