1
# Edam's general-purpose makefile v1.11
2
#_______________________________________________________________________________
1
#_______________________________________________________________________________
3
# edam's general-purpose makefile v2.0
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))
47
#_______________________________________________________________________________
50
# set debug mode if profiling
64
CPPFLAGS := $(if $(PROFILEMODE),-pg -D PROFILE) $(CPPFLAGS)
65
CPPFLAGS := $(if $(DEBUGMODE),-g -D DEBUG,-O2) $(CPPFLAGS)
66
ASFLAGS := -f elf $(if $(DEBUGMODE),-g -dDEBUG,-O2) $(ASFLAGS)
67
LDFLAGS := $(if $(PROFILEMODE),-pg,$(if $(DEBUGMODE),,-s)) $(LDFLAGS)
68
LDFLAGS := -Wall $(if $(LINKSTATIC),-static) $(LDFLAGS)
69
LDPOSTFLAGS := $(addprefix -l,$(LIBRARIES)) $(LDPOSTFLAGS)
71
#_______________________________________________________________________________
73
# object debug/profile suffix
74
BUILDSUFFIX := $(if $(PROFILEMODE),_p,$(if $(DEBUGMODE),_d))
77
OBJECTS := $(addsuffix $(BUILDSUFFIX).o,$(basename $(SOURCES)))
78
DEPFILES := $(addsuffix .d,$(basename $(SOURCES)))
82
TARGET := $(basename $(TARGET))$(BUILDSUFFIX)$(suffix $(TARGET))
41
83
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
84
TARGET := $(TARGET)$(if $(MKSHAREDLIB),.so,$(if $(MKSTATICLIB),.a))
87
# Set up dependancy generation build flags and a commands to be executed after
88
# generating any dependancy file. The commands append the names of all the
89
# depended-on files in the dependancy file to the end of the dependancy file as
90
# empty rules with no prerequesits or commands. This causes make not to fail if
91
# one of these files becomes non-existant, but causes files dependant on these
92
# files to be rebuilt (and thus also have their dependancies regenerated).
95
FIXUP_DEPENDANCY_FILES = \
96
sed 's/\#.*//;s/^[^:]*://;s/^[ \t]*//;s/ *\\$$//;/^$$/d;s/$$/:/' < \
97
$(basename $<).d > .$$$$~; cat .$$$$~ >> $(basename $<).d; rm .$$$$~;
98
DEPFLAGS = -MD -MF $(basename $<).d
102
# include dependancies
103
ifneq "$(MAKECMDGOALS)" "clean"
104
ifneq "$(MAKECMDGOALS)" "clean_all"
112
#_______________________________________________________________________________
50
115
.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) *~
125
ifneq "$(MAKECMDGOALS)" "clean_all"
126
@echo "NOT RECURSING: use 'make clean_all' to clean subdirectories as well."
129
rm -f $(OBJECTS) $(TARGET) core
131
clean_all: subdirs clean
74
# makedepend -f- -- $(CPPFLAGS) -- $(SOURCES) > $(DEPFILE)
77
@echo ==== Sub directory: $@
78
@$(MAKE) --no-print-directory -C $@ $(filter-out $(SUBDIRS),$(MAKECMDGOALS))
141
@$(MAKE) -C $@ $(filter-out $(SUBDIRS),$(MAKECMDGOALS))
80
144
$(TARGET): $(OBJECTS)
82
146
$(AR) rcs $(TARGET) $(OBJECTS)
84
$(CXX) $(if $(BUILDSOLIB),-shared) -o $(TARGET) $(LDFLAGS) $(OBJECTS) $(LDLIBS)
148
$(LD) $(if $(MKSHAREDLIB),-shared) -o $(TARGET) $(LDFLAGS) $(OBJECTS) $(LDPOSTFLAGS)
88
# $(CC) -c $(CPPFLAGS) $(CFLAGS)
90
# $(CXX) -c $(CPPFLAGS) $(CXXFLAGS)
152
$(CC) -c $(CPPFLAGS) $(DEPFLAGS) $(CFLAGS) -o $@ $<
153
$(FIXUP_DEPENDANCY_FILES)
155
%.o %_d.o %_p.o: %.cc
156
$(CXX) -c $(CPPFLAGS) $(DEPFLAGS) $(CXXFLAGS) -o $@ $<
157
$(FIXUP_DEPENDANCY_FILES)
159
$(CXX) -c $(CPPFLAGS) $(DEPFLAGS) $(CXXFLAGS) -o $@ $<
160
$(FIXUP_DEPENDANCY_FILES)
161
%.o %_d.o %_p.o: %.cpp
162
$(CXX) -c $(CPPFLAGS) $(DEPFLAGS) $(CXXFLAGS) -o $@ $<
163
$(FIXUP_DEPENDANCY_FILES)
166
$(AS) $(ASFLAGS) -o $@ $<
168
$(AS) $(ASFLAGS) -o $@ $<
169
%.o %_d.o %_p.o: %.asm
170
$(AS) $(ASFLAGS) -o $@ $<
173
$(AS) $(ASFLAGS) -M $< > $@
174
$(FIXUP_DEPENDANCY_FILES)
176
$(AS) $(ASFLAGS) -M $< > $@
177
$(FIXUP_DEPENDANCY_FILES)
179
$(AS) $(ASFLAGS) -M $< > $@
180
$(FIXUP_DEPENDANCY_FILES)
96
182
#_______________________________________________________________________________