1
0
mirror of https://git.sr.ht/~sircmpwn/gmni synced 2024-11-23 04:51:59 +01:00
gmni/config.sh

196 lines
3.4 KiB
Bash
Raw Normal View History

2020-09-20 16:17:39 +02:00
outdir=${OUTDIR:-.build}
srcdir=${SRCDIR:-$(dirname "$0")}
AR=${AR:-ar}
AS=${AS:-as}
CC=${CC:-cc}
CFLAGS=${CFLAGS:-}
LD=${LD:-ld}
2020-09-20 20:44:30 +02:00
SCDOC=${SCDOC:-scdoc}
2020-09-20 16:17:39 +02:00
for arg
do
case "$arg" in
--bindir=*)
BINDIR=${arg#*=}
;;
--libdir=*)
LIBDIR=${arg#*=}
;;
--mandir=*)
MANDIR=${arg#*=}
;;
2020-09-20 16:17:39 +02:00
--prefix=*)
PREFIX=${arg#*=}
;;
esac
done
subdir() {
eval ". $srcdir/$1/configure"
}
genrules() {
target="$1"
shift
printf '# Begin generated rules for %s\n' "$target"
for file in "$@"
do
ext="${file#*.}"
file="${file%.*}"
deps=
printf '%s.o: %s.%s%s\n' "$file" "$file" "$ext" "$deps"
done
printf '%s_objects=\\\n' "$target"
n=0
for file in "$@"
do
file="${file%.*}"
n=$((n+1))
if [ $n -eq $# ]
then
printf '\t%s.o\n' "$file"
else
printf '\t%s.o \\\n' "$file"
fi
done
printf '# End generated rules for %s\n' "$target"
}
append_cflags() {
for flag
do
CFLAGS="$(printf '%s \\\n\t%s' "$CFLAGS" "$flag")"
done
}
test_cflags() {
[ ! -e "$outdir"/check.c ] && cat <<-EOF > "$outdir"/check.c
int main(void) { return 0; }
EOF
werror=""
case "$CFLAGS" in
*-Werror*)
werror="-Werror"
;;
esac
if $CC $werror "$@" -o "$outdir"/check "$outdir"/check.c >/dev/null 2>&1
2020-09-20 16:17:39 +02:00
then
append_cflags "$@"
else
return 1
fi
}
2020-09-20 19:06:34 +02:00
find_library() {
name="$1"
pc="$2"
printf "Checking for %s... " "$name"
if ! command -v pkg-config >/dev/null
then
printf "ERROR: pkg-config not found\n"
return 1
fi
2020-09-20 19:06:34 +02:00
if ! pkg-config "$pc" 2>/dev/null
then
printf "NOT FOUND\n"
printf "Tried pkg-config %s\n" "$pc"
return 1
fi
printf "OK\n"
CFLAGS="$CFLAGS $(pkg-config --cflags "$pc")"
LIBS="$LIBS $(pkg-config --libs "$pc")"
}
docs() { true; }
2020-09-20 16:17:39 +02:00
run_configure() {
mkdir -p $outdir
for flag in -g -std=c11 -D_XOPEN_SOURCE=700 -Wall -Wextra -Werror -pedantic
do
2020-09-20 19:06:34 +02:00
printf "Checking for %s... " "$flag"
2020-09-20 16:17:39 +02:00
if test_cflags "$flag"
then
echo yes
else
echo no
fi
done
# XXX: Asked the maintainer to provide a .pc file
LIBS="$LIBS -lbearssl"
2020-09-20 19:06:34 +02:00
2020-09-20 20:44:30 +02:00
printf "Checking for scdoc... "
if scdoc -v >/dev/null 2>&1
then
echo yes
all="$all docs"
install_docs="
mkdir -p \$(MANDIR)/man1
install -m644 doc/gmni.1 \$(MANDIR)/man1/gmni.1
install -m644 doc/gmnlm.1 \$(MANDIR)/man1/gmnlm.1"
2020-09-20 20:44:30 +02:00
else
echo no
fi
2020-09-20 16:17:39 +02:00
printf "Creating $outdir/config.mk... "
cat <<-EOF > "$outdir"/config.mk
CC=$CC
2020-09-20 20:44:30 +02:00
SCDOC=$SCDOC
2020-09-20 19:06:34 +02:00
LIBS=$LIBS
2020-09-20 16:17:39 +02:00
PREFIX=${PREFIX:-/usr/local}
OUTDIR=${outdir}
_INSTDIR=\$(DESTDIR)\$(PREFIX)
BINDIR?=${BINDIR:-\$(_INSTDIR)/bin}
LIBDIR?=${LIBDIR:-\$(_INSTDIR)/lib}
2020-10-24 19:40:39 +02:00
INCLUDEDIR?=${INCLUDEDIR:-\$(_INSTDIR)/include}
2020-09-20 16:17:39 +02:00
MANDIR?=${MANDIR:-\$(_INSTDIR)/share/man}
CACHE=\$(OUTDIR)/cache
CFLAGS=${CFLAGS}
CFLAGS+=-Iinclude -I\$(OUTDIR)
CFLAGS+=-DPREFIX='"\$(PREFIX)"'
CFLAGS+=-DLIBDIR='"\$(LIBDIR)"'
all: ${all}
install_docs: ${install_docs}
2020-09-20 16:17:39 +02:00
EOF
for target in $(printf '%s\n' $all | tr '.' '_')
do
$target
done >>"$outdir"/config.mk
2020-09-20 16:17:39 +02:00
echo done
touch $outdir/cppcache
if [ "$srcdir" = "." ]
then
return
fi
populate() (
path="$1"
mkdir -p "${path#$srcdir/}"
fullpath() ( cd "$1" && pwd )
for orig in "$path"/*
do
link="${orig#$srcdir/}"
if [ -d "$orig" ]
then
mkdir -p $link
populate "$orig"
elif [ -f "$orig" ]
then
ln -sf "$(fullpath "$path")"/"$(basename "$orig")" "$link"
fi
done
)
printf "Populating build dir... "
populate "$srcdir/include"
populate "$srcdir/src"
2020-09-20 20:44:30 +02:00
populate "$srcdir/doc"
2020-09-20 16:17:39 +02:00
ln -sf "$srcdir"/Makefile ./
echo done
}