1
#_______________________________________________________________________________
3
# edam's general-purpose makefile v2.2
4
#_______________________________________________________________________________
9
# (better to specify in environment/command line)
11
#export DEBUGMODE := 1
12
#export PROFILEMODE := 1
13
#export LINKSTATIC := 1
16
# target binary/library
28
# libraries to link against
33
# subdirectories to make first
38
# additional build flags
48
#_______________________________________________________________________________
51
# set debug mode if profiling
65
# debug/profile build flags
66
CPPFLAGS := $(if $(PROFILEMODE),-pg -D PROFILE) $(CPPFLAGS)
67
CPPFLAGS := $(if $(DEBUGMODE),-g -D DEBUG -Wall,-O2) $(CPPFLAGS)
68
DFLAGS := $(if $(DEBUGMODE),,-frelease)
69
ASFLAGS := -f elf $(if $(DEBUGMODE),-g -dDEBUG,-O2) $(ASFLAGS)
70
LDFLAGS := $(if $(PROFILEMODE),-pg) $(LDFLAGS)
71
LDFLAGS := $(if $(or $(PROFILEMODE), $(DEBUGMODE)),,-Wl,-S) $(LDFLAGS)
73
# setup options for shared/static libs
74
CPPFLAGS := $(if $(MKSHAREDLIB),-fPIC) $(CPPFLAGS)
75
LDFLAGS := $(if $(LINKSTATIC),-static) $(LDFLAGS)
78
LIBRARIES := $(LIBRARIES) $(if $(filter %.d, $(SOURCES)), gphobos pthread m)
80
# build flags for libraries
81
LDPOSTFLAGS := $(addprefix -l,$(LIBRARIES)) $(LDPOSTFLAGS)
83
#_______________________________________________________________________________
86
# object debug/profile suffix
87
BUILDSUFFIX := $(if $(PROFILEMODE),_p,$(if $(DEBUGMODE),_d))
89
# work out object and dependency files
90
OBJECTS := $(addsuffix $(BUILDSUFFIX).o,$(basename $(SOURCES)))
91
DEPFILES := $(addsuffix .dep,$(basename $(SOURCES)))
95
TARGET := $(basename $(TARGET))$(BUILDSUFFIX)$(suffix $(TARGET))
96
TARGET := $(patsubst %.so,%,$(patsubst %.a,%,$(TARGET)))
97
ifneq ($(strip $(MKSHAREDLIB) $(MKSTATICLIB)),)
98
TARGET := $(TARGET)$(if $(MKSHAREDLIB),.so,$(if $(MKSTATICLIB),.a))
99
TARGET := lib$(patsubst lib%,%,$(TARGET))
103
# Set up dependancy generation build flags and a commands to be executed after
104
# generating any dependancy file. The commands append the names of all the
105
# depended-on files in the dependancy file to the end of the dependancy file as
106
# empty rules with no prerequesits or commands. This causes make not to fail if
107
# one of these files becomes non-existant, but causes files dependant on these
108
# files to be rebuilt (and thus also have their dependancies regenerated).
111
FIXUP_DEPENDANCY_FILES = \
112
sed 's/\#.*//;s/^[^:]*://;s/^[ \t]*//;s/ *\\$$//;/^$$/d;s/$$/:/' < \
113
$(basename $<).dep > .$$$$~; cat .$$$$~ >> $(basename $<).dep; rm .$$$$~;
114
DEPFLAGS = -MD -MF $(basename $<).dep
118
# include dependancies
119
ifneq "$(MAKECMDGOALS)" "clean"
120
ifneq "$(MAKECMDGOALS)" "clean_all"
128
#_______________________________________________________________________________
131
.PHONY: all subdirs target clean clean_all run depend dep $(SUBDIRS)
2
#___________________________________________________________________________
20
CPPFLAGS = $(DEBUG) -O2
21
ASFLAGS = $(DEBUG) -f elf
22
LDFLAGS = $(DEBUG) -Wall -s -nostartfiles
24
#___________________________________________________________________________
32
_SRCS = $(CC_SRCS) $(C_SRCS) $(S_SRCS)
33
_OBJS = $(addsuffix .o,$(basename $(_SRCS)))
34
LDLIBS = $(addprefix -l,$(LIBRARIES))
38
gcc -o $(TARGET) $(LDFLAGS) $(_OBJS) $(LDLIBS)
40
#___________________________________________________________________________
43
.PHONY: all clean depend dep
45
all: clean depend $(TARGET)
141
ifneq "$(MAKECMDGOALS)" "clean_all"
142
@echo "NOT RECURSING: use 'make clean_all' to clean subdirectories as well."
145
rm -f $(OBJECTS) $(TARGET) *.dep core
147
clean_all: subdirs clean
157
@if [ "$@" = "$(firstword $(SUBDIRS))" ]; then echo; fi
158
@$(MAKE) -C $@ $(filter-out $(SUBDIRS),$(MAKECMDGOALS))
161
$(TARGET): $(OBJECTS)
163
$(AR) rcs $(TARGET) $(OBJECTS)
165
$(LD) $(if $(MKSHAREDLIB),-shared) -o $(TARGET) $(LDFLAGS) $(OBJECTS) $(LDPOSTFLAGS)
169
$(CC) -c $(CPPFLAGS) $(DEPFLAGS) $(CFLAGS) -o $@ $<
170
$(FIXUP_DEPENDANCY_FILES)
172
%.o %_d.o %_p.o: %.cc
173
$(CXX) -c $(CPPFLAGS) $(DEPFLAGS) $(CXXFLAGS) -o $@ $<
174
$(FIXUP_DEPENDANCY_FILES)
176
$(CXX) -c $(CPPFLAGS) $(DEPFLAGS) $(CXXFLAGS) -o $@ $<
177
$(FIXUP_DEPENDANCY_FILES)
178
%.o %_d.o %_p.o: %.cpp
179
$(CXX) -c $(CPPFLAGS) $(DEPFLAGS) $(CXXFLAGS) -o $@ $<
180
$(FIXUP_DEPENDANCY_FILES)
183
$(GDC) -c $(CPPFLAGS) $(DFLAGS) -o $@ $<
186
$(AS) $(ASFLAGS) -o $@ $<
188
$(AS) $(ASFLAGS) -o $@ $<
189
%.o %_d.o %_p.o: %.asm
190
$(AS) $(ASFLAGS) -o $@ $<
193
$(AS) $(ASFLAGS) -M $< > $@
194
$(FIXUP_DEPENDANCY_FILES)
196
$(AS) $(ASFLAGS) -M $< > $@
197
$(FIXUP_DEPENDANCY_FILES)
199
$(AS) $(ASFLAGS) -M $< > $@
200
$(FIXUP_DEPENDANCY_FILES)
202
#_______________________________________________________________________________
48
rm -f core $(DEPFILE) $(_OBJS)
52
# makedepend -f- -- $(CPPFLAGS) -- $(_SRCS) > $(DEPFILE)
54
#___________________________________________________________________________