-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathMakefile
163 lines (131 loc) · 3.76 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
# (C) Copyleft 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020
# Late Lee([email protected]) from http://www.latelee.org
#
# A simple Makefile for *ONE* project(c or/and cpp file) in *ONE* or *MORE* directory
#
# note:
# you can put head file(s) in 'include' directory, so it looks
# a little neat.
#
# usage:
# $ make
# $ make V=1 # verbose ouput
# $ make CROSS_COMPILE=arm-arago-linux-gnueabi- # cross compile for ARM, etc.
# $ make debug=y # debug
#
# log
# 2013-05-14 sth about debug...
# 2016-02-29 sth for c/c++ multi diretory
# 2017-04-17 -s for .a/.so if no debug
# 2017-05-05 Add V for verbose ouput
###############################################################################
# !!!=== cross compile...
CROSS_COMPILE ?=
MKDIR_P ?= mkdir -p
CC = $(CROSS_COMPILE)gcc
CXX = $(CROSS_COMPILE)g++
AR = $(CROSS_COMPILE)ar
# !!!===
# in case all .c/.cpp need g++, specify CC=CXX...
# CC = $(CXX)
ARFLAGS = -cr
RM = -rm -rf
MAKE = make
# !!!===
# target executable file or .a or .so
target = a.out
# !!!===
# compile flags
CFLAGS += -Wall -Wfatal-errors -MMD
# !!!=== pkg-config here
#CFLAGS += $(shell pkg-config --cflags --libs glib-2.0 gattlib)
#LDFLAGS += $(shell pkg-config --cflags --libs glib-2.0 gattlib)
#****************************************************************************
# debug can be set to y to include debugging info, or n otherwise
debug = y
#****************************************************************************
ifeq ($(debug), y)
CFLAGS += -ggdb -rdynamic
else
CFLAGS += -O2 -s
endif
# !!!===
# Macros define here
DEFS += -DJIMKENT
# !!! compile flags define here...
CFLAGS += $(DEFS)
# !!! c++ flags
CXXFLAGS = $(CFLAGS)
# !!! library here...
LIBS +=
# !!! gcc/g++ link flags here
LDFLAGS += $(LIBS) -lpthread -lrt
# !!!===
# include head file directory here
INC = ./ ./inc
# or try this
#INC := $(shell find $(INC) -type d)
# !!!===
# build directory
BUILD_DIR ?= #./build/
# !!!===
# source file(s), including ALL c file(s) or cpp file(s)
# just need the directory.
SRC_DIRS = .
# or try this
#SRC_DIRS = . ../outbox
# !!!===
# gcc/g++ compile flags
CFLAGS += $(INCDIRS)
CXXFLAGS += -std=c++11
# dynamic library build flags
DYNC_FLAGS += -fpic -shared
# include directory(s)
INCDIRS := $(addprefix -I, $(INC))
# source file(s)
SRCS := $(shell find $(SRC_DIRS) -maxdepth 1 -name '*.cpp' -or -name '*.c')
# or try this
#SRCS := $(shell find $(SRC_DIRS) -name '*.cpp' -or -name '*.c')
OBJS = $(patsubst %.c,$(BUILD_DIR)%.o, $(patsubst %.cpp,$(BUILD_DIR)%.o, $(SRCS)))
# depend files(.d)
DEPS := $(OBJS:.o=.d)
ifeq ($(V),1)
Q=
NQ=true
else
Q=@
NQ=echo
endif
###############################################################################
all: $(BUILD_DIR)$(target)
$(BUILD_DIR)$(target): $(OBJS)
ifeq ($(suffix $(target)), .so)
@$(NQ) "Generating dynamic lib file..." $(notdir $(target))
$(Q)$(CXX) $^ $(LDFLAGS) $(DYNC_FLAGS) -o $(target)
else ifeq ($(suffix $(target)), .a)
@$(NQ) "Generating static lib file..." $(notdir $(target))
$(Q)$(AR) $(ARFLAGS) -o $(target) $^
else
@$(NQ) "Generating executable file..." $(notdir $(target))
$(Q)$(CXX) $^ $(LDFLAGS) -o $(target)
endif
# make all .c or .cpp
$(BUILD_DIR)%.o: %.c
@$(MKDIR_P) $(dir $@)
@$(NQ) "Compiling: " $(addsuffix .c, $(basename $(notdir $@)))
$(Q)$(CC) $(CFLAGS) -c $< -o $@
$(BUILD_DIR)%.o: %.cpp
@$(MKDIR_P) $(dir $@)
@$(NQ) "Compiling: " $(addsuffix .cpp, $(basename $(notdir $@)))
$(Q)$(CXX) $(CXXFLAGS) -c $< -o $@
clean:
@$(NQ) "Cleaning..."
$(Q)$(RM) $(OBJS) $(target) $(DEPS)
# delete build directory if needed
ifneq ($(BUILD_DIR),)
$(Q)$(RM) $(BUILD_DIR)
endif
# use 'grep -v soapC.o' to skip the file
@find . -iname '*.o' -o -iname '*.bak' -o -iname '*.d' | xargs rm -f
.PHONY: all clean
-include $(DEPS)