File: Makefile

package info (click to toggle)
splix 2.0.0-2.2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 2,912 kB
  • ctags: 653
  • sloc: cpp: 4,514; makefile: 332; sh: 86; ansic: 42
file content (389 lines) | stat: -rw-r--r-- 11,797 bytes parent folder | download | duplicates (6)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#
#	Makefile		(C) 2006-2008, Aurélien Croc (AP²C)
#
# This project is under the GPL Licence
#
# Available MAKE options:
#      * V sets to 1 for verbose mode
# 
# Available variables in module.mk :
#      * SUBDIRS
#      * MODE = {default,debug,optimized}
#      * CFLAGS
#      * CXXFLAGS
#      * LDFLAGS
#      * DEBUG_CFLAGS
#      * DEBUG_CXXFLAGS
#      * OPTIMIZED_CFLAGS
#      * OPTIMIZED_CXXFLAGS
#      * TARGETS
#      * GENERIC_TARGETS
#      * PRE_GENERIC_TARGETS
#      * directory1_directory2_file_ext_FLAGS
#
# Each project must be listed in the TARGETS variable. Next:
#      * project1_SRC
#      * project1_LIB
#      * project1_CFLAGS
#      * project1_LDFLAGS
#      * project1_MODULES
#      * project1_CXXFLAGS
#      * ...
#
# This Makefile will generate:
#      * project1_TARGET
#      * project1_OBJ
#
#
# In the file rules.mk:
#
# You can add your own rules
# 
# To add a new target file support, please add the target extension in the
# TARGET_RULES variable and define the function targetDefinition_{extension}.
# This function will be called with one parameters: the target name
# Like the examples above, use $(value $(1)_TARGET) or _OBJ, .. to access
# to useful informations
#
#
# /!\ = Please do your modifications in the module.mk file = /!\
# 


# +--------------------------------------------------------------------------+
# |   SUPPORTED LANGUAGES, DIRECTORIES ARCHI AND TOOLS LOCATIONS & FLAGS     |
# +--------------------------------------------------------------------------+
LANGUAGES	:= cpp c

CC		:= gcc
CXX		:= g++
RM		:= rm -f
AR		:= ar crs
LEX		:= flex
YACC		:= bison
LINKER		:= $(CXX)

DEPDIR          := .deps
BUILDDIR        := .build
TARGETDIR       := .


empty           :=
space           := $(empty) $(empty)
comma           := ,

DEBUG_CFLAGS    := -O0 -g
DEBUG_CXXFLAGS  := -O0 -g
OPTIM_CFLAGS	:= -O2
OPTIM_CXXFLAGS	:= -O2

ARCHI           := $(shell uname -s)

ifeq ($(ARCHI),Darwin)
PLUGIN_EXT      := bundle
LIBRARY_EXT     := dylib
else
PLUGIN_EXT      := so
LIBRARY_EXT     := so
endif


# +--------------------------------------------------------------------------+
# |   DEFINITIONS VARIABLE LOADING                                           |
# +--------------------------------------------------------------------------+

MODE		:= default
DEFFILE		:= .defs.mk
-include $(DEFFILE)



# +--------------------------------------------------------------------------+
# |   SUBDIRS LOADING                                                        |
# +--------------------------------------------------------------------------+

include module.mk
ifeq ($(DEFLOADED),1)
include $(patsubst %, %/module.mk, $(_SUBDIRS))
endif # DEFLOADED == 1
ifeq ($(DEFDONE),1)




# +--------------------------------------------------------------------------+
# |   COMPILATION MODE INITIALIZATION                                        |
# +--------------------------------------------------------------------------+

ifeq ($(MAKECMDGOALS),debug)
MODE            := debug
endif #MAKECMDGOALS == debug
ifeq ($(MAKECMDGOALS),optimized)
MODE            := optimized
endif #MAKECMDGOALS == optimized
ifeq ($(MAKECMDGOALS),default)
MODE            := default
endif #MAKECMDGOALS == default

ifeq ($(MODE),debug)            # DEBUG
CFLAGS		+= $(DEBUG_CFLAGS)
CXXFLAGS	+= $(DEBUG_CXXFLAGS)
BUILDDIR	:= debug
TARGETDIR	:= debug
DEPDIR		:= debug
else
ifeq ($(MODE),optimized)        # OPTIMIZED
CFLAGS		+= $(OPTIM_CFLAGS)
CXXFLAGS	+= $(OPTIM_CXXFLAGS)
BUILDDIR	:= optimized
TARGETDIR	:= optimized
DEPDIR		:= optimized
endif # MODE == optimized
endif # MODE == debug



# +--------------------------------------------------------------------------+
# |   VERBOSE MODE AND INITIALIZATION                                        |
# +--------------------------------------------------------------------------+

V := 
ifeq ($(V),1) 
	Q := 
else
	Q := @
endif



# +--------------------------------------------------------------------------+
# |   MAIN RULES AND TARGETS                                                 |
# +--------------------------------------------------------------------------+

_TARGETS	:= $(foreach target,$(TARGETS),$(TARGETDIR)/$(target))
_TARGETS	:= $(PRE_GENERIC_TARGETS) $(_TARGETS) $(GENERIC_TARGETS)

all: $(_TARGETS)
debug: $(_TARGETS)
optimized: $(_TARGETS)



# +--------------------------------------------------------------------------+
# |   MACRO DEFINITIONS                                                      |
# +--------------------------------------------------------------------------+

# Function to print smart messages
printCmd	= $(if $(filter $(V),1),,$(shell echo "@echo \"    $(1)\""))

# Get the target variable name
targetName	= $(subst .,_,$(subst /,_,$(1)))

# Specific flags definition
flags            = $(value $(subst /,_,$(subst .,_,$(1)))_FLAGS)
flag            = $(subst /,_,$(subst .,_,$(1)))_FLAGS



# +--------------------------------------------------------------------------+
# |   LOAD AND GENERATE DEPENDENCIES FILES                                   |
# +--------------------------------------------------------------------------+

# Get all the source files in ALLSRC
ALLSRC          := $(foreach target,$(call targetName,$(TARGETS)),$(value \
                        $(target)_SRC))

# Get the dependencies files
DEPENDENCIES    := $(foreach lang,$(LANGUAGES),$(patsubst %.$(lang), %.d, \
                   $(filter %.$(lang),$(ALLSRC))))

# Generate dependencies files for C++ source file
$(DEPDIR)/%.d: %.cpp
	@mkdir -p $(dir $@)
	@$(CXX) $(CXXFLAGS) -MM -MP -MG -MT "\$$(DEPDIR)/$(basename $<).d \
		\$$(BUILDDIR)/$(basename $<).o" -MG "$<" -MF $@

# Load dependencies files 
-include $(foreach dep,$(DEPENDENCIES),$(DEPDIR)/$(dep))



# +--------------------------------------------------------------------------+
# |   PREVENT LOADING RULES IF DEFFILE IS NOT COMPLET                        |
# +--------------------------------------------------------------------------+

else
TARGETS	:= $(empty)

endif # DEFDONE == 1



# +--------------------------------------------------------------------------+
# |   OBJECTS AND TARGETS DEFINITIONS                                        |
# +--------------------------------------------------------------------------+


# Define target variables
defineTarget	= $(call targetName,$(1))_TARGET  := $(TARGETDIR)/$(1)

# Define target object variables
define defineObject
$(1)_OBJ	:= $(foreach obj,$(foreach lang,$(LANGUAGES),$(patsubst \
			%.$(lang),%.o,$(filter %.$(lang),$(value $(1)_SRC)))), \
			$(BUILDDIR)/$(obj)) \
		   $(foreach module,$(value \
			$(1)_MODULES),$(TARGETDIR)/$(module)) \
		   $(foreach obj,$(patsubst %.l,%.l.o,$(filter %.l,$(value \
			$(1)_SRC))),$(BUILDDIR)/$(obj)) \
		   $(foreach obj,$(patsubst %.y,%.yy.o,$(filter %.y,$(value \
			$(1)_SRC))),$(BUILDDIR)/$(obj))
$(1)_CLEAN	:= $(foreach obj,$(patsubst %.y,%.yy.h,$(filter %.y,$(value \
			$(1)_SRC))),$(BUILDDIR)/$(obj))
endef

# Create these definitions
$(foreach target,$(TARGETS),$(eval $(call defineTarget,$(strip $(target)))) \
	$(eval $(call defineObject,$(strip $(call targetName,$(target))))))



# +--------------------------------------------------------------------------+
# |   SMART MESSAGE PRINTING                                                 |
# +--------------------------------------------------------------------------+


# Smart messages
cmd_ar_a_o	= AR                $@
cmd_cc_o_c	= CC                $<
cmd_cxx_o_cpp	= CXX               $<
cmd_yacc_h	= YACC [H]          $<
cmd_yacc_cpp	= YACC [CPP]        $<
cmd_lex_cpp	= LEX               $<
cmd_link	= LINK              $@
cmd_ln_so_o	= LINK [M]          $@
cmd_rm_clean	= RM                *.o
cmd_rm_distclean = RM                $(_TARGETS) *.d $(DEFFILE)



# +--------------------------------------------------------------------------+
# |   TARGET RULES                                                           |
# +--------------------------------------------------------------------------+

TARGET_RULES    := a so bundle

# Archives
define targetDefinition_a
$(value $(1)_TARGET): $(value $(1)_OBJ)
	$$(call printCmd, $$(cmd_ar_a_o))
	$$(Q)$$(AR) $$@ $$^
endef
-include rules.mk

# Plugins (for MacOS X)
define targetDefinition_bundle
$(value $(1)_TARGET): $(value $(1)_OBJ) $(value $(1)_LOADER)
	$$(call printCmd, $$(cmd_ln_so_o))
	$$(Q)$$(LINKER) $$(MODULE_FLAGS) $$(LDFLAGS) -o $$@ $$(value $(1)_OBJ) \
		-bundle -bundle_loader $$(value $(1)_LOADER) \
		$$(value $(1)_LIBS) $$(value $(1)_FLAGS) $$(LIBS)
endef

# Plugins and libaries (for UNIXes)
define targetDefinition_so
$(value $(1)_TARGET): $(value $(1)_OBJ)
	$$(call printCmd, $$(cmd_ln_so_o))
	$$(Q)$$(LINKER) $$(MODULE_FLAGS) $$(LDFLAGS) -o $$@ $$(value $(1)_OBJ) \
		-rdynamic -shared $$(value $(1)_LIBS) $$(value $(1)_FLAGS) \
		$$(LIBS)
endef

rulesTarget	:= $(foreach rules,$(TARGET_RULES),$(filter \
			%.$(rules),$(TARGETS)))
$(foreach target,$(rulesTarget),$(eval $(call targetDefinition_$(subst \
	.,,$(suffix $(target))),$(call targetName,$(target)))))



# +--------------------------------------------------------------------------+
# |   COMPILATION RULES                                                      |
# +--------------------------------------------------------------------------+

# C Files
$(BUILDDIR)/%.o: $(BUILDDIR)/%.c
	$(call printCmd, $(cmd_cc_o_c))
	@mkdir -p $(dir $@)
	$(Q)$(CC) $(CFLAGS) $(call flags,$<) -o $@ -c $<
$(BUILDDIR)/%.o: %.c
	$(call printCmd, $(cmd_cc_o_c))
	@mkdir -p $(dir $@)
	$(Q)$(CC) $(CFLAGS) $(call flags,$<) -o $@ -c $<

# C++ Files
$(BUILDDIR)/%.o: $(BUILDDIR)/%.cpp
	$(call printCmd, $(cmd_cxx_o_cpp))
	@mkdir -p $(dir $@)
	$(Q)$(CXX) $(CXXFLAGS) $(call flags,$<) -o $@ -c $<
$(BUILDDIR)/%.o: %.cpp
	$(call printCmd, $(cmd_cxx_o_cpp))
	@mkdir -p $(dir $@)
	$(Q)$(CXX) $(CXXFLAGS) $(call flags,$<) -o $@ -c $<

# Yacc compilation
$(BUILDDIR)/%.yy.h: %.y
	$(call printCmd, $(cmd_yacc_h))
	@mkdir -p $(dir $@)
	$(Q)$(YACC) $(call flags,$<) -d -b $(basename $(basename $@)) \
		-p $(basename $(notdir $<)) $<
	$(Q)rm $(basename $(basename $@)).tab.c
	$(Q)mv $(basename $(basename $@)).tab.h $(basename $@).h
$(BUILDDIR)/%.yy.cpp: %.y
	$(call printCmd, $(cmd_yacc_cpp))
	@mkdir -p $(dir $@)
	$(Q)$(YACC) $(call flags,$<) -b $(basename $(basename $@)) \
		-p $(basename $(notdir $<)) -o $@ $<

# Lex compilation
$(BUILDDIR)/%.l.cpp: %.l $(BUILDDIR)/%.yy.h
	$(call printCmd, $(cmd_lex_cpp))
	$(Q)$(LEX) $(call flags,$<) -P$(basename $(notdir $<)) -t $< > $@



# +--------------------------------------------------------------------------+
# |   CLEAN RULES                                                            |
# +--------------------------------------------------------------------------+

.PHONY: clean distclean
clean:
	$(call printCmd, $(cmd_rm_clean))
	$(Q)$(RM) $(foreach target,$(TARGETS),$(value $(call \
		targetName,$(target))_OBJ) $(value $(target)_CLEAN))

distclean: clean
	$(call printCmd, $(cmd_rm_distclean))
	$(Q)$(RM) $(foreach dep,$(DEPENDENCIES),$(DEPDIR)/$(dep))
	$(Q)$(RM) $(_TARGETS)
	$(Q)$(RM) $(DEFFILE)



# +--------------------------------------------------------------------------+
# |   GET ALL SUBDIRS TO EXPLORE                                             |
# +--------------------------------------------------------------------------+

# Generate the defs.mk file which contains sub directories
$(DEFFILE): Makefile $(patsubst %, %/module.mk, $(SUBDIRS)) module.mk
	@echo -n "     GEN               $(DEFFILE)"
	@echo "" > $@
	@make -s -C ./ _depsreload

.PHONY: _depsreload
_depsreload:
	@echo -n "."
	@echo "DEFLOADED := 1" > $(DEFFILE)
	@echo "_SUBDIRS := $(SUBDIRS)" >> $(DEFFILE)
	@if [ "$(SUBDIRS)" != "$(_SUBDIRS)" ]; then make -j 1 -s -C ./ _depsreload; \
		else echo "DEFDONE := 1" >> $(DEFFILE); echo ""; fi