-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMakefile
136 lines (97 loc) · 3.04 KB
/
Makefile
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# Project variables
PROJECT=cp-tools
# Compiler and flags variables
CXX=g++-10
CPPFLAGS=-W -Wall -Werror
CXXFLAGS=-std=c++20
RELEASE_CPPFLAGS=-O2
DEBUG_CPPFLAGS=-g -O0
# Condition to check if is release or debug
ifeq ($(MAKECMDGOALS),release)
CPPFLAGS+=$(RELEASE_CPPFLAGS)
else
CPPFLAGS+=$(DEBUG_CPPFLAGS)
endif
# Environment variables
INCLUDE_FLAG=-I
OUTPUT_FLAG=-o
STATIC_LIB_SUFFIX=.a
STATIC_LIB_PREFIX=lib
OBJ_EXTENSION=.o
# Directories
SRC_DIR=src
LIBS_DIR=${shell find libs -type d}
INC_DIR=include
TESTS_DIR=tests
CLASSES_DIR=classes
SCRIPTS_DIR=scripts
TEMPLATE_DIR=templates
# Installation directories
INSTALL_BIN_DIR=/usr/local/bin
INSTALL_MAN_DIR=/usr/share/man/man1
INSTALL_CLASSES_DIR=/usr/local/lib
INSTALL_TEMPLATE_DIR=/usr/local/lib
INSTALL_COMPLETION_DIR=/etc/bash_completion.d
# Temporary directories
CP_TEMP_DIR=.cp-tmp
CP_BUILD_DIR=.cp-build
# Project targets
LIBRARY=$(STATIC_LIB_PREFIX)$(PROJECT)$(STATIC_LIB_SUFFIX)
TEST_SUIT=cp-run_tests
# Include flags
LDFLAGS=-lssl -lcrypto
INCLUDES_DIRS=${addprefix $(INCLUDE_FLAG), $(INC_DIR)} ${addprefix $(INCLUDE_FLAG), $(LIBS_DIR)}
CPPFLAGS+=$(INCLUDES_DIRS)
# Project source files
PROJECT_MAIN=$(SRC_DIR)/main.cpp
PROJECT_OBJECT=$(PROJECT_MAIN:.cpp=$(OBJ_EXTENSION))
# Finds all .cpp files and filters src/main.cpp out
SOURCES=${shell find $(SRC_DIR) -type f -name *.cpp}
SOURCES:=${filter-out $(PROJECT_MAIN), $(SOURCES)}
# Generates objects file names
OBJECTS=$(SOURCES:.cpp=$(OBJ_EXTENSION))
# Completion script and manual
COMPLETION_SCRIPT=$(PROJECT)-completion.sh
MAN_FILE=cp-tools.1
# Tests source files and objects
TEST_SOURCES=${wildcard $(TESTS_DIR)/*.cpp}
TEST_OBJECTS=$(TEST_SOURCES:.cpp=$(OBJ_EXTENSION))
# ar and linker
AR=ar
AR_FLAGS=-rs
LINKER=$(CXX)
# Rules
.SUFFIXES: .cpp .$(OBJ_EXTENSION)
.PHONY: all clean build tests format
all: format build tests
format:
-clang-format-10 --version
@-find $(INC_DIR) $(TESTS_DIR) $(SRC_DIR) -type f -name "*.cpp" -or -name "*.h" | xargs clang-format-10 -i
build: $(PROJECT)
tests: $(TEST_SUIT)
$(LIBRARY): $(OBJECTS)
$(AR) $(AR_FLAGS) $@ $(OBJECTS)
$(PROJECT): $(LIBRARY) $(PROJECT_OBJECT)
$(LINKER) $(PROJECT_OBJECT) $(LIBRARY) $(OUTPUT_FLAG) $@ $(LDFLAGS)
$(TEST_SUIT): $(LIBRARY) $(TEST_OBJECTS)
$(LINKER) $(TEST_OBJECTS) $(LIBRARY) $(OUTPUT_FLAG) $@ $(LDFLAGS)
update_release:
@./scripts/gen_defs.sh
release: update_release $(LIBRARY) $(PROJECT)
install: $(PROJECT)
@cp $(PROJECT) $(INSTALL_BIN_DIR)
@mkdir -p $(INSTALL_TEMPLATE_DIR)/$(PROJECT)
@cp -r $(TEMPLATE_DIR) $(INSTALL_TEMPLATE_DIR)/$(PROJECT)/
@mkdir -p $(INSTALL_CLASSES_DIR)/$(PROJECT)
@cp -r $(CLASSES_DIR) $(INSTALL_CLASSES_DIR)/$(PROJECT)/
@cp $(SCRIPTS_DIR)/$(COMPLETION_SCRIPT) $(INSTALL_COMPLETION_DIR)
@cp $(MAN_FILE) $(INSTALL_MAN_DIR)
uninstall:
@rm -f $(INSTALL_COMPLETION_DIR)/$(COMPLETION_SCRIPT)
@rm -rf $(INSTALL_TEMPLATE_DIR)/$(PROJECT)
@rm -f $(INSTALL_BIN_DIR)/$(PROJECT)
@rm -f $(INSTALL_MAN_DIR)/$(MAN_FILE)
clean:
@rm -f *~ $(LIBRARY) $(PROJECT) $(TEST_SUIT)
@find . -name '*.o' -exec rm -f {} \;
@rm -rf *~ $(CP_TEMP_DIR) $(CP_BUILD_DIR)