Version 1.2.0 with new build/configure system
[tennix.git] / configure
blob4c19a64c02c35d6be692caa1f98184fff0b97731
1 #!/bin/sh
2 # Configuration script for Tennix
3 # 2013-07-27 Thomas Perl <m@thp.io>
5 V="1.2.0"
6 SILENT="1"
7 PREFIX="/usr/local"
8 DEFAULT_CFLAGS="-I. -Wno-char-subscripts -Wno-maybe-uninitialized"
10 if [ -z "$CC" ]; then
11 CC="gcc"
14 if [ -z "$CXX" ]; then
15 CXX="g++"
18 if [ -z "$CFLAGS" ]; then
19 CFLAGS=""
22 if [ -z "$LDFLAGS" ]; then
23 LDFLAGS=""
26 CFLAGS="$DEFAULT_CFLAGS $CFLAGS"
28 PLATFORM=""
30 GENERATED_HEADER="Generated: $(date)"
31 CONFIG_H="/* $GENERATED_HEADER */"
32 CONFIG_MK="# $GENERATED_HEADER"
34 NL='
37 fail() {
38 echo "ERROR: $1"
39 exit 1
42 set_make_variable() {
43 CONFIG_MK="$CONFIG_MK$NL$1 := $2"
46 define_macro() {
47 CONFIG_H="$CONFIG_H$NL#define $1"
50 undefine_macro() {
51 CONFIG_H="$CONFIG_H$NL#undef $1"
54 add_definition() {
55 define_macro $1
56 set_make_variable $1 1
59 no_definition() {
60 undefine_macro $1
61 set_make_variable $1 0
64 cond_definition() {
65 if [ $1 -eq 0 ]; then
66 add_definition $2
67 else
68 no_definition $2
72 check_cc() {
73 echo "int main() { return 0; }" | $CC $CFLAGS -x c -c -o /dev/null - 2>/dev/null
76 check_cxx() {
77 echo "class A { public: A() {} }; int main() { A a; }" | $CXX $CFLAGS -x c++ -c -o /dev/null -
80 check_linker_cc() {
81 echo "int main() { return 0; }" | $CC $CFLAGS $LDFLAGS -l$1 -x c -o /dev/null - 2>/dev/null
84 check_header_cc() {
85 echo "#include <$1>" | $CC $CFLAGS -c -x c -o /dev/null -c - 2>/dev/null
88 message_wrapper() {
89 echo -n "$1 ... "
90 shift
92 if [ $? -eq 0 ]; then
93 echo "yes"
94 return 0
95 else
96 echo "no"
97 return 1
101 check_sdl_lib() {
102 if ! which sdl-config >/dev/null 2>&1; then
103 return 1
106 CFLAGS="$(sdl-config --cflags) $CFLAGS"
107 LDFLAGS="$(sdl-config --libs) $LDFLAGS"
108 return 0
111 check_os() {
112 echo -n "Detecting operating system ... "
113 UNAME=$(uname -s)
114 case $UNAME in
115 Linux)
116 echo "Linux"
117 PLATFORM="linux"
119 Darwin)
120 echo "Mac OS X"
121 PLATFORM="osx"
124 echo "???"
125 fail "Unknown platform: $UNAME"
127 esac
130 check_compiler() {
131 message_wrapper "Testing working C compiler ($CC)" check_cc || fail "C compiler ($CC) not working"
132 message_wrapper "Testing working C++ compiler ($CXX)" check_cxx || fail "C++ compiler ($CXX) not working"
135 check_sdl() {
136 message_wrapper "Checking for SDL 1.2" check_sdl_lib
137 cond_definition $? HAVE_SDL
140 require_sdl() {
141 check_sdl || fail "SDL not found"
144 check_voice_files() {
145 message_wrapper "Checking for voice files" [ -f voice/deuce.ogg ]
146 cond_definition $? HAVE_VOICE_FILES
149 check_python_lib() {
150 if ! which python-config >/dev/null 2>&1; then
151 return 1
154 CFLAGS="$(python-config --cflags) $CFLAGS"
155 LDFLAGS="$(python-config --libs) $LDFLAGS"
156 return 0
159 check_tool_path() {
160 COMMAND=$1
161 VARIABLE=$2
163 if ! which $COMMAND >/dev/null 2>&1; then
164 return 1
167 if [ "$VARIABLE" != "" ]; then
168 set_make_variable $VARIABLE $(which $COMMAND 2>/dev/null)
172 check_tool() {
173 message_wrapper "Checking for $1" check_tool_path $*
176 require_tool() {
177 check_tool $* || fail "Required command '$1' not found"
180 check_python() {
181 message_wrapper "Checking for libpython" check_python_lib
182 cond_definition $? HAVE_PYTHON
185 check_header() {
186 message_wrapper "Checking for $1" check_header_cc $1
189 check_linker() {
190 message_wrapper "Checking for -l$1" check_linker_cc $1
193 check_library() {
194 LIBRARY=$1
195 HEADER=$2
196 DEFINE=$3
197 check_linker $LIBRARY
198 HAVE_LINKER=$?
199 check_header $HEADER
200 HAVE_HEADER=$?
201 if [ $HAVE_LINKER -eq 0 -a $HAVE_HEADER -eq 0 ]; then
202 add_definition $DEFINE
203 LDFLAGS="-l$LIBRARY $LDFLAGS"
204 else
205 no_definition $DEFINE
206 return 1
208 return 0
211 require_library() {
212 check_library $* || fail "Library $1 ($2) not found"
215 generate_dependencies() {
216 echo "Generating dependencies.mk"
217 rm -f dependencies.mk
219 echo "# $GENERATED_HEADER"
220 for filename in src/*.cc; do $CXX -MM $filename $CFLAGS -o -; done
221 )>dependencies.mk
224 write_config() {
225 echo "Writing config.h"
226 rm -f config.h
228 echo "$CONFIG_H"
229 echo "#define VERSION \"$V\""
230 echo "#define PREFIX \"$PREFIX\""
231 )>config.h
233 echo "Writing config.mk"
234 rm -f config.mk
236 echo "$CONFIG_MK"
237 echo "V = $V"
238 echo "SILENT = $SILENT"
239 echo "PREFIX = $PREFIX"
240 echo "PLATFORM = $PLATFORM"
241 echo "CC = $CC"
242 echo "CXX = $CXX"
243 echo "CFLAGS = $CFLAGS"
244 echo "CXXFLAGS = $CFLAGS"
245 echo "LDFLAGS = $LDFLAGS"
246 )>config.mk
249 usage() {
250 cat <<EOF
252 Usage: $0 [options]
254 Supported options:
255 --prefix PREFIX Install into PREFIX (default: $PREFIX)
256 --help | -h Show this help message
257 --with-debug Enable additional debugging output
258 --enable-update-rect Visualize screen updates
259 --enable-nonfree-locations Additional tennis courts on world map
260 --enable-fps-limit Limit frame rate
261 --disable-silent Disable silent make output
266 parse_arguments() {
267 while [ $# -gt 0 ]; do
268 case $1 in
269 --prefix)
270 [ $# -gt 1 ] || fail "Not enough arguments."
271 PREFIX="$2"
272 shift
274 --with-debug)
275 define_macro DEBUG
276 CFLAGS="-g $CFLAGS"
278 --enable-update-rect)
279 define_macro DRAW_UPDATE_RECTANGLE
281 --enable-nonfree-locations)
282 define_macro NONFREE_LOCATIONS
284 --enable-fps-limit)
285 define_macro ENABLE_FPS_LIMIT
287 --disable-silent)
288 SILENT="0"
290 -h|--help)
291 usage
292 exit 0
295 usage
296 fail "Unknown argument: $1"
298 esac
299 shift
300 done
303 # Parse command-line arguments
304 parse_arguments $*
306 # Check for compatible OS and working compiler
307 check_os
308 check_compiler
310 # Check for command-line tools needed for build
311 require_tool make
313 # Check for command-line tools required in makefile
314 require_tool ln LN
315 require_tool install INSTALL
316 require_tool rm RM
318 # Check for SDL 1.2 and libpython
319 require_sdl
320 check_python
322 # Check for mandatory and optional libraries
323 require_library SDL_image SDL_image.h HAVE_SDL_IMAGE
324 require_library SDL_ttf SDL_ttf.h HAVE_SDL_TTF
325 check_library SDL_net SDL_net.h HAVE_SDL_NET
326 require_library SDL_mixer SDL_mixer.h HAVE_SDL_MIXER
328 # Check for optional game content
329 check_voice_files
331 # Save configuration results and generate dependency file
332 write_config
333 generate_dependencies