228 lines
4 KiB
Bash
Executable file
228 lines
4 KiB
Bash
Executable file
#!/usr/bin/env sh
|
|
# simple and flexible configure script for people who don't like to waste time
|
|
# licensed to the public domain
|
|
|
|
set -e
|
|
|
|
mkf=config.mk
|
|
# these also apply to all libraries in external/
|
|
cflags='-std=c99'
|
|
cppflags=''
|
|
ext_cppflags='-DLUA_USE_POSIX -DLUA_USE_DLOPEN'
|
|
ldflags='-lm -Wl,-E'
|
|
# optional libraries to build with support for; a dynamically
|
|
# linked Lua 5.4 library may be supported here later
|
|
optlibs='readline'
|
|
withlibs=''
|
|
|
|
#
|
|
## utility functions
|
|
#
|
|
|
|
present () {
|
|
command -v "$1" 1>/dev/null 2>/dev/null
|
|
}
|
|
using () {
|
|
printf "using $1\n"
|
|
}
|
|
throw () {
|
|
>&2 printf "$(basename $0): $1\n"
|
|
exit 1
|
|
}
|
|
|
|
#
|
|
## list utilities
|
|
#
|
|
|
|
# Returns 0 if the given item is in the given list, otherwise returns 1.
|
|
inlist () {
|
|
local item="$1"
|
|
local list="$2"
|
|
|
|
for elem in $list; do
|
|
if [ "$elem" = "$item" ]; then
|
|
return 0
|
|
fi
|
|
done
|
|
|
|
return 1
|
|
}
|
|
|
|
# Removes the given item from the given list and returns the new list.
|
|
removefrom () {
|
|
local item="$1"
|
|
local list="$2"
|
|
local newlist=""
|
|
|
|
for elem in $list; do
|
|
if ! [ "$elem" = "$item" ]; then
|
|
newlist="$newlist $elem"
|
|
fi
|
|
done
|
|
|
|
printf '%s' "$newlist"
|
|
}
|
|
|
|
# Trims the given string of leading and trailing whitespace.
|
|
trim () {
|
|
printf '%s' "$1" | xargs
|
|
}
|
|
|
|
#
|
|
## flag generators
|
|
#
|
|
|
|
## C compiler
|
|
gen_CC () {
|
|
if ! [ -z "$cc" ]; then
|
|
using "$cc"
|
|
return 0
|
|
fi
|
|
if present clang; then
|
|
cc=clang
|
|
elif present gcc; then
|
|
cc=gcc
|
|
elif present cc; then
|
|
cc=cc
|
|
else
|
|
throw "C compiler not found, please acquire a copy of LLVM Clang or the GNU C Compiler"
|
|
fi
|
|
|
|
using "$cc"
|
|
}
|
|
|
|
## flags used in the compilation step
|
|
gen_CFLAGS () {
|
|
if [ -z "$debug" ]; then
|
|
cflags="$cflags -O3"
|
|
else
|
|
cflags="$cflags -O0 -g"
|
|
fi
|
|
|
|
if inlist readline "$withlibs"; then
|
|
cppflags="$cppflags -DLUA_USE_READLINE"
|
|
fi
|
|
|
|
for flag in $cflags $cppflags; do
|
|
using "$flag"
|
|
done
|
|
|
|
cflags="$(trim "$cflags")"
|
|
cppflags="$(trim "$cppflags")"
|
|
}
|
|
|
|
## flags used in the linking step
|
|
gen_LDFLAGS () {
|
|
if inlist readline "$withlibs"; then
|
|
ldflags="$ldflags $(pkg-config --libs readline)"
|
|
fi
|
|
if present ld.lld; then
|
|
ldflags="$ldflags -fuse-ld=lld"
|
|
elif present ld.gold; then
|
|
ldflags="$ldflags -fuse-ld=gold"
|
|
fi
|
|
[ -z "$debug" ] && ldflags="$ldflags -Wl,--gc-sections"
|
|
|
|
for flag in $ldflags; do
|
|
using "$flag"
|
|
done
|
|
|
|
ldflags="$(trim "$ldflags")"
|
|
}
|
|
|
|
## Lua objects
|
|
gen_LUAOBJS () {
|
|
touch config.mk
|
|
for obj in $(make -Cexternal/lua -s echo | grep -E '^BASE_O= ' | cut -d " " -f2-); do
|
|
luaobjs="$luaobjs external/lua/$obj"
|
|
done
|
|
luaobjs="$(trim "$luaobjs")"
|
|
}
|
|
|
|
#
|
|
## misc. functions
|
|
#
|
|
|
|
assertvalidlib () {
|
|
if ! inlist "$OPTARG" "$optlibs"; then
|
|
throw "invalid library '$OPTARG'; supported libraries are:
|
|
$optlibs"
|
|
fi
|
|
}
|
|
|
|
#
|
|
## command line interface
|
|
#
|
|
|
|
while getopts c:dhrw:W: ch; do
|
|
case "$ch" in
|
|
c) cc="$OPTARG" ;;
|
|
d) debug=1 ;;
|
|
r) unset debug ;;
|
|
h)
|
|
cat <<EOF
|
|
configure: create an optimised makefile for the current environment
|
|
|
|
options:
|
|
-c: force use of a particular compiler
|
|
-d: build in debug mode, with debug symbols enabled
|
|
-r: build in release mode with optimisation flags enabled (default)
|
|
-h: show this help message
|
|
-wname: build with support for the given library; see the list below
|
|
-Wname: build without support for the given library
|
|
|
|
If a -w or -W option is supplied on the command line, then the
|
|
argument is checked against the following list of libraries:
|
|
EOF
|
|
for lib in $optlibs; do
|
|
printf ' - %s\n' "$lib"
|
|
done
|
|
exit 0
|
|
;;
|
|
w)
|
|
assertvalidlib
|
|
if ! inlist "$OPTARG" "$withlibs"; then
|
|
withlibs="$withlibs $OPTARG"
|
|
fi
|
|
;;
|
|
W)
|
|
assertvalidlib
|
|
if inlist "$OPTARG" "$withlibs"; then
|
|
withlibs="$(removefrom "$OPTARG" "$withlibs")"
|
|
fi
|
|
;;
|
|
|
|
?) exit 1 ;;
|
|
:) exit 1 ;;
|
|
esac
|
|
done
|
|
|
|
#
|
|
## create the makefile
|
|
#
|
|
|
|
cd $(dirname $0)
|
|
|
|
gen_CC
|
|
gen_CFLAGS
|
|
gen_LDFLAGS
|
|
gen_LUAOBJS
|
|
|
|
printf '# generated by configure
|
|
|
|
_CC = %s
|
|
_CFLAGS = %s
|
|
_CPPFLAGS = %s
|
|
_LDFLAGS =%s
|
|
|
|
_EXT_CPPFLAGS = %s
|
|
|
|
_LUAOBJS = %s
|
|
' \
|
|
"$cc" \
|
|
"$cflags" \
|
|
"$cppflags"\
|
|
"$ldflags" \
|
|
"$ext_cppflags"\
|
|
"$luaobjs"\
|
|
>"$mkf"
|