1
1
#_______________________________________________________________________________
3
# edam's general-purpose makefile
4
#_______________________________________________________________________________
7
# Copyright (c) 2009 Tim Marston <edam@waxworlds.org>.
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:
16
# The above copyright notice and this permission notice shall be included in
17
# all copies or substantial portions of the Software.
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
27
#_______________________________________________________________________________
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
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.
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:
40
# SOURCES = main.cc foo.cc
42
# include ~/src/edam.mk
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:
50
# and overridding on the command line when necessary:
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.
57
# Here is a list of all configuration parameters:
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.
67
# LINKSTATIC Boolean. Set to build a target that staticly links against all
68
# its libraries and doesn't use shared libraries.
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
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
76
# NOLIBPREFIX Boolean. When building a static or shared library, do not ensure
77
# that the target's name is prefixed with "lib".
79
# TARGET The name of the target file.
81
# SOURCES A list of all source files of whatever language. The language
82
# type is determined by the file extension.
84
# LIBRARIES A list of libraries to link against. Don't include the 'lib'
87
# SUBDIRS A list of subdirectories to build before attempting to build the
88
# target. These subdirectories are also included in a clean_all.
89
# Note that if the file 'subdir.mk' exists in a subdirectory, it
90
# will be used explicitly, rather than any default makefile.
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.
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
105
# This general-purpose makefile also defines the following goals for use on the
106
# command line when you run make:
108
# all This is the default if no goal is specified. It builds subdirs
109
# and subprojects first and then the target.
111
# subdirs Goes through the list of specified subdirectories, changing to
112
# them, and runs make there.
114
# subprojs Goes through the list of specified subprojects, running the
115
# makefiles for each of them.
117
# target Builds the target of your Makefile.
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.
122
# clean Deletes temporary files.
124
# clean_all Deletes temporary files and then goes through then project's
125
# subdirectories doing the same.
127
# <subdir> Builds the specified subdirectory from those that are listed for
130
# <subproj> Builds the specified subproject from those listed for the
133
# <file> Builds the specified file, either an object file or the target,
134
# from those that that would be built for the project.
136
# Please report any problems to Tim Marston <edam@waxworlds.org>
138
# Known shortcommings:
139
# - Using C is probably broken because g++ is currently used for linking. We
140
# should be using ld and specifying the crt libs as required by the sources.
142
#_______________________________________________________________________________
3
# edam's general-purpose makefile v2.2
4
#_______________________________________________________________________________
9
# (better to specify in environment/command line)
11
#export DEBUGMODE := 1
12
#export PROFILEMODE := 1
13
#export LINKSTATIC := 1
16
# target binary/library
28
# libraries to link against
33
# subdirectories to make first
38
# additional build flags
48
#_______________________________________________________________________________
145
51
# set debug mode if profiling
159
65
# debug/profile build flags
160
CPPFLAGS := $(if $(PROFILEMODE),-pg -D PROFILE) $(if $(DEBUGMODE),\
161
-g -D DEBUG -Wall -Wextra,-D NDEBUG -O2) $(CPPFLAGS)
162
CXXFLAGS := $(if $(DEBUGMODE),-Woverloaded-virtual -Wreorder \
163
-Wctor-dtor-privacy) $(CXXFLAGS)
66
CPPFLAGS := $(if $(PROFILEMODE),-pg -D PROFILE) $(CPPFLAGS)
67
CPPFLAGS := $(if $(DEBUGMODE),-g -D DEBUG -Wall,-O2) $(CPPFLAGS)
164
68
DFLAGS := $(if $(DEBUGMODE),,-frelease)
165
ASFLAGS := -f elf $(if $(DEBUGMODE),-g -dDEBUG,-dNDEBUG -O2) $(ASFLAGS)
166
LDFLAGS := $(if $(PROFILEMODE),-pg) \
167
$(if $(or $(PROFILEMODE), $(DEBUGMODE)),,-Wl,-S) $(LDFLAGS)
69
ASFLAGS := -f elf $(if $(DEBUGMODE),-g -dDEBUG,-O2) $(ASFLAGS)
70
LDFLAGS := $(if $(PROFILEMODE),-pg) $(LDFLAGS)
71
LDFLAGS := $(if $(or $(PROFILEMODE), $(DEBUGMODE)),,-Wl,-S) $(LDFLAGS)
169
73
# setup options for shared/static libs
170
CPPFLAGS := $(if $(or $(MKSHAREDLIB),$(MKSTATICLIB)),-fPIC) $(CPPFLAGS)
74
CPPFLAGS := $(if $(MKSHAREDLIB),-fPIC) $(CPPFLAGS)
171
75
LDFLAGS := $(if $(LINKSTATIC),-static) $(LDFLAGS)
173
77
# add libraries for d
189
96
TARGET := $(patsubst %.so,%,$(patsubst %.a,%,$(TARGET)))
190
97
ifneq ($(strip $(MKSHAREDLIB) $(MKSTATICLIB)),)
191
98
TARGET := $(TARGET)$(if $(MKSHAREDLIB),.so,$(if $(MKSTATICLIB),.a))
193
99
TARGET := lib$(patsubst lib%,%,$(TARGET))
198
# Set up dependency generation build flags and, for those languages where the
199
# the compiler/assembler doesn't support dependency generation, commands to be
200
# executed after generating any dependency file. The commands append the names
201
# of all the depended-on files in the dependency file to the end of the file as
103
# Set up dependancy generation build flags and a commands to be executed after
104
# generating any dependancy file. The commands append the names of all the
105
# depended-on files in the dependancy file to the end of the dependancy file as
202
106
# empty rules with no prerequesits or commands. This causes make not to fail if
203
# one of these files becomes non-existant, but causes files dependent on these
204
# files to be rebuilt (and thus also have their dependencies regenerated).
107
# one of these files becomes non-existant, but causes files dependant on these
108
# files to be rebuilt (and thus also have their dependancies regenerated).
206
110
ifndef PROFILEMODE
207
FIXUP_DEPENDENCY_FILES = \
111
FIXUP_DEPENDANCY_FILES = \
208
112
sed 's/\#.*//;s/^[^:]*://;s/^[ \t]*//;s/ *\\$$//;/^$$/d;s/$$/:/' < \
209
113
$(basename $<).dep > .$$$$~; cat .$$$$~ >> $(basename $<).dep; rm .$$$$~;
210
DEPFLAGS = -MMD -MP -MF $(basename $<).dep
114
DEPFLAGS = -MD -MF $(basename $<).dep
214
# include dependencies
118
# include dependancies
215
119
ifneq "$(MAKECMDGOALS)" "clean"
216
120
ifneq "$(MAKECMDGOALS)" "clean_all"
217
121
-include $(DEPFILES)
256
$(SUBDIRS) $(SUBPROJS):
257
@if [ "$@" = "$(firstword $(SUBDIRS) $(SUBPROJS))" ]; then echo; fi
258
@$(MAKE) $(if $(filter $@,$(SUBPROJS)), -f $@.mk, \
259
-C $@ $(if $(wildcard $@/subdir.mk),-f subdir.mk,)) \
260
$(filter-out $(SUBDIRS) $(SUBPROJS) subdirs subprojs,$(MAKECMDGOALS))
157
@if [ "$@" = "$(firstword $(SUBDIRS))" ]; then echo; fi
158
@$(MAKE) -C $@ $(filter-out $(SUBDIRS),$(MAKECMDGOALS))
263
161
$(TARGET): $(OBJECTS)
264
162
ifdef MKSTATICLIB
265
163
$(AR) rcs $(TARGET) $(OBJECTS)
267
$(LD) $(if $(MKSHAREDLIB),-shared) -o $(TARGET) $(LDFLAGS) $(OBJECTS) $(LDPOSTFLAGS)
165
$(LD) $(if $(MKSHAREDLIB),-shared) -o $(TARGET) $(LDFLAGS) $(OBJECTS) $(LDPOSTFLAGS)
270
168
%.o %_d.o %_p.o: %.c
271
169
$(CC) -c $(CPPFLAGS) $(DEPFLAGS) $(CFLAGS) -o $@ $<
170
$(FIXUP_DEPENDANCY_FILES)
273
172
%.o %_d.o %_p.o: %.cc
274
173
$(CXX) -c $(CPPFLAGS) $(DEPFLAGS) $(CXXFLAGS) -o $@ $<
174
$(FIXUP_DEPENDANCY_FILES)
275
175
%.o %_d.o %_p.o: %.C
276
176
$(CXX) -c $(CPPFLAGS) $(DEPFLAGS) $(CXXFLAGS) -o $@ $<
177
$(FIXUP_DEPENDANCY_FILES)
277
178
%.o %_d.o %_p.o: %.cpp
278
179
$(CXX) -c $(CPPFLAGS) $(DEPFLAGS) $(CXXFLAGS) -o $@ $<
180
$(FIXUP_DEPENDANCY_FILES)
280
182
%.o %_d.o %_p.o: %.d
281
183
$(GDC) -c $(CPPFLAGS) $(DFLAGS) -o $@ $<
283
185
%.o %_d.o %_p.o: %.s
284
186
$(AS) $(ASFLAGS) -o $@ $<
286
$(AS) $(ASFLAGS) -M $< > $(basename $<).dep
287
$(FIXUP_DEPENDENCY_FILES)
289
187
%.o %_d.o %_p.o: %.S
290
188
$(AS) $(ASFLAGS) -o $@ $<
292
$(AS) $(ASFLAGS) -M $< > $(basename $<).dep
293
$(FIXUP_DEPENDENCY_FILES)
295
189
%.o %_d.o %_p.o: %.asm
296
190
$(AS) $(ASFLAGS) -o $@ $<
298
$(AS) $(ASFLAGS) -M $< > $(basename $<).dep
299
$(FIXUP_DEPENDENCY_FILES)
193
$(AS) $(ASFLAGS) -M $< > $@
194
$(FIXUP_DEPENDANCY_FILES)
196
$(AS) $(ASFLAGS) -M $< > $@
197
$(FIXUP_DEPENDANCY_FILES)
199
$(AS) $(ASFLAGS) -M $< > $@
200
$(FIXUP_DEPENDANCY_FILES)
302
202
#_______________________________________________________________________________