/make/edam-mk

To get this branch, use:
bzr branch http://bzr.ed.am/make/edam-mk

« back to all changes in this revision

Viewing changes to edam.mk

  • Committer: edam
  • Date: 2009-03-05 14:26:08 UTC
  • Revision ID: edam@waxworlds.org-20090305142608-63gav9dxralm0mm8
Tags: 3.2
- added subprojects via SUBPROJS variable
- tweaked some comments

Show diffs side-by-side

added added

removed removed

1
 
#                                          Edam's general-purpose makefile v1.10
2
 
#_______________________________________________________________________________
3
 
#                                                                S E T T I N G S
4
 
 
5
 
# Overridable options
6
 
# (better to specify these in environment/command line)
7
 
#export DEBUGMODE       := 1
8
 
#export STATICLIBS      := 1
9
 
 
10
 
# Target binary/library
11
 
#BUILDALIB      := 1
12
 
#BUILDSOLIB     := 1
13
 
TARGET          := tim.a
14
 
 
15
 
# All source files
16
 
SOURCES         := tim.cc
17
 
 
18
 
# Libraries to link
19
 
LIBRARIES       := 
20
 
 
21
 
# Subdirectories to make
22
 
SUBDIRS         := 
23
 
 
24
 
# Software
25
 
AS              := nasm
26
 
CC              := gcc
27
 
CXX             := g++
28
 
 
29
 
# Flags
30
 
CPPFLAGS        := 
31
 
CFLAGS          := $(if $(DEBUGMODE),-g -D DEBUG,-O2)
32
 
CXXFLAGS        := $(if $(DEBUGMODE),-g -D DEBUG,-O2)
33
 
ASFLAGS         := -f elf $(if $(DEBUGMODE),-g -dDEBUG,-O2)
34
 
LDFLAGS         := -Wall $(if $(DEBUGMODE),,-s) $(if $(STATICLIBS), -static)
35
 
 
36
 
#_______________________________________________________________________________
37
 
 
38
 
DEPFILE         := depends.mk
39
 
OBJECTS         := $(addsuffix .o,$(basename $(SOURCES)))
40
 
LDLIBS          := $(addprefix -l,$(LIBRARIES))
 
1
#_______________________________________________________________________________
 
2
#
 
3
#                       edam's general-purpose makefile
 
4
#_______________________________________________________________________________
 
5
#                                                                    version 3.2
 
6
#
 
7
# Copyright (c) 2008 Tim Marston <edam@waxworlds.org>.
 
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.
 
26
#
 
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
 
32
# can also find more information/documentation on it's use. The following text
 
33
# can only really be considered a reference to it's use.
 
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.
 
76
#
 
77
# TARGET       The name of the target file.
 
78
#
 
79
# SOURCES      A list of all source files of whatever language. The language
 
80
#              type is determined by the file extension.
 
81
#
 
82
# LIBRARIES    A list of libraries to link against. Don't include the 'lib'
 
83
#              prefix.
 
84
#
 
85
# SUBDIRS      A list of subdirectories to build before attempting to build the
 
86
#              target. These subdirectories are also included in a clean_all.
 
87
#
 
88
# SUBPROJS     A list of the names of other makefiles to run before attempting
 
89
#              to build the target. This allows you to build multiple targets.
 
90
#              Note that ".mk" is appended to the subproject names. These
 
91
#              subprojects are also included in a clean_all.
 
92
#
 
93
# CPPFLAGS     Flags to give to the C and C++ compilers
 
94
# CFLAGS       Flags to give to the C compiler
 
95
# CXXFLAGS     Flags to give to the C++ compiler
 
96
# DFLAGS       Flags to give to the D compiler
 
97
# ASFLAGS      Flags to give to the assembler
 
98
# LDFLAGS      Flags to give to the linker before librarys
 
99
# LDPOSTFLAGS  Flags to give to the linker after libraries
 
100
#
 
101
# This general-purpose makefile also defines the following goals for use on the
 
102
# command line when you run make:
 
103
#
 
104
# all          This is the default if no goal is specified. It builds subdirs
 
105
#              and subprojects first and then the target.
 
106
#
 
107
# subdirs      Goes through the list of specified subdirectories, changing to
 
108
#              them, and runs make there.
 
109
#
 
110
# subproj      Goes through the list of specified subprojects, running the
 
111
#              makefiles for each of them.
 
112
#
 
113
# target       Builds the target of your Makefile.
 
114
#
 
115
# run          Builds the target of your Makefile and, if successful, runs it.
 
116
#              This is not available if you're building a library of some kind.
 
117
#
 
118
# clean        Deletes temporary files.
 
119
#
 
120
# clean_all    Deletes temporary files and then goes through then project's
 
121
#              subdirectories doing the same.
 
122
#
 
123
# <subdir>     Builds the specified subdirectory from those that are listed for
 
124
#              the project.
 
125
#
 
126
# <subproj>    Builds the specified subproject from those listed for the
 
127
#              project.
 
128
#
 
129
# <file>       Builds the specified file, either an object file or the target,
 
130
#              from those that that would be built for the project.
 
131
#
 
132
# Please report any problems to Tim Marston <edam@waxworlds.org>
 
133
#
 
134
# Known shortcommings:
 
135
# - Using C is probably broken because g++ is currently used for linking. We
 
136
#   should be using ld and specifying the crt libs as required by the sources.
 
137
#
 
138
#_______________________________________________________________________________
 
139
#
 
140
 
 
141
# set debug mode if profiling
 
142
ifdef PROFILEMODE
 
143
export DEBUGMODE := 1
 
144
endif
 
145
 
 
146
# software
 
147
CC                      := gcc
 
148
CXX                     := g++
 
149
GDC                     := gdc
 
150
AS                      := nasm
 
151
LD                      := g++
 
152
AR                      := ar
 
153
MAKE            := make
 
154
 
 
155
# debug/profile build flags
 
156
CPPFLAGS        := $(if $(PROFILEMODE),-pg -D PROFILE) $(CPPFLAGS)
 
157
CPPFLAGS        := $(if $(DEBUGMODE),-g -D DEBUG -Wall,-D NDEBUG -O2) $(CPPFLAGS)
 
158
DFLAGS          := $(if $(DEBUGMODE),,-frelease)
 
159
ASFLAGS         := -f elf $(if $(DEBUGMODE),-g -dDEBUG,-dNDEBUG -O2) $(ASFLAGS)
 
160
LDFLAGS         := $(if $(PROFILEMODE),-pg) $(LDFLAGS)
 
161
LDFLAGS         := $(if $(or $(PROFILEMODE), $(DEBUGMODE)),,-Wl,-S) $(LDFLAGS)
 
162
 
 
163
# setup options for shared/static libs
 
164
CPPFLAGS        := $(if $(MKSHAREDLIB),-fPIC) $(CPPFLAGS)
 
165
LDFLAGS         := $(if $(LINKSTATIC),-static) $(LDFLAGS)
 
166
 
 
167
# add libraries for d
 
168
LIBRARIES       := $(LIBRARIES) $(if $(filter %.d, $(SOURCES)), gphobos pthread m)
 
169
 
 
170
# build flags for libraries
 
171
LDPOSTFLAGS := $(addprefix -l,$(LIBRARIES)) $(LDPOSTFLAGS)
 
172
 
 
173
#_______________________________________________________________________________
 
174
#
 
175
 
 
176
# object debug/profile suffix
 
177
BUILDSUFFIX     := $(if $(PROFILEMODE),_p,$(if $(DEBUGMODE),_d))
 
178
 
 
179
# work out object and dependency files
 
180
OBJECTS         := $(addsuffix $(BUILDSUFFIX).o,$(basename $(SOURCES)))
 
181
DEPFILES        := $(addsuffix .dep,$(basename $(SOURCES)))
 
182
 
 
183
# fixup target name
 
184
ifdef TARGET
 
185
TARGET          := $(basename $(TARGET))$(BUILDSUFFIX)$(suffix $(TARGET))
41
186
TARGET          := $(patsubst %.so,%,$(patsubst %.a,%,$(TARGET)))
42
 
TARGET          := $(TARGET)$(if $(BUILDALIB),.a,)$(if $(BUILDSOLIB),.so,)
43
 
 
44
 
.PHONY: all subdirs target clean clean_all run depend dep $(SUBDIRS)
45
 
 
46
 
all: subdirs target
 
187
ifneq ($(strip $(MKSHAREDLIB) $(MKSTATICLIB)),)
 
188
TARGET          := $(TARGET)$(if $(MKSHAREDLIB),.so,$(if $(MKSTATICLIB),.a))
 
189
TARGET          := lib$(patsubst lib%,%,$(TARGET))
 
190
endif
 
191
endif
 
192
 
 
193
# Set up dependency generation build flags and, for those languages where the
 
194
# the compiler/assembler doesn't support dependency generation, commands to be
 
195
# executed after generating any dependency file. The commands append the names
 
196
# of all the depended-on files in the dependency file to the end of the file as
 
197
# empty rules with no prerequesits or commands. This causes make not to fail if
 
198
# one of these files becomes non-existant, but causes files dependent on these
 
199
# files to be rebuilt (and thus also have their dependencies regenerated).
 
200
ifdef DEBUGMODE
 
201
ifndef PROFILEMODE
 
202
FIXUP_DEPENDENCY_FILES = \
 
203
        sed 's/\#.*//;s/^[^:]*://;s/^[ \t]*//;s/ *\\$$//;/^$$/d;s/$$/:/' < \
 
204
        $(basename $<).dep > .$$$$~; cat .$$$$~ >> $(basename $<).dep; rm .$$$$~;
 
205
DEPFLAGS        = -MMD -MP -MF $(basename $<).dep
 
206
endif
 
207
endif
 
208
 
 
209
# include dependencies
 
210
ifneq "$(MAKECMDGOALS)" "clean"
 
211
ifneq "$(MAKECMDGOALS)" "clean_all"
 
212
-include $(DEPFILES)
 
213
endif
 
214
endif
 
215
 
 
216
# default rule
 
217
.DEFAULT_GOAL := all
 
218
 
 
219
#_______________________________________________________________________________
 
220
#                                                                          RULES
 
221
 
 
222
.PHONY: all subdirs subprojs target clean clean_all run depend dep $(SUBDIRS) $(SUBPROJS)
 
223
 
 
224
all: subdirs subprojs target
47
225
 
48
226
subdirs: $(SUBDIRS)
49
227
 
 
228
subprojs: $(SUBPROJS)
 
229
 
50
230
target: $(TARGET)
51
231
 
52
232
clean:
53
 
ifeq "$(MAKECMDGOALS)" "clean"
54
 
ifdef SUBDIRS
55
 
        @echo NOT RECURSING: Use \"make clean_all\" to clean subdirectoried as well...
56
 
endif
57
 
endif
58
 
        rm -f core $(DEPFILE) $(OBJECTS) $(TARGET)
59
 
        rm -f *~
60
 
 
61
 
clean_all: subdirs clean
62
 
 
63
 
ifndef BUILDALIB
64
 
ifndef BUILDSOLIB
 
233
ifneq ($(or $(SUBDIRS),$(SUBPROJS)),)
 
234
ifneq "$(MAKECMDGOALS)" "clean_all"
 
235
        @echo "NOT RECURSING - use 'make clean_all' to clean subdirs and " \
 
236
                "subprojs as well"
 
237
endif
 
238
endif
 
239
        rm -f $(OBJECTS) $(TARGET) $(DEPFILES) core
 
240
 
 
241
clean_all: subdirs subprojs clean
 
242
 
 
243
ifndef MKSTATICLIB
 
244
ifndef MKSHAREDLIB
65
245
run: target
66
246
        ./$(TARGET)
67
247
endif
68
248
endif
69
249
 
70
 
#depend dep:
71
 
#       makedepend -f- -- $(CPPFLAGS) -- $(SOURCES) > $(DEPFILE)
72
 
 
73
 
$(SUBDIRS):
74
 
        @$(MAKE) --no-print-directory -C $@ $(MAKECMDGOALS)
 
250
$(SUBDIRS) $(SUBPROJS):
 
251
        @if [ "$@" = "$(firstword $(SUBDIRS) $(SUBPROJS))" ]; then echo; fi
 
252
        @$(MAKE) $(if $(filter $@,$(SUBDIRS)),-C $@,-f $@.mk) \
 
253
                $(filter-out $(SUBDIRS) $(SUBPROJS) subdirs subprojs,$(MAKECMDGOALS))
 
254
        @echo
75
255
 
76
256
$(TARGET): $(OBJECTS)
77
 
ifdef BUILDALIB
 
257
ifdef MKSTATICLIB
78
258
        $(AR) rcs $(TARGET) $(OBJECTS)
79
259
else
80
 
        $(CXX) $(if $(BUILDSOLIB),-shared) -o $(TARGET) $(LDFLAGS) $(OBJECTS) $(LDLIBS) 
81
 
endif
82
 
 
83
 
#%.o:   %.c
84
 
#       $(CC) -c $(CPPFLAGS) $(CFLAGS)
85
 
#%.o:   %.cc
86
 
#       $(CXX) -c $(CPPFLAGS) $(CXXFLAGS)
87
 
#%.o:   %.s
88
 
#       $(AS) $(ASFLAGS)
89
 
 
90
 
-include $(DEPFILE)
 
260
        $(LD) $(if $(MKSHAREDLIB),-shared) -o $(TARGET) $(LDFLAGS) $(OBJECTS) $(LDPOSTFLAGS)
 
261
endif
 
262
 
 
263
%.o %_d.o %_p.o: %.c
 
264
        $(CC) -c $(CPPFLAGS) $(DEPFLAGS) $(CFLAGS) -o $@ $<
 
265
 
 
266
%.o %_d.o %_p.o: %.cc
 
267
        $(CXX) -c $(CPPFLAGS) $(DEPFLAGS) $(CXXFLAGS) -o $@ $<
 
268
%.o %_d.o %_p.o: %.C
 
269
        $(CXX) -c $(CPPFLAGS) $(DEPFLAGS) $(CXXFLAGS) -o $@ $<
 
270
%.o %_d.o %_p.o: %.cpp
 
271
        $(CXX) -c $(CPPFLAGS) $(DEPFLAGS) $(CXXFLAGS) -o $@ $<
 
272
 
 
273
%.o %_d.o %_p.o: %.d
 
274
        $(GDC) -c $(CPPFLAGS) $(DFLAGS) -o $@ $<
 
275
 
 
276
%.o %_d.o %_p.o: %.s
 
277
        $(AS) $(ASFLAGS) -o $@ $<
 
278
ifdef DEBUGMODE
 
279
        $(AS) $(ASFLAGS) -M $< > $(basename $<).dep
 
280
        $(FIXUP_DEPENDENCY_FILES)
 
281
endif
 
282
%.o %_d.o %_p.o: %.S
 
283
        $(AS) $(ASFLAGS) -o $@ $<
 
284
ifdef DEBUGMODE
 
285
        $(AS) $(ASFLAGS) -M $< > $(basename $<).dep
 
286
        $(FIXUP_DEPENDENCY_FILES)
 
287
endif
 
288
%.o %_d.o %_p.o: %.asm
 
289
        $(AS) $(ASFLAGS) -o $@ $<
 
290
ifdef DEBUGMODE
 
291
        $(AS) $(ASFLAGS) -M $< > $(basename $<).dep
 
292
        $(FIXUP_DEPENDENCY_FILES)
 
293
endif
91
294
 
92
295
#_______________________________________________________________________________
 
296