Tennix 1.3.4
[tennix.git] / configure
blobe5da2ceaec8eaad563454dc910ca36dd74c5057a
1 #!/bin/sh
2 # Configuration script for Tennix
3 # 2013-07-27 Thomas Perl <m@thp.io>
5 V="1.3.4"
6 SILENT="1"
7 PYTHON="1"
8 PREFIX="/usr/local"
9 DEFAULT_CFLAGS="-I. -Wall -fPIC"
11 if [ -z "$CC" ]; then
12 CC="gcc"
15 if [ -z "$CXX" ]; then
16 CXX="g++"
19 if [ -z "$CFLAGS" ]; then
20 CFLAGS=""
23 if [ -z "$LDFLAGS" ]; then
24 LDFLAGS=""
27 if [ -z "$SDL_CONFIG" ]; then
28 SDL_CONFIG="sdl2-config"
31 CFLAGS="$DEFAULT_CFLAGS $CFLAGS"
33 PLATFORM=""
35 GENERATED_HEADER="Generated: $(date)"
36 CONFIG_H="/* $GENERATED_HEADER */"
37 CONFIG_MK="# $GENERATED_HEADER"
39 NL='
42 fail() {
43 echo "ERROR: $1"
44 exit 1
47 set_make_variable() {
48 CONFIG_MK="$CONFIG_MK$NL$1 := $2"
51 define_macro() {
52 CONFIG_H="$CONFIG_H$NL#define $1"
55 undefine_macro() {
56 CONFIG_H="$CONFIG_H$NL#undef $1"
59 add_definition() {
60 define_macro $1
61 set_make_variable $1 1
64 no_definition() {
65 undefine_macro $1
66 set_make_variable $1 0
69 cond_definition() {
70 if [ $1 -eq 0 ]; then
71 add_definition $2
72 else
73 no_definition $2
77 check_cc() {
78 echo "int main() { return 0; }" | $CC $CFLAGS -x c -c -o configtest.o - 2>/dev/null
81 check_cxx() {
82 echo "class A { public: A() {} }; int main() { A a; }" | $CXX $CFLAGS -x c++ -c -o configtest.o -
85 check_linker_cc() {
86 echo "int main() { return 0; }" | $CC $CFLAGS $LDFLAGS -l$1 -x c -o configtest.o - 2>/dev/null
89 check_header_cc() {
90 echo "#include <$1>" | $CC $CFLAGS -c -x c -o configtest.o -c - 2>/dev/null
93 message_wrapper() {
94 printf "$1 ... "
95 shift
97 if [ $? -eq 0 ]; then
98 echo "yes"
99 return 0
100 else
101 echo "no"
102 return 1
106 check_sdl_lib() {
107 if ! which "$SDL_CONFIG" >/dev/null 2>&1; then
108 return 1
111 CFLAGS="$("$SDL_CONFIG" --cflags) $CFLAGS"
112 LDFLAGS="$("$SDL_CONFIG" --libs) $LDFLAGS"
113 return 0
116 check_os() {
117 printf "Detecting operating system ... "
118 UNAME=$(uname -s)
119 case $UNAME in
120 Linux)
121 echo "Linux"
122 PLATFORM="linux"
124 Darwin)
125 echo "macOS"
126 PLATFORM="osx"
129 echo "???"
130 fail "Unknown platform: $UNAME"
132 esac
135 check_compiler() {
136 message_wrapper "Testing working C compiler ($CC)" check_cc || fail "C compiler ($CC) not working"
137 message_wrapper "Testing working C++ compiler ($CXX)" check_cxx || fail "C++ compiler ($CXX) not working"
140 check_sdl2() {
141 message_wrapper "Checking for SDL2" check_sdl_lib
142 cond_definition $? HAVE_SDL
145 require_sdl2() {
146 check_sdl2 || fail "SDL2 not found"
149 check_voice_files() {
150 message_wrapper "Checking for voice files" [ -f voice/deuce.ogg ]
151 cond_definition $? HAVE_VOICE_FILES
154 check_python_lib() {
155 if [ -z "$PYTHON3_CONFIG" ]; then
156 PYTHON3_CONFIG="python3-config"
159 if ! which "$PYTHON3_CONFIG" >/dev/null 2>&1; then
160 return 1
163 CFLAGS="$("$PYTHON3_CONFIG" --cflags) $CFLAGS"
164 if "$PYTHON3_CONFIG" --embed --libs >/dev/null 2>&1; then
165 # Python 3.8 and newer
166 LDFLAGS="$("$PYTHON3_CONFIG" --embed --libs) $LDFLAGS"
167 else
168 # Python 3.7 and older
169 LDFLAGS="$("$PYTHON3_CONFIG" --libs) $LDFLAGS"
171 return 0
174 check_tool_path() {
175 COMMAND=$1
176 VARIABLE=$2
178 if ! which $COMMAND >/dev/null 2>&1; then
179 return 1
182 if [ "$VARIABLE" != "" ]; then
183 set_make_variable $VARIABLE $(which $COMMAND 2>/dev/null)
187 check_tool() {
188 message_wrapper "Checking for $1" check_tool_path $*
191 require_tool() {
192 check_tool $* || fail "Required command '$1' not found"
195 check_python() {
196 message_wrapper "Checking for libpython3" check_python_lib
197 cond_definition $? HAVE_PYTHON
200 check_header() {
201 message_wrapper "Checking for $1" check_header_cc $1
204 check_linker() {
205 message_wrapper "Checking for -l$1" check_linker_cc $1
208 check_library() {
209 LIBRARY=$1
210 HEADER=$2
211 DEFINE=$3
212 check_linker $LIBRARY
213 HAVE_LINKER=$?
214 check_header $HEADER
215 HAVE_HEADER=$?
216 if [ $HAVE_LINKER -eq 0 -a $HAVE_HEADER -eq 0 ]; then
217 add_definition $DEFINE
218 LDFLAGS="-l$LIBRARY $LDFLAGS"
219 else
220 no_definition $DEFINE
221 return 1
223 return 0
226 require_library() {
227 check_library $* || fail "Library $1 ($2) not found"
230 generate_dependencies() {
231 echo "Generating dependencies.mk"
232 rm -f dependencies.mk
234 echo "# $GENERATED_HEADER"
235 for filename in src/*.cc; do $CXX -MM $filename $CFLAGS -o -; done
236 )>dependencies.mk
239 write_config() {
240 echo "Writing config.h"
241 rm -f config.h
243 echo "$CONFIG_H"
244 echo "#define VERSION \"$V\""
245 echo "#define PREFIX \"$PREFIX\""
246 )>config.h
248 echo "Writing config.mk"
249 rm -f config.mk
251 echo "$CONFIG_MK"
252 echo "V = $V"
253 echo "SILENT = $SILENT"
254 echo "PREFIX = $PREFIX"
255 echo "PLATFORM = $PLATFORM"
256 echo "CC = $CC"
257 echo "CXX = $CXX"
258 echo "CFLAGS = $CFLAGS"
259 echo "CXXFLAGS = $CFLAGS"
260 echo "LDFLAGS = $LDFLAGS"
261 )>config.mk
264 usage() {
265 cat <<EOF
267 Usage: $0 [options]
269 Supported options:
270 --prefix PREFIX Install into PREFIX (default: $PREFIX)
271 --help | -h Show this help message
272 --with-debug Enable additional debugging output
273 --enable-update-rect Visualize screen updates
274 --enable-nonfree-locations Additional tennis courts on world map
275 --enable-fps-limit Limit frame rate
276 --disable-silent Disable silent make output
277 --disable-python Disable checking for Python
282 parse_arguments() {
283 while [ $# -gt 0 ]; do
284 case $1 in
285 --prefix)
286 [ $# -gt 1 ] || fail "Not enough arguments."
287 PREFIX="$2"
288 CFLAGS="-I$PREFIX/include $CFLAGS"
289 LDFLAGS="-L$PREFIX/lib $LDFLAGS"
290 shift
292 --with-debug)
293 define_macro DEBUG
294 CFLAGS="-g $CFLAGS"
296 --enable-update-rect)
297 define_macro DRAW_UPDATE_RECTANGLE
299 --enable-nonfree-locations)
300 define_macro NONFREE_LOCATIONS
302 --enable-fps-limit)
303 define_macro ENABLE_FPS_LIMIT
305 --disable-silent)
306 SILENT="0"
308 --disable-python)
309 PYTHON="0"
311 -h|--help)
312 usage
313 exit 0
316 usage
317 fail "Unknown argument: $1"
319 esac
320 shift
321 done
324 # Parse command-line arguments
325 parse_arguments $*
327 # Cleanup
328 trap "rm -f configtest.o" EXIT
330 # Check for compatible OS and working compiler
331 check_os
332 check_compiler
334 # Check for command-line tools needed for build
335 require_tool make
337 # Check for command-line tools required in makefile
338 require_tool ln LN
339 require_tool install INSTALL
340 require_tool rm RM
342 # Check for SDL 2 and libpython3
343 require_sdl2
344 [ "$PYTHON" != "0" ] && check_python
346 # Check for mandatory and optional libraries
347 require_library SDL2_image SDL_image.h HAVE_SDL_IMAGE
348 require_library SDL2_ttf SDL_ttf.h HAVE_SDL_TTF
349 check_library SDL2_net SDL_net.h HAVE_SDL_NET
350 require_library SDL2_mixer SDL_mixer.h HAVE_SDL_MIXER
351 require_library SDL2_gfx SDL2_rotozoom.h HAVE_SDL_GFX
353 # Check for optional game content
354 check_voice_files
356 # Save configuration results and generate dependency file
357 write_config
358 generate_dependencies