aboutsummaryrefslogtreecommitdiff
path: root/configure
blob: 2fd2666ca868c799e8cb01ffd41c4385b0c48c4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#!/bin/sh

# Defaults

prefix="/usr/local"
sysconfdir="$prefix/etc"
rocks_tree="$prefix"

# ----------------------------------------------------------------------------
# FUNCTION DEFINITIONS
# ----------------------------------------------------------------------------

# Utility functions
# -----------------

# Resolves a full path
# - alternative to "readlink -f", which is not available on solaris
# based on https://stackoverflow.com/a/6554854/1793220
canonicalpath() {
   oldpwd="$PWD"
   if [ -d "$1" ]
   then
      if cd "$1" >/dev/null 2>&1
      then
         echo "$PWD"
      else
         echo "$1"
      fi
   else
      if cd "$(dirname "$1")" >/dev/null 2>&1
      then
         echo "$PWD/$(basename "$1")"
      else
         echo "$1"
      fi
   fi
   cd "$oldpwd" >/dev/null 2>&1 || return
}

find_program() {
   prog=$(command -v "$1" 2>/dev/null)
   if [ -n "$prog" ]
   then
      dirname "$prog"
   fi
}

die() {
   echo "$*"
   echo
   RED "configure failed."
   echo
   echo
   exit 1
}

echo_n() {
   printf "%s" "$*"
}

bold='\033[1m'
red='\033[1;31m'
green='\033[1;32m'
blue='\033[1;36m'
reset='\033[0m'

BOLD() {
   printf "$bold%s$reset" "$*"
}

RED() {
   printf "$red%s$reset" "$*"
}

GREEN() {
   printf "$green%s$reset" "$*"
}

BLUE() {
   printf "$blue%s$reset" "$*"
}

# Help
# ----

show_help() {
cat <<EOF
Usage: ./configure [OPTION]... [VAR=VALUE]...

To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE.  See below for descriptions of some of the useful variables.

Defaults for the options are specified in brackets.

Configuration:
  -h, --help                   display this help and exit

Installation directories:
  --prefix=PREFIX              Directory where LuaRocks should be installed
                               [/usr/local]

By default, \`make install' will install all the files in \`/usr/local',
\`/usr/local/lib' etc.  You can specify an installation prefix other than
\`/usr/local/' using \`--prefix', for instance \`--prefix=\$HOME'.

For better control, use the options below.

Fine tuning of the installation directories:
  --sysconfdir=SYSCONFDIR      Directory for single-machine config [PREFIX/etc]

Where to install files provided by rocks:
  --rocks-tree=DIR             Root of the local tree of installed rocks.
                               To make files installed in this location
                               accessible to Lua and your \$PATH, see
                               "luarocks path --help" after installation.
                               Avoid using paths controlled by your
                               system's package manager, such as /usr.
                               - Default is [PREFIX]

Where is your Lua interpreter:
  --lua-version=VERSION        Use specific Lua version: 5.1, 5.2, 5.3, or 5.4
                               - Default is auto-detected.
  --with-lua-bin=LUA_BINDIR    Location of your Lua binar(y/ies).
                               - Default is the directory of the
                               auto-detected Lua interpreter,
                               (or DIR/bin if --with-lua is used)
  --with-lua=LUA_DIR           Use Lua from given directory. [LUA_BINDIR/..]
  --with-lua-include=DIR       Lua's includes dir. [LUA_DIR/include]
  --with-lua-lib=DIR           Lua's libraries dir. [LUA_DIR/lib]
  --with-lua-interpreter=NAME  Lua interpreter name.
                               - Default is to auto-detected

For specialized uses of LuaRocks:
  --force-config               Force using a single config location.
                               Do not honor the \$LUAROCKS_CONFIG_5_x
                               or \$LUAROCKS_CONFIG environment
                               variable or the user's local config.
                               Useful to avoid conflicts when LuaRocks
                               is embedded within an application.
  --disable-incdir-check       If you do not wish to use "luarocks build",
                               (e.g. when only deploying binary packages)
                               you do not need lua.h installed. This flag
                               skips the check for lua.h in "configure".

EOF
}

# Lua detection
# -------------

detect_lua_version() {
   detected_lua=$($1 -e 'print(_VERSION:match(" (5%.[1234])$"))' 2> /dev/null)
   if [ "$detected_lua" != "nil" ]
   then
      if [ "$LUA_VERSION_SET" != "yes" ]
      then
         echo "Lua version detected: $(GREEN "$detected_lua")"
         LUA_VERSION=$detected_lua
         return 0
      elif [ "$LUA_VERSION" = "$detected_lua" ]
      then
         return 0
      fi
   fi
   return 1
}

search_interpreter() {
   name="$1"
   lua_at=""
   if [ "$LUA_BINDIR_SET" = "yes" ]
   then
      lua_at="$LUA_BINDIR"
   elif [ "$LUA_DIR_SET" = "yes" ]
   then
      LUA_BINDIR="$LUA_DIR/bin"
      if [ -f "$LUA_BINDIR/$name" ]
      then
         lua_at="$LUA_BINDIR"
      fi
   else
      lua_at=$(find_program "$name")
   fi
   if [ -n "$lua_at" ] && [ -x "$lua_at/$name" ]
   then
      if detect_lua_version "$lua_at/$name"
      then
         echo "Lua interpreter found: $(GREEN "$lua_at/$name")"
         if [ "$LUA_BINDIR_SET" != "yes" ]
         then
            LUA_BINDIR="$lua_at"
         fi
         if [ "$LUA_DIR_SET" != "yes" ]
         then
            LUA_DIR=$(dirname "$lua_at")
         fi
         LUA_INTERPRETER="$name"
         return 0
      fi
   fi
   return 1
}

# ----------------------------------------------------------------------------
# MAIN PROGRAM
# ----------------------------------------------------------------------------

# Parse options

while [ -n "$1" ]
do
   value="$(echo "$1" | sed 's/[^=]*.\(.*\)/\1/')"
   key="$(echo "$1" | sed 's/=.*//')"
   if echo "$value" | grep "~" >/dev/null 2>/dev/null
   then
      echo
      echo "$(RED WARNING:) the '~' sign is not expanded in flags."
      echo "If you mean the home directory, use \$HOME instead."
      echo
   fi
   case "$key" in

   # Help
   # ----
   -h|--help)
      show_help
      exit 0
      ;;

   # Where to install LuaRocks:
   # --------------------------
   --prefix)
      [ -n "$value" ] || die "Missing value in flag $key."
      prefix="$(canonicalpath "$value")"
      prefix_SET=yes
      ;;
   --sysconfdir)
      [ -n "$value" ] || die "Missing value in flag $key."
      sysconfdir="$(canonicalpath "$value")"
      sysconfdir_SET=yes
      ;;


   # Where to install files provided by rocks:
   # -----------------------------------------
   --rocks-tree)
      [ -n "$value" ] || die "Missing value in flag $key."
      rocks_tree="$(canonicalpath "$value")"
      rocks_tree_SET=yes
      ;;

   # Where is your Lua interpreter:
   # ------------------------------
   --lua-version|--with-lua-version)
      [ -n "$value" ] || die "Missing value in flag $key."
      LUA_VERSION="$value"
      case "$LUA_VERSION" in
      5.1|5.2|5.3|5.4) ;;
      *) die "Invalid Lua version in flag $key."
      esac
      LUA_VERSION_SET=yes
      ;;
   --with-lua)
      [ -n "$value" ] || die "Missing value in flag $key."
      LUA_DIR="$(canonicalpath "$value")"
      [ -d "$LUA_DIR" ] || die "Bad value for --with-lua: $LUA_DIR is not a valid directory."
      LUA_DIR_SET=yes
      ;;
   --with-lua-bin)
      [ -n "$value" ] || die "Missing value in flag $key."
      LUA_BINDIR="$(canonicalpath "$value")"
      [ -d "$LUA_BINDIR" ] || die "Bad value for --with-lua-bin: $LUA_BINDIR is not a valid directory."
      LUA_BINDIR_SET=yes
      ;;
   --with-lua-include)
      [ -n "$value" ] || die "Missing value in flag $key."
      LUA_INCDIR="$(canonicalpath "$value")"
      [ -d "$LUA_INCDIR" ] || die "Bad value for --with-lua-include: $LUA_INCDIR is not a valid directory."
      LUA_INCDIR_SET=yes
      ;;
   --with-lua-lib)
      [ -n "$value" ] || die "Missing value in flag $key."
      LUA_LIBDIR="$(canonicalpath "$value")"
      [ -d "$LUA_LIBDIR" ] || die "Bad value for --with-lua-lib: $LUA_LIBDIR is not a valid directory."
      LUA_LIBDIR_SET=yes
      ;;
   --with-lua-interpreter)
      [ -n "$value" ] || die "Missing value in flag $key."
      LUA_INTERPRETER="$value"
      LUA_INTERPRETER_SET=yes
      ;;

   # For specialized uses of LuaRocks:
   # ---------------------------------
   --force-config)
      FORCE_CONFIG=yes
      ;;
   --disable-incdir-check)
      DISABLE_INCDIR_CHECK=yes
      ;;

   # Old options that no longer apply
   # --------------------------------
   --versioned-rocks-dir)
      echo "--versioned-rocks-dir is no longer necessary."
      echo "The rocks tree in LuaRocks 3.0 is always versioned."
      ;;
   --lua-suffix)
      echo "--lua-suffix is no longer necessary."
      echo "The suffix is automatically detected."
      ;;

   *)
      die "Error: Unknown flag: $1"
      ;;
   esac
   shift
done

echo
BLUE "Configuring LuaRocks version 3.0.4..."
echo
echo

# ----------------------------------------
# Derive options from the ones given
# ----------------------------------------

if [ "$prefix_SET" = "yes" ] && [ ! "$sysconfdir_SET" = "yes" ]
then
   if [ "$prefix" = "/usr" ]
   then sysconfdir=/etc
   else sysconfdir=$prefix/etc
   fi
   sysconfdir_SET=yes
fi

if [ "$prefix_SET" = "yes" ] && [ ! "$rocks_tree_SET" = "yes" ]
then
   rocks_tree=$prefix
fi

# ----------------------------------------
# Search for Lua
# ----------------------------------------

lua_interp_found=no

case "$LUA_VERSION" in
5.1)
   names="lua5.1 lua51 lua-5.1 lua-51 luajit lua"
   ;;
5.2)
   names="lua5.2 lua52 lua-5.2 lua-52 lua"
   ;;
5.3)
   names="lua5.3 lua53 lua-5.3 lua-53 lua"
   ;;
5.4)
   names="lua5.4 lua54 lua-5.4 lua-54 lua"
   ;;
*)
   names="lua5.4 lua54 lua-5.4 lua-54 lua5.3 lua53 lua-5.3 lua-53 lua5.2 lua52 lua-5.2 lua-52 lua5.1 lua51 lua-5.1 lua-51 luajit lua"
   ;;
esac

if [ "$LUA_INTERPRETER_SET" = "yes" ]
then
   names="$LUA_INTERPRETER"
fi

for name in $names
do
   search_interpreter "$name" && {
      lua_interp_found=yes
      break
   }
done

if [ "$lua_interp_found" != "yes" ]
then
   if [ "$LUA_VERSION_SET" ]
   then
      interp="Lua $LUA_VERSION"
   else
      interp="Lua"
   fi
   if [ "$LUA_DIR_SET" ] || [ "$LUA_BINDIR_SET" ]
   then
      where="$LUA_BINDIR"
   else
      where="\$PATH"
   fi
   echo "$(RED $interp interpreter not found) in $where"
   echo "You may want to use the flags $(BOLD --with-lua), $(BOLD --with-lua-bin) and/or $(BOLD --lua-version)"
   die "Run $(BOLD ./configure --help) for details."
fi

if [ "$LUA_VERSION_SET" = "yes" ]
then
   echo_n "Checking if $LUA_BINDIR/$LUA_INTERPRETER is Lua version $LUA_VERSION... "
   if detect_lua_version "$LUA_BINDIR/$LUA_INTERPRETER"
   then
      echo "yes"
   else
      echo "no"
      die "You may want to use the flags --with-lua, --with-lua-bin and/or --lua-version. See --help."
   fi
fi

# ----------------------------------------
# Additional checks
# ----------------------------------------

check_incdir() {
   if [ "$LUA_INCDIR_SET" = "yes" ]
   then
      incdir="$LUA_INCDIR"
   else
      incdir="$LUA_DIR/include"
   fi

   tried=""
   # Verification of the Lua include path happens at LuaRocks runtime,
   # but we also perform it here just so that the user gets an early failure
   # if they try to install LuaRocks with the Lua interpreter package
   # but not the "development files" that many Linux distros ship separately.
   for lua_h in \
      "$incdir/lua/$LUA_VERSION/lua.h" \
      "$incdir/lua$LUA_VERSION/lua.h" \
      "$incdir/lua$(echo "$LUA_VERSION" | tr -d .)/lua.h" \
      "$incdir/lua.h" \
      $("$LUA_BINDIR/$LUA_INTERPRETER" -e 'print(jit and [['"$incdir"'/luajit-]]..jit.version:match("(%d+%.%d+)")..[[/lua.h]] or "")')
   do
      [ -f "$lua_h" ] && return
      tried="$tried $lua_h"
   done

   echo "$(RED "lua.h not found") (tried$tried)"
   echo
   echo "If the development files for Lua (headers and libraries)"
   echo "are installed in your system, you may need to use the"
   echo "$(BOLD --with-lua) or $(BOLD --with-include) flags to specify their location."
   echo
   echo "If those files are not yet installed, you need to install"
   echo "them using the appropriate method for your operating system."
   echo
   die "Run $(BOLD ./configure --help) for details on flags."
}

if [ "$DISABLE_INCDIR_CHECK" != "yes" ]
then
   check_incdir
   echo "lua.h found: $(GREEN "$lua_h")"
fi

unzip_found=$(find_program "unzip")
if [ -n "$unzip_found" ]
then
   echo "unzip found in PATH: $(GREEN "$unzip_found")"
else
   RED "Could not find 'unzip'."
   echo
   die "Make sure it is installed and available in your PATH."
fi

# ----------------------------------------
# Write config
# ----------------------------------------

make clean > /dev/null 2> /dev/null

rm -f built
cat <<EOF > config.unix
# This file was automatically generated by the configure script.
# Run "./configure --help" for details.

prefix=$prefix
sysconfdir=$sysconfdir
rocks_tree=$rocks_tree
LUA_VERSION=$LUA_VERSION
LUA_INTERPRETER=$LUA_INTERPRETER
LUA_DIR=$LUA_DIR
LUA_BINDIR=$LUA_BINDIR
LUA_INCDIR=$LUA_INCDIR
LUA_LIBDIR=$LUA_LIBDIR
FORCE_CONFIG=$FORCE_CONFIG
EOF

echo
BLUE "Done configuring."
echo
echo
echo "LuaRocks will be installed at......: $(GREEN "$prefix")"
echo "LuaRocks will install rocks at.....: $(GREEN "$rocks_tree")"
echo "LuaRocks configuration directory...: $(GREEN "$sysconfdir/luarocks")"
echo "Using Lua from.....................: $(GREEN "$LUA_DIR")"
if [ "$LUA_BINDIR_SET" = "yes" ]; then echo "Lua bin directory..................: $(GREEN "$LUA_BINDIR")" ; fi
if [ "$LUA_INCDIR_SET" = "yes" ]; then echo "Lua include directory..............: $(GREEN "$LUA_INCDIR")" ; fi
if [ "$LUA_LIBDIR_SET" = "yes" ]; then echo "Lua lib directory..................: $(GREEN "$LUA_LIBDIR")" ; fi
echo
echo "* Type $(BOLD make) and $(BOLD make install):"
echo "  to install to $prefix as usual."
echo "* Type $(BOLD make bootstrap):"
echo "  to install LuaRocks into $rocks_tree as a rock."
echo