Sun, 28 Nov 2010 23:08:06 +0000
add preliminary main loop
1 # Phil's multiplatform makefile template
2 # With auto-incrementing build number and automatic version.h generation
3 # Version 1.9, 2010-02-15
4 #
5 # The latest version of this Makefile can be found at http://www.philpem.me.uk/
6 #
7 #
8 # Copyright (c) 2010 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 #
85 #
86 # Change history:
87 # 1.9 - Bugfix -- if CFLAGS contained a forward-slash, sed would fall over.
88 # Also added SDL support and fixed the date/time formats. To use SDL,
89 # set ENABLE_SDL to "yes".
90 # 1.8 - Now supports the use of the wxWidgets GUI framework. To turn
91 # this on, set ENABLE_WX to "yes".
92 # 1.7 - Now creates a basic Hgignore file and directory keepers for the
93 # dep and obj directories.
94 # 1.6 - Added CFLAGS and CXXFLAGS to the command-lines for the dependency
95 # building commands. This was causing issues with C99 / C++0x mode.
96 # 1.5 - Added support for Mercurial revision (changeset ID) display
97 # Fixed a few issues with Subversion support (svn: and version 0 would
98 # be displayed for exported code)
99 #
101 ####
102 # Build configuration
103 ####
105 # version information -- major.minor.extra
106 # note that VER_EXTRA can be overridden on the command line, e.g.:
107 # make VER_EXTRA=12345 all
108 VER_MAJOR = 0
109 VER_MINOR = 0
110 VER_EXTRA ?=
112 # build platform: win32 or linux
113 PLATFORM ?= linux
114 # build type: release or debug
115 BUILD_TYPE ?= debug
117 # target executable
118 TARGET = freebee
120 # source files that produce object files
121 SRC = main.c musashi/m68kcpu.c musashi/m68kdasm.c musashi/m68kops.c musashi/m68kopac.c musashi/m68kopdm.c musashi/m68kopnz.c
123 # source type - either "c" or "cpp" (C or C++)
124 SRC_TYPE = c
126 # additional object files that don't necessarily include source
127 EXT_OBJ =
128 # libraries to link in -- these will be specified as "-l" parameters, the -l
129 # is prepended automatically
130 LIB =
131 # library paths -- where to search for the above libraries
132 LIBPATH =
133 # include paths -- where to search for #include files (in addition to the
134 # standard paths
135 INCPATH =
136 # garbage files that should be deleted on a 'make clean' or 'make tidy'
137 GARBAGE = obj/musashi/m68kmake obj/musashi/m68kmake.exe obj/musashi/m68kmake.o
139 # extra dependencies - files that we don't necessarily know how to build, but
140 # that are required for building the application; e.g. object files or
141 # libraries in sub or parent directories
142 EXTDEP =
144 # Extra libraries
145 # wxWidgets: set to "yes" to enable, anything else to disable
146 ENABLE_WX = no
147 # wxWidgets: list of wxWidgets libraries to enable
148 WX_LIBS = std
149 # SDL: set to "yes" to enable, anything else to disable
150 ENABLE_SDL = yes
152 ####
153 # Win32 target-specific settings
154 ####
155 ifeq ($(strip $(PLATFORM)),win32)
156 # windows executables have a .exe suffix
157 TARGET := $(addsuffix .exe,$(TARGET))
158 # console mode application
159 EXT_CFLAGS = -mconsole
160 endif
163 ####
164 # Tool setup
165 ####
166 MAKE = make
167 CC = gcc
168 CXX = g++
169 CFLAGS = -Wall -pedantic -std=gnu99 $(EXT_CFLAGS)
170 CXXFLAGS= -Wall -pedantic -std=gnu++0x $(EXT_CXXFLAGS)
171 LDFLAGS = $(EXT_LDFLAGS)
172 RM = rm
173 STRIP = strip
175 ###############################################################################
176 # You should not need to touch anything below here, unless you're adding a new
177 # platform or build type (or changing the version string format)
178 ###############################################################################
180 ####
181 # A quick sanity check on the platform type
182 ####
183 ifneq ($(PLATFORM),linux)
184 ifneq ($(PLATFORM),win32)
185 $(error Platform '$(PLATFORM)' not supported. Supported platforms are: linux, win32)
186 endif
187 endif
189 ####
190 # Version info generation
191 ####
192 # get the current build number
193 VER_BUILDNUM = $(shell cat .buildnum)
195 #### --- begin Subversion revision grabber ---
196 # there are two ways to get the SVN revision - use svnversion, or use svn info
197 # then pipe through awk. which one you use is up to you.
198 VER_SVNREV = $(shell LANG=C svn info 2>/dev/null || echo 'Revision: exported' | awk '/^Revision:/ { print$$2 }' )
199 #VER_SVNREV = $(shell svnversion .)
201 # if the version string is "exported", then the CSD was not checked out of SVN
202 # note that if the CSD is not an SVN checkout, then @@svnrev@@ will be set to
203 # zero.
204 ifeq ($(VER_SVNREV),exported)
205 VER_VCS = none
206 VER_VCSREV = 0
207 else
208 VER_VCS = svn
209 VER_VCSREV = $(VER_SVNREV)
210 endif
212 #### --- begin Mercurial revision grabber ---
213 # If SVN didn't give us a revision, try Mercurial instead
214 ifeq ($(VER_VCS),none)
215 # get the current Mercurial changeset number
216 VER_HGREV=$(shell ((hg tip --template "{node|short}") || echo "000000000000") 2>/dev/null)
217 ifneq ($(VER_HGREV),000000000000)
218 # a non-empty repo
219 VER_VCS = hg
220 VER_VCSREV = $(VER_HGREV)
221 else
222 # either an empty Hg repo, or no repo at all
223 VER_VCS = none
224 VER_VCSREV = 0
225 endif
226 endif
228 #### --- end version grabbers ---
230 # start creating the revision string
231 VER_FULLSTR = $(VER_MAJOR).$(VER_MINOR).$(VER_BUILDNUM)$(VER_EXTRA)
233 # if this is a VCS release, include the SVN revision in the version string
234 # also create a revision string that is either "svn:12345", "hg:12345" or
235 # blank
236 ifneq ($(VER_VCS),none)
237 VER_FULLSTR += ($(VER_VCS) $(VER_VCSREV))
238 VER_VCSSTR = $(VER_VCS):$(VER_VCSREV)
239 else
240 VER_VCSSTR =
241 endif
244 ####
245 # Build-type specific configuration
246 ####
247 ifeq ($(BUILD_TYPE),debug)
248 CFLAGS += -g -ggdb -DDEBUG
249 CXXFLAGS += -g -ggdb -DDEBUG
250 else
251 ifeq ($(BUILD_TYPE),release)
252 CFLAGS += -O2
253 CXXFLAGS += -O2
254 else
255 $(error Unsupported build type: '$(BUILD_TYPE)')
256 endif
257 endif
259 ####
260 # wxWidgets support
261 ####
262 ifeq ($(ENABLE_WX),yes)
263 ifeq ($(BUILD_TYPE),debug)
264 LIBLNK += `wx-config --debug --libs $(WX_LIBS)`
265 CFLAGS += `wx-config --debug --cflags $(WX_LIBS)`
266 CXXFLAGS += `wx-config --debug --cxxflags $(WX_LIBS)`
267 CPPFLAGS += `wx-config --debug --cppflags $(WX_LIBS)`
268 else
269 ifeq ($(BUILD_TYPE),release)
270 LIBLNK += `wx-config --libs $(WX_LIBS)`
271 CFLAGS += `wx-config --cflags $(WX_LIBS)`
272 CPPFLAGS += `wx-config --cppflags $(WX_LIBS)`
273 CXXFLAGS += `wx-config --cxxflags $(WX_LIBS)`
274 else
275 $(error Unsupported build type: '$(BUILD_TYPE)')
276 endif
277 endif
278 endif
280 ####
281 # SDL support
282 ####
283 ifeq ($(ENABLE_SDL),yes)
284 LIBLNK += $(shell sdl-config --libs)
285 CFLAGS += $(shell sdl-config --cflags)
286 endif
289 ####
290 # rules
291 ####
293 # object files
294 OBJ = $(addprefix obj/, $(addsuffix .o, $(basename $(SRC))) $(EXT_OBJ)) $(addsuffix .o, $(basename $(EXTSRC)))
296 # dependency files
297 DEPFILES = $(addprefix dep/, $(addsuffix .d, $(basename $(SRC))) $(EXT_OBJ)) $(addsuffix .d, $(basename $(EXTSRC)))
299 # path commands
300 LIBLNK += $(addprefix -l, $(LIB))
301 LIBPTH += $(addprefix -L, $(LIBPATH))
302 INCPTH += $(addprefix -I, $(INCPATH))
304 CPPFLAGS += $(INCPTH)
306 ####
307 # Make sure there is at least one object file to be linked in
308 ####
309 ifeq ($(strip $(OBJ)),)
310 $(error Unable to build: no object or source files specified in Makefile)
311 endif
313 ####
314 # targets
315 ####
316 .PHONY: default all update-revision versionheader clean-versioninfo init cleandep clean tidy
318 all: update-revision
319 @$(MAKE) versionheader
320 $(MAKE) $(TARGET)
322 # increment the current build number
323 NEWBUILD=$(shell expr $(VER_BUILDNUM) + 1)
324 update-revision:
325 @echo $(NEWBUILD) > .buildnum
327 versionheader:
328 @sed -e 's/@@datetime@@/$(shell LC_ALL=C date +"%a %d-%b-%Y %T %Z")/g' \
329 -e 's/@@date@@/$(shell LC_ALL=C date +"%a %d-%b-%Y")/g' \
330 -e 's/@@time@@/$(shell LC_ALL=C date +"%T %Z")/g' \
331 -e 's/@@whoami@@/$(shell whoami)/g' \
332 -e 's/@@hostname@@/$(shell hostname)/g' \
333 -e 's|@@compiler@@|$(shell $(CC) $(CFLAGS) -v 2>&1 | tail -n 1 | sed -e "s;|;/;")|g' \
334 -e 's/@@majorver@@/$(VER_MAJOR)/g' \
335 -e 's/@@minorver@@/$(VER_MINOR)/g' \
336 -e 's/@@extraver@@/$(subst \",,$(VER_EXTRA))/g' \
337 -e 's/@@buildnum@@/$(VER_BUILDNUM)/g' \
338 -e 's/@@buildtype@@/$(BUILD_TYPE)/g' \
339 -e 's/@@vcs@@/$(VER_VCS)/g' \
340 -e 's/@@vcsrev@@/$(VER_VCSREV)/g' \
341 -e 's/@@vcsstr@@/$(VER_VCSSTR)/g' \
342 -e 's/@@fullverstr@@/$(VER_FULLSTR)/g' \
343 -e 's#@@cflags@@#$(CFLAGS)#g' \
344 < src/version.h.in > src/version.h
346 # version.h creation stuff based on code from the Xen makefile
347 clean-versioninfo:
348 @if [ ! -r src/version.h -o -O src/version.h ]; then \
349 rm -f src/version.h; \
350 fi
351 @echo 0 > .buildnum
353 # initialise the build system for a new project
354 init:
355 @mkdir -p src dep obj
356 @echo "This file is a directory-keeper. Do not delete it." > dep/.keepme
357 @echo "This file is a directory-keeper. Do not delete it." > obj/.keepme
358 @echo 0 > .buildnum
359 @echo 'syntax: glob' > .hgignore
360 @echo 'obj/*.o' >> .hgignore
361 @echo 'dep/*.d' >> .hgignore
362 @echo '*~' >> .hgignore
363 @echo '.*.sw?' >> .hgignore
364 @echo '#define VER_COMPILE_DATETIME "@@datetime@@"' > src/version.h.in
365 @echo '#define VER_COMPILE_DATE "@@date@@"' >> src/version.h.in
366 @echo '#define VER_COMPILE_TIME "@@time@@"' >> src/version.h.in
367 @echo '#define VER_COMPILE_BY "@@whoami@@"' >> src/version.h.in
368 @echo '#define VER_COMPILE_HOST "@@hostname@@"' >> src/version.h.in
369 @echo '#define VER_COMPILER "@@compiler@@"' >> src/version.h.in
370 @echo '#define VER_BUILD_TYPE "@@buildtype@@"' >> src/version.h.in
371 @echo '#define VER_CFLAGS "@@cflags@@"' >> src/version.h.in
372 @echo '' >> src/version.h.in
373 @echo '#define VER_MAJOR @@majorver@@' >> src/version.h.in
374 @echo '#define VER_MINOR @@minorver@@' >> src/version.h.in
375 @echo '#define VER_BUILDNUM @@buildnum@@' >> src/version.h.in
376 @echo '#define VER_EXTRA "@@extraver@@"' >> src/version.h.in
377 @echo '#define VER_VCSREV "@@vcsstr@@"' >> src/version.h.in
378 @echo '' >> src/version.h.in
379 @echo '#define VER_FULLSTR "@@fullverstr@@"' >> src/version.h.in
380 @echo '' >> src/version.h.in
381 @echo Build system initialised
383 # remove the dependency files
384 cleandep:
385 -rm $(DEPFILES)
387 # remove the dependency files and any target or intermediate build files
388 clean: cleandep clean-versioninfo
389 -rm $(OBJ) $(TARGET) $(GARBAGE)
391 # remove any dependency or intermediate build files
392 tidy: cleandep clean-versioninfo
393 -rm $(OBJ) $(GARBAGE)
395 #################################
397 $(TARGET): $(OBJ) $(EXTDEP)
398 ifeq ($(SRC_TYPE),c)
399 $(CC) $(CXXFLAGS) $(LDFLAGS) $(OBJ) $(LIBPTH) $(LIBLNK) -o $@
400 else
401 $(CXX) $(CXXFLAGS) $(LDFLAGS) $(OBJ) $(LIBPTH) $(LIBLNK) -o $@
402 endif
403 ifeq ($(BUILD_TYPE),release)
404 $(STRIP) $(TARGET)
405 endif
407 ###
408 # extra rules
409 # example:
410 #src/parser.c: src/parser.h
413 ####
414 ## musashi build rules
415 # 68k CPU builder
416 obj/musashi/m68kmake: obj/musashi/m68kmake.o
417 $(CC) $(CFLAGS) $(CPPFLAGS) obj/musashi/m68kmake.o -o $@
418 # 68k CPU sources
419 src/musashi/m68kops.h src/musashi/m68kops.c src/musashi/m68kopac.c src/musashi/m68kopdm.c src/musashi/m68kopnz.c: obj/musashi/m68kmake src/musashi/m68k_in.c
420 ./obj/musashi/m68kmake src/musashi src/musashi/m68k_in.c
422 ####
423 # make object files from C source files
424 obj/%.o: src/%.c
425 $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
427 ##
428 # make object files from C++ source files
429 obj/%.o: src/%.cc
430 $(CXX) -c $(CXXFLAGS) $(CPPFLAGS) $< -o $@
432 obj/%.o: src/%.cpp
433 $(CXX) -c $(CXXFLAGS) $(CPPFLAGS) $< -o $@
435 ###
436 # make C files from yacc/bison source
437 src/%.h src/%.c: src/%.y
438 $(YACC) $(YFLAGS) -d $<
439 mv -f y.tab.c $*.c
440 mv -f y.tab.h $*.h
442 ###
443 # make C files from lex/flex source
444 src/%.c: src/%.l
445 $(LEX) $(LFLAGS) -o$@ $<
447 ###
448 # make dependencies for our source files
449 dep/%.d: src/%.c
450 $(CC) -MM $(CFLAGS) $(CPPFLAGS) $< > $@.$$$$; \
451 sed 's,\($*\)\.o[ :]*,obj/\1.o $@ : ,g' < $@.$$$$ > $@; \
452 rm -f $@.$$$$
454 dep/%.d: src/%.cpp
455 $(CXX) -MM $(CXXFLAGS) $(CPPFLAGS) $< > $@.$$$$; \
456 sed 's,\($*\)\.o[ :]*,obj/\1.o $@ : ,g' < $@.$$$$ > $@; \
457 rm -f $@.$$$$
459 dep/%.d: src/%.cc
460 $(CXX) -MM $(CXXFLAGS) $(CPPFLAGS) $< > $@.$$$$; \
461 sed 's,\($*\)\.o[ :]*,obj/\1.o $@ : ,g' < $@.$$$$ > $@; \
462 rm -f $@.$$$$
464 ####
465 # pull in the dependency files, but only for 'make $(TARGET)'
466 ####
468 ifeq ($(MAKECMDGOALS),$(TARGET))
469 -include $(DEPFILES)
470 endif