/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
#
16 by edam
- renamed to edam.mk; makefile should now be included rather than duplicated and modified
3
#                       edam's general-purpose makefile
4
#_______________________________________________________________________________
20 by edam
- a file names 'subdir.mk' in subdirectories mentioned in the SUBDIRS variable is now explicitly used during the making the subdirectory, if it is present
5
#                                                                    version 3.4
18 by edam
- added subprojects via SUBPROJS variable
6
#
19 by edam
- added NOLIBPROFIX switch
7
# Copyright (c) 2009 Tim Marston <edam@waxworlds.org>.
16 by edam
- renamed to edam.mk; makefile should now be included rather than duplicated and modified
8
#
9
# Permission is hereby granted, free of charge, to any person obtaining a copy
10
# of this software and associated documentation files (the "Software"), to deal
11
# in the Software without restriction, including without limitation the rights
12
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
# copies of the Software, and to permit persons to whom the Software is
14
# furnished to do so, subject to the following conditions:
15
#
16
# The above copyright notice and this permission notice shall be included in
17
# all copies or substantial portions of the Software.
18
#
19
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
# THE SOFTWARE.
18 by edam
- added subprojects via SUBPROJS variable
26
#
16 by edam
- renamed to edam.mk; makefile should now be included rather than duplicated and modified
27
#_______________________________________________________________________________
28
#
29
#
30
# This is a general-purpose makefile for use with GNU make. It can be downloaded
31
# from http://www.waxworlds.org/edam/software/general-purpose-makefile where you
20 by edam
- a file names 'subdir.mk' in subdirectories mentioned in the SUBDIRS variable is now explicitly used during the making the subdirectory, if it is present
32
# can also find more information and documentation on it's use. The following
33
# text can only really be considered a reference to it's use.
16 by edam
- renamed to edam.mk; makefile should now be included rather than duplicated and modified
34
#
35
# To use this makefile, put a file named "Makefile" in your project directory.
36
# Add all your project's settings to your Makefile and then include this file
37
# from it. For example, your Makefile might look something like this:
38
#
39
#       TARGET = my_program
40
#       SOURCES = main.cc foo.cc
41
#       LIBRARIES = bar
42
#       include ~/src/edam.mk
43
#
44
# A complete list of all the settings you can use in your Makefile follows. It
45
# should be noted, though, that some settings are better defined in the shell's
46
# environment and/or specified on the make command line than hard-coded straight
47
# in to the Makefile. For example, the "DEBUGMODE" is an ideal candidate for
48
# exporting from your shell:
49
#       export DEBUGMODE=1
50
# and overridding on the command line when necessary:
51
#       make DEBUGMODE=
52
#
53
# It should also be noted that boolean parameters should either be undefined (or
54
# defined as an empty string) for "off", and defined as "1" for "on"; as in the
55
# above example with setting DEBUGMODE.
56
#
57
# Here is a list of all configuration parameters:
58
#
59
# DEBUGMODE    Boolean. Build a debugable version of the target suitable for
60
#              debugging with gdb. It's probably better to set this from the
61
#              command line or the shell's environment than to hard-code it.
62
# PROFILEMODE  Boolean. When set, DEBUGMODE is also implied. Build a profiling
63
#              version of the target, for use with gprof. It's probably better
64
#              to set this from the command line or the shell's environment
65
#              than to hard-code it.
66
#
67
# LINKSTATIC   Boolean. Set to build a target that staticly links against all
68
#              its libraries and doesn't use shared libraries.
69
#
70
# MKSTATICLIB  Boolean. Target type: Set to build a static library target. If
71
#              neither this nor MKSHAREDLIB are set, the target defaults to a
72
#              binary executable.
73
# MKSHAREDLIB  Boolean. Target type: Set to build a shared library target. If
74
#              neither this nor MKSTATICLIB are set, the target defaults to a
75
#              binary executable.
19 by edam
- added NOLIBPROFIX switch
76
# NOLIBPREFIX  Boolean. When building a static or shared library, do not ensure
77
#              that the target's name is prefixed with "lib".
16 by edam
- renamed to edam.mk; makefile should now be included rather than duplicated and modified
78
#
79
# TARGET       The name of the target file.
80
#
81
# SOURCES      A list of all source files of whatever language. The language
82
#              type is determined by the file extension.
83
#
84
# LIBRARIES    A list of libraries to link against. Don't include the 'lib'
85
#              prefix.
86
#
87
# SUBDIRS      A list of subdirectories to build before attempting to build the
88
#              target. These subdirectories are also included in a clean_all.
21 by edam
- changed name of makefile used in subdirs to emake.mk
89
#              Note that if the file 'emake.mk' exists in a subdirectory, it
20 by edam
- a file names 'subdir.mk' in subdirectories mentioned in the SUBDIRS variable is now explicitly used during the making the subdirectory, if it is present
90
#              will be used explicitly, rather than any default makefile.
16 by edam
- renamed to edam.mk; makefile should now be included rather than duplicated and modified
91
#
18 by edam
- added subprojects via SUBPROJS variable
92
# SUBPROJS     A list of the names of other makefiles to run before attempting
93
#              to build the target. This allows you to build multiple targets.
94
#              Note that ".mk" is appended to the subproject names. These
95
#              subprojects are also included in a clean_all.
96
#
16 by edam
- renamed to edam.mk; makefile should now be included rather than duplicated and modified
97
# CPPFLAGS     Flags to give to the C and C++ compilers
98
# CFLAGS       Flags to give to the C compiler
99
# CXXFLAGS     Flags to give to the C++ compiler
100
# DFLAGS       Flags to give to the D compiler
101
# ASFLAGS      Flags to give to the assembler
102
# LDFLAGS      Flags to give to the linker before librarys
103
# LDPOSTFLAGS  Flags to give to the linker after libraries
104
#
105
# This general-purpose makefile also defines the following goals for use on the
106
# command line when you run make:
107
#
108
# all          This is the default if no goal is specified. It builds subdirs
18 by edam
- added subprojects via SUBPROJS variable
109
#              and subprojects first and then the target.
110
#
111
# subdirs      Goes through the list of specified subdirectories, changing to
112
#              them, and runs make there.
113
#
19 by edam
- added NOLIBPROFIX switch
114
# subprojs     Goes through the list of specified subprojects, running the
18 by edam
- added subprojects via SUBPROJS variable
115
#              makefiles for each of them.
16 by edam
- renamed to edam.mk; makefile should now be included rather than duplicated and modified
116
#
117
# target       Builds the target of your Makefile.
118
#
119
# run          Builds the target of your Makefile and, if successful, runs it.
120
#              This is not available if you're building a library of some kind.
121
#
23 by edam
- added the debug goal
122
# debug        The same as the run goal, except instead of running the target,
123
#              it is debugged with gdb.
124
#
16 by edam
- renamed to edam.mk; makefile should now be included rather than duplicated and modified
125
# clean        Deletes temporary files.
126
#
127
# clean_all    Deletes temporary files and then goes through then project's
128
#              subdirectories doing the same.
129
#
130
# <subdir>     Builds the specified subdirectory from those that are listed for
131
#              the project.
132
#
18 by edam
- added subprojects via SUBPROJS variable
133
# <subproj>    Builds the specified subproject from those listed for the
134
#              project.
135
#
16 by edam
- renamed to edam.mk; makefile should now be included rather than duplicated and modified
136
# <file>       Builds the specified file, either an object file or the target,
137
#              from those that that would be built for the project.
138
#
139
# Please report any problems to Tim Marston <edam@waxworlds.org>
140
#
141
# Known shortcommings:
142
# - Using C is probably broken because g++ is currently used for linking. We
18 by edam
- added subprojects via SUBPROJS variable
143
#   should be using ld and specifying the crt libs as required by the sources.
16 by edam
- renamed to edam.mk; makefile should now be included rather than duplicated and modified
144
#
145
#_______________________________________________________________________________
146
#
11 by edam
- smartened up comments
147
148
# set debug mode if profiling
149
ifdef PROFILEMODE
150
export DEBUGMODE := 1
151
endif
152
153
# software
154
CC			:= gcc
155
CXX			:= g++
13 by edam
- added support for D!
156
GDC			:= gdc
157
AS			:= nasm
11 by edam
- smartened up comments
158
LD			:= g++
159
AR			:= ar
160
MAKE		:= make
161
13 by edam
- added support for D!
162
# debug/profile build flags
20 by edam
- a file names 'subdir.mk' in subdirectories mentioned in the SUBDIRS variable is now explicitly used during the making the subdirectory, if it is present
163
CPPFLAGS	:= $(if $(PROFILEMODE),-pg -D PROFILE) $(if $(DEBUGMODE),\
22 by edam
- changed debugging info level to highest
164
	-g3 -D DEBUG -Wall -Wextra,-D NDEBUG -O2) $(CPPFLAGS)
20 by edam
- a file names 'subdir.mk' in subdirectories mentioned in the SUBDIRS variable is now explicitly used during the making the subdirectory, if it is present
165
CXXFLAGS	:= $(if $(DEBUGMODE),-Woverloaded-virtual -Wreorder \
166
	-Wctor-dtor-privacy) $(CXXFLAGS)
13 by edam
- added support for D!
167
DFLAGS		:= $(if $(DEBUGMODE),,-frelease)
15 by edam
added NDEBUG compiler directive
168
ASFLAGS		:= -f elf $(if $(DEBUGMODE),-g -dDEBUG,-dNDEBUG -O2) $(ASFLAGS)
20 by edam
- a file names 'subdir.mk' in subdirectories mentioned in the SUBDIRS variable is now explicitly used during the making the subdirectory, if it is present
169
LDFLAGS		:= $(if $(PROFILEMODE),-pg) \
170
	$(if $(or $(PROFILEMODE), $(DEBUGMODE)),,-Wl,-S) $(LDFLAGS)
13 by edam
- added support for D!
171
172
# setup options for shared/static libs
20 by edam
- a file names 'subdir.mk' in subdirectories mentioned in the SUBDIRS variable is now explicitly used during the making the subdirectory, if it is present
173
CPPFLAGS	:= $(if $(or $(MKSHAREDLIB),$(MKSTATICLIB)),-fPIC) $(CPPFLAGS)
12 by edam
- compiler/linker gives all wornings when building in debug
174
LDFLAGS		:= $(if $(LINKSTATIC),-static) $(LDFLAGS)
13 by edam
- added support for D!
175
176
# add libraries for d
24 by edam
- updated gphobos to gphobos2, for D2
177
LIBRARIES	:= $(LIBRARIES) $(if $(filter %.d, $(SOURCES)), gphobos2 pthread m)
13 by edam
- added support for D!
178
179
# build flags for libraries
11 by edam
- smartened up comments
180
LDPOSTFLAGS := $(addprefix -l,$(LIBRARIES)) $(LDPOSTFLAGS)
181
182
# object debug/profile suffix
183
BUILDSUFFIX	:= $(if $(PROFILEMODE),_p,$(if $(DEBUGMODE),_d))
184
13 by edam
- added support for D!
185
# work out object and dependency files
11 by edam
- smartened up comments
186
OBJECTS		:= $(addsuffix $(BUILDSUFFIX).o,$(basename $(SOURCES)))
12 by edam
- compiler/linker gives all wornings when building in debug
187
DEPFILES	:= $(addsuffix .dep,$(basename $(SOURCES)))
11 by edam
- smartened up comments
188
13 by edam
- added support for D!
189
# fixup target name
11 by edam
- smartened up comments
190
ifdef TARGET
13 by edam
- added support for D!
191
TARGET		:= $(basename $(TARGET))$(BUILDSUFFIX)$(suffix $(TARGET))
192
TARGET		:= $(patsubst %.so,%,$(patsubst %.a,%,$(TARGET)))
12 by edam
- compiler/linker gives all wornings when building in debug
193
ifneq ($(strip $(MKSHAREDLIB) $(MKSTATICLIB)),)
13 by edam
- added support for D!
194
TARGET		:= $(TARGET)$(if $(MKSHAREDLIB),.so,$(if $(MKSTATICLIB),.a))
19 by edam
- added NOLIBPROFIX switch
195
ifndef NOLIBPREFIX
13 by edam
- added support for D!
196
TARGET		:= lib$(patsubst lib%,%,$(TARGET))
12 by edam
- compiler/linker gives all wornings when building in debug
197
endif
11 by edam
- smartened up comments
198
endif
19 by edam
- added NOLIBPROFIX switch
199
endif
11 by edam
- smartened up comments
200
14 by edam
- optimised dependency generation: generate stub dependencies from the compiler and don't use the sed script where possible
201
# Set up dependency generation build flags and, for those languages where the
202
# the compiler/assembler doesn't support dependency generation, commands to be
203
# executed after generating any dependency file. The commands append the names
204
# of all the depended-on files in the dependency file to the end of the file as
11 by edam
- smartened up comments
205
# 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
206
# one of these files becomes non-existant, but causes files dependent on these
207
# files to be rebuilt (and thus also have their dependencies regenerated).
11 by edam
- smartened up comments
208
ifdef DEBUGMODE
209
ifndef PROFILEMODE
14 by edam
- optimised dependency generation: generate stub dependencies from the compiler and don't use the sed script where possible
210
FIXUP_DEPENDENCY_FILES = \
11 by edam
- smartened up comments
211
	sed 's/\#.*//;s/^[^:]*://;s/^[ \t]*//;s/ *\\$$//;/^$$/d;s/$$/:/' < \
12 by edam
- compiler/linker gives all wornings when building in debug
212
	$(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
213
DEPFLAGS	= -MMD -MP -MF $(basename $<).dep
11 by edam
- smartened up comments
214
endif
215
endif
216
14 by edam
- optimised dependency generation: generate stub dependencies from the compiler and don't use the sed script where possible
217
# include dependencies
11 by edam
- smartened up comments
218
ifneq "$(MAKECMDGOALS)" "clean"
219
ifneq "$(MAKECMDGOALS)" "clean_all"
220
-include $(DEPFILES)
221
endif
222
endif
223
224
# default rule
225
.DEFAULT_GOAL := all
226
227
#_______________________________________________________________________________
228
#                                                                          RULES
10 by edam
- fixed recursive cleaning
229
23 by edam
- added the debug goal
230
.PHONY:	all subdirs subprojs target clean clean_all run debug depend dep \
20 by edam
- a file names 'subdir.mk' in subdirectories mentioned in the SUBDIRS variable is now explicitly used during the making the subdirectory, if it is present
231
	$(SUBDIRS) $(SUBPROJS)
9 by edam
- combines sources in to one variable, SOURCES
232
18 by edam
- added subprojects via SUBPROJS variable
233
all: subdirs subprojs target
9 by edam
- combines sources in to one variable, SOURCES
234
235
subdirs: $(SUBDIRS)
236
18 by edam
- added subprojects via SUBPROJS variable
237
subprojs: $(SUBPROJS)
238
9 by edam
- combines sources in to one variable, SOURCES
239
target: $(TARGET)
1 by edam
initial makefile
240
11 by edam
- smartened up comments
241
clean:
18 by edam
- added subprojects via SUBPROJS variable
242
ifneq ($(or $(SUBDIRS),$(SUBPROJS)),)
11 by edam
- smartened up comments
243
ifneq "$(MAKECMDGOALS)" "clean_all"
18 by edam
- added subprojects via SUBPROJS variable
244
	@echo "NOT RECURSING - use 'make clean_all' to clean subdirs and " \
245
		"subprojs as well"
11 by edam
- smartened up comments
246
endif
247
endif
20 by edam
- a file names 'subdir.mk' in subdirectories mentioned in the SUBDIRS variable is now explicitly used during the making the subdirectory, if it is present
248
	rm -f $(OBJECTS) $(TARGET) $(DEPFILES) core *~
11 by edam
- smartened up comments
249
18 by edam
- added subprojects via SUBPROJS variable
250
clean_all: subdirs subprojs clean
11 by edam
- smartened up comments
251
252
ifndef MKSTATICLIB
253
ifndef MKSHAREDLIB
9 by edam
- combines sources in to one variable, SOURCES
254
run: target
255
	./$(TARGET)
23 by edam
- added the debug goal
256
257
debug: target
258
	gdb ./$(TARGET)
9 by edam
- combines sources in to one variable, SOURCES
259
endif
260
endif
4 by edam
- replaced debug option with debug switch
261
18 by edam
- added subprojects via SUBPROJS variable
262
$(SUBDIRS) $(SUBPROJS):
263
	@if [ "$@" = "$(firstword $(SUBDIRS) $(SUBPROJS))" ]; then echo; fi
20 by edam
- a file names 'subdir.mk' in subdirectories mentioned in the SUBDIRS variable is now explicitly used during the making the subdirectory, if it is present
264
	@$(MAKE) $(if $(filter $@,$(SUBPROJS)), -f $@.mk, \
21 by edam
- changed name of makefile used in subdirs to emake.mk
265
		-C $@ $(if $(wildcard $@/emake.mk),-f emake.mk,)) \
18 by edam
- added subprojects via SUBPROJS variable
266
		$(filter-out $(SUBDIRS) $(SUBPROJS) subdirs subprojs,$(MAKECMDGOALS))
11 by edam
- smartened up comments
267
	@echo
8 by edam
- added SUBDIRS for recursing
268
9 by edam
- combines sources in to one variable, SOURCES
269
$(TARGET): $(OBJECTS)
11 by edam
- smartened up comments
270
ifdef MKSTATICLIB
9 by edam
- combines sources in to one variable, SOURCES
271
	$(AR) rcs $(TARGET) $(OBJECTS)
8 by edam
- added SUBDIRS for recursing
272
else
18 by edam
- added subprojects via SUBPROJS variable
273
	$(LD) $(if $(MKSHAREDLIB),-shared) -o $(TARGET) $(LDFLAGS) $(OBJECTS) $(LDPOSTFLAGS)
8 by edam
- added SUBDIRS for recursing
274
endif
275
11 by edam
- smartened up comments
276
%.o %_d.o %_p.o: %.c
277
	$(CC) -c $(CPPFLAGS) $(DEPFLAGS) $(CFLAGS) -o $@ $<
278
279
%.o %_d.o %_p.o: %.cc
280
	$(CXX) -c $(CPPFLAGS) $(DEPFLAGS) $(CXXFLAGS) -o $@ $<
281
%.o %_d.o %_p.o: %.C
282
	$(CXX) -c $(CPPFLAGS) $(DEPFLAGS) $(CXXFLAGS) -o $@ $<
283
%.o %_d.o %_p.o: %.cpp
284
	$(CXX) -c $(CPPFLAGS) $(DEPFLAGS) $(CXXFLAGS) -o $@ $<
285
13 by edam
- added support for D!
286
%.o %_d.o %_p.o: %.d
287
	$(GDC) -c $(CPPFLAGS) $(DFLAGS) -o $@ $<
288
11 by edam
- smartened up comments
289
%.o %_d.o %_p.o: %.s
290
	$(AS) $(ASFLAGS) -o $@ $<
14 by edam
- optimised dependency generation: generate stub dependencies from the compiler and don't use the sed script where possible
291
ifdef DEBUGMODE
292
	$(AS) $(ASFLAGS) -M $< > $(basename $<).dep
293
	$(FIXUP_DEPENDENCY_FILES)
294
endif
11 by edam
- smartened up comments
295
%.o %_d.o %_p.o: %.S
296
	$(AS) $(ASFLAGS) -o $@ $<
14 by edam
- optimised dependency generation: generate stub dependencies from the compiler and don't use the sed script where possible
297
ifdef DEBUGMODE
298
	$(AS) $(ASFLAGS) -M $< > $(basename $<).dep
299
	$(FIXUP_DEPENDENCY_FILES)
300
endif
11 by edam
- smartened up comments
301
%.o %_d.o %_p.o: %.asm
302
	$(AS) $(ASFLAGS) -o $@ $<
14 by edam
- optimised dependency generation: generate stub dependencies from the compiler and don't use the sed script where possible
303
ifdef DEBUGMODE
304
	$(AS) $(ASFLAGS) -M $< > $(basename $<).dep
305
	$(FIXUP_DEPENDENCY_FILES)
306
endif
8 by edam
- added SUBDIRS for recursing
307
9 by edam
- combines sources in to one variable, SOURCES
308
#_______________________________________________________________________________
16 by edam
- renamed to edam.mk; makefile should now be included rather than duplicated and modified
309