/make/edam-mk

To get this branch, use:
bzr branch http://bzr.ed.am/make/edam-mk
11 by edam
- smartened up comments
1
#_______________________________________________________________________________
2
#
14 by edam
- optimised dependency generation: generate stub dependencies from the compiler and don't use the sed script where possible
3
#                       edam's general-purpose makefile                     v2.3
11 by edam
- smartened up comments
4
#_______________________________________________________________________________
5
#                                                                COMMON SETTINGS
3 by edam
- added propper header
6
11 by edam
- smartened up comments
7
#
8
# overridable options
9
# (better to specify in environment/command line)
10
#
8 by edam
- added SUBDIRS for recursing
11
#export DEBUGMODE	:= 1
11 by edam
- smartened up comments
12
#export PROFILEMODE := 1
13
#export LINKSTATIC	:= 1
14
15
#
16
# target binary/library
17
#
18
#MKSTATICLIB	:= 1
19
#MKSHAREDLIB	:= 1
20
TARGET		:= tim
21
22
#
23
# all source files
24
#
9 by edam
- combines sources in to one variable, SOURCES
25
SOURCES		:= tim.cc
3 by edam
- added propper header
26
11 by edam
- smartened up comments
27
#
28
# libraries to link against
29
#
7 by edam
- set CC correctly
30
LIBRARIES	:= 
31
11 by edam
- smartened up comments
32
#
33
# subdirectories to make first
34
#
8 by edam
- added SUBDIRS for recursing
35
SUBDIRS		:= 
3 by edam
- added propper header
36
11 by edam
- smartened up comments
37
#
38
# additional build flags
39
#
9 by edam
- combines sources in to one variable, SOURCES
40
CPPFLAGS	:= 
11 by edam
- smartened up comments
41
CFLAGS		:= 
42
CXXFLAGS	:= 
13 by edam
- added support for D!
43
DFLAGS		:= 
11 by edam
- smartened up comments
44
ASFLAGS		:= 
45
LDFLAGS		:= 
46
LDPOSTFLAGS	:= 
47
48
#_______________________________________________________________________________
49
#                                                                 OTHER SETTINGS
50
51
# set debug mode if profiling
52
ifdef PROFILEMODE
53
export DEBUGMODE := 1
54
endif
55
56
# software
57
CC			:= gcc
58
CXX			:= g++
13 by edam
- added support for D!
59
GDC			:= gdc
60
AS			:= nasm
11 by edam
- smartened up comments
61
LD			:= g++
62
AR			:= ar
63
MAKE		:= make
64
13 by edam
- added support for D!
65
# debug/profile build flags
11 by edam
- smartened up comments
66
CPPFLAGS	:= $(if $(PROFILEMODE),-pg -D PROFILE) $(CPPFLAGS)
12 by edam
- compiler/linker gives all wornings when building in debug
67
CPPFLAGS	:= $(if $(DEBUGMODE),-g -D DEBUG -Wall,-O2) $(CPPFLAGS)
13 by edam
- added support for D!
68
DFLAGS		:= $(if $(DEBUGMODE),,-frelease)
11 by edam
- smartened up comments
69
ASFLAGS		:= -f elf $(if $(DEBUGMODE),-g -dDEBUG,-O2) $(ASFLAGS)
12 by edam
- compiler/linker gives all wornings when building in debug
70
LDFLAGS		:= $(if $(PROFILEMODE),-pg) $(LDFLAGS)
13 by edam
- added support for D!
71
LDFLAGS		:= $(if $(or $(PROFILEMODE), $(DEBUGMODE)),,-Wl,-S) $(LDFLAGS)
72
73
# setup options for shared/static libs
74
CPPFLAGS	:= $(if $(MKSHAREDLIB),-fPIC) $(CPPFLAGS)
12 by edam
- compiler/linker gives all wornings when building in debug
75
LDFLAGS		:= $(if $(LINKSTATIC),-static) $(LDFLAGS)
13 by edam
- added support for D!
76
77
# add libraries for d
78
LIBRARIES	:= $(LIBRARIES) $(if $(filter %.d, $(SOURCES)), gphobos pthread m)
79
80
# build flags for libraries
11 by edam
- smartened up comments
81
LDPOSTFLAGS := $(addprefix -l,$(LIBRARIES)) $(LDPOSTFLAGS)
82
83
#_______________________________________________________________________________
12 by edam
- compiler/linker gives all wornings when building in debug
84
#
11 by edam
- smartened up comments
85
86
# object debug/profile suffix
87
BUILDSUFFIX	:= $(if $(PROFILEMODE),_p,$(if $(DEBUGMODE),_d))
88
13 by edam
- added support for D!
89
# work out object and dependency files
11 by edam
- smartened up comments
90
OBJECTS		:= $(addsuffix $(BUILDSUFFIX).o,$(basename $(SOURCES)))
12 by edam
- compiler/linker gives all wornings when building in debug
91
DEPFILES	:= $(addsuffix .dep,$(basename $(SOURCES)))
11 by edam
- smartened up comments
92
13 by edam
- added support for D!
93
# fixup target name
11 by edam
- smartened up comments
94
ifdef TARGET
13 by edam
- added support for D!
95
TARGET		:= $(basename $(TARGET))$(BUILDSUFFIX)$(suffix $(TARGET))
96
TARGET		:= $(patsubst %.so,%,$(patsubst %.a,%,$(TARGET)))
12 by edam
- compiler/linker gives all wornings when building in debug
97
ifneq ($(strip $(MKSHAREDLIB) $(MKSTATICLIB)),)
13 by edam
- added support for D!
98
TARGET		:= $(TARGET)$(if $(MKSHAREDLIB),.so,$(if $(MKSTATICLIB),.a))
99
TARGET		:= lib$(patsubst lib%,%,$(TARGET))
12 by edam
- compiler/linker gives all wornings when building in debug
100
endif
11 by edam
- smartened up comments
101
endif
102
14 by edam
- optimised dependency generation: generate stub dependencies from the compiler and don't use the sed script where possible
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
11 by edam
- smartened up comments
107
# empty rules with no prerequesits or commands. This causes make not to fail if
14 by edam
- optimised dependency generation: generate stub dependencies from the compiler and don't use the sed script where possible
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).
11 by edam
- smartened up comments
110
ifdef DEBUGMODE
111
ifndef PROFILEMODE
14 by edam
- optimised dependency generation: generate stub dependencies from the compiler and don't use the sed script where possible
112
FIXUP_DEPENDENCY_FILES = \
11 by edam
- smartened up comments
113
	sed 's/\#.*//;s/^[^:]*://;s/^[ \t]*//;s/ *\\$$//;/^$$/d;s/$$/:/' < \
12 by edam
- compiler/linker gives all wornings when building in debug
114
	$(basename $<).dep > .$$$$~; cat .$$$$~ >> $(basename $<).dep; rm .$$$$~;
14 by edam
- optimised dependency generation: generate stub dependencies from the compiler and don't use the sed script where possible
115
DEPFLAGS	= -MMD -MP -MF $(basename $<).dep
11 by edam
- smartened up comments
116
endif
117
endif
118
14 by edam
- optimised dependency generation: generate stub dependencies from the compiler and don't use the sed script where possible
119
# include dependencies
11 by edam
- smartened up comments
120
ifneq "$(MAKECMDGOALS)" "clean"
121
ifneq "$(MAKECMDGOALS)" "clean_all"
122
-include $(DEPFILES)
123
endif
124
endif
125
126
# default rule
127
.DEFAULT_GOAL := all
128
129
#_______________________________________________________________________________
130
#                                                                          RULES
10 by edam
- fixed recursive cleaning
131
9 by edam
- combines sources in to one variable, SOURCES
132
.PHONY:	all subdirs target clean clean_all run depend dep $(SUBDIRS)
133
134
all: subdirs target
135
136
subdirs: $(SUBDIRS)
137
138
target: $(TARGET)
1 by edam
initial makefile
139
11 by edam
- smartened up comments
140
clean:
141
ifdef SUBDIRS
142
ifneq "$(MAKECMDGOALS)" "clean_all"
143
	@echo "NOT RECURSING: use 'make clean_all' to clean subdirectories as well."
144
endif
145
endif
13 by edam
- added support for D!
146
	rm -f $(OBJECTS) $(TARGET) *.dep core
11 by edam
- smartened up comments
147
148
clean_all: subdirs clean
149
150
ifndef MKSTATICLIB
151
ifndef MKSHAREDLIB
9 by edam
- combines sources in to one variable, SOURCES
152
run: target
153
	./$(TARGET)
154
endif
155
endif
4 by edam
- replaced debug option with debug switch
156
8 by edam
- added SUBDIRS for recursing
157
$(SUBDIRS):
12 by edam
- compiler/linker gives all wornings when building in debug
158
	@if [ "$@" = "$(firstword $(SUBDIRS))" ]; then echo; fi
11 by edam
- smartened up comments
159
	@$(MAKE) -C $@ $(filter-out $(SUBDIRS),$(MAKECMDGOALS))
160
	@echo
8 by edam
- added SUBDIRS for recursing
161
9 by edam
- combines sources in to one variable, SOURCES
162
$(TARGET): $(OBJECTS)
11 by edam
- smartened up comments
163
ifdef MKSTATICLIB
9 by edam
- combines sources in to one variable, SOURCES
164
	$(AR) rcs $(TARGET) $(OBJECTS)
8 by edam
- added SUBDIRS for recursing
165
else
11 by edam
- smartened up comments
166
	$(LD) $(if $(MKSHAREDLIB),-shared) -o $(TARGET) $(LDFLAGS) $(OBJECTS) $(LDPOSTFLAGS) 
8 by edam
- added SUBDIRS for recursing
167
endif
168
11 by edam
- smartened up comments
169
%.o %_d.o %_p.o: %.c
170
	$(CC) -c $(CPPFLAGS) $(DEPFLAGS) $(CFLAGS) -o $@ $<
171
172
%.o %_d.o %_p.o: %.cc
173
	$(CXX) -c $(CPPFLAGS) $(DEPFLAGS) $(CXXFLAGS) -o $@ $<
174
%.o %_d.o %_p.o: %.C
175
	$(CXX) -c $(CPPFLAGS) $(DEPFLAGS) $(CXXFLAGS) -o $@ $<
176
%.o %_d.o %_p.o: %.cpp
177
	$(CXX) -c $(CPPFLAGS) $(DEPFLAGS) $(CXXFLAGS) -o $@ $<
178
13 by edam
- added support for D!
179
%.o %_d.o %_p.o: %.d
180
	$(GDC) -c $(CPPFLAGS) $(DFLAGS) -o $@ $<
181
11 by edam
- smartened up comments
182
%.o %_d.o %_p.o: %.s
183
	$(AS) $(ASFLAGS) -o $@ $<
14 by edam
- optimised dependency generation: generate stub dependencies from the compiler and don't use the sed script where possible
184
ifdef DEBUGMODE
185
	$(AS) $(ASFLAGS) -M $< > $(basename $<).dep
186
	$(FIXUP_DEPENDENCY_FILES)
187
endif
11 by edam
- smartened up comments
188
%.o %_d.o %_p.o: %.S
189
	$(AS) $(ASFLAGS) -o $@ $<
14 by edam
- optimised dependency generation: generate stub dependencies from the compiler and don't use the sed script where possible
190
ifdef DEBUGMODE
191
	$(AS) $(ASFLAGS) -M $< > $(basename $<).dep
192
	$(FIXUP_DEPENDENCY_FILES)
193
endif
11 by edam
- smartened up comments
194
%.o %_d.o %_p.o: %.asm
195
	$(AS) $(ASFLAGS) -o $@ $<
14 by edam
- optimised dependency generation: generate stub dependencies from the compiler and don't use the sed script where possible
196
ifdef DEBUGMODE
197
	$(AS) $(ASFLAGS) -M $< > $(basename $<).dep
198
	$(FIXUP_DEPENDENCY_FILES)
199
endif
8 by edam
- added SUBDIRS for recursing
200
9 by edam
- combines sources in to one variable, SOURCES
201
#_______________________________________________________________________________