/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 Makefile

  • Committer: edam
  • Date: 2009-03-05 13:45:42 UTC
  • Revision ID: edam@waxworlds.org-20090305134542-llxrwg3d7xucnf60
Tags: 1.11
- fixed recursive cleaning
- notify when recursing during build

Show diffs side-by-side

added added

removed removed

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