rtbuf/rtbuf/config.subr

Branch

Thomas de Grivel 98fc22aef 2022-11-03T10:29:37
configure
#!/bin/sh
## rtbuf
## Copyright 2018-2022 kmx.io <contact@kmx.io>
##
## Permission is hereby granted to use this software 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/'
}

lo_rule() {
    o_rule "$1" | sed -e 's/[.]o:/.lo:/'
}

prog_rule() {
    { o_rule "$1" || exit 1; } | sed -e 's/[.]o:/:/'
}

c2prog() {
    echo "$@" | sed -e 's/[.]c$//' -e 's/[.]c / /g'
}

c2o() {
    echo "$@" | sed -e 's/[.]c$/.o/' -e 's/[.]c /.o /g'
}

c2lo() {
    echo "$@" | sed -e 's/[.]c$/.lo/' -e 's/[.]c /.lo /g'
}

c2la() {
    echo "$@" | sed -e 's/[.]c$/.la/' -e 's/[.]c /.la /g'
}

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.4
echo "VER = $VER" >> ${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 "$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 "$INSTALL"; then
    if test -x "$(which install)"; then
        INSTALL=install
    fi
fi
echo "INSTALL = $INSTALL" >> ${CONFIG_MK}

if test -z "$PREFIX"; then
    if test -d "$HOME/.rtbuf"; then
        PREFIX="$HOME/.rtbuf"
    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 "$HOME/.rtbuf/" | tr -s ' ' | cut -d ' ' -f 3)"
fi
echo "OWNER = $OWNER" >> ${CONFIG_MK}

if test -z "$GROUP"; then
    GROUP="$(ls -ld "$HOME/.rtbuf/" | 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}

if test -z "$RTBUF_LIBDIR"; then
    RTBUF_LIBDIR=${LIBDIR}/rtbuf/${VER}
fi
echo "RTBUF_LIBDIR = $RTBUF_LIBDIR" >> ${CONFIG_MK}

rtbuf_library() {
    RTBUF_LIBRARY_LIBDIR="${RTBUF_LIBDIR}/$1"
    echo "RTBUF_LIBRARY_LIBDIR = $RTBUF_LIBRARY_LIBDIR" >> ${CONFIG_MK}

    LIBRARIES=

    echo sources: "'$SOURCES'"
    for SRC in $SOURCES; do
        SRC_LO="$(c2lo "$SRC")"
        echo >> ${CONFIG_MK}
        lo_rule "$SRC" >> ${CONFIG_MK}
        echo "	${LIBTOOL} --tag=CC --mode=compile \${CC} \${CPPFLAGS} \${CFLAGS} -c $SRC -o $SRC_LO" >> ${CONFIG_MK}
        SRC_LA="$(c2la "$SRC")"
        echo >> ${CONFIG_MK}
        echo "$SRC_LA: $SRC_LO" >> ${CONFIG_MK}
        echo "	${LIBTOOL} --tag=CC --mode=link \${CC} -no-undefined -module \${LDFLAGS} $SRC_LO \${LIBS} -o $SRC_LA -rpath ${RTBUF_LIBRARY_LIBDIR}" >> ${CONFIG_MK}
        LIBRARIES="$LIBRARIES $SRC_LA"
    done

    echo >> ${CONFIG_MK}
    echo "libraries:${LIBRARIES}" >> ${CONFIG_MK}

    echo >> ${CONFIG_MK}
    echo "install:${LIBRARIES}" >> ${CONFIG_MK}
    for LIB in $LIBRARIES; do
        echo "	\${INSTALL} -o \${OWNER} -g \${GROUP} -m 0755 -d \${RTBUF_LIBRARY_LIBDIR}" >> ${CONFIG_MK}
        echo "	\${LIBTOOL} --tag=CC --mode=install \${INSTALL} -o \${OWNER} -g \${GROUP} -m 0755 $LIB \${RTBUF_LIBRARY_LIBDIR}/$LIB" >> ${CONFIG_MK}
    done
}

check_glfw3() {
    LOG=.lib.glfw3.config.log
    OLD_CFLAGS=$CFLAGS
    OLD_LIBS=$LIBS
    CFLAGS="$CFLAGS $(pkg-config --cflags glfw3)"
    LIBS="$LIBS $(pkg-config --libs glfw3)"
    echo "$CC" $CPPFLAGS $CFLAGS "${LIB_SRCDIR}/glfw3/config.c" -o /dev/null ${LDFLAGS} ${LIBS} > $LOG
    if   "$CC" $CPPFLAGS $CFLAGS "${LIB_SRCDIR}/glfw3/config.c" -o /dev/null ${LDFLAGS} ${LIBS} >> $LOG 2>&1; then
        HAVE_GLFW3=Yes
    else
        HAVE_GLFW3=No
        CFLAGS=$OLD_CFLAGS
        LIBS=$OLD_LIBS
    fi
    echo "Checking library glfw3: $HAVE_GLFW3" >&2
    echo "HAVE_GLFW3 = $HAVE_GLFW3" >> ${CONFIG_MK}
}

check_libbsd() {
    if pkg-config libbsd-overlay; then
	HAVE_LIBBSD=Yes
    else
	HAVE_LIBBSD=No
    fi
}

check_portaudio() {
    if pkg-config portaudio-2.0; then
	CFLAGS="$CFLAGS $(pkg-config --cflags portaudio-2.0)"
	LIBS="$LIBS $(pkg-config --libs portaudio-2.0)"
        HAVE_PORTAUDIO=Yes
    else
        HAVE_PORTAUDIO=No
    fi
    echo "Checking library portaudio: $HAVE_PORTAUDIO" >&2
    echo "HAVE_PORTAUDIO = $HAVE_PORTAUDIO" >> ${CONFIG_MK}
}

check_sndio() {
    if pkg-config sndio; then
	CFLAGS="$CFLAGS $(pkg-config --cflags sndio)"
	LIBS="$LIBS $(pkg-config --libs sndio)"
        HAVE_SNDIO=Yes
    else
	OLD_LIBS="$LIBS"
	LIBS="$LIBS -lsndio"
	if "${CC}" ${CPPFLAGS} ${CFLAGS} ${LDFLAGS} "${SRCDIR}/lib/sndio/check_sndio.c" ${LIBS} -o /dev/null; then
	    HAVE_SNDIO=Yes
	else
	    LIBS="${OLD_LIBS}"
            HAVE_SNDIO=No
	fi
    fi
    echo "Checking library sndio: $HAVE_SNDIO" >&2
    echo "HAVE_SNDIO = $HAVE_SNDIO" >> ${CONFIG_MK}
}

type_prog() {
    TYPE_SRC="$1_type.c"
    TYPE_PROG="$(c2prog "$TYPE_SRC")"
    TYPE_H="${TYPE_PROG}.h"
    echo >> ${CONFIG_MK}
    prog_rule "$TYPE_SRC" >> ${CONFIG_MK}
    echo "	\${CC} \${CPPFLAGS} \${CFLAGS} $TYPE_SRC -o $TYPE_PROG" >> ${CONFIG_MK}
    echo >> ${CONFIG_MK}
    echo "${TYPE_H}: $TYPE_PROG" >> ${CONFIG_MK}
    echo "	./$TYPE_PROG > $TYPE_H" >> ${CONFIG_MK}
    echo >> ${CONFIG_MK}
    echo "CLEANFILES += $TYPE_PROG $TYPE_H" >> ${CONFIG_MK}
    if ! test -f "${TYPE_H}"; then
        touch "${TYPE_H}"
    fi
    TYPES="$TYPES $TYPE_H"
}
Download