/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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#_______________________________________________________________________________
#
#                       edam's general-purpose makefile                     v2.3
#_______________________________________________________________________________
#                                                                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	:= 
DFLAGS		:= 
ASFLAGS		:= 
LDFLAGS		:= 
LDPOSTFLAGS	:= 

#_______________________________________________________________________________
#                                                                 OTHER SETTINGS

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

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

# debug/profile build flags
CPPFLAGS	:= $(if $(PROFILEMODE),-pg -D PROFILE) $(CPPFLAGS)
CPPFLAGS	:= $(if $(DEBUGMODE),-g -D DEBUG -Wall,-O2) $(CPPFLAGS)
DFLAGS		:= $(if $(DEBUGMODE),,-frelease)
ASFLAGS		:= -f elf $(if $(DEBUGMODE),-g -dDEBUG,-O2) $(ASFLAGS)
LDFLAGS		:= $(if $(PROFILEMODE),-pg) $(LDFLAGS)
LDFLAGS		:= $(if $(or $(PROFILEMODE), $(DEBUGMODE)),,-Wl,-S) $(LDFLAGS)

# setup options for shared/static libs
CPPFLAGS	:= $(if $(MKSHAREDLIB),-fPIC) $(CPPFLAGS)
LDFLAGS		:= $(if $(LINKSTATIC),-static) $(LDFLAGS)

# add libraries for d
LIBRARIES	:= $(LIBRARIES) $(if $(filter %.d, $(SOURCES)), gphobos pthread m)

# build flags for libraries
LDPOSTFLAGS := $(addprefix -l,$(LIBRARIES)) $(LDPOSTFLAGS)

#_______________________________________________________________________________
#

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

# work out object and dependency files
OBJECTS		:= $(addsuffix $(BUILDSUFFIX).o,$(basename $(SOURCES)))
DEPFILES	:= $(addsuffix .dep,$(basename $(SOURCES)))

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

# Set up dependency generation build flags and, for those languages where the
# the compiler/assembler doesn't support dependency generation, commands to be
# executed after generating any dependency file. The commands append the names
# of all the depended-on files in the dependency file to the end of the 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 dependent on these
# files to be rebuilt (and thus also have their dependencies regenerated).
ifdef DEBUGMODE
ifndef PROFILEMODE
FIXUP_DEPENDENCY_FILES = \
	sed 's/\#.*//;s/^[^:]*://;s/^[ \t]*//;s/ *\\$$//;/^$$/d;s/$$/:/' < \
	$(basename $<).dep > .$$$$~; cat .$$$$~ >> $(basename $<).dep; rm .$$$$~;
DEPFLAGS	= -MMD -MP -MF $(basename $<).dep
endif
endif

# include dependencies
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) *.dep core

clean_all: subdirs clean

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

$(SUBDIRS):
	@if [ "$@" = "$(firstword $(SUBDIRS))" ]; then echo; fi
	@$(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 $@ $<

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

%.o %_d.o %_p.o: %.d
	$(GDC) -c $(CPPFLAGS) $(DFLAGS) -o $@ $<

%.o %_d.o %_p.o: %.s
	$(AS) $(ASFLAGS) -o $@ $<
ifdef DEBUGMODE
	$(AS) $(ASFLAGS) -M $< > $(basename $<).dep
	$(FIXUP_DEPENDENCY_FILES)
endif
%.o %_d.o %_p.o: %.S
	$(AS) $(ASFLAGS) -o $@ $<
ifdef DEBUGMODE
	$(AS) $(ASFLAGS) -M $< > $(basename $<).dep
	$(FIXUP_DEPENDENCY_FILES)
endif
%.o %_d.o %_p.o: %.asm
	$(AS) $(ASFLAGS) -o $@ $<
ifdef DEBUGMODE
	$(AS) $(ASFLAGS) -M $< > $(basename $<).dep
	$(FIXUP_DEPENDENCY_FILES)
endif

#_______________________________________________________________________________