PTdecode/Makefile

Thu, 24 Sep 2009 22:29:12 +0100

author
Philip Pemberton <philpem@philpem.me.uk>
date
Thu, 24 Sep 2009 22:29:12 +0100
changeset 20
f8ca98e5f586
parent 5
1204ebf9340d
child 23
f2c7acb4a258
permissions
-rw-r--r--

remove accidentally-included debug code (oops!)

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