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