-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
182 lines (136 loc) · 4.25 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# Makefile
TREE_ROOT := $(shell pwd)
BASE_DIR:=$(TREE_ROOT)
# Project Directory Name
PROJECT_DIR:=$(shell basename $(CURDIR))
# Peoject Name, Name of current Directory
PROJECT_NAME ?= $(PROJECT_DIR)
# Target Name
TARGET_NAME ?= $(PROJECT_NAME)
# Executables
CC:=$(PREFIX)gcc
CPP:=$(PREFIX)g++
AR:=$(PREFIX)ar
OBJCOPY:=$(PREFIX)objcopy
OBJDUMP:=$(PREFIX)objdump
SIZE:=$(PREFIX)size
GDB:=$(PREFIX)gdb
REMOVE:=rm -rf
MAKE:=make -s
# Directory Structure
SRC_DIR?=src
OBJ_DIR?=obj
LIB_DIR?=lib
INC_DIR?=inc
BIN_DIR?=bin
# Supported Extension
C_EXT := c
H_EXT := h
CPP_EXT := cpp
ASM_EXT := s
OBJ_EXT := o
LIB_EXT := a
# If SRC dir is empty search in Current Directory
ifeq "$(wildcard $(SRC_DIR) )" ""
# src does not exists or is empty use current directory
SRC_DIR :=.
C_FILES := $(wildcard $(SRC_DIR)/*.$(C_EXT))
# C++ Langauge
CPP_FILES := $(wildcard $(SRC_DIR)/*.$(CPP_EXT) )
# Assembly Langauge
ASM_FILES := $(wildcard $(SRC_DIR)/*.$(ASM_EXT) )
else
# C Langauge
C_FILES := $(shell find $(SRC_DIR) -name \*.$(C_EXT) )
# C++ Langauge
CPP_FILES := $(shell find $(SRC_DIR) -name \*.$(CPP_EXT) )
# Assembly Langauge
ASM_FILES := $(shell find $(SRC_DIR) -name \*.$(ASM_EXT) )
endif
# If INC dir is empty search in Current Directory
ifeq "$(wildcard $(INC_DIR) )" ""
INC_DIRS +=.
else
# Include dirs
INC_DIRS += $(sort $(dir $(shell find $(INC_DIR) -name \*.$(H_EXT) )))
endif
INCLUDE_FLAG = $(addprefix -I,$(INC_DIRS))
GLOBAL_CFLAG += $(INCLUDE_FLAG)
# Object Files and dependency files
C_OBJFILES := $(patsubst $(SRC_DIR)/%.$(C_EXT),$(OBJ_DIR)/%.$(OBJ_EXT),$(C_FILES))
C_DEPEND_FILES := $(patsubst %.$(OBJ_EXT),%.$(DEPEND_EXT),$(C_OBJFILES))
CPP_OBJFILES := $(patsubst $(SRC_DIR)/%.$(CPP_EXT),$(OBJ_DIR)/%.$(OBJ_EXT),$(CPP_FILES))
CPP_DEPEND_FILES := $(patsubst %.$(OBJ_EXT),%.$(DEPEND_EXT),$(CPP_OBJFILES))
ASM_OBJFILES := $(patsubst $(SRC_DIR)/%.$(ASM_EXT),$(OBJ_DIR)/%.$(OBJ_EXT),$(ASM_FILES))
ASM_DEPEND_FILES := $(patsubst %.$(OBJ_EXT),%.$(DEPEND_EXT),$(ASM_OBJFILES))
# Objects
OBJECTS := $(C_OBJFILES) $(CPP_OBJFILES) $(ASM_OBJFILES)
# Depend Files
DEPEND_FILES := $(C_DEPEND_FILES) $(CPP_DEPEND_FILES) $(ASM_DEPEND_FILES)
# Add Custom libraries
LIBS+=
# Libraries
LIB_FLAG+= -pthread -lpthread
############################################
# Create obj Dirs
############################################
OBJ_DIRS:=$(dir $(OBJECTS))
ifneq ("$(OBJ_DIRS)","")
$(shell mkdir -p $(OBJ_DIRS))
endif
# If Verbose mode is not requsted then silent output
ifeq (,$(V))
.SILENT:
endif
# Optimization level
OPTIMIZE := -Os
#Disable logging
ifneq ("$(TRACE_LEVEL)","")
GLOBAL_CFLAG+=-D_TRACE_LEVEL_=$(TRACE_LEVEL)
endif
GLOBAL_CFLAG += -Wall
CFLAGS := $(GLOBAL_CFLAG)
LDFLAGS := $(LIB_FLAG)
# Target Binary
TARGET ?= $(BIN_DIR)/$(TARGET_NAME)
all:$(TARGET)
# Executable file
$(BIN_DIR)/$(TARGET_NAME): $(OBJECTS) $(LIB_DEPENDS)
@echo Creating $(BIN_DIR)/$(TARGET_NAME)
@mkdir -p $(BIN_DIR);
$(CPP) $(OBJECTS) $(LDFLAGS) -o $@
#Compile C files
$(C_OBJFILES): $(OBJ_DIR)/%.$(OBJ_EXT): $(SRC_DIR)/%.$(C_EXT)
@$(CC) -E $(CFLAGS) $< > $(OBJ_DIR)/$*.E
$(CC) $(CFLAGS) -c $< -o $@
@$(CC) -MM $(CFLAGS) $< > $(OBJ_DIR)/$*.d
@mv -f $(OBJ_DIR)/$*.d $(OBJ_DIR)/$*.d.tmp;
@sed -e 's|.*:|$(OBJ_DIR)/$*.o:|' < $(OBJ_DIR)/$*.d.tmp > $(OBJ_DIR)/$*.d
@rm -f $(OBJ_DIR)/$*.d.tmp
# Compile cpp files
$(CPP_OBJFILES): $(OBJ_DIR)/%.$(OBJ_EXT): $(SRC_DIR)/%.$(CPP_EXT)
@$(CPP) -E $(CPPFLAGS) $< > $(OBJ_DIR)/$*.E
$(CPP) $(CPPFLAGS) -c $< -o $@
@$(CPP) -MM $(CPPFLAGS) $< > $(OBJ_DIR)/$*.d
@mv -f $(OBJ_DIR)/$*.d $(OBJ_DIR)/$*.d.tmp
@sed -e 's|.*:|$(OBJ_DIR)/$*.o:|' < $(OBJ_DIR)/$*.d.tmp > $(OBJ_DIR)/$*.d
@rm -f $(OBJ_DIR)/$*.d.tmp
# Compile assembly file
$(ASM_OBJFILES): $(OBJ_DIR)/%.$(OBJ_EXT): $(SRC_DIR)/%.$(ASM_EXT)
@$(CPP) -E $(CPPFLAGS) $< > $(OBJ_DIR)/$*.E
$(CPP) $(CPPFLAGS) -c $< -o $@
@$(CPP) -MM $(CPPFLAGS) $< > $(OBJ_DIR)/$*.d
@mv -f $(OBJ_DIR)/$*.d $(OBJ_DIR)/$*.d.tmp
@sed -e 's|.*:|$(OBJ_DIR)/$*.o:|' < $(OBJ_DIR)/$*.d.tmp > $(OBJ_DIR)/$*.d
@rm -f $(OBJ_DIR)/$*.d.tmp
# Library File
$(LIB_DIR)/$(LIB_PREFIX)$(LIBNAME).$(LIB_EXT) : $(OBJECTS)
@mkdir -p $(LIB_DIR);
$(AR) $(ARFLAGS) $@ $(OBJECTS)
# Clean Everything
clean cleanDebug cleanRelease:
@echo "Remove binary and objects Files"
@$(REMOVE) $(OBJ_DIR)
@$(REMOVE) $(LIB_DIR)
@$(REMOVE) $(BIN_DIR)
.PHONY: clean