/make/edam-mk

To get this branch, use:
bzr branch http://bzr.ed.am/make/edam-mk
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
#_______________________________________________________________________________
#
#					edam's general-purpose makefile v2.0
#_______________________________________________________________________________
#                                                                COMMON SETTINGS

#
# overridable options
# (better to specify in environment/command line)
#
#export DEBUGMODE	:= 1
#export PROFILEMODE := 1
#export LINKSTATIC	:= 1

#
# target binary/library
#
#MKSTATICLIB	:= 1
#MKSHAREDLIB	:= 1
TARGET		:= tim

#
# all source files
#
SOURCES		:= tim.cc

#
# libraries to link against
#
LIBRARIES	:= 

#
# subdirectories to make first
#
SUBDIRS		:= 

#
# additional build flags
#
CPPFLAGS	:= 
CFLAGS		:= 
CXXFLAGS	:= 
ASFLAGS		:= 
LDFLAGS		:= 
LDPOSTFLAGS	:= 

#_______________________________________________________________________________
#                                                                 OTHER SETTINGS

# set debug mode if profiling
ifdef PROFILEMODE
export DEBUGMODE := 1
endif

# software
AS			:= nasm
CC			:= gcc
CXX			:= g++
LD			:= g++
AR			:= ar
MAKE		:= make

# build flags
CPPFLAGS	:= $(if $(PROFILEMODE),-pg -D PROFILE) $(CPPFLAGS)
CPPFLAGS	:= $(if $(DEBUGMODE),-g -D DEBUG,-O2) $(CPPFLAGS)
ASFLAGS		:= -f elf $(if $(DEBUGMODE),-g -dDEBUG,-O2) $(ASFLAGS)
LDFLAGS		:= $(if $(PROFILEMODE),-pg,$(if $(DEBUGMODE),,-s)) $(LDFLAGS)
LDFLAGS		:= -Wall $(if $(LINKSTATIC),-static) $(LDFLAGS)
LDPOSTFLAGS := $(addprefix -l,$(LIBRARIES)) $(LDPOSTFLAGS)

#_______________________________________________________________________________

# object debug/profile suffix
BUILDSUFFIX	:= $(if $(PROFILEMODE),_p,$(if $(DEBUGMODE),_d))

# files
OBJECTS		:= $(addsuffix $(BUILDSUFFIX).o,$(basename $(SOURCES)))
DEPFILES	:= $(addsuffix .d,$(basename $(SOURCES)))

# fixup target
ifdef TARGET
TARGET		:= $(basename $(TARGET))$(BUILDSUFFIX)$(suffix $(TARGET))
TARGET		:= $(patsubst %.so,%,$(patsubst %.a,%,$(TARGET)))
TARGET		:= $(TARGET)$(if $(MKSHAREDLIB),.so,$(if $(MKSTATICLIB),.a))
endif

# Set up dependancy generation build flags and a commands to be executed after
# generating any dependancy file. The commands append the names of all the
# depended-on files in the dependancy file to the end of the dependancy file as
# empty rules with no prerequesits or commands. This causes make not to fail if
# one of these files becomes non-existant, but causes files dependant on these
# files to be rebuilt (and thus also have their dependancies regenerated).
ifdef DEBUGMODE
ifndef PROFILEMODE
FIXUP_DEPENDANCY_FILES = \
	sed 's/\#.*//;s/^[^:]*://;s/^[ \t]*//;s/ *\\$$//;/^$$/d;s/$$/:/' < \
		$(basename $<).d > .$$$$~; cat .$$$$~ >> $(basename $<).d; rm .$$$$~;
DEPFLAGS	= -MD -MF $(basename $<).d
endif
endif

# include dependancies
ifneq "$(MAKECMDGOALS)" "clean"
ifneq "$(MAKECMDGOALS)" "clean_all"
-include $(DEPFILES)
endif
endif

# default rule
.DEFAULT_GOAL := all

#_______________________________________________________________________________
#                                                                          RULES

.PHONY:	all subdirs target clean clean_all run depend dep $(SUBDIRS)

all: subdirs target

subdirs: $(SUBDIRS)

target: $(TARGET)

clean:
ifdef SUBDIRS
ifneq "$(MAKECMDGOALS)" "clean_all"
	@echo "NOT RECURSING: use 'make clean_all' to clean subdirectories as well."
endif
endif
	rm -f $(OBJECTS) $(TARGET) core

clean_all: subdirs clean

ifndef MKSTATICLIB
ifndef MKSHAREDLIB
run: target
	./$(TARGET)
endif
endif

$(SUBDIRS):
	@$(MAKE) -C $@ $(filter-out $(SUBDIRS),$(MAKECMDGOALS))
	@echo

$(TARGET): $(OBJECTS)
ifdef MKSTATICLIB
	$(AR) rcs $(TARGET) $(OBJECTS)
else
	$(LD) $(if $(MKSHAREDLIB),-shared) -o $(TARGET) $(LDFLAGS) $(OBJECTS) $(LDPOSTFLAGS) 
endif

%.o %_d.o %_p.o: %.c
	$(CC) -c $(CPPFLAGS) $(DEPFLAGS) $(CFLAGS) -o $@ $<
	$(FIXUP_DEPENDANCY_FILES)

%.o %_d.o %_p.o: %.cc
	$(CXX) -c $(CPPFLAGS) $(DEPFLAGS) $(CXXFLAGS) -o $@ $<
	$(FIXUP_DEPENDANCY_FILES)
%.o %_d.o %_p.o: %.C
	$(CXX) -c $(CPPFLAGS) $(DEPFLAGS) $(CXXFLAGS) -o $@ $<
	$(FIXUP_DEPENDANCY_FILES)
%.o %_d.o %_p.o: %.cpp
	$(CXX) -c $(CPPFLAGS) $(DEPFLAGS) $(CXXFLAGS) -o $@ $<
	$(FIXUP_DEPENDANCY_FILES)

%.o %_d.o %_p.o: %.s
	$(AS) $(ASFLAGS) -o $@ $<
%.o %_d.o %_p.o: %.S
	$(AS) $(ASFLAGS) -o $@ $<
%.o %_d.o %_p.o: %.asm
	$(AS) $(ASFLAGS) -o $@ $<

%.d: %.s
	$(AS) $(ASFLAGS) -M $< > $@
	$(FIXUP_DEPENDANCY_FILES)
%.d: %.S
	$(AS) $(ASFLAGS) -M $< > $@
	$(FIXUP_DEPENDANCY_FILES)
%.d: %.asm
	$(AS) $(ASFLAGS) -M $< > $@
	$(FIXUP_DEPENDANCY_FILES)

#_______________________________________________________________________________