#!/bin/sh
. "$(dirname $0)"/comain/libcomain
usageoptions()
{
local name=$2
local desc
local disabled
local invisible
local i
eval desc="\$optdesc$name"
eval disabled="\$optdisabled$name"
eval invisible="\$optinvisible$name"
if test $invisible -ne 0; then return; fi
i=$((23 - ${#name}))
printf " --with-$name%${i}sEnable $desc"
if test "$disabled" -eq 0; then
echo " [default]"
else
echo ""
fi
i=$(($i - 3))
printf " --without-$name%${i}sDisable $desc"
if test "$disabled" -eq 1; then
echo " [default]"
else
echo ""
fi
}
usage()
{
local r="$1"
s=
if test $has_skalibs -eq 1; then
s="
--sysdeps=DIR Use skalibs' sysdeps from DIR [LIBDIR/skalibs/sysdeps]"
fi
cat <<EOF
usage: $0 [OPTION..]
--target=TARGET Set target to run as TARGET [detected]
--prefix=PREFIX Set main installation prefix to PREFIX [/usr]
--bindir=DIR Set binary directory to DIR [PREFIX/bin]
--libdir=DIR Set library directory to DIR [PREFIX/lib]
--incdir=DIR Set include directory to DIR [PREFIX/include]
--sharedir=DIR Set share directory to DIR [PREFIX/share]
$s
--add-deppath=DIR Add DIR to the path to look for dependencies
--add-incpath=DIR Add DIR to the path to look for dependencies' headers
--no-shared Don't build shared libraries
--no-static Don't build static libraries
--no-doc Don't build documentation
--prefer-shared Prefer to link shared libraires [default]
--prefer-static Prefer to link static libraires
--set-shared=DEP Use shared linking for dependency DEP
--set-static=DEP Use static linking for dependency DEP
EOF
foreach optname $nb_options usageoptions
cat <<EOF
-h, --help Show this help screen and exit
EOF
exit $r
}
##
target=
prefix=
bindir=
libdir=
incdir=
sharedir=
deppath=/lib:/usr/lib
incpath=/include:/usr/include
# this isn't quite right, since skalibs could be optional, but it'll do.
has_skalibs=0
if test -d meta/deps/skalibs; then has_skalibs=1; fi
sysdeps=
prefer=shared
noshared=0
nostatic=0
nodoc=0
CVARS=
CPPFLAGS=$CPPFLAGS
CFLAGS=$CFLAGS
LDFLAGS=$LDFLAGS
COMMON_CPPFLAGS=
COMMON_CFLAGS=
COMMON_LDFLAGS=
COMMON_CPPFLAGS_SHARED=
COMMON_CFLAGS_SHARED=
COMMON_LDFLAGS_SHARED=
VPATHS="# this will handle prefer or set shared/static"
loadoptions
setoption()
{
local opt=$1
local v=$2
if test ! $(optexist $opt) = "1"; then error 1 "unknown option: $opt"; fi
eval optdisabled$opt=$v
}
for arg ; do
case "$arg" in
--target=*) target=${arg#*=} ;;
--prefix=*) prefix=${arg#*=} ;;
--bindir=*) bindir=${arg#*=} ;;
--libdir=*) libdir=${arg#*=} ;;
--incdir=*) incdir=${arg#*=} ;;
--sharedir=*) sharedir=${arg#*=} ;;
--sysdeps=*) if test $has_skalibs -eq 1; then sysdeps=${arg#*=};
else error 1 "invalid option '$arg'"; fi;;
--no-shared) noshared=1 ;;
--no-static) nostatic=1 ;;
--no-doc|--no-docs|--no-documentation) nodoc=1 ;;
--add-deppath=*) deppath="${arg#*=}:$deppath" ;;
--add-incpath=*) incpath="${arg#*=}:$incpath" ;;
--prefer-shared) prefer=shared ;;
--prefer-static) prefer=static ;;
--set-shared=*) eval "link${arg#*=}=1" ;;
--set-static=*) eval "link${arg#*=}=2" ;;
--with-*) setoption $(expr substr "$arg" 8 ${#arg}) 0 ;;
--without-*) setoption $(expr substr "$arg" 11 ${#arg}) 1 ;;
--enable-*) setoption $(expr substr "$arg" 10 ${#arg}) 0 ;;
--disable-*) setoption $(expr substr "$arg" 11 ${#arg}) 1 ;;
-h|--help) usage 0 ;;
-*) error 1 "invalid option '$arg'" ;;
*) target=$arg ;;
esac
done
if test -n "$prefix"; then
deppath="$prefix/lib:$deppath"
incpath="$prefix/include:$incpath"
fi
if test -z "$prefix"; then prefix="/usr"; fi
if test -z "$bindir"; then bindir="$prefix/bin"; fi
if test -z "$libdir"; then libdir="$prefix/lib"; fi
if test -z "$incdir"; then incdir="$prefix/include"; fi
if test -z "$sharedir"; then sharedir="$prefix/share"; fi
deppath="$(echo $deppath | sed 's/\/\+/\//g')"
incpath="$(echo $incpath | sed 's/\/\+/\//g')"
bindir="$(echo $bindir | sed 's/\/\+/\//g')"
libdir="$(echo $libdir | sed 's/\/\+/\//g')"
incdir="$(echo $incdir | sed 's/\/\+/\//g')"
sharedir="$(echo $sharedir | sed 's/\/\+/\//g')"
ndeppath=$(echo $deppath | tr -dc : | wc -c)
nincpath=$(echo $incpath | tr -dc : | wc -c)
ndeppath=$(( ndeppath + 1 ))
nincpath=$(( nincpath + 1 ))
# pass 2, to be able to use state of other options as selection
setoptionsdefault
cprefix=$(sanitize "$name")
procdep_real()
{
local depdir="$1"
local intdir="$2"
local depname="${depdir##*/}"
# internal or not?
local isint=0
if test -d "$intdir/$depname"; then isint=1; fi
# is it lila?
local islila=0
if ! test -e "$depdir/get_version"; then islila=1; fi
if test $isint -eq 1; then
procdep_search "$depname" "$depdir" "$intdir" $isint $islila "$intdir/$depname"
else
local i=1
local s
while test $i -le $ndeppath; do
s="$(echo $deppath | cut -d: -f$i)"
s="$(procdep_search "$depname" "$depdir" "$intdir" $isint $islila "$s")"
if ! test "$(expr substr "$s" 3 8)" = "NOTFOUND"; then
echo $s
return
fi
i=$(( i + 1 ))
done
echo "$isint NOTFOUND"
fi
}
procdep_search()
{
local depname="$1"
local depdir="$2"
local intdir="$3"
local isint="$4"
local islila="$5"
local libdir="$6"
local d="$libdir"
local incdir
# look for it
local vpath
local vpatha
local inc
if ! test -e "$depdir/cpnt"; then
if test $islila -eq 1; then
vpath="$d"
vpatha="$d"
if test $isint -eq 1; then d="$d/build"; fi
if ! test -d "$d/comain/$depname"; then
echo $isint NOTFOUND
return
fi
else
r=0
vpath="$d/$($depdir/is_found $isint shared "$d" 2>/dev/null)"
if test $? -eq 0; then r=1; fi
vpatha="$d/$($depdir/is_found $isint static "$d" 2>/dev/null)"
if test $? -eq 0; then r=1; fi
if test $r -eq 0; then
echo $isint NOTFOUND
return
fi
local p
local n
if test $isint -eq 1; then
n=1
p="$d"
else
n=$nincpath
p="$incpath"
fi
local i=1
local s
r=0
while test $i -le $n; do
s="$(echo $p | cut -d: -f$i)"
inc="$($depdir/is_found $isint include "$s" 2>/dev/null)"
if test $? -eq 0; then
r=1
break
fi
i=$(( i + 1 ))
done
if test $r -eq 0; then
echo $isint INCNOTFOUND
return
fi
if test "$inc" = "-"; then
incdir=
else
incdir="$s/$inc"
fi
fi
fi
# get version
local version
if test $islila -eq 1; then
if test -e "$depdir/cpnt"; then
d="$intdir/$depname/meta"
else
if test $isint -eq 1; then
d="$intdir/$depname/build"
else
d="$libdir"
fi
d="$d/comain/$depname"
fi
version="$(cat "$d/version" 2>/dev/null)"
else
version="$("$depdir/get_version" $isint "$intdir/$depname" "$libdir" "$incdir" 2>/dev/null)"
fi
if test -z "$version"; then
echo $isint NOVERSION
return
fi
# needed version
local needver="$(cat "$depdir/version" 2>/dev/null)"
if test -z "$needver"; then
echo $isint NONEEDVER
return
fi
local verok=$(vercmp "$version" "$needver")
if test $verok -lt 0; then
echo $isint BADVERSION $version $needver $verok
return
fi
# get data: libs, flags...
local libs
local cflags
local cppflags
local ldflags
if test $islila -eq 1; then
libs="$(cat "$d/libs" 2>/dev/null)"
libsA="$(cat "$d/libs.static" 2>/dev/null)"
if test $isint -eq 1; then
inc="$intdir/$depname/src/lib$depname/include"
else
inc="$(cat "$d/incdir" 2>/dev/null)"
fi
ldflags="$(cat "$d/ldflags" 2>/dev/null)"
ldflagsA="$(cat "$d/ldflags.static" 2>/dev/null)"
cflags="$(cat "$d/cflags" 2>/dev/null)"
cflagsA="$(cat "$d/cflags.static" 2>/dev/null)"
cppflags="$(cat "$d/cppflags" 2>/dev/null)"
cppflagsA="$(cat "$d/cppflags.static" 2>/dev/null)"
else
libs="$("$depdir/get_libs" shared $isint "$intdir/$depname" "$libdir" "$incdir" 2>/dev/null)"
libsA="$("$depdir/get_libs" static $isint "$intdir/$depname" "$libdir" "$incdir" 2>/dev/null)"
inc="$incdir"
ldflags="$("$depdir/get_ldflags" shared $isint "$intdir/$depname" "$libdir" "$incdir" 2>/dev/null)"
ldflagsA="$("$depdir/get_ldflags" static $isint "$intdir/$depname" "$libdir" "$incdir" 2>/dev/null)"
cflags="$("$depdir/get_cflags" shared $isint "$intdir/$depname" "$libdir" "$incdir" 2>/dev/null)"
cflagsA="$("$depdir/get_cflags" static $isint "$intdir/$depname" "$libdir" "$incdir" 2>/dev/null)"
cppflags="$("$depdir/get_cppflags" shared $isint "$intdir/$depname" "$libdir" "$incdir" 2>/dev/null)"
cppflagsA="$("$depdir/get_cppflags" static $isint "$intdir/$depname" "$libdir" "$incdir" 2>/dev/null)"
fi
test -n "$inc" && cppflags="-isystem $inc $cppflags"
# lila: dependencies?
if test $islila -eq 1; then
if test $isint -eq 1; then
d="$intdir/$depname/build"
else
d="$libdir"
fi
d="$d/comain/$depname/deps"
if test -d "$d"; then
for d in "$d/"*; do
local tmp=configure-$PPID-$$
while test -e $tmp; do
tmp=configure-$PPID-$$-"$(date +%s 2>/dev/null)"
done
local err
local i=0
mkfifo $tmp
procdep_real "$d" "$intdir" > "$tmp" &
while IFS= read -r arg; do
if test $i -ne 1; then
i=1
err="$(expr substr "$arg" 3 ${#arg})"
else
case "$arg" in
LIBS=*) libs="$libs $(expr substr "$arg" 6 ${#arg}) " ;;
LIBSA=*) libsA="$libsA $(expr substr "$arg" 7 ${#arg}) " ;;
LDFLAGS=*) ldflags="$ldflags $(expr substr "$arg" 9 ${#arg}) " ;;
LDFLAGSA=*) ldflagsA="$ldflagsA $(expr substr "$arg" 10 ${#arg}) " ;;
CFLAGS=*) cflags="$cflags $(expr substr "$arg" 8 ${#arg}) " ;;
CFLAGSA=*) cflagsA="$cflagsA $(expr substr "$arg" 9 ${#arg}) " ;;
CPPFLAGS=*) cppflags="$cppflags $(expr substr "$arg" 10 ${#arg}) " ;;
CPPFLAGSA=*) cppflagsA="$cppflagsA $(expr substr "$arg" 11 ${#arg}) " ;;
VPATH=*) vpath="$vpath $(expr substr "$arg" 7 ${#arg}) " ;;
VPATHA=*) vpathA="$vpathA $(expr substr "$arg" 8 ${#arg}) " ;;
esac
fi
done < $tmp
rm -r $tmp
if ! test "$(expr substr "$err" 1 3)" = "OK "; then
echo $isint ERRDEP ${d##*/} $err
return
fi
done
fi
fi
cat <<EOF
$isint OK $version $needver $verok
LIBS=$(echo $libs | tr '\n' ' ')
LIBSA=$(echo $libsA | tr '\n' ' ')
LDFLAGS=$(echo $ldflags | tr '\n' ' ')
LDFLAGSA=$(echo $ldflagsA | tr '\n' ' ')
CFLAGS=$(echo $cflags | tr '\n' ' ')
CFLAGSA=$(echo $cflagsA | tr '\n' ' ')
CPPFLAGS=$(echo $cppflags | tr '\n' ' ')
CPPFLAGSA=$(echo $cppflagsA | tr '\n' ' ')
VPATH=$(echo $vpath | tr '\n' ' ')
VPATHA=$(echo $vpathA | tr '\n' ' ')
EOF
}
procneeddep()
{
local depname=$1
local needed
eval needed=\"\$need$depname\"
if test -z "$needed"; then return 0; fi
return 1
}
procdep_error()
{
local s
s="$@"
case ${s%% *} in
BADVERSION) echo "version too old" ;;
NOTFOUND) echo "not found" ;;
INCNOTFOUND) echo "include dir not found" ;;
NOVERSION) echo "version unknown";;
NONEEDVER) echo "needed version unknown" ;;
ERRDEP) echo "dependency error" ;;
*) echo "unexpected error" ;;
esac
}
procdep()
{
local idx=$1
local dep="$2"
local depname="${dep##*/}"
local cpnt="$3"
local link=0
local needver="$(cat $dep/version 2>/dev/null)"
# include dir: defaults to $incdir/NAME
local path="$depname"
# a different dirname (inside $incdir) might be specified
if test -e "$dep/include"; then path="$(cat $dep/include)"; fi
local path="$incdir/$path"
local internal=0
local found=0
local depdir=
if test "$cpnt" -ne 1; then
type="Dependency"
depdir="deps/$depname"
else
type="Component"
internal=1
depdir="./$depname"
fi
if procneeddep "$depname"; then
echo " -> $type $depname not needed; SKipping"
eval idx$depname=$1
eval after$depname=0
return
fi
local tmp=configure-$PPID-$$
while test -e $tmp; do
tmp=configure-$PPID-$$-"$(date +%s 2>/dev/null)"
done
local s
local i=0
mkfifo $tmp
procdep_real "$dep" "deps" > "$tmp" &
while IFS= read -r arg; do
if test $i -ne 1; then
i=1
s="$arg"
else
case "$arg" in
LIBS=*) deplibs="$(expr substr "$arg" 6 ${#arg}) " ;;
LIBSA=*) deplibsA="$(expr substr "$arg" 7 ${#arg}) " ;;
LDFLAGS=*) depldflags="$(expr substr "$arg" 9 ${#arg}) " ;;
LDFLAGSA=*) depldflagsA="$(expr substr "$arg" 10 ${#arg}) " ;;
CFLAGS=*) depcflags="$(expr substr "$arg" 8 ${#arg}) " ;;
CFLAGSA=*) depcflagsA="$(expr substr "$arg" 9 ${#arg}) " ;;
CPPFLAGS=*) depcppflags="$(expr substr "$arg" 10 ${#arg}) " ;;
CPPFLAGSA=*) depcppflagsA="$(expr substr "$arg" 11 ${#arg}) " ;;
VPATH=*) vpath="$(expr substr "$arg" 7 ${#arg}) " ;;
VPATHA=*) vpathA="$(expr substr "$arg" 8 ${#arg}) " ;;
esac
fi
done < $tmp
rm -r $tmp
if test "$(expr substr "$s" 1 1)" = "1"; then internal=1; fi
s="$(expr substr "$s" 3 ${#s})"
echon " -> $type $depname "
if test $internal -ne 1; then
echon "(system) "
else
echon "(internal) "
# internal deps defaults to static linking
link=2
fi
local err="$(procdep_error $s)"
local code="${s%% *}"
if test "$code" = "OK" || test "$code" = "BADVERSION"; then
local i=0
for s in $s; do
case $i in
1) depver=$s ;;
2) needver=$s ;;
3) cmp=$s ;;
esac
i=$(( $i + 1 ))
done
echon "version $depver ($needver needed)"
fi
if ! test "$code" = "OK"; then
echo
local m=
if test "$code" = "ERRDEP"; then
m=${s#* }
m=" (${m%% *}: $(procdep_error ${m#* }))"
fi
test -z "$fail" && fail="$type $depname: $err$m"
return
fi
# linking type
local l="$(eval "echo \$link$depname")"
if test -n "$l" && test "$l" -ge 0; then link="$l"; fi
if test $cpnt -ne 1 && test $link -gt 0; then
if test $link -eq 1; then
echo " (shared linking)"
else
echo " (static linking)"
fi
else
echo
fi
if test $cpnt -ne 1; then
eval idx$depname=$1
eval after$depname=0
eval "cflags$depname=\"$depcflags\""
eval "cflagsA$depname=\"$depcflagsA\""
eval "cppflags$depname=\"$depcppflags\""
eval "cppflagsA$depname=\"$depcppflagsA\""
eval "ldflags$depname=\"$depldflags\""
eval "ldflagsA$depname=\"$depldflagsA\""
eval "libs$depname=\"$deplibs\""
eval "libsA$depname=\"$deplibsA\""
mkvpath()
{
local link=$1
local vpath="$2"
shift 2
if test $link -eq 0; then link=3; fi
local ext1=so
local ext2=a
if test $prefer = "static"; then
ext1=a
ext2=so
if test $link -lt 3; then
link=$((1 + ($link & 1)))
fi
fi
for lib in $@; do
if test $(($link & 5)) -ge 1; then
echo vpath lib$(expr substr "$lib" 3 ${#lib}).$ext1 $vpath
fi
if test $(($link & 6)) -ge 2; then
echo vpath lib$(expr substr "$lib" 3 ${#lib}).$ext2 $vpath
fi
link=$(($link | 4))
done
}
VPATHS="$VPATHS
$(mkvpath $link "$vpath" $deplibs)"
if test $depname = "skalibs"; then
if test $internal -eq 1; then
sysdeps="$depdir/sysdeps.cfg"
else
if test -z "$sysdeps"; then sysdeps="$(echo $vpath)/skalibs/sysdeps"; fi
sysdeps="$(echo $sysdeps | sed 's/\/\+/\//g')"
fi
fi
fi
}
if test -d deps; then
echo "Building internal dependencies..."
comain/blddeps build
fi
fail=
cat <<EOF
Configuring $name $version :
=> Prefer $prefer linking for dependencies
=> Checking requirements...
EOF
loaddeps
foreach "cpnt" $nb_cpnt procdep 1
foreach "deps" $nb_deps procdep 0
setdepsafters()
{
local dep
for dep in meta/deps/*/after; do
if test "$dep" = "meta/deps/*/after"; then break; fi
local depname="$(expr substr "$dep" 11 $((${#dep} - 16)))"
local depidx=$(($(eval echo \$idx$depname) + 0))
local after=$(($(eval echo \$after$depname) + 0))
local name
for name in meta/deps/$depname/after/*; do
name=${name##*/}
local idx=$(eval echo \$idx$name)
after=$(($after | (1 << ($idx - 1))))
done
eval "after$depname=$after"
done
}
setdepsafters
if test -n "$fail"; then
error 2 "Cannot continue: $fail"
fi
cmdexists()
{
type "$1" > /dev/null 2>&1
}
trycmd()
{
local name="$1"
shift
test -z "$(eval echo \$$name)" && cmdexists "$1" && eval "$name=\"$*\""
}
tryflag()
{
echon " -> Checking whether compiler accepts $2.."
echo "typedef int x;" > "tmp.c"
if $CC $CPPFLAGS $CFLAGS "$2" -c -o "tmp.o" "tmp.c" > /dev/null 2>&1; then
echo ".. yes"
eval "$1=\"\${$1} \$2\""
eval "$1=\${$1# }"
return 0
else
echo ".. no"
return 1
fi
}
tryldflag()
{
echon " -> Checking whether linker accepts $2.."
echo "typedef int x;" > "tmp.c"
if $CC $CPPFLAGS $CFLAGS $LDFLAGS -nostdlib "$2" -o "tmp.o" "tmp.c" > /dev/null 2>&1; then
echo ".. yes"
eval "$1=\"\${$1} \$2\""
eval "$1=\${$1# }"
return 0
else
echo ".. no"
return 1
fi
}
trap 'rm -f tmp.c tmp.o config.mk.tmp config.h.tmp' EXIT ABRT INT QUIT TERM HUP
echo " => Checking compiler..."
echon " -> Looking for a C compiler.."
trycmd CC "gcc"
trycmd CC "clang"
trycmd CC "cc"
if test -z "$CC"; then
echo ".. not found"
error 2 "Unable to find a C compiler"
else
echo ".. $CC"
fi
trycmd AR "ar"
trycmd RANLIB "ranlib"
ENDIAN=0
if test $has_skalibs -eq 1; then
echo " => Checking sysdeps... $sysdeps"
echon " -> Checking target.."
if test -z "$target" ; then
target=$($CC -dumpmachine 2>/dev/null) || target=unknown
fi
BITS=32
case "$target" in
*x86_64*) BITS=64 ;;
esac
echo ".. $target [${BITS}bit]"
if test ! -d "$sysdeps" || test ! -f "$sysdeps/target"; then
error 2 "$sysdeps is not a valid sysdeps directory"
fi
if test "x$target" != "x$(cat "$sysdeps/target")"; then
error 2 "target $target does not match the contents of $sysdeps/target ($(cat "$sysdeps/target"))"
fi
echon " -> Checking endianness.."
case "$(grep ^endianness: "$sysdeps/sysdeps" | cut -d' ' -f2)" in
little) ENDIAN=LITTLE; echo ".. little" ;;
big) ENDIAN=BIG; echo ".. big" ;;
pdp) ENDIAN=PDP; echo ".. pdp" ;;
*) echo ".. unsupported";
error 2 "unsupported endianness ($(grep ^endianness: "$sysdeps/sysdeps"))"
;;
esac
fi
echo " => Testing C compiler..."
tryflag COMMON_CFLAGS -std=c99
tryflag COMMON_CFLAGS -fomit-frame-pointer
tryflag COMMON_CFLAGS -fno-exceptions
tryflag COMMON_CFLAGS -fno-unwind-tables
tryflag COMMON_CFLAGS -fno-asynchronous-unwind-tables
tryflag COMMON_CPPFLAGS -Werror=implicit-function-declaration
tryflag COMMON_CPPFLAGS -Werror=implicit-int
tryflag COMMON_CPPFLAGS -Werror=pointer-sign
tryflag COMMON_CPPFLAGS -Werror=pointer-arith
tryflag COMMON_CPPFLAGS -Wno-unused-value
tryflag COMMON_CFLAGS -ffunction-sections
tryflag COMMON_CFLAGS -fdata-sections
tryldflag COMMON_LDFLAGS -Wl,--as-needed
tryldflag COMMON_LDFLAGS -Wl,--sort-section=alignment
tryldflag COMMON_LDFLAGS -Wl,--sort-common
tryflag COMMON_CPPFLAGS_SHARED -fPIC
tryldflag COMMON_LDFLAGS_SHARED -Wl,--hash-style=gnu
tryldflag COMMON_LDFLAGS_SHARED -nostartfiles
echo " => Paths :"
cat <<EOF
-> bindir : $bindir
-> libdir : $libdir
-> incdir : $incdir
-> sharedir : $sharedir
EOF
excl=
options=
optleft=0
showoption()
{
local idx=$1
local name=$2
local disabled
local invisible
local cvar
eval disabled=\$optdisabled$name
eval invisible=\$optinvisible$name
eval cvar=\$optcvar$name
if test $disabled -eq 1; then
CVARS="$CVARS
#undef ${cprefix}_$cvar"
if test -e meta/options/$name/objs; then
while IFS= read -r line; do
line="$(printf $line | sed -e 's/\//\\\//g')"
excl="-e s/build\\/$line//g $excl"
if test "$(expr substr "$line" $((${#line}-1)) 2)" = ".o"; then
line="$(expr substr "$line" 1 $((${#line}-2))).lo"
excl="-e s/build\\/$line//g $excl"
fi
done < meta/options/$name/objs
fi
else
CVARS="$CVARS
#define ${cprefix}_$cvar 1"
optleft=$(($optleft | (1 << ($idx - 1))))
local desc
eval desc=\$optdesc$name
if test -n "$desc"; then
options="$options
-> $name : $desc"
fi
fi
}
if test $nb_options -gt 0; then
foreach "optname" $nb_options showoption
if test -n "$options"; then
echo " => Options :$options"
fi
fi
setoptionflags()
{
local name=$1
local cppflags
local cflags
local ldflags
eval cppflags=\"\$optcppflags$name\"
eval cflags=\"\$optcflags$name\"
eval ldflags=\"\$optldflags$name\"
if test -n "$cppflags"; then COMMON_CPPFLAGS="$COMMON_CPPFLAGS $cppflags"; fi
if test -n "$cflags"; then COMMON_CFLAGS="$COMMON_CFLAGS $cflags"; fi
if test -n "$ldflags"; then COMMON_LDFLAGS="$COMMON_LDFLAGS $ldflags"; fi
}
setoptionsflags()
{
local i=1
while test $optleft -gt 0; do
local n=$((1 << ($i - 1)))
if test $(($optleft & $n)) -gt 0; then
local name="$(eval echo \$optname$i)"
local after=$((($(eval echo \$optafter$name) + 0) & $optleft))
eval "optafter$name=$after"
if test $after -eq 0; then
setoptionflags "$name"
optleft=$(($optleft & ~$n))
fi
fi
if test $i -eq $nb_options; then
i=1
else
i=$(($i + 1))
fi
done
}
setoptionsflags
if test $(($noshared + $nostatic)) -gt 0; then
echo " => Libraries"
if test $noshared -eq 1; then
echo " -> Don't build shared libraries"
fi
if test $nostatic -eq 1; then
echo " -> Don't build static libraries"
fi
fi
if test $nodoc -eq 1; then
cat <<EOF
=> Miscellaneous
-> Don't build documentation
EOF
fi
echo " => Generating files..."
echo " -> Generating config.mk..."
exec 5>&1 1>config.mk.tmp
cat <<EOF
# This file was generated by:
# $0 $@
# Any changes you make in here will be lost on the next ./configure
# paths
BINDIR = $bindir
LIBDIR = $libdir
INCDIR = $incdir
SHAREDIR = $sharedir
# general variables
PROJECT_NAME = $name
PROJECT_VERSION = $version
CC = $CC
AR = $AR
RANLIB = $RANLIB
NOSHARED := $noshared
NOSTATIC := $nostatic
EOF
if test -n "$BITS"; then
echo "BITS := $BITS"
echo ""
fi
# add our own libraries so we can have binaries depending on them
intlib=$nb_deps
intaft=0
for lib in src/lib*; do
if test $lib = "src/lib*"; then break; fi
lib="$(expr substr "$lib" 8 ${#lib})"
intaft=$(( $intaft + (1 << $nb_deps) ))
nb_deps=$((nb_deps + 1))
eval "idx$lib=$nb_deps"
eval "deps$nb_deps=$lib"
if test -e meta/lib$lib.libs; then
eval "libs$lib=\"$(cat meta/lib$lib.libs)\""
else
eval "libs$lib=-l$lib"
fi
if test -e meta/lib$lib.libs.static; then
eval "libsA$lib=\"$(cat meta/lib$lib.libs.static)\""
fi
if test -e meta/lib$lib.cflags; then
eval "cflags$lib=\"$(cat meta/lib$lib.cflags)\""
fi
if test -e meta/lib$lib.cflags.static; then
eval "cflagsA$lib=\"$(cat meta/lib$lib.cflags.static)\""
fi
if test -e meta/lib$lib.ldflags; then
eval "ldflags$lib=\"$(cat meta/lib$lib.ldflags)\""
fi
if test -e meta/lib$lib.ldflags.static; then
eval "ldflagsA$lib=\"$(cat meta/lib$lib.ldflags.static)\""
fi
# in case there was a --set-shared/--set-static for it
link=$(eval echo \$link$lib)
# if not, we shall default to static (if built)
if test -z "$link"; then
if test $nostatic -eq 1; then
link=1
else
link=2
fi
fi
if test $link -eq 1; then
VPATHS="$VPATHS
vpath lib$lib.so ."
else
VPATHS="$VPATHS
vpath lib$lib.a ."
fi
eval "cppflags$lib=\"-isystem src/lib$lib/include\""
done
# not sure this really matters, so long as both are mentioned it will be the
# order of our vpath that will matter really - still, let's keep it that way
if test "$prefer" = "static"; then
echo "# prefer static linking"
echo ".LIBPATTERN := lib%.a lib%.so"
else
echo "# prefer shared linking"
echo ".LIBPATTERN := lib%.so lib%.a"
fi
echo $VPATHS
PER_FLAGS=
echo "# dependencies"
echo ".SECONDEXPANSION:"
BLD_STATIC_LIBS=
BLD_SHARED_LIBS=
BLD_BINS=
BLD_DOCS=
INST_HDR=
HEADERS=
setupobjs()
{
local docs_objs
local d
# FIXME find's -printf isn't POSIX
lf="
"
for d in src/*; do
local name="$(expr substr "$d" 5 ${#d})"
if test "$name" = "*" || test "$name" = "include"; then continue; fi
local objs
if test "$name" = "extra"; then
continue
elif test "$name" = "doc"; then
if test $nodoc -eq 1; then continue; fi
objs="$(find "$d" -name '*.md' \! -path "$d"/include/'*')"
for i in 9 8 7 6 5 4 3 2 1 0; do
local s="$(echo "$objs" \
| sed -e 's/^src\/doc\/\(.\+\).'$i'.md$/build\/doc\/\1.'$i'.md/g' \
-e 's/src\/.\+$//g' \
| tr '\n' ' ' | sed -e 's/ \+/ /g')"
BLD_DOCS="BLD_MD$i=$s$lf$BLD_DOCS"
done
BLD_DOCS="BLD_DOCS=$(echo "$objs" \
| sed -e 's/^src\/doc.*\/\(.\+\.\(.\)\.md\)$/\2\/\1/g' \
| tr '\n' ' ' | sed -e 's/ \+/ /g')$lf$BLD_DOCS"
BLD_DOCS="$(echo "$objs" \
| sed -e 's/^src\/doc\(.*\)\/\(.\+\.\(.\)\.md\)$/\$(DESTMD)\/\3\/\2: build\/doc\1\/\2\n $(INSTMD)/g' \
)$lf$BLD_DOCS"
continue
elif test "$(expr substr "$name" 1 3)" = "lib"; then
objs="$(find "$d"/include -type f)"
INST_HDR="$(echo "$objs" \
| sed -e 's/^src\/lib\(.\+\)\/include\/\(.\+\)$/$(DESTDIR)$(INCDIR)\/\2: \0\n $(INSTHDR)/g' \
)$lf$INST_HDR"
HEADERS="$(echo "$objs" \
| sed -e 's/^src\/lib.\+\/include\/\(.\+\)$/\1/g' \
| tr '\n' ' ') $HEADERS"
fi
objs="$(find "$d" -name '*.[cS]' | sed -e s/^src/build/g \
-e s/\\.c$/.o/g -e s/\\.S/.o/g $excl | tr '\n' ' ')"
objs="$objs $(find "$d" -type l -name '*.o' -printf "build/%l ")"
local cppflags=
local cflags=
local cflagsA=
local ldflags=
local ldflagsA=
local libs=
local libsA=
local deps=0
local depname
local tmp=configure-$PPID-$$
while test -e $tmp; do
tmp=configure-$PPID-$$-"$(date +%s 2>/dev/null)"
done
mkfifo $tmp
find "$d" -type f -name '+*' > $tmp &
while IFS= read -r depname; do
depname=${depname##*/+}
local idx=$(($(eval echo \$idx$depname) + 0))
if test $idx -ge 1; then
deps=$(($deps | (1 << ($idx - 1))))
fi
done < $tmp
rm -r $tmp
local i=1
while test $deps -gt 0; do
local n=$((1 << ($i - 1)))
if test $(($deps & $n)) -gt 0; then
depname=$(eval echo \$deps$i)
depname=${depname##*/}
local after=0
if test $i -le $intlib; then
# this is an "actual" deps, i.e. not an internal one
# so we get its actual value for "after"
after=$((($(eval echo \$after$depname) + 0) & $deps))
# and make sure that it is also after all internal deps
after=$(( ($intaft & $deps) + $after ))
fi
if test $after -eq 0; then
libs="$libs $(eval echo \$libs$depname)"
libsA="$libsA $(eval echo \$libsA$depname)"
cppflags="$cppflags $(eval echo \$cppflags$depname)"
cflags="$cflags $(eval echo \$cflags$depname)"
cflagsA="$cflagsA $(eval echo \$cflagsA$depname)"
ldflags="$ldflags $(eval echo \$ldflags$depname)"
ldflagsA="$ldflagsA $(eval echo \$ldflagsA$depname)"
deps=$(($deps & ~$n))
fi
fi
if test $i -eq $nb_deps; then
i=1
else
i=$(($i + 1))
fi
done
if test "$(expr substr "$name" 1 3)" = "lib"; then
local l="$(expr substr "$name" 4 ${#name})"
COMMON_CPPFLAGS="-isystem src/$name/include $COMMON_CPPFLAGS"
if test $nostatic -eq 0; then
echo "$name.a: $objs"
BLD_STATIC_LIBS="$name.a $BLD_STATIC_LIBS"
fi
if test $noshared -eq 0; then
objs="$(find "$d" -type f -name '*.[cS]' | sed -e s/^src/build/g \
-e s/\\.c$/.lo/g -e s/\\.S/.o/g $excl | tr '\n' ' ')"
objs="$objs $(find "$d" -type l -printf "build/%l " | sed -e s/\\.o/.lo/g)"
echo "$name.so: $objs $libs"
BLD_SHARED_LIBS="$name.so $BLD_SHARED_LIBS"
fi
else
BLD_BINS="$name $BLD_BINS"
echo "$name: $objs $libs"
fi
cppflags=$(trim "$cppflags")
if test -n "$cppflags"; then
append PER_FLAGS "CPPFLAGS_$name = $cppflags"
fi
cflags=$(trim "$cflags")
if test -n "$cflags"; then
append PER_FLAGS "CFLAGS_$name = $cflags"
fi
cflagsA=$(trim "$cflagsA")
if test -n "$cflagsA"; then
append PER_FLAGS "CFLAGS_STATIC_$name = $cflagsA"
fi
ldflags=$(trim "$ldflags")
if test -n "$ldflags"; then
append PER_FLAGS "LDFLAGS_$name = $ldflags"
fi
ldflagsA=$(trim "$ldflagsA")
if test -n "$ldflagsA"; then
append PER_FLAGS "LDFLAGS_STATIC_$name = $ldflagsA $libsA $libs"
fi
done
echo "# docs"
echo "$docs_objs"
}
setupobjs
cat <<EOF
# build targets
$BLD_DOCS
$INST_HDR
BLD_STATIC_LIBS = $BLD_STATIC_LIBS
BLD_SHARED_LIBS = $BLD_SHARED_LIBS
BLD_BINS = $BLD_BINS
HEADERS = $HEADERS
# some flags
COMMON_CPPFLAGS += $COMMON_CPPFLAGS
COMMON_CFLAGS += $COMMON_CFLAGS
COMMON_LDFLAGS += $COMMON_LDFLAGS
COMMON_CPPFLAGS_SHARED += $COMMON_CPPFLAGS_SHARED
COMMON_CFLAGS_SHARED += $COMMON_CFLAGS_SHARED
COMMON_LDFLAGS_SHARED += $COMMON_LDFLAGS_SHARED
CPPFLAGS += $CPPFLAGS
CFLAGS += $CFLAGS
LDFLAGS += $LDFLAGS
$PER_FLAGS
EOF
exec 1>&5 5>&-
mv -f config.mk.tmp config.mk
echo " -> Generating config.h..."
url="$(head -1 meta/site 2>/dev/null)"
if test -z "$url"; then url="https://lila.oss"; fi
exec 5>&1 1>config.h.tmp
cat <<EOF
/* This file was generated by:
* $0 $@
* Any changes you make in here will be lost on the next ./configure
*/
#ifndef ${cprefix}_CONFIG_H
#define ${cprefix}_CONFIG_H
#define ${cprefix}_CURYEAR "$(date +%Y)"
#define ${cprefix}_AUTHOR "Olivier Brunel"
#define ${cprefix}_VERSION "$version"
#define ${cprefix}_URL "$url"
#define ${cprefix}_LIBDIR "$libdir/$name"
#define ${cprefix}_SHAREDIR "$sharedir/$name"
$CVARS
#endif /* ${cprefix}_CONFIG_H */
EOF
exec 1>&5 5>&-
if ! test -d src/include; then mkdir src/include; fi
mv -f config.h.tmp src/include/config.h
rm -rf config.tmp
mkdir config.tmp
echo $incdir > config.tmp/incdir
if test $has_skalibs -eq 1; then
echo $ENDIAN > config.tmp/endian
fi
echo "done."