/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:24:59 UTC
  • Revision ID: edam@waxworlds.org-20090305142459-qwm14g5rsttq2f9b
Tags: 3.1
properly cleanup dependencies

Show diffs side-by-side

added added

removed removed

2
2
#
3
3
#                       edam's general-purpose makefile
4
4
#_______________________________________________________________________________
5
 
#                                                                    version 3.5
6
 
#
7
 
# Copyright (c) 2009 Tim Marston <edam@waxworlds.org>.
 
5
#                                                                    version 3.1
 
6
# Copyright (c) 2008 Tim Marston <edam@waxworlds.org>.
8
7
#
9
8
# Permission is hereby granted, free of charge, to any person obtaining a copy
10
9
# of this software and associated documentation files (the "Software"), to deal
23
22
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
23
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
24
# THE SOFTWARE.
26
 
#
27
25
#_______________________________________________________________________________
28
26
#
29
27
#
30
28
# This is a general-purpose makefile for use with GNU make. It can be downloaded
31
29
# 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.
 
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.
34
32
#
35
33
# To use this makefile, put a file named "Makefile" in your project directory.
36
34
# Add all your project's settings to your Makefile and then include this file
73
71
# MKSHAREDLIB  Boolean. Target type: Set to build a shared library target. If
74
72
#              neither this nor MKSTATICLIB are set, the target defaults to a
75
73
#              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
74
#
79
75
# TARGET       The name of the target file.
80
76
#
86
82
#
87
83
# SUBDIRS      A list of subdirectories to build before attempting to build the
88
84
#              target. These subdirectories are also included in a clean_all.
89
 
#              Note that if the file 'emake.mk' exists in a subdirectory, it
90
 
#              will be used explicitly, rather than any default makefile.
91
 
#
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
85
#
97
86
# CPPFLAGS     Flags to give to the C and C++ compilers
98
87
# CFLAGS       Flags to give to the C compiler
106
95
# command line when you run make:
107
96
#
108
97
# all          This is the default if no goal is specified. It builds subdirs
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
 
#
114
 
# subprojs     Goes through the list of specified subprojects, running the
115
 
#              makefiles for each of them.
 
98
#              first and then the target.
 
99
#
 
100
# subdirs      Changes to the subdirectories specified for the project and runs
 
101
#              make in them.
116
102
#
117
103
# target       Builds the target of your Makefile.
118
104
#
119
105
# run          Builds the target of your Makefile and, if successful, runs it.
120
106
#              This is not available if you're building a library of some kind.
121
107
#
122
 
# debug        The same as the run goal, except instead of running the target,
123
 
#              it is debugged with gdb.
124
 
#
125
108
# clean        Deletes temporary files.
126
109
#
127
110
# clean_all    Deletes temporary files and then goes through then project's
130
113
# <subdir>     Builds the specified subdirectory from those that are listed for
131
114
#              the project.
132
115
#
133
 
# <subproj>    Builds the specified subproject from those listed for the
134
 
#              project.
135
 
#
136
116
# <file>       Builds the specified file, either an object file or the target,
137
117
#              from those that that would be built for the project.
138
118
#
140
120
#
141
121
# Known shortcommings:
142
122
# - Using C is probably broken because g++ is currently used for linking. We
143
 
#   should be using ld and specifying the crt libs as required by the sources.
 
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!
144
126
#
145
127
#_______________________________________________________________________________
146
128
#
160
142
MAKE            := make
161
143
 
162
144
# debug/profile build flags
163
 
CPPFLAGS        := $(if $(PROFILEMODE),-pg -D PROFILE) $(if $(DEBUGMODE),\
164
 
        -g3 -D DEBUG -Wall -Wextra,-D NDEBUG -O2) $(CPPFLAGS)
165
 
CXXFLAGS        := $(if $(DEBUGMODE),-Woverloaded-virtual -Wreorder \
166
 
        -Wctor-dtor-privacy) $(CXXFLAGS)
 
145
CPPFLAGS        := $(if $(PROFILEMODE),-pg -D PROFILE) $(CPPFLAGS)
 
146
CPPFLAGS        := $(if $(DEBUGMODE),-g -D DEBUG -Wall,-D NDEBUG -O2) $(CPPFLAGS)
167
147
DFLAGS          := $(if $(DEBUGMODE),,-frelease)
168
148
ASFLAGS         := -f elf $(if $(DEBUGMODE),-g -dDEBUG,-dNDEBUG -O2) $(ASFLAGS)
169
 
LDFLAGS         := $(if $(PROFILEMODE),-pg) \
170
 
        $(if $(or $(PROFILEMODE), $(DEBUGMODE)),,-Wl,-S) $(LDFLAGS)
 
149
LDFLAGS         := $(if $(PROFILEMODE),-pg) $(LDFLAGS)
 
150
LDFLAGS         := $(if $(or $(PROFILEMODE), $(DEBUGMODE)),,-Wl,-S) $(LDFLAGS)
171
151
 
172
152
# setup options for shared/static libs
173
 
CPPFLAGS        := $(if $(or $(MKSHAREDLIB),$(MKSTATICLIB)),-fPIC) $(CPPFLAGS)
 
153
CPPFLAGS        := $(if $(MKSHAREDLIB),-fPIC) $(CPPFLAGS)
174
154
LDFLAGS         := $(if $(LINKSTATIC),-static) $(LDFLAGS)
175
155
 
176
156
# add libraries for d
177
 
LIBRARIES       := $(LIBRARIES) $(if $(filter %.d, $(SOURCES)), gphobos2 rt)
 
157
LIBRARIES       := $(LIBRARIES) $(if $(filter %.d, $(SOURCES)), gphobos pthread m)
178
158
 
179
159
# build flags for libraries
180
160
LDPOSTFLAGS := $(addprefix -l,$(LIBRARIES)) $(LDPOSTFLAGS)
181
161
 
 
162
#_______________________________________________________________________________
 
163
#
 
164
 
182
165
# object debug/profile suffix
183
166
BUILDSUFFIX     := $(if $(PROFILEMODE),_p,$(if $(DEBUGMODE),_d))
184
167
 
192
175
TARGET          := $(patsubst %.so,%,$(patsubst %.a,%,$(TARGET)))
193
176
ifneq ($(strip $(MKSHAREDLIB) $(MKSTATICLIB)),)
194
177
TARGET          := $(TARGET)$(if $(MKSHAREDLIB),.so,$(if $(MKSTATICLIB),.a))
195
 
ifndef NOLIBPREFIX
196
178
TARGET          := lib$(patsubst lib%,%,$(TARGET))
197
179
endif
198
180
endif
199
 
endif
200
181
 
201
182
# Set up dependency generation build flags and, for those languages where the
202
183
# the compiler/assembler doesn't support dependency generation, commands to be
227
208
#_______________________________________________________________________________
228
209
#                                                                          RULES
229
210
 
230
 
.PHONY: all subdirs subprojs target clean clean_all run debug depend dep \
231
 
        $(SUBDIRS) $(SUBPROJS)
 
211
.PHONY: all subdirs target clean clean_all run depend dep $(SUBDIRS)
232
212
 
233
 
all: subdirs subprojs target
 
213
all: subdirs target
234
214
 
235
215
subdirs: $(SUBDIRS)
236
216
 
237
 
subprojs: $(SUBPROJS)
238
 
 
239
217
target: $(TARGET)
240
218
 
241
219
clean:
242
 
ifneq ($(or $(SUBDIRS),$(SUBPROJS)),)
 
220
ifdef SUBDIRS
243
221
ifneq "$(MAKECMDGOALS)" "clean_all"
244
 
        @echo "NOT RECURSING - use 'make clean_all' to clean subdirs and " \
245
 
                "subprojs as well"
246
 
endif
247
 
endif
248
 
        rm -f $(OBJECTS) $(TARGET) $(DEPFILES) core *~
 
222
        @echo "NOT RECURSING: use 'make clean_all' to clean subdirectories as well."
 
223
endif
 
224
endif
 
225
        rm -f $(OBJECTS) $(TARGET) $(DEPFILES) core
249
226
 
250
 
clean_all: subdirs subprojs clean
 
227
clean_all: subdirs clean
251
228
 
252
229
ifndef MKSTATICLIB
253
230
ifndef MKSHAREDLIB
254
231
run: target
255
232
        ./$(TARGET)
256
 
 
257
 
debug: target
258
 
        gdb ./$(TARGET)
259
 
endif
260
 
endif
261
 
 
262
 
$(SUBDIRS) $(SUBPROJS):
263
 
        @if [ "$@" = "$(firstword $(SUBDIRS) $(SUBPROJS))" ]; then echo; fi
264
 
        @$(MAKE) $(if $(filter $@,$(SUBPROJS)), -f $@.mk, \
265
 
                -C $@ $(if $(wildcard $@/emake.mk),-f emake.mk,)) \
266
 
                $(filter-out $(SUBDIRS) $(SUBPROJS) subdirs subprojs,$(MAKECMDGOALS))
 
233
endif
 
234
endif
 
235
 
 
236
$(SUBDIRS):
 
237
        @if [ "$@" = "$(firstword $(SUBDIRS))" ]; then echo; fi
 
238
        @$(MAKE) -C $@ $(filter-out $(SUBDIRS),$(MAKECMDGOALS))
267
239
        @echo
268
240
 
269
241
$(TARGET): $(OBJECTS)
270
242
ifdef MKSTATICLIB
271
243
        $(AR) rcs $(TARGET) $(OBJECTS)
272
244
else
273
 
        $(LD) $(if $(MKSHAREDLIB),-shared) -o $(TARGET) $(LDFLAGS) $(OBJECTS) $(LDPOSTFLAGS)
 
245
        $(LD) $(if $(MKSHAREDLIB),-shared) -o $(TARGET) $(LDFLAGS) $(OBJECTS) $(LDPOSTFLAGS) 
274
246
endif
275
247
 
276
248
%.o %_d.o %_p.o: %.c