reworked how Makefile works wrt to win32 and final builds
[physics.git] / Makefile
CommitLineData
ad9f1fb6 1
d6897213
PG
2# set this on the command line to 1 to get a windows 32 build
3WIN32 := 0
4
5# set this on the command line to 1 to get a final (none debug) build
6FINAL := 0
7
8
9ifeq (${WIN32},1)
10 LIBGL := -lopengl32 -lglu32
11 LIBSDL := `/usr/mingw32/bin/sdl-config --libs`
12else
13 LIBGL := -lGL -lGLU
14 LIBSDL := `sdl-config --libs`
15endif
16LIBS := ${LIBSDL} ${LIBGL}
17
44b079f8
PG
18
19OPTFLAGS := -O2
20DBGFLAGS := -ggdb
21PRFFLAGS := ${DBGFLAGS} -pg
a5aefcc6 22MYFLAGS := -Wall -pedantic -ansi
ad9f1fb6 23
88e62c4f 24VALFLAGS := --leak-check=full
d6897213
PG
25ifeq (${WIN32},1)
26 CXXFLAGS := ${OPTFLAGS}
27else ifeq (${FINAL},1)
28 CXXFLAGS := ${OPTFLAGS}
29else
30 CXXFLAGS := ${MYFLAGS} ${DBGFLAGS}
31endif
88e62c4f 32
d6897213
PG
33ifeq (${WIN32},1)
34 CXX := mingw32-g++
35else
36 CXX := g++
37endif
ad9f1fb6 38
0376f786 39DIRS := # := start
0376f786 40
ec424960
PG
41SRCSDIR := src/
42SRCS := # := start
44b079f8 43
d6897213
PG
44ifeq (${WIN32},1)
45 OBJSDIR := objs-mingw32/
46else ifeq (${FINAL},1)
47 OBJSDIR := objs/
48else
49 OBJSDIR := objsd/
50endif
51OBJS := # := start
16f14459
PG
52
53DEPSDIR := deps/
54DEPS := # := start
55
70468808 56# include all of the dir.mk
07af3cf8
PG
57DIRMK := dir.mk
58include ${SRCSDIR}${DIRMK}
869b3330 59
d6897213
PG
60ifeq (${WIN32},1)
61 WORKINGDIR := bin-mingw32/
62else ifeq (${FINAL},1)
63 WORKINGDIR := bin/
64else
65 WORKINGDIR := bind/
66endif
ec424960 67
47b9b09e
PG
68CFGDIRNAME := configs/
69SRCCFGDIR := ${CFGDIRNAME}
70DSTCFGDIR := ${WORKINGDIR}${CFGDIRNAME}
869b3330
PG
71
72CFGS := # := start
73CFGS += keys.cfg
74CFGS := $(addprefix ${DSTCFGDIR},${CFGS})
75
47b9b09e
PG
76SRCLIBSDIR := libs/
77DSTLIBSDIR := ${WORKINGDIR}
78
bcd46b8f
PG
79LIBSTXT := # := start
80LIBSTXT += COPYING-SDL
81LIBSTXT += README-SDL
82LIBSTXT += VERSION-SDL
83LIBSTXT := $(addprefix ${DSTLIBSDIR},${LIBSTXT})
47b9b09e 84
bcd46b8f 85LIBSCPY := # := start
d6897213
PG
86ifeq (${WIN32},1)
87 LIBSCPY += SDL.dll
88else
89 LIBSCPY += libSDL.so
90endif
bcd46b8f
PG
91LIBSCPY := $(addprefix ${DSTLIBSDIR},${LIBSCPY})
92
93SRCTXTDIR :=
94DSTTXTDIR := ${WORKINGDIR}
95
96TXT := # := start
97TXT += COPYING
98TXT := $(addprefix ${DSTTXTDIR},${TXT})
47b9b09e 99
d6897213
PG
100ifeq (${WIN32},1)
101 TARGETNAME := run_physics.exe
102else
103 TARGETNAME := run_physics
104endif
9d5863c5 105TARGETTMP := ${OBJSDIR}${TARGETNAME}
ec424960 106TARGET := ${WORKINGDIR}${TARGETNAME}
9d5863c5 107
07af3cf8
PG
108DEPSBLDDIRS := ${DEPSDIR} $(addprefix ${DEPSDIR},${DIRS})
109OBJSBLDDIRS := ${OBJSDIR} $(addprefix ${OBJSDIR},${DIRS})
869b3330 110BLDDIRS := ${OBJSBLDDIRS} ${DEPSBLDDIRS} ${WORKINGDIR} ${DSTCFGDIR}
42f62a75 111
3d1f0813
PG
112INCDIRS := ${SRCSDIR}
113
ad9f1fb6 114
70468808
PG
115PRNTFMT := printf "%-5s: %s\n"
116
393654cf
PG
117VERBOSE := 0
118
119ifeq (${VERBOSE},0)
07af3cf8 120 # quiet the printf command
393654cf
PG
121 Q1 := @
122 # quiet the command that is `replaced' by an echo
123 Q2 := @
124else
07af3cf8 125 # EAT the printf command as if it was not there
393654cf
PG
126 Q1 := @true # NOTE: the space between @true and the # is VERY important!!
127 # do not quiet the command output
128 Q2 :=
129endif
ad9f1fb6 130
046b034c 131.PHONY: all
bcd46b8f 132all: ${TARGET} ${CFGS} ${LIBSTXT} ${LIBSCPY} ${TXT}
47b9b09e
PG
133
134# cause the fancy $$ directory rules to work out
135.SECONDEXPANSION:
37a74d97 136
869b3330
PG
137# how to link the main target
138${TARGETTMP}: ${OBJS}
bf7e6563 139 ${Q1}${PRNTFMT} "${CXX}" "$@"
869b3330 140 ${Q2}${CXX} ${CXXFLAGS} -o $@ $^ ${LIBS}
ad9f1fb6 141
9d5863c5 142# rule to copy tmp target to working directory
ebea1138 143${TARGET}: ${TARGETTMP} | ${WORKINGDIR}
bf7e6563 144 ${Q1}${PRNTFMT} "cp" "$@"
37a74d97 145 ${Q2}cp $< $@
9d5863c5 146
47b9b09e 147# rule to copy the config files into the working directory
bcd46b8f 148${DSTCFGDIR}%: ${SRCCFGDIR}% | $$(dir $$@)
47b9b09e
PG
149 ${Q1}${PRNTFMT} "cp" "$@"
150 ${Q2}cp $< $@
151
152# rule to copy the library files into the working directory
153${DSTLIBSDIR}%: ${SRCLIBSDIR}% | $$(dir $$@)
154 ${Q1}${PRNTFMT} "cp" "$@"
155 ${Q2}cp $< $@
156
bcd46b8f
PG
157# rule to copy the library files into the working directory
158${DSTTXTDIR}%: ${SRCTXTDIR}% | $$(dir $$@)
159 ${Q1}${PRNTFMT} "cp" "$@"
160 ${Q2}cp $< $@
161
42f62a75
PG
162# how to make a directory
163${BLDDIRS}:
2688190c 164 ${Q1}${PRNTFMT} "mkdir" "$@"
42f62a75
PG
165 ${Q2}mkdir -p $@
166
869b3330 167# rule to make an object file from a .cpp
2688190c 168${OBJSDIR}%.o: ${SRCSDIR}%.cpp | $$(dir $$@)
bf7e6563 169 ${Q1}${PRNTFMT} "${CXX}" "$@"
3d1f0813 170 ${Q2}${CXX} ${CXXFLAGS} -c -o $@ $< -I "${INCDIRS}"
869b3330 171
a4bb9ac2 172# rule to make a depend file from a .cpp
5f264bf7
PG
173# be clever and escape the / chars in file paths
174# DON'T simply use another sed delimiter or it can't appear in the file paths
2688190c 175${DEPSDIR}%.d: ${SRCSDIR}%.cpp | $$(dir $$@)
bf7e6563 176 ${Q1}${PRNTFMT} "DEP" "$@"
3d1f0813 177 ${Q2}${CXX} -MM ${CXXFLAGS} $< -I "${INCDIRS}" | \
5f264bf7 178 sed 's/\(^.*\):/$(subst /,\/,${OBJSDIR}\1 $@):/' > $@
ad9f1fb6 179
d6897213
PG
180CLEANCMDS := cleanbin cleanobjs cleandeps clean cleanall
181
47b9b09e
PG
182.PHONY: cleanbin
183cleanbin:
184 ${Q1}${PRNTFMT} "rm" "${WORKINGDIR}"
185 ${Q2}rm -rf ${WORKINGDIR}
186
4537196f
PG
187.PHONY: cleanobjs
188cleanobjs:
70468808
PG
189 ${Q1}${PRNTFMT} "rm" "${OBJSDIR}"
190 ${Q2}rm -rf ${OBJSDIR}
ad9f1fb6 191
4537196f
PG
192.PHONY: cleandeps
193cleandeps:
70468808
PG
194 ${Q1}${PRNTFMT} "rm" "${DEPSDIR}"
195 ${Q2}rm -rf ${DEPSDIR}
4537196f
PG
196
197.PHONY: clean
47b9b09e 198clean: cleanobjs
d6897213
PG
199 ${Q1}${PRNTFMT} "rm" "${TARGET}"
200 ${Q2}rm -rf ${TARGET}
4537196f
PG
201
202.PHONY: cleanall
47b9b09e 203cleanall: clean cleanbin cleandeps
ad9f1fb6 204
d893cce2
PG
205.PHONY: gitclean
206gitclean:
bf7e6563 207 ${Q1}${PRNTFMT} "git clean" "showing; use gitcleanf to force removal"
5f264bf7 208 ${Q2}git clean -nxd
d893cce2
PG
209
210.PHONY: gitcleanf
211gitcleanf:
bf7e6563 212 ${Q1}${PRNTFMT} "git clean" "forcing"
5f264bf7 213 ${Q2}git clean -fxd
d893cce2 214
7adc59fe 215.PHONY: tar
5f264bf7 216tar: physics.tar.bz2
8381f595 217
5f264bf7
PG
218.PHONY: physics.tar.bz2
219physics.tar.bz2:
bf7e6563
PG
220 @${PRNTFMT} "git archive" "Warning, archives HEAD not current"
221 ${Q1}${PRNTFMT} "git archive" "physics.tar.bz2"
222 ${Q2}git archive --prefix=physics/ HEAD | bzip2 > physics.tar.bz2
ad9f1fb6 223
046b034c
PG
224.PHONY: run
225run: all
d6897213
PG
226ifeq (${WIN32},1)
227 cd ${WORKINGDIR}; wine ${TARGETNAME}
228else
67b5017f 229 cd ${WORKINGDIR}; ./${TARGETNAME}
d6897213 230endif
ad9f1fb6 231
6aad402a
PG
232.PHONY: gdb
233gdb: all
67b5017f
PG
234 cd ${WORKINGDIR}; gdb ${TARGETNAME}
235
236.PHONY: cgdb
237cgdb: all
238 cd ${WORKINGDIR}; cgdb ${TARGETNAME}
6aad402a 239
63a52c99
PG
240.PHONY: val
241val: all
823da4ea 242 cd ${WORKINGDIR}; valgrind ${VALFLAGS} ./${TARGETNAME}
63a52c99
PG
243
244.PHONY: prof
67b5017f 245prof: run
06699cc7
PG
246 cd ${WORKINGDIR}; gprof -b ${TARGETNAME} > prof
247 cd ${WORKINGDIR}; kprof -f prof
ad9f1fb6 248
4e77f48b 249
d6897213
PG
250# Do not include deps files when doing a _single_ clean operation
251ifeq ($(findstring ${MAKECMDGOALS},${CLEANCMDS}),)
252 -include ${DEPS}
253endif