-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
123 lines (95 loc) · 4.08 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
#===== User defined variables =====#
DIR_SRC:=src/
DIR_INC:=src/include
DIR_OBJ:=obj/
# Source, header and object files directories.
# The object files directory also serves for dependency files.
# All directories must end with a slash ("/") or else the makefile won't work !
CC:=g++
EXT:=cpp
TARGET:=ftrl_train
#TARGET:=ftrl_predict
#TARGET:=auc
# The target executable file.
#COMMONFLAGS:=-Wall -Wextra -O2 -Wunused-parameter
COMMONFLAGS:=-Wall -O2 -g -Wextra
#COMMONFLAGS:=-Wall -O2 -g
CFLAGS=$(COMMONFLAGS) -std=c11
CXXFLAGS=$(COMMONFLAGS) -std=c++11
# Flags added to the compilation.
COMMONLIBS:=-lpthread
LINUXLIBS:=$(COMMONLIBS)
# Libraries needed to link the TARGET executable, for windows and for linux.
#===== Functions =====#
# All directories used in the functions must end with a slash ("/") or else they won't work !
search-dir=$(filter-out $1,$(dir $(wildcard $1*/)))
# Returns the list of subdirectories in Arg1 directory.
search-dir-all=$(strip $(call search-dir,$1) $(foreach DIR,$(call search-dir,$1),$(call search-dir-all,$(DIR))))
# Returns the list of directories and subdirectories in Arg1.
search-file=$(foreach DIR,$1,$(wildcard $(DIR)*.$(EXT)))
# Returns the list of files in Arg1 directories and their subdirectories.
#===== Variables =====#
DIR_SRC_ALL:=$(DIR_SRC) $(call search-dir-all,$(DIR_SRC))
DIR_INC_ALL:=$(DIR_INC) $(call search-dir-all,$(DIR_INC))
# List of all directories and subdirectories that can contain source and header files.
#$(warning $(TARGET))
train=ftrl_train
predict=ftrl_predict
ifeq ($(TARGET), $(train))
vout := ftrl_predict.cpp auc.cpp
else ifeq ($(TARGET), $(predict))
vout := ftrl_train.cpp auc.cpp
else
vout := ftrl_train.cpp ftrl_predict.cpp
endif
#$(warning $(vout))
SRC:=$(notdir $(call search-file,$(DIR_SRC_ALL)))
SRC:=$(filter-out $(vout), $(SRC) )
# List of all source files in the main source directory and its subdirectories.
OBJ:=$(SRC:.$(EXT)=.o)
# List of all object files.
I_SRC:=$(addprefix -I,$(DIR_SRC_ALL))
I_INC:=$(addprefix -I,$(DIR_INC_ALL))
# The list of all -I for finding source and header files during the compilation.
#VPATH:=$(DIR_SRC_ALL) $(DIR_OBJ)
# The VPATH variable contains all the source, header, object and dependency files directories.
# This is the variable Make uses to know where the targets and prerequisites are.
COMMONFLAGS+=$(I_SRC) $(I_INC) -MMD
# The flags the compiler needs to know where the source and header files are (I_SRC and I_INC).
# It compiles source files with the -MMD option that creates dependency files.
LIBS:=$(LINUXLIBS)
AND:=;
CLEAR:=clear
MOVE:=mv
CLEAN:=rm
OBJ_DEP:=$(DIR_OBJ)*
# All these variables contain OS dependent information :
# LIBS is the list of linking flags.
# AND is the character that allows two commands to be written on one line.
# CLEAR is the command that clears the console.
# MOVE is the command used to move files from a directory to another.
# CLEAN is the command used to delete files.
# OBJ_DEP is what needs to be deleted during cleaning operations : object and dependency files.
#===== Rules =====#
VPATH:=$(filter-out $(vout) , $(DIR_SRC_ALL) $(DIR_OBJ))
.PHONY: all init clear %-depend clean
$(TARGET):$(OBJ)
-@$(MOVE) *.o $(DIR_OBJ)
-@$(MOVE) *.d $(DIR_OBJ)
$(CC) $(CXXFLAGS) $(addprefix $(DIR_OBJ),$(OBJ)) $(LIBS) -o $(TARGET)
# The main rule of the makefile : the compilation of TARGET.
# The new object and dependency files are moved in the object directory.
init:
-@mkdir $(DIR_SRC) $(DIR_INC) $(DIR_OBJ)
@echo Project created in the current directory
%-depend:
-@cd obj $(AND) $(CLEAN) $(*F).o $(AND) $(CLEAN) $(*F).d
@echo $(*F).$(EXT) dependencies will be rebuilt with next compilation.
# Deletes dependency file so it is rebuilt with next compilation.
# This rule is made to be manually called if a source file was moved from one directory to another.
clean:
-@$(CLEAN) $(OBJ_DEP) $(TARGET)
@echo Object directory purged and $(TARGET) deleted !
# Purges the object files directory from object and dependency files, and deletes TARGET.
-include $(DIR_OBJ)*.d
# Inclusion of all dependency files, which contains all the dependency information for creating object files.