configure: Make it possible to override ostype presets with values specified later...
[prosody.git] / configure
1 #!/bin/sh
2
3 # Defaults
4
5 PREFIX=/usr/local
6 SYSCONFDIR="$PREFIX/etc/prosody"
7 DATADIR="$PREFIX/var/lib/prosody"
8 LUA_SUFFIX=""
9 LUA_DIR="/usr"
10 LUA_BINDIR="/usr/bin"
11 LUA_INCDIR="/usr/include"
12 LUA_LIBDIR="/usr/lib"
13 IDN_LIB=idn
14 OPENSSL_LIB=crypto
15 CC=gcc
16 LD=gcc
17
18 CFLAGS="-fPIC -Wall"
19 LDFLAGS="-shared"
20
21 # Help
22
23 show_help() {
24 cat <<EOF
25 Configure Prosody prior to building.
26
27 --help                      This help.
28 --ostype=OS                 Use one of the OS presets.
29                             May be one of: debian, macosx, linux, freebsd
30 --prefix=DIR                Prefix where Prosody should be installed.
31                             Default is $PREFIX
32 --sysconfdir=DIR            Location where the config file should be installed.
33                             Default is \$PREFIX/etc/prosody
34 --datadir=DIR               Location where the server data should be stored.
35                             Default is \$PREFIX/var/lib/prosody
36 --lua-suffix=SUFFIX         Versioning suffix to use in Lua filenames.
37                             Default is "$LUA_SUFFIX" (lua$LUA_SUFFIX...)
38 --with-lua=PREFIX           Use Lua from given prefix.
39                             Default is $LUA_DIR
40 --with-lua-include=DIR      You can also specify Lua's includes dir.
41                             Default is \$LUA_DIR/include
42 --with-lua-lib=DIR          You can also specify Lua's libraries dir.
43                             Default is \$LUA_DIR/lib
44 --with-idn=LIB              The name of the IDN library to link with.
45                             Default is $IDN_LIB
46 --with-ssl=LIB              The name of the SSL to link with.
47                             Default is $OPENSSL_LIB
48 --cflags=FLAGS              Flags to pass to the compiler
49                             Default is $CFLAGS
50 --ldflags=FLAGS             Flags to pass to the linker
51                             Default is $LDFLAGS
52 --c-compiler=CC             The C compiler to use when building modules.
53                             Default is $CC
54 --linker=CC                 The linker to use when building modules.
55                             Default is $LD
56 --require-config            Will cause Prosody to refuse to run when
57                             it fails to find a configuration file
58 EOF
59 }
60
61
62 while [ "$1" ]
63 do
64    value="`echo $1 | sed 's/[^=]*=\(.*\)/\1/'`"
65    if echo "$value" | grep -q "~"
66    then
67       echo
68       echo '*WARNING*: the "~" sign is not expanded in flags.'
69       echo 'If you mean the home directory, use $HOME instead.'
70       echo
71    fi
72    case "$1" in
73    --help)
74       show_help
75       exit 0
76       ;;
77    --prefix=*)
78       PREFIX="$value"
79       PREFIX_SET=yes
80       ;;
81    --sysconfdir=*)
82       SYSCONFDIR="$value"
83       SYSCONFDIR_SET=yes
84       ;;
85    --ostype=*)
86       OSTYPE="$value"
87       OSTYPE_SET=yes
88       if [ "$OSTYPE" = "debian" ]
89       then LUA_SUFFIX="5.1";
90         LUA_SUFFIX_SET=yes
91         LUA_INCDIR=/usr/include/lua5.1;
92         LUA_INCDIR_SET=yes
93         fi
94         if [ "$OSTYPE" = "macosx" ]
95         then LUA_INCDIR=/usr/local/include;
96         LUA_INCDIR_SET=yes
97         LUA_LIBDIR=/usr/local/lib
98         LUA_LIBDIR_SET=yes
99         CFLAGS="-Wall"
100         LDFLAGS="-bundle -undefined dynamic_lookup"
101         fi
102         if [ "$OSTYPE" = "linux" ]
103         then LUA_INCDIR=/usr/local/include;
104         LUA_INCDIR_SET=yes
105         LUA_LIBDIR=/usr/local/lib
106         LUA_LIBDIR_SET=yes
107         CFLAGS="-Wall -fPIC"
108         LDFLAGS="-shared"
109         fi
110         if [ "$OSTYPE" = "freebsd" ]
111         then LUA_INCDIR="/usr/local/include/lua51"
112         LUA_INCDIR_SET=yes
113         CFLAGS="-Wall -fPIC -I/usr/local/include"
114         LDFLAGS="-I/usr/local/include -L/usr/local/lib -shared"
115         LUA_SUFFIX="-5.1"
116         LUA_SUFFIX_SET=yes
117         LUA_DIR=/usr/local
118         LUA_DIR_SET=yes
119         fi
120       ;;
121    --datadir=*)
122         DATADIR="$value"
123         DATADIR_SET=yes
124       ;;
125    --require-config)
126       REQUIRE_CONFIG=yes
127       ;;
128    --lua-suffix=*)
129       LUA_SUFFIX="$value"
130       LUA_SUFFIX_SET=yes
131       ;;
132    --with-lua=*)
133       LUA_DIR="$value"
134       LUA_DIR_SET=yes
135       ;;
136    --with-lua-include=*)
137       LUA_INCDIR="$value"
138       LUA_INCDIR_SET=yes
139       ;;
140    --with-lua-lib=*)
141       LUA_LIBDIR="$value" LUA_LIBDIR_SET=yes
142       ;;
143    --with-idn=*)
144       IDN_LIB="$value"
145       ;;
146    --with-ssl=*)
147       OPENSSL_LIB="$value"
148       ;;
149    --cflags=*)
150       CFLAGS="$value"
151       ;;
152    --ldflags=*)
153       LDFLAGS="$value"
154       ;;
155    --c-compiler=*)
156       CC="$value"
157       ;;
158    --linker=*)
159       LD="$value"
160       ;;
161    *)
162       echo "Error: Unknown flag: $1"
163       exit 1
164       ;;
165    esac
166    shift
167 done
168
169 if [ "$PREFIX_SET" = "yes" -a ! "$SYSCONFDIR_SET" = "yes" ]
170 then
171    if [ "$PREFIX" = "/usr" ]
172    then SYSCONFDIR=/etc/prosody
173    else SYSCONFDIR=$PREFIX/etc/prosody
174    fi
175 fi
176
177 if [ "$PREFIX_SET" = "yes" -a ! "$DATADIR_SET" = "yes" ]
178 then
179    if [ "$PREFIX" = "/usr" ]
180    then DATADIR=/var/lib/prosody
181    else DATADIR=$PREFIX/var/lib/prosody
182    fi
183 fi
184
185 find_program() {
186    path="$PATH"
187    item="`echo "$path" | sed 's/\([^:]*\):.*/\1/'`"
188    path="`echo "$path" | sed -n 's/[^:]*::*\(.*\)/\1/p'`"
189    found="no"
190    while [ "$item" ]
191    do
192       if [ -e "$item/$1" ]
193       then
194          found="yes"
195          break
196       fi
197       item="`echo "$path" | sed 's/\([^:]*\):.*/\1/'`"
198       path="`echo "$path" | sed -n 's/[^:]*::*\(.*\)/\1/p'`"
199    done
200    if [ "$found" = "yes" ]
201    then
202       echo "$item"
203    else
204       echo ""
205    fi
206 }
207
208 if [ "$LUA_SUFFIX_SET" != "yes" ]
209 then
210    for suffix in "5.1" "51" ""
211    do
212       LUA_SUFFIX="$suffix"
213       if [ "$LUA_DIR_SET" = "yes" ]
214       then
215          if [ -e "$LUA_DIR/bin/lua$suffix" ]
216          then
217             find_lua="$LUA_DIR"
218          fi
219       else
220          find_lua=`find_program lua$suffix`
221       fi
222       if [ "$find_lua" ]
223       then
224          echo "Lua interpreter found: $find_lua/lua$suffix..."
225          break
226       fi
227    done
228 fi
229
230 if ! [ "$LUA_DIR_SET" = "yes" ]
231 then
232    echo -n "Looking for Lua... "
233    if [ ! "$find_lua" ]
234    then
235       find_lua=`find_program lua$LUA_SUFFIX`
236       echo "lua$LUA_SUFFIX found in \$PATH: $find_lua"
237    fi
238    if [ "$find_lua" ]
239    then
240       LUA_DIR=`dirname $find_lua`
241       LUA_BINDIR="$find_lua"
242    else
243       echo "lua$LUA_SUFFIX not found in \$PATH."
244       echo "You may want to use the flags --with-lua and/or --lua-suffix. See --help."
245       exit 1
246    fi
247 fi
248
249 if ! [ "$LUA_INCDIR_SET" = "yes" ]
250 then
251    LUA_INCDIR="$LUA_DIR/include"
252 fi
253
254 if ! [ "$LUA_LIBDIR_SET" = "yes" ]
255 then
256    LUA_LIBDIR="$LUA_DIR/lib"
257 fi
258
259 if [ "$LUA_DIR_SET" = "yes" ]
260 then
261    LUA_BINDIR="$LUA_DIR/bin"
262 fi
263
264 echo -n "Checking Lua includes... "
265 lua_h="$LUA_INCDIR/lua.h"
266 if [ -e "$lua_h" ]
267 then
268    echo "lua.h found in $lua_h"
269 else
270    echo "lua.h not found (looked in $lua_h)"
271    echo "You may want to use the flag --with-lua-include. See --help."
272    exit 1
273 fi
274
275 find_helper() {
276    explanation="$1"
277    shift
278    tried="$*"
279    while [ "$1" ]
280    do
281       found=`find_program "$1"`
282       if [ "$found" ]
283       then
284          echo "$1 found at $found"
285          HELPER=$1
286          return
287       fi
288       shift
289    done
290    echo "Could not find a $explanation. Tried: $tried."
291    echo "Make sure one of them is installed and available in your PATH."
292    exit 1
293 }
294
295 # Write config
296
297 echo "Writing configuration..."
298 echo
299
300 cat <<EOF > config.unix
301 # This file was automatically generated by the configure script.
302 # Run "./configure --help" for details.
303
304 PREFIX=$PREFIX
305 SYSCONFDIR=$SYSCONFDIR
306 DATADIR=$DATADIR
307 LUA_SUFFIX=$LUA_SUFFIX
308 LUA_DIR=$LUA_DIR
309 LUA_INCDIR=$LUA_INCDIR
310 LUA_LIBDIR=$LUA_LIBDIR
311 LUA_BINDIR=$LUA_BINDIR
312 REQUIRE_CONFIG=$REQUIRE_CONFIG
313 IDN_LIB=$IDN_LIB
314 OPENSSL_LIB=$OPENSSL_LIB
315 CFLAGS=$CFLAGS
316 LDFLAGS=$LDFLAGS
317 CC=$CC
318 LD=$LD
319
320 EOF
321
322 echo "Installation prefix: $PREFIX"
323 echo "Prosody configuration directory: $SYSCONFDIR"
324 echo "Using Lua from: $LUA_DIR"
325
326 make clean > /dev/null 2> /dev/null
327
328 echo
329 echo "Done. You can now run 'make' to build."
330 echo