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