1
# Edam's general-purpose makefile v1.11
2
#_______________________________________________________________________________
1
#_______________________________________________________________________________
3
# edam's general-purpose makefile v2.4
4
#_______________________________________________________________________________
6
# (better to specify these in environment/command line)
9
# (better to specify in environment/command line)
7
11
#export DEBUGMODE := 1
8
#export STATICLIBS := 1
10
# Target binary/library
12
#export PROFILEMODE := 1
13
#export LINKSTATIC := 1
16
# target binary/library
28
# libraries to link against
21
# Subdirectories to make
33
# subdirectories to make first
38
# additional build flags
31
CFLAGS := $(if $(DEBUGMODE),-g -D DEBUG,-O2)
32
CXXFLAGS := $(if $(DEBUGMODE),-g -D DEBUG,-O2)
33
ASFLAGS := -f elf $(if $(DEBUGMODE),-g -dDEBUG,-O2)
34
LDFLAGS := -Wall $(if $(DEBUGMODE),,-s) $(if $(STATICLIBS), -static)
36
#_______________________________________________________________________________
39
OBJECTS := $(addsuffix .o,$(basename $(SOURCES)))
40
LDLIBS := $(addprefix -l,$(LIBRARIES))
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,-D NDEBUG -O2) $(CPPFLAGS)
68
DFLAGS := $(if $(DEBUGMODE),,-frelease)
69
ASFLAGS := -f elf $(if $(DEBUGMODE),-g -dDEBUG,-dNDEBUG -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))
41
96
TARGET := $(patsubst %.so,%,$(patsubst %.a,%,$(TARGET)))
42
TARGET := $(TARGET)$(if $(BUILDALIB),.a,)$(if $(BUILDSOLIB),.so,)
44
# Turn clean_all in to a recursive clean
45
ifeq "$(MAKECMDGOALS)" "clean_all"
46
export RECURSIVE_CLEAN := 1
97
ifneq ($(strip $(MKSHAREDLIB) $(MKSTATICLIB)),)
98
TARGET := $(TARGET)$(if $(MKSHAREDLIB),.so,$(if $(MKSTATICLIB),.a))
99
TARGET := lib$(patsubst lib%,%,$(TARGET))
103
# Set up dependency generation build flags and, for those languages where the
104
# the compiler/assembler doesn't support dependency generation, commands to be
105
# executed after generating any dependency file. The commands append the names
106
# of all the depended-on files in the dependency file to the end of the file as
107
# empty rules with no prerequesits or commands. This causes make not to fail if
108
# one of these files becomes non-existant, but causes files dependent on these
109
# files to be rebuilt (and thus also have their dependencies regenerated).
112
FIXUP_DEPENDENCY_FILES = \
113
sed 's/\#.*//;s/^[^:]*://;s/^[ \t]*//;s/ *\\$$//;/^$$/d;s/$$/:/' < \
114
$(basename $<).dep > .$$$$~; cat .$$$$~ >> $(basename $<).dep; rm .$$$$~;
115
DEPFLAGS = -MMD -MP -MF $(basename $<).dep
119
# include dependencies
120
ifneq "$(MAKECMDGOALS)" "clean"
121
ifneq "$(MAKECMDGOALS)" "clean_all"
129
#_______________________________________________________________________________
50
132
.PHONY: all subdirs target clean clean_all run depend dep $(SUBDIRS)
59
clean clean_all: subdirs
62
@echo "NOT RECURSING: use 'make clean_all' to clean subdirectories as well."
64
rm -f $(OBJECTS) $(TARGET) core $(DEPFILE) *~
142
ifneq "$(MAKECMDGOALS)" "clean_all"
143
@echo "NOT RECURSING: use 'make clean_all' to clean subdirectories as well."
146
rm -f $(OBJECTS) $(TARGET) *.dep core
148
clean_all: subdirs clean
74
# makedepend -f- -- $(CPPFLAGS) -- $(SOURCES) > $(DEPFILE)
77
@echo ==== Sub directory: $@
78
@$(MAKE) --no-print-directory -C $@ $(filter-out $(SUBDIRS),$(MAKECMDGOALS))
158
@if [ "$@" = "$(firstword $(SUBDIRS))" ]; then echo; fi
159
@$(MAKE) -C $@ $(filter-out $(SUBDIRS),$(MAKECMDGOALS))
80
162
$(TARGET): $(OBJECTS)
82
164
$(AR) rcs $(TARGET) $(OBJECTS)
84
$(CXX) $(if $(BUILDSOLIB),-shared) -o $(TARGET) $(LDFLAGS) $(OBJECTS) $(LDLIBS)
88
# $(CC) -c $(CPPFLAGS) $(CFLAGS)
90
# $(CXX) -c $(CPPFLAGS) $(CXXFLAGS)
166
$(LD) $(if $(MKSHAREDLIB),-shared) -o $(TARGET) $(LDFLAGS) $(OBJECTS) $(LDPOSTFLAGS)
170
$(CC) -c $(CPPFLAGS) $(DEPFLAGS) $(CFLAGS) -o $@ $<
172
%.o %_d.o %_p.o: %.cc
173
$(CXX) -c $(CPPFLAGS) $(DEPFLAGS) $(CXXFLAGS) -o $@ $<
175
$(CXX) -c $(CPPFLAGS) $(DEPFLAGS) $(CXXFLAGS) -o $@ $<
176
%.o %_d.o %_p.o: %.cpp
177
$(CXX) -c $(CPPFLAGS) $(DEPFLAGS) $(CXXFLAGS) -o $@ $<
180
$(GDC) -c $(CPPFLAGS) $(DFLAGS) -o $@ $<
183
$(AS) $(ASFLAGS) -o $@ $<
185
$(AS) $(ASFLAGS) -M $< > $(basename $<).dep
186
$(FIXUP_DEPENDENCY_FILES)
189
$(AS) $(ASFLAGS) -o $@ $<
191
$(AS) $(ASFLAGS) -M $< > $(basename $<).dep
192
$(FIXUP_DEPENDENCY_FILES)
194
%.o %_d.o %_p.o: %.asm
195
$(AS) $(ASFLAGS) -o $@ $<
197
$(AS) $(ASFLAGS) -M $< > $(basename $<).dep
198
$(FIXUP_DEPENDENCY_FILES)
96
201
#_______________________________________________________________________________