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