Thu, 24 Sep 2009 22:29:12 +0100
remove accidentally-included debug code (oops!)
1 # Phil's multiplatform makefile template
2 # With auto-incrementing build number and automatic version.h generation
3 # Version 1.4, 2009-01-27
4 #
5 # The latest version of this Makefile can be found at http://www.philpem.me.uk/
6 #
7 #
8 # Copyright (c) 2009 Philip Pemberton <code@philpem.me.uk>
9 #
10 # Permission is hereby granted, free of charge, to any person obtaining a copy
11 # of this software and associated documentation files (the "Software"), to deal
12 # in the Software without restriction, including without limitation the rights
13 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 # copies of the Software, and to permit persons to whom the Software is
15 # furnished to do so, subject to the following conditions:
16 #
17 # The above copyright notice and this permission notice shall be included in
18 # all copies or substantial portions of the Software.
19 #
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 # THE SOFTWARE.
27 #
28 #
29 # Instructions for use:
30 # Run 'make init' to create the required directories
31 # Add your source files to the 'SOURCES' list, and change the TARGET filename
32 # Set the desired build type and platform in the BUILD_TYPE and PLATFORM
33 # variables respectively
34 # Set your project type (C only, or C++) in the SRC_TYPE variable
35 # Add any libraries you need to link against to the 'LIB' list
36 # Run 'make'
37 #
38 # Object files are created in the 'obj' subdirectory, from source code in the
39 # 'src' directory. Dependency files are created in the 'dep' directory from
40 # the same source code the object files are created from.
41 #
42 # Supported targets are:
43 # all Build everything.
44 # update-revision Increment the build number without building anything.
45 # clean-versioninfo Delete src/version.h (will be rebuilt on the next
46 # 'make all').
47 # init Initialise the build system for a new project.
48 # WARNING: overwrites .buildnum and src/version.h.in!
49 # cleandep Delete all dependency files.
50 # clean Delete all dependency, intermediate and target files.
51 # tidy Delete all dependency and intermediate files, leaving
52 # the target file intact.
53 #
54 # If you want to reset the build number to zero, delete '.buildnum'. This
55 # should be done whenever the major or minor version changes. Excluding
56 # .buildnum from version control may also be a good idea, depending on how
57 # you want your build numbers to work.
58 #
59 # The BUILD_TYPE variable contains the current build type. There are two
60 # supported build types:
61 # debug Debug mode - object files are compiled with debug information
62 # and the target is left unstripped.
63 # release Release mode - object files are not compiled with debug info,
64 # and the target is fed through strip to remove redundant
65 # data.
66 #
67 # The PLATFORM variable contains the current target platform. There are two
68 # supported platforms:
69 # linux GNU/Linux with GNU Compiler Collection
70 # win32 Windows 32-bit with MinGW
71 #
72 # The EXTSRC variable is used to specify other files to build. It is typically
73 # used to specify platform or build-type specific source files, e.g.
74 #
75 # ifeq ($(BUILD_TYPE),debug-memwatch)
76 # CFLAGS += -g -ggdb
77 # CPPFLAGS += -DMEMWATCH
78 # INCPATH += ./memwatch
79 # EXTSRC += memwatch/memwatch.c
80 # endif
81 #
82 # (example taken from one of my projects that allowed the use of Memwatch to
83 # track down memory allocation/deallocation bugs)
84 #
86 ####
87 # Build configuration
88 ####
90 # version information -- major.minor.extra
91 # note that VER_EXTRA can be overridden on the command line, e.g.:
92 # make VER_EXTRA=12345 all
93 VER_MAJOR = 0
94 VER_MINOR = 0
95 VER_EXTRA ?=
97 # build platform: win32 or linux
98 PLATFORM ?= linux
99 # build type: release or debug
100 BUILD_TYPE ?= debug
102 # target executable
103 TARGET = ptdecode
105 # source files that produce object files
106 SRC = main.cpp
108 # source type - either "c" or "cpp" (C or C++)
109 SRC_TYPE = cpp
111 # additional object files that don't necessarily include source
112 EXT_OBJ =
113 # libraries to link in -- these will be specified as "-l" parameters, the -l
114 # is prepended automatically
115 LIB = m pthread X11
116 # library paths -- where to search for the above libraries
117 LIBPATH =
118 # include paths -- where to search for #include files (in addition to the
119 # standard paths
120 INCPATH = CImg-1.3.0
121 # garbage files that should be deleted on a 'make clean' or 'make tidy'
122 GARBAGE =
124 # extra dependencies - files that we don't necessarily know how to build, but
125 # that are required for building the application; e.g. object files or
126 # libraries in sub or parent directories
127 EXTDEP =
129 ####
130 # Win32 target-specific settings
131 ####
132 ifeq ($(strip $(PLATFORM)),win32)
133 # windows executables have a .exe suffix
134 TARGET := $(addsuffix .exe,$(TARGET))
135 # console mode application
136 EXT_CFLAGS = -mconsole
137 endif
140 ####
141 # Tool setup
142 ####
143 MAKE = make
144 CC = gcc
145 CXX = g++
146 CFLAGS = -Wall -pedantic -std=gnu99 $(EXT_CFLAGS)
147 CXXFLAGS= -Wall -pedantic $(EXT_CXXFLAGS)
148 LDFLAGS = $(EXT_LDFLAGS)
149 RM = rm
150 STRIP = strip
152 ###############################################################################
153 # You should not need to touch anything below here, unless you're adding a new
154 # platform or build type (or changing the version string format)
155 ###############################################################################
157 ####
158 # A quick sanity check on the platform type
159 ####
160 ifneq ($(PLATFORM),linux)
161 ifneq ($(PLATFORM),win32)
162 $(error Platform '$(PLATFORM)' not supported. Supported platforms are: linux, win32)
163 endif
164 endif
166 ####
167 # Version info generation
168 ####
169 # get the current build number
170 VER_BUILDNUM = $(shell cat .buildnum)
172 # there are two ways to get the SVN rev - use svnversion, or use svn info
173 # then pipe through awk. which one you use is up to you.
174 VER_SVNREV = $(shell LANG=C svn info 2>/dev/null || echo 'Revision: 0' | awk '/^Revision:/ { print$$2 }' )
175 #VER_SVNREV = $(shell svnversion .)
177 # if the version string is "exported", then the CSD was not checked out of SVN
178 # note that if the CSD is not an SVN checkout, then @@svnrev@@ will be set to
179 # zero.
180 ifeq ($(VER_SVNREV),exported)
181 VER_SVNREV = 0
182 endif
184 # start creating the revision string
185 VER_FULLSTR = $(VER_MAJOR).$(VER_MINOR).$(VER_BUILDNUM)$(VER_EXTRA)
187 # if this is an SVN release, include the SVN revision in the version string
188 ifneq ($(VER_SVNREV),0)
189 VER_FULLSTR += (svn $(VER_SVNREV))
190 endif
193 ####
194 # Build-type specific configuration
195 ####
196 ifeq ($(BUILD_TYPE),debug)
197 CFLAGS += -g -ggdb
198 CXXFLAGS += -g -ggdb
199 else
200 ifeq ($(BUILD_TYPE),release)
201 CFLAGS += -O2
202 CXXFLAGS += -O2
203 else
204 $(error Unsupported build type: '$(BUILD_TYPE)')
205 endif
206 endif
208 ####
209 # rules
210 ####
212 # object files
213 OBJ = $(addprefix obj/, $(addsuffix .o, $(basename $(SRC))) $(EXT_OBJ)) $(addsuffix .o, $(basename $(EXTSRC)))
215 # dependency files
216 DEPFILES = $(addprefix dep/, $(addsuffix .d, $(basename $(SRC))) $(EXT_OBJ)) $(addsuffix .d, $(basename $(EXTSRC)))
218 # path commands
219 LIBLNK = $(addprefix -l, $(LIB))
220 LIBPTH = $(addprefix -L, $(LIBPATH))
221 INCPTH = $(addprefix -I, $(INCPATH))
223 CPPFLAGS += $(INCPTH)
225 ####
226 # Make sure there is at least one object file to be linked in
227 ####
228 ifeq ($(strip $(OBJ)),)
229 $(error Unable to build: no object or source files specified in Makefile)
230 endif
232 ####
233 # targets
234 ####
235 .PHONY: default all update-revision versionheader clean-versioninfo init cleandep clean tidy
237 all: update-revision
238 @$(MAKE) versionheader
239 $(MAKE) $(TARGET)
241 # increment the current build number
242 NEWBUILD=$(shell expr $(VER_BUILDNUM) + 1)
243 update-revision:
244 @echo $(NEWBUILD) > .buildnum
246 versionheader:
247 @sed -e 's/@@date@@/$(shell LC_ALL=C date)/g' \
248 -e 's/@@time@@/$(shell LC_ALL=C date +%T)/g' \
249 -e 's/@@whoami@@/$(shell whoami)/g' \
250 -e 's/@@hostname@@/$(shell hostname)/g' \
251 -e 's|@@compiler@@|$(shell $(CC) $(CFLAGS) -v 2>&1 | tail -n 1 | sed -e "s;|;/;")|g' \
252 -e 's/@@majorver@@/$(VER_MAJOR)/g' \
253 -e 's/@@minorver@@/$(VER_MINOR)/g' \
254 -e 's/@@extraver@@/$(subst \",,$(VER_EXTRA))/g' \
255 -e 's/@@buildnum@@/$(VER_BUILDNUM)/g' \
256 -e 's/@@buildtype@@/$(BUILD_TYPE)/g' \
257 -e 's/@@svnrev@@/$(VER_SVNREV)/g' \
258 -e 's/@@fullverstr@@/$(VER_FULLSTR)/g' \
259 -e 's/@@cflags@@/$(CFLAGS)/g' \
260 < src/version.h.in > src/version.h
262 # version.h creation stuff based on code from the Xen makefile
263 clean-versioninfo:
264 @if [ ! -r src/version.h -o -O src/version.h ]; then \
265 rm -f src/version.h; \
266 fi
267 @echo 0 > .buildnum
269 # initialise the build system for a new project
270 init:
271 @mkdir -p src dep obj
272 @echo 0 > .buildnum
273 @echo '#define VER_COMPILE_DATE "@@date@@"' > src/version.h.in
274 @echo '#define VER_COMPILE_TIME "@@time@@"' >> src/version.h.in
275 @echo '#define VER_COMPILE_BY "@@whoami@@"' >> src/version.h.in
276 @echo '#define VER_COMPILE_HOST "@@hostname@@"' >> src/version.h.in
277 @echo '#define VER_COMPILER "@@compiler@@"' >> src/version.h.in
278 @echo '#define VER_BUILD_TYPE "@@buildtype@@"' >> src/version.h.in
279 @echo '#define VER_CFLAGS "@@cflags@@"' >> src/version.h.in
280 @echo '' >> src/version.h.in
281 @echo '#define VER_MAJOR @@majorver@@' >> src/version.h.in
282 @echo '#define VER_MINOR @@minorver@@' >> src/version.h.in
283 @echo '#define VER_BUILDNUM @@buildnum@@' >> src/version.h.in
284 @echo '#define VER_EXTRA "@@extraver@@"' >> src/version.h.in
285 @echo '#define VER_SVNREV @@svnrev@@' >> src/version.h.in
286 @echo '' >> src/version.h.in
287 @echo '#define VER_FULLSTR "@@fullverstr@@"' >> src/version.h.in
288 @echo '' >> src/version.h.in
289 @echo Build system initialised
291 # remove the dependency files
292 cleandep:
293 -rm $(DEPFILES)
295 # remove the dependency files and any target or intermediate build files
296 clean: cleandep clean-versioninfo
297 -rm $(OBJ) $(TARGET) $(GARBAGE)
299 # remove any dependency or intermediate build files
300 tidy: cleandep clean-versioninfo
301 -rm $(OBJ) $(GARBAGE)
303 #################################
305 $(TARGET): $(OBJ) $(EXTDEP)
306 ifeq ($(SRC_TYPE),c)
307 $(CC) $(CXXFLAGS) $(LDFLAGS) $(OBJ) $(LIBPTH) $(LIBLNK) -o $@
308 else
309 $(CXX) $(CXXFLAGS) $(LDFLAGS) $(OBJ) $(LIBPTH) $(LIBLNK) -o $@
310 endif
311 ifeq ($(BUILD_TYPE),release)
312 $(STRIP) $(TARGET)
313 endif
315 ###
316 # extra rules
317 # example:
318 #src/parser.c: src/parser.h
320 ####
321 # make object files from C source files
322 obj/%.o: src/%.c
323 $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
325 ##
326 # make object files from C++ source files
327 obj/%.o: src/%.cc
328 $(CXX) -c $(CXXFLAGS) $(CPPFLAGS) $< -o $@
330 obj/%.o: src/%.cpp
331 $(CXX) -c $(CXXFLAGS) $(CPPFLAGS) $< -o $@
333 ###
334 # make C files from yacc/bison source
335 src/%.h src/%.c: src/%.y
336 $(YACC) $(YFLAGS) -d $<
337 mv -f y.tab.c $*.c
338 mv -f y.tab.h $*.h
340 ###
341 # make C files from lex/flex source
342 src/%.c: src/%.l
343 $(LEX) $(LFLAGS) -o$@ $<
345 ###
346 # make dependencies for our source files
347 dep/%.d: src/%.c
348 $(CC) -MM $(CPPFLAGS) $< > $@.$$$$; \
349 sed 's,\($*\)\.o[ :]*,obj/\1.o $@ : ,g' < $@.$$$$ > $@; \
350 rm -f $@.$$$$
352 dep/%.d: src/%.cpp
353 $(CXX) -MM $(CPPFLAGS) $< > $@.$$$$; \
354 sed 's,\($*\)\.o[ :]*,obj/\1.o $@ : ,g' < $@.$$$$ > $@; \
355 rm -f $@.$$$$
357 dep/%.d: src/%.cc
358 $(CXX) -MM $(CPPFLAGS) $< > $@.$$$$; \
359 sed 's,\($*\)\.o[ :]*,obj/\1.o $@ : ,g' < $@.$$$$ > $@; \
360 rm -f $@.$$$$
362 ####
363 # pull in the dependency files, but only for 'make $(TARGET)'
364 ####
366 ifeq ($(MAKECMDGOALS),$(TARGET))
367 -include $(DEPFILES)
368 endif