Branch :
#!/bin/sh
## c3
## Copyright 2022 kmx.io <contact@kmx.io>
##
## Permission is hereby granted to use this software excepted
## on Apple computers granted the above copyright notice and
## this permission paragraph are included in all copies and
## substantial portions of this software.
##
## THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
## PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
## AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
## THIS SOFTWARE.
set -e
o_rule() {
"$CC" $CPPFLAGS $CFLAGS -M "$1" || { echo "$1" | sed -e 's/^\(.*\)\.c$/\1.o: \1.c/'; }
echo "$1" | sed -e 's/^\(.*\)\.c$/\1.o: Makefile config.mk/'
}
ext_rule() {
o_rule "$2" | sed -e "s/[.]o:/$1:/"
}
lo_rule() {
ext_rule .lo "$1"
}
prog_rule() {
{ o_rule "$1" || exit 1; } | sed -e 's/[.]o:/:/'
}
c2prog() {
echo "$@" | sed -e 's/[.]c$//' -e 's/[.]c / /g'
}
c2ext() {
EXT="$1"; shift
echo "$@" | sed -e "s/[.]c$/${EXT}/" -e "s/[.]c /${EXT} /g"
}
c2o() {
c2ext .o "$@"
}
c2lo() {
c2ext .lo "$@"
}
c2la() {
c2ext .la "$@"
}
config_asan() {
echo "int main() { return 0; }" > .config_asan.c
if $CC $CFLAGS .config_asan.c $LDFLAGS -lasan -o /dev/null 2>/dev/null; then
HAVE_ASAN=true
else
HAVE_ASAN=false
fi
rm .config_asan.c
}
config_libbsd() {
if pkg-config libbsd-overlay; then
CPPFLAGS="$CPPFLAGS -D_NOT_GNU_SOURCE -D_GNU_SOURCE"
CFLAGS="$CFLAGS $(pkg-config --cflags libbsd-overlay)"
LIBS="$LIBS $(pkg-config --libs libbsd-overlay) -lmd"
fi
}
config_libcrypto() {
if pkg-config libcrypto; then
CFLAGS="$CFLAGS $(pkg-config --cflags libcrypto)"
LIBS="$LIBS $(pkg-config --libs libcrypto)"
fi
}
config_libffi() {
if pkg-config libffi; then
CFLAGS="$CFLAGS $(pkg-config --cflags libffi)"
LIBS="$LIBS $(pkg-config --libs libffi)"
fi
}
require_pkg_config() {
if ! which pkg-config >/dev/null; then
echo "please install pkg-config" >&2
exit 1
fi
}
update_config_mk() {
if ! cmp "${CONFIG_MK}" config.mk >/dev/null 2>&1; then
mv "${CONFIG_MK}" config.mk
echo "$PWD/config.mk"
else
rm "${CONFIG_MK}"
fi
}
CONFIG_MK=config.mk
CONFIG_MK=".config.mk.tmp"
echo "# config.mk generated by configure" > ${CONFIG_MK}
VER=0.1
echo "VER = $VER" >> ${CONFIG_MK}
echo "C3_TOP = $C3_TOP" >> ${CONFIG_MK}
if test -z "$CC"; then
if test -n $(which cc); then
CC=cc
elif test -n $(which gcc); then
CC=gcc
fi
fi
echo "CC = $CC" >> ${CONFIG_MK}
if test -z "$GCOV"; then
if test -x "$(which gcov)"; then
GCOV=gcov
elif test -x "$(which egcov)"; then
GCOV=egcov
else
GCOV=gcov
fi
fi
echo "GCOV = $GCOV" >> ${CONFIG_MK}
if test -z "$INSTALL"; then
if test -x "$(which install)"; then
INSTALL=install
fi
fi
echo "INSTALL = $INSTALL" >> ${CONFIG_MK}
if test -z "$LIBTOOL"; then
if test -x "$(which glibtool 2>/dev/null)"; then
LIBTOOL="$(which glibtool)"
elif test -x "/usr/local/bin/libtool"; then
LIBTOOL="/usr/local/bin/libtool"
elif test -x "$(which libtool 2>/dev/null)"; then
LIBTOOL="$(which libtool)"
else
LIBTOOL=libtool
fi
fi
echo "LIBTOOL = $LIBTOOL" >> ${CONFIG_MK}
if test -z "$PREFIX"; then
if test -d "$HOME/.c3"; then
PREFIX="$HOME/.c3"
elif test -d /usr/local; then
PREFIX=/usr/local
elif test -d /usr; then
PREFIX=/usr
elif test -d /opt/local; then
PREFIX=/opt/local
elif test -d /opt; then
PREFIX=/opt
fi
fi
echo "PREFIX = $PREFIX" >> ${CONFIG_MK}
if test -z "$OWNER"; then
OWNER="$(ls -ld "$PREFIX" | tr -s ' ' | cut -d ' ' -f 3)"
fi
echo "OWNER = $OWNER" >> ${CONFIG_MK}
if test -z "$GROUP"; then
GROUP="$(ls -ld "$PREFIX" | tr -s ' ' | cut -d ' ' -f 4)"
fi
echo "GROUP = $GROUP" >> ${CONFIG_MK}
if test -z "$BINDIR"; then
BINDIR=${PREFIX}/bin
fi
echo "BINDIR = $BINDIR" >> ${CONFIG_MK}
if test -z "$LIBDIR"; then
LIBDIR=${PREFIX}/lib
fi
echo "LIBDIR = $LIBDIR" >> ${CONFIG_MK}