-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.mk
198 lines (157 loc) · 4.17 KB
/
common.mk
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# Makefile
# Project Directory Name
PROJECT_DIR:=$(shell basename $(CURDIR))
# Peoject Name, Name of current Directory
PROJECT_NAME ?= $(PROJECT_DIR)
# Project dependency
TARGET_EXT_DEPEND ?= $(PROJECT_EXT_DEPEND)
# Directory Structure
SRC_DIR ?= src
OBJ_DIR ?= obj
LIB_DIR ?= lib
BIN_DIR ?= bin
INC_DIR ?= inc
# Execuatbles
CC := gcc
CPP := g++
AR := ar
RM := rm -rf
CSCOPE := cscope
TAG := etags
MAKE := make
# Supported Extension
C_EXT := c
H_EXT := h
OBJ_EXT := o
LIB_EXT := a
LIB_PREFIX := lib
DEPEND_EXT := d
# Get OS Name
OS := $(shell uname)
# 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))
else
# C Langauge
C_FILES := $(shell find $(SRC_DIR) -name \*.$(C_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
# 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))
# Objects
OBJECTS := $(C_OBJFILES)
# Depend Files
DEPEND_FILES := $(C_DEPEND_FILES)
############################################
# Create obj and bin Dirs
############################################
OBJ_DIRS:=$(dir $(OBJECTS))
ifneq ("$(OBJ_DIRS)","")
$(shell mkdir -p $(OBJ_DIRS))
endif
ifneq ("$(BIN_DIR)","")
$(shell mkdir -p $(BIN_DIR))
endif
# If Verbose mode is not requsted then silent output
ifeq (,$(V))
.SILENT:
endif
###########################################
# Flags
############################################
# Optimization level
OPTIMIZE := -O0
# Debug Flag
DEBUG := -g
GLOBAL_CFLAG += -Wall -Werror
LIB_FLAG +=$(addprefix -L,$(LIB_DIRS))
LIB_FLAG +=$(addprefix -l,$(LIBS))
INCLUDE_FLAG = $(addprefix -I,$(INC_DIRS))
GLOBAL_CFLAG += $(INCLUDE_FLAG)
# Target Binary
ifeq (,$(TARGET_NAME))
ifneq (,$(LIB_NAME))
LIB_FULL_NAME ?= $(LIB_PREFIX)$(LIB_NAME).$(LIB_EXT)
TARGET ?= $(LIB_DIR)/$(LIB_FULL_NAME)
else
TARGET_NAME := $(BIN_DIR)/$(PROJECT_NAME)
TARGET ?= $(BIN_DIR)/$(TARGET_NAME)
BIN_NAME = $(TARGET_NAME)
endif
else
TARGET ?= $(BIN_DIR)/$(TARGET_NAME)
BIN_NAME = $(TARGET_NAME)
endif
# OS Specific Flags
ifeq ($(OS),Darwin)
GLOBAL_CFLAG += '-DFUNOPEN_SUPPORT' '-DDARWIN_SYS'
else
ifeq ($(OS),Linux)
GLOBAL_CFLAG += '-DFMEMOPEN_SUPPORT'
endif
endif
#Disable logging
ifneq (,$(TRACE_LEVEL))
GLOBAL_CFLAG+='-D_TRACE_LEVEL_'=$(TRACE_LEVEL)
else
GLOBAL_CFLAG+='-D_TRACE_LEVEL_'=3
endif
CFLAGS += $(GLOBAL_CFLAG)
LDFLAGS += $(LIB_FLAG)
############################################
# Targets
############################################
all: release lib bin $(TARGET)
release: CFLAGS += $(OPTIMIZE)
release:$(TARGET)
debug: CFLAGS += $(DEBUG)
debug: $(TARGET)
lib bin : $(TARGET)
clean : $(TARGET_EXT_DEPEND)
# Include Depend Files
-include $(DEPEND_FILES)
$(TARGET):$(TARGET_EXT_DEPEND)
###########################################
# Rules
###########################################
#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) $(OBJ_DIR)/$*.d.tmp
# Library File
$(LIB_DIR)/%.$(LIB_EXT) : $(OBJECTS)
echo $(OBJECTS)
mkdir -p $(LIB_DIR);
$(AR) $(ARFLAGS) $@ $(OBJECTS)
# Executable file
$(BIN_DIR)/$(TARGET_NAME): $(OBJECTS)
$(CC) $(OBJECTS) $(LDFLAGS) -o $@
# Clean Everything
clean cleanDebug cleanRelease:
@echo "Remove Files"
$(RM) $(OBJ_DIR)
$(RM) $(LIB_DIR)
$(RM) $(BIN_DIR)
# Generate Tag files
# ctags
tags: cscope $(C_FILES) $(CPP_FILES) $(ASM_FILES)
$(TAG) $(TAG_FLAGS) $(C_FILES) $(CPP_FILES) $(ASM_FILES)
# cscope tags
cscope : $(C_FILES) $(CPP_FILES) $(ASM_FILES)
$(CSCOPE) $(CSCOPE_FLAGS)
$(TARGET_EXT_DEPEND)::
$(MAKE) -C $@ $(MAKECMDGOALS)
.PHONY: clean install connect tags cscope $(TARGET_EXT_DEPEND)