renamed to dir.mk files
[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 DIRS += ./
18 DIRS += Entities/
19 DIRS += GameStates/
20 DIRS += Effects/
21 DIRS += config/
22 DIRS += input/
23 DIRS += graphics/
24 DIRS += locks/
25
26 SRCSDIR := src/
27 SRCS    := # := start
28
29 # include all of the dir.mk
30 include $(addprefix ${SRCSDIR},$(addsuffix dir.mk,${DIRS}))
31
32 WORKINGDIR  := bind/
33
34 OBJSDIR := objsd/
35 OBJS    := ${SRCS:.cpp=.o}
36 OBJS    := $(addprefix ${OBJSDIR},${OBJS})
37
38 DEPSDIR := deps/
39 DEPS    := ${SRCS:.cpp=.d}
40 DEPS    := $(addprefix ${DEPSDIR},${DEPS})
41
42 CFGDIRNAME := configs/
43 SRCCFGDIR  := ${CFGDIRNAME}
44 DSTCFGDIR  := ${WORKINGDIR}${CFGDIRNAME}
45
46 CFGS := # := start
47 CFGS += keys.cfg
48 CFGS := $(addprefix ${DSTCFGDIR},${CFGS})
49
50 TARGETNAME  := run_physics
51 TARGETTMP   := ${OBJSDIR}${TARGETNAME}
52 TARGET      := ${WORKINGDIR}${TARGETNAME}
53
54 DEPSBLDDIRS := $(addprefix ${DEPSDIR},${DIRS})
55 OBJSBLDDIRS := $(addprefix ${OBJSDIR},${DIRS})
56 BLDDIRS     := ${OBJSBLDDIRS} ${DEPSBLDDIRS} ${WORKINGDIR} ${DSTCFGDIR}
57
58
59 PRNTFMT := printf "%-5s: %s\n"
60
61 VERBOSE := 0
62
63 ifeq (${VERBOSE},0)
64     # quiet the echo command
65     Q1 := @
66     # quiet the command that is `replaced' by an echo
67     Q2 := @
68 else
69     # EAT the echo command as if it was not there
70     Q1 := @true # NOTE: the space between @true and the # is VERY important!!
71     # do not quiet the command output
72     Q2 :=
73 endif
74
75 .PHONY: all
76 all: ${TARGET} ${CFGS}
77
78 # how to link the main target
79 ${TARGETTMP}: ${OBJS}
80         ${Q1}${PRNTFMT} "${CXX}" "$@"
81         ${Q2}${CXX} ${CXXFLAGS} -o $@ $^ ${LIBS}
82
83 # rule to copy tmp target to working directory
84 ${TARGET}: ${TARGETTMP} | ${WORKINGDIR}
85         ${Q1}${PRNTFMT} "cp" "$@"
86         ${Q2}cp $< $@
87
88 # how to make a directory
89 ${BLDDIRS}:
90         ${Q2}mkdir -p $@
91
92 # rule to make an object file from a .cpp
93 ${OBJSDIR}%.o: ${SRCSDIR}%.cpp | ${OBJSBLDDIRS}
94         ${Q1}${PRNTFMT} "${CXX}" "$@"
95         ${Q2}${CXX} ${CXXFLAGS} -c -o $@ $<
96
97 # rule to make a depend file from a .cpp
98 #   be clever and escape the / chars in file paths
99 #   DON'T simply use another sed delimiter or it can't appear in the file paths
100 ${DEPSDIR}%.d: ${SRCSDIR}%.cpp | ${DEPSBLDDIRS}
101         ${Q1}${PRNTFMT} "DEP" "$@"
102         ${Q2}${CXX} -MM ${CXXFLAGS} $< | \
103                 sed 's/\(^.*\):/$(subst /,\/,${OBJSDIR}\1 $@):/' > $@
104
105 # rule to copy the config files into the working directory
106 ${DSTCFGDIR}%.cfg: ${SRCCFGDIR}%.cfg | ${DSTCFGDIR}
107         ${Q1}${PRNTFMT} "cp" "$@"
108         ${Q2}cp $< $@
109
110
111 tags: ${SRCS}
112         ${Q1}${PRNTFMT} "ctags" "$@"
113         ${Q2}ctags $^
114
115
116 .PHONY: clean
117 clean:
118         ${Q1}${PRNTFMT} "rm" "${OBJSDIR}"
119         ${Q2}rm -rf ${OBJSDIR}
120         ${Q1}${PRNTFMT} "rm" "${TARGET} ${TARGETTMP}"
121         ${Q2}rm -f ${TARGET}
122
123 .PHONY: distclean
124 distclean: clean
125         ${Q1}${PRNTFMT} "rm" "${DEPSDIR}"
126         ${Q2}rm -rf ${DEPSDIR}
127         ${Q1}${PRNTFMT} "rm" "tags prof gmon.out"
128         ${Q2}rm -f tags prof gmon.out
129
130 .PHONY: gitclean
131 gitclean:
132         ${Q1}${PRNTFMT} "git clean" "showing; use gitcleanf to force removal"
133         ${Q2}git clean -nxd
134
135 .PHONY: gitcleanf
136 gitcleanf:
137         ${Q1}${PRNTFMT} "git clean" "forcing"
138         ${Q2}git clean -fxd
139
140 .PHONY: tar
141 tar: physics.tar.bz2
142
143 .PHONY: physics.tar.bz2
144 physics.tar.bz2:
145         @${PRNTFMT} "git archive" "Warning, archives HEAD not current"
146         ${Q1}${PRNTFMT} "git archive" "physics.tar.bz2"
147         ${Q2}git archive --prefix=physics/ HEAD | bzip2 > physics.tar.bz2
148
149 .PHONY: run
150 run: all
151         cd ${WORKINGDIR}; ./${TARGETNAME}
152
153 .PHONY: gdb
154 gdb: all
155         cd ${WORKINGDIR}; gdb ${TARGETNAME}
156
157 .PHONY: cgdb
158 cgdb: all
159         cd ${WORKINGDIR}; cgdb ${TARGETNAME}
160
161 .PHONY: val
162 val: all
163         cd ${WORKINGDIR}; valgrind ${VALFLAGS} ./${TARGETNAME}
164
165 .PHONY: prof
166 prof: run
167         cd ${WORKINGDIR}; gprof -b ${TARGETNAME} > src/prof
168         kprof -f prof
169
170
171 MINGMAKEARGS := "LIBGL      := -lopengl32 -lglu32" \
172                 "LIBSDL     := `/usr/mingw32/bin/sdl-config --libs`" \
173                 "CXXFLAGS   := ${OPTFLAGS}" \
174                 "CXX        := mingw32-g++" \
175                 "OBJSDIR    := objs-mingw32/" \
176                 "WORKINGDIR := bin-mingw32/" \
177                 "TARGETNAME := run_physics.exe"
178
179 .PHONY: mingw32
180 mingw32:
181         ${Q1}${PRNTFMT} "make" "mingw32"
182         ${Q2}${MAKE} ${MINGMAKEARGS} clean all
183
184 FINALMAKEARGS := "CXXFLAGS   := ${OPTFLAGS}" \
185                  "OBJSDIR    := objs/" \
186                  "WORKINGDIR := bin/"
187
188 .PHONY: final
189 final:
190         ${Q1}${PRNTFMT} "make" "final"
191         ${Q2}${MAKE} ${FINALMAKEARGS} clean all
192
193 -include ${DEPS}