reworked how Makefile works wrt to win32 and final builds
[physics.git] / Makefile
... / ...
CommitLineData
1
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
18
19OPTFLAGS := -O2
20DBGFLAGS := -ggdb
21PRFFLAGS := ${DBGFLAGS} -pg
22MYFLAGS := -Wall -pedantic -ansi
23
24VALFLAGS := --leak-check=full
25ifeq (${WIN32},1)
26 CXXFLAGS := ${OPTFLAGS}
27else ifeq (${FINAL},1)
28 CXXFLAGS := ${OPTFLAGS}
29else
30 CXXFLAGS := ${MYFLAGS} ${DBGFLAGS}
31endif
32
33ifeq (${WIN32},1)
34 CXX := mingw32-g++
35else
36 CXX := g++
37endif
38
39DIRS := # := start
40
41SRCSDIR := src/
42SRCS := # := start
43
44ifeq (${WIN32},1)
45 OBJSDIR := objs-mingw32/
46else ifeq (${FINAL},1)
47 OBJSDIR := objs/
48else
49 OBJSDIR := objsd/
50endif
51OBJS := # := start
52
53DEPSDIR := deps/
54DEPS := # := start
55
56# include all of the dir.mk
57DIRMK := dir.mk
58include ${SRCSDIR}${DIRMK}
59
60ifeq (${WIN32},1)
61 WORKINGDIR := bin-mingw32/
62else ifeq (${FINAL},1)
63 WORKINGDIR := bin/
64else
65 WORKINGDIR := bind/
66endif
67
68CFGDIRNAME := configs/
69SRCCFGDIR := ${CFGDIRNAME}
70DSTCFGDIR := ${WORKINGDIR}${CFGDIRNAME}
71
72CFGS := # := start
73CFGS += keys.cfg
74CFGS := $(addprefix ${DSTCFGDIR},${CFGS})
75
76SRCLIBSDIR := libs/
77DSTLIBSDIR := ${WORKINGDIR}
78
79LIBSTXT := # := start
80LIBSTXT += COPYING-SDL
81LIBSTXT += README-SDL
82LIBSTXT += VERSION-SDL
83LIBSTXT := $(addprefix ${DSTLIBSDIR},${LIBSTXT})
84
85LIBSCPY := # := start
86ifeq (${WIN32},1)
87 LIBSCPY += SDL.dll
88else
89 LIBSCPY += libSDL.so
90endif
91LIBSCPY := $(addprefix ${DSTLIBSDIR},${LIBSCPY})
92
93SRCTXTDIR :=
94DSTTXTDIR := ${WORKINGDIR}
95
96TXT := # := start
97TXT += COPYING
98TXT := $(addprefix ${DSTTXTDIR},${TXT})
99
100ifeq (${WIN32},1)
101 TARGETNAME := run_physics.exe
102else
103 TARGETNAME := run_physics
104endif
105TARGETTMP := ${OBJSDIR}${TARGETNAME}
106TARGET := ${WORKINGDIR}${TARGETNAME}
107
108DEPSBLDDIRS := ${DEPSDIR} $(addprefix ${DEPSDIR},${DIRS})
109OBJSBLDDIRS := ${OBJSDIR} $(addprefix ${OBJSDIR},${DIRS})
110BLDDIRS := ${OBJSBLDDIRS} ${DEPSBLDDIRS} ${WORKINGDIR} ${DSTCFGDIR}
111
112INCDIRS := ${SRCSDIR}
113
114
115PRNTFMT := printf "%-5s: %s\n"
116
117VERBOSE := 0
118
119ifeq (${VERBOSE},0)
120 # quiet the printf command
121 Q1 := @
122 # quiet the command that is `replaced' by an echo
123 Q2 := @
124else
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 :=
129endif
130
131.PHONY: all
132all: ${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
180CLEANCMDS := cleanbin cleanobjs cleandeps clean cleanall
181
182.PHONY: cleanbin
183cleanbin:
184 ${Q1}${PRNTFMT} "rm" "${WORKINGDIR}"
185 ${Q2}rm -rf ${WORKINGDIR}
186
187.PHONY: cleanobjs
188cleanobjs:
189 ${Q1}${PRNTFMT} "rm" "${OBJSDIR}"
190 ${Q2}rm -rf ${OBJSDIR}
191
192.PHONY: cleandeps
193cleandeps:
194 ${Q1}${PRNTFMT} "rm" "${DEPSDIR}"
195 ${Q2}rm -rf ${DEPSDIR}
196
197.PHONY: clean
198clean: cleanobjs
199 ${Q1}${PRNTFMT} "rm" "${TARGET}"
200 ${Q2}rm -rf ${TARGET}
201
202.PHONY: cleanall
203cleanall: clean cleanbin cleandeps
204
205.PHONY: gitclean
206gitclean:
207 ${Q1}${PRNTFMT} "git clean" "showing; use gitcleanf to force removal"
208 ${Q2}git clean -nxd
209
210.PHONY: gitcleanf
211gitcleanf:
212 ${Q1}${PRNTFMT} "git clean" "forcing"
213 ${Q2}git clean -fxd
214
215.PHONY: tar
216tar: physics.tar.bz2
217
218.PHONY: physics.tar.bz2
219physics.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
225run: all
226ifeq (${WIN32},1)
227 cd ${WORKINGDIR}; wine ${TARGETNAME}
228else
229 cd ${WORKINGDIR}; ./${TARGETNAME}
230endif
231
232.PHONY: gdb
233gdb: all
234 cd ${WORKINGDIR}; gdb ${TARGETNAME}
235
236.PHONY: cgdb
237cgdb: all
238 cd ${WORKINGDIR}; cgdb ${TARGETNAME}
239
240.PHONY: val
241val: all
242 cd ${WORKINGDIR}; valgrind ${VALFLAGS} ./${TARGETNAME}
243
244.PHONY: prof
245prof: 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
251ifeq ($(findstring ${MAKECMDGOALS},${CLEANCMDS}),)
252 -include ${DEPS}
253endif