/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:28:02 UTC
  • Revision ID: edam@waxworlds.org-20090305142802-um4qc3qwifzlc0ei
Tags: 3.3
- added NOLIBPROFIX switch
- fixed typo in documentation

Show diffs side-by-side

added added

removed removed

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