2023-09-28 19:27:50 -04:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
|
|
|
#
|
|
|
|
# Argument Parsing
|
|
|
|
#
|
|
|
|
|
|
|
|
echo "Build Configuration"
|
|
|
|
echo "-------------------"
|
|
|
|
|
2023-09-28 23:21:31 -04:00
|
|
|
BUILD="build"
|
|
|
|
OUT="out"
|
|
|
|
SRC="src"
|
2024-01-13 18:40:31 -05:00
|
|
|
INCLUDE="include/Cytoplasm"
|
2023-09-28 23:21:31 -04:00
|
|
|
TOOLS="tools"
|
|
|
|
|
2024-01-13 17:13:45 -05:00
|
|
|
# Default compiler flags. These must be supported by all POSIX C compilers.
|
|
|
|
# "Fancy" compilers that have additional options must be detected and set below.
|
2024-08-07 14:45:53 -04:00
|
|
|
CFLAGS="-O1 -D_DEFAULT_SOURCE -I${INCLUDE} -I${SRC}";
|
2024-01-13 17:13:45 -05:00
|
|
|
LIBS="-lm -lpthread"
|
2023-09-28 19:27:50 -04:00
|
|
|
|
2024-01-13 17:13:45 -05:00
|
|
|
# Default args for all platforms.
|
|
|
|
SCRIPT_ARGS="--prefix=/usr/local --lib-name=Cytoplasm"
|
2023-09-28 23:21:31 -04:00
|
|
|
|
2024-01-13 17:13:45 -05:00
|
|
|
# Set SSL flags depending on the platform.
|
2023-09-28 19:27:50 -04:00
|
|
|
case "$(uname)" in
|
|
|
|
OpenBSD)
|
2024-08-07 06:48:33 -04:00
|
|
|
SCRIPT_ARGS="${SCRIPT_ARGS} --with-libressl --disable-lmdb"
|
2023-09-28 19:27:50 -04:00
|
|
|
;;
|
|
|
|
*)
|
2024-08-07 06:48:33 -04:00
|
|
|
SCRIPT_ARGS="${SCRIPT_ARGS} --with-openssl --disable-lmdb"
|
2023-09-28 19:27:50 -04:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2024-01-13 17:13:45 -05:00
|
|
|
# Set compiler depending on the platform.
|
|
|
|
case "$(uname)" in
|
|
|
|
Linux|NetBSD)
|
|
|
|
# These systems typically use GCC.
|
|
|
|
SCRIPT_ARGS="${SCRIPT_ARGS} --cc=gcc"
|
|
|
|
;;
|
|
|
|
OpenBSD|FreeBSD)
|
|
|
|
# These systems typically use Clang.
|
|
|
|
SCRIPT_ARGS="${SCRIPT_ARGS} --cc=clang"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
# Use default compiler which is required to be present on
|
|
|
|
# all POSIX-compliant systems.
|
|
|
|
SCRIPT_ARGS="${SCRIPT_ARGS} --cc=c99"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
# Append any additional args specified by user
|
|
|
|
SCRIPT_ARGS="${SCRIPT_ARGS} $@"
|
|
|
|
|
2023-09-28 19:27:50 -04:00
|
|
|
echo "Processing options..."
|
|
|
|
echo "Ran with arguments: $SCRIPT_ARGS"
|
|
|
|
|
|
|
|
# Process all arguments
|
|
|
|
for arg in $SCRIPT_ARGS; do
|
|
|
|
case "$arg" in
|
2023-11-04 15:58:23 -04:00
|
|
|
--cc=*)
|
|
|
|
CC=$(echo "$arg" | cut -d '=' -f 2-)
|
2024-01-13 17:13:45 -05:00
|
|
|
case "${CC}" in
|
|
|
|
gcc*|clang*)
|
|
|
|
# "Fancy" compilers that support a plethora of additional flags we
|
|
|
|
# want to enable if present.
|
2024-01-13 18:22:37 -05:00
|
|
|
CFLAGS="-Wall -Wextra -Werror -pedantic -std=c99 -O3 ${CFLAGS}"
|
2024-01-13 17:13:45 -05:00
|
|
|
LDFLAGS="-flto -fdata-sections -ffunction-sections -s -Wl,-gc-sections"
|
|
|
|
;;
|
|
|
|
esac
|
2023-11-04 15:58:23 -04:00
|
|
|
;;
|
2023-09-28 19:27:50 -04:00
|
|
|
--with-openssl)
|
2023-10-04 15:02:38 -04:00
|
|
|
TLS_IMPL="TLS_OPENSSL"
|
2023-09-28 19:27:50 -04:00
|
|
|
TLS_LIBS="-lcrypto -lssl"
|
|
|
|
;;
|
|
|
|
--with-libressl)
|
2023-10-04 15:02:38 -04:00
|
|
|
TLS_IMPL="TLS_LIBRESSL"
|
2023-09-28 19:27:50 -04:00
|
|
|
TLS_LIBS="-ltls -lcrypto -lssl"
|
|
|
|
;;
|
|
|
|
--disable-tls)
|
2023-10-30 11:29:15 -04:00
|
|
|
TLS_IMPL=""
|
2023-09-28 19:27:50 -04:00
|
|
|
TLS_LIBS=""
|
|
|
|
;;
|
2024-08-07 06:48:33 -04:00
|
|
|
--with-lmdb)
|
|
|
|
EDB_IMPL="EDB_LMDB"
|
|
|
|
EDB_LIBS="-llmdb"
|
|
|
|
;;
|
|
|
|
--disable-lmdb)
|
|
|
|
EDB_IMPL=""
|
|
|
|
EDB_LIBS=""
|
|
|
|
;;
|
2023-09-28 19:27:50 -04:00
|
|
|
--prefix=*)
|
|
|
|
PREFIX=$(echo "$arg" | cut -d '=' -f 2-)
|
|
|
|
;;
|
|
|
|
--lib-name=*)
|
|
|
|
LIB_NAME=$(echo "$arg" | cut -d '=' -f 2-)
|
|
|
|
;;
|
|
|
|
--enable-debug)
|
|
|
|
DEBUG="-O0 -g"
|
|
|
|
;;
|
|
|
|
--disable-debug)
|
|
|
|
DEBUG=""
|
|
|
|
;;
|
|
|
|
*)
|
2023-11-04 15:58:23 -04:00
|
|
|
echo "Invalid argument: $arg"
|
2023-09-28 19:27:50 -04:00
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ -n "$TLS_IMPL" ]; then
|
|
|
|
CFLAGS="${CFLAGS} -DTLS_IMPL=${TLS_IMPL}"
|
2023-09-28 19:53:57 -04:00
|
|
|
LIBS="${LIBS} ${TLS_LIBS}"
|
2023-09-28 19:27:50 -04:00
|
|
|
fi
|
|
|
|
|
2024-08-07 06:48:33 -04:00
|
|
|
if [ -n "$EDB_IMPL" ]; then
|
2024-08-09 06:03:09 -04:00
|
|
|
CFLAGS="${CFLAGS} -D${EDB_IMPL}"
|
2024-08-07 06:48:33 -04:00
|
|
|
LIBS="${LIBS} ${EDB_LIBS}"
|
|
|
|
fi
|
|
|
|
|
2024-01-13 17:13:45 -05:00
|
|
|
CFLAGS="${CFLAGS} '-DLIB_NAME=\"${LIB_NAME}\"' ${DEBUG}"
|
|
|
|
LDFLAGS="${LIBS} ${LDFLAGS}"
|
2023-09-28 19:27:50 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Makefile generation
|
|
|
|
#
|
|
|
|
|
2023-09-28 23:21:31 -04:00
|
|
|
collect() {
|
|
|
|
from="$1"
|
|
|
|
orig_ext="$2"
|
|
|
|
new_ext="$3"
|
|
|
|
prefix="$4"
|
|
|
|
exec="$5"
|
|
|
|
|
2023-10-02 16:02:35 -04:00
|
|
|
find "${from}" -name "*${orig_ext}" -type f | while IFS= read -r src; do
|
2023-09-28 23:21:31 -04:00
|
|
|
src=$(echo "$src" | sed -e "s|^${from}||g")
|
|
|
|
obj=$(echo "$src" | sed -e "s|${orig_ext}\$|${new_ext}|g")
|
2023-09-28 19:27:50 -04:00
|
|
|
|
2023-09-28 23:21:31 -04:00
|
|
|
obj="${prefix}${obj}"
|
|
|
|
src="${from}${src}"
|
|
|
|
|
|
|
|
"${exec}" "${src}" "${obj}"
|
|
|
|
done
|
2023-09-28 19:27:50 -04:00
|
|
|
}
|
|
|
|
|
2023-09-28 23:21:31 -04:00
|
|
|
print_src() {
|
|
|
|
printf '%s ' "$1"
|
|
|
|
}
|
2023-09-28 19:27:50 -04:00
|
|
|
|
2023-09-28 23:21:31 -04:00
|
|
|
print_obj() {
|
|
|
|
printf '%s ' "$2"
|
2023-09-28 19:27:50 -04:00
|
|
|
}
|
|
|
|
|
2024-01-13 17:13:45 -05:00
|
|
|
get_deps() {
|
|
|
|
src="$1"
|
|
|
|
|
2024-08-07 14:45:53 -04:00
|
|
|
${CC} -I${SRC} -I${INCLUDE} -E "$src" \
|
2024-01-13 17:13:45 -05:00
|
|
|
| grep '^#' \
|
|
|
|
| awk '{print $3}' \
|
|
|
|
| cut -d '"' -f 2 \
|
|
|
|
| sort \
|
|
|
|
| uniq \
|
|
|
|
| grep -v '^[/<]' \
|
|
|
|
| grep "^${SRC}/" \
|
|
|
|
| while IFS= read -r dep; do
|
|
|
|
printf "%s " "$dep"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2023-09-28 23:21:31 -04:00
|
|
|
compile_obj() {
|
|
|
|
src="$1"
|
|
|
|
obj="$2"
|
|
|
|
|
2024-01-13 17:13:45 -05:00
|
|
|
echo "${obj}: $(get_deps ${src})"
|
2023-09-28 23:21:31 -04:00
|
|
|
echo "${TAB}@mkdir -p $(dirname ${obj})"
|
|
|
|
echo "${TAB}\$(CC) \$(CFLAGS) -fPIC -c -o \"${obj}\" \"${src}\""
|
|
|
|
}
|
|
|
|
|
|
|
|
compile_bin() {
|
|
|
|
src="$1"
|
|
|
|
out="$2"
|
|
|
|
|
2023-10-30 14:11:22 -04:00
|
|
|
echo "${out}: ${OUT}/lib/lib${LIB_NAME}.a ${OUT}/lib/lib${LIB_NAME}.so ${src}"
|
2023-09-28 23:21:31 -04:00
|
|
|
echo "${TAB}@mkdir -p ${OUT}/bin"
|
2024-01-13 17:13:45 -05:00
|
|
|
echo "${TAB}\$(CC) \$(CFLAGS) -o \"${out}\" \"${src}\" -L${OUT}/lib \$(LDFLAGS) -l${LIB_NAME}"
|
2023-09-28 23:21:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
compile_doc() {
|
|
|
|
src="$1"
|
|
|
|
out="$2"
|
2024-01-13 17:13:45 -05:00
|
|
|
pref="LD_LIBRARY_PATH=${OUT}/lib "
|
2023-10-30 14:41:58 -04:00
|
|
|
|
2023-09-28 23:21:31 -04:00
|
|
|
echo "${out}: ${OUT}/bin/hdoc ${src}"
|
|
|
|
echo "${TAB}@mkdir -p ${OUT}/man/man3"
|
2023-10-30 14:41:58 -04:00
|
|
|
echo "${TAB}${pref}${OUT}/bin/hdoc -D \"Os=${LIB_NAME}\" -i \"${src}\" -o \"${out}\""
|
2023-09-28 19:27:50 -04:00
|
|
|
}
|
|
|
|
|
2023-10-02 16:02:35 -04:00
|
|
|
install_out() {
|
|
|
|
src="$1"
|
|
|
|
out="$2"
|
2024-01-13 17:26:17 -05:00
|
|
|
dir=$(dirname "$out")
|
2023-10-02 16:02:35 -04:00
|
|
|
|
2024-01-13 17:26:17 -05:00
|
|
|
echo "${TAB}mkdir -p \"$dir\""
|
|
|
|
echo "${TAB}cp \"$src\" \"$out\""
|
2023-10-02 16:02:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
install_man() {
|
|
|
|
src="${OUT}/man/man3/${LIB_NAME}-$(basename $1 .h).3"
|
|
|
|
out="$2"
|
2024-01-13 17:26:17 -05:00
|
|
|
dir=$(dirname "$out")
|
2023-10-02 16:02:35 -04:00
|
|
|
|
2024-01-13 17:26:17 -05:00
|
|
|
echo "${TAB}mkdir -p \"$dir\""
|
|
|
|
echo "${TAB}cp \"$src\" \"$out\""
|
2023-10-02 16:02:35 -04:00
|
|
|
}
|
|
|
|
|
2023-10-30 12:21:14 -04:00
|
|
|
install_tool() {
|
|
|
|
src=${OUT}/bin/$(basename "$1" .c)
|
|
|
|
out="$2"
|
2024-01-13 17:26:17 -05:00
|
|
|
dir=$(dirname "$out")
|
2023-10-30 12:21:14 -04:00
|
|
|
|
2024-01-13 17:26:17 -05:00
|
|
|
echo "${TAB}mkdir -p \"$dir\""
|
|
|
|
echo "${TAB}cp \"$src\" \"$out\""
|
2023-10-30 12:21:14 -04:00
|
|
|
}
|
|
|
|
|
2023-10-02 16:02:35 -04:00
|
|
|
uninstall_out() {
|
|
|
|
src="$1"
|
|
|
|
out="$2"
|
|
|
|
|
|
|
|
echo "${TAB}rm \"$out\""
|
|
|
|
}
|
|
|
|
|
2023-09-28 23:21:31 -04:00
|
|
|
echo "Generating Makefile..."
|
|
|
|
|
|
|
|
OBJS=$(collect ${SRC}/ .c .o ${BUILD}/ print_obj)
|
|
|
|
TAB=$(printf '\t')
|
|
|
|
|
2023-09-28 19:27:50 -04:00
|
|
|
cat << EOF > Makefile
|
|
|
|
.POSIX:
|
|
|
|
|
|
|
|
# Generated by '$0' on $(date).
|
|
|
|
# This file should generally not be manually edited.
|
|
|
|
|
2023-11-04 15:58:23 -04:00
|
|
|
CC = ${CC}
|
2023-09-28 19:27:50 -04:00
|
|
|
PREFIX = ${PREFIX}
|
|
|
|
CFLAGS = ${CFLAGS}
|
|
|
|
LDFLAGS = ${LDFLAGS}
|
|
|
|
|
|
|
|
all: ${LIB_NAME} docs tools
|
2023-09-28 23:21:31 -04:00
|
|
|
docs: $(collect ${INCLUDE}/ .h .3 ${OUT}/man/man3/${LIB_NAME}- print_obj)
|
|
|
|
tools: $(collect ${TOOLS}/ .c '' ${OUT}/bin/ print_obj)
|
2023-09-28 19:27:50 -04:00
|
|
|
|
2023-09-28 19:53:57 -04:00
|
|
|
print-libs:
|
|
|
|
${TAB}@echo ${LIBS}
|
|
|
|
|
2023-09-28 19:27:50 -04:00
|
|
|
format:
|
|
|
|
${TAB}find . -name '*.c' | while IFS= read -r src; do \\
|
|
|
|
${TAB} if indent "\$\$src"; then \\
|
|
|
|
${TAB} rm \$\$(basename "\$\$src").BAK; \\
|
|
|
|
${TAB} fi \\
|
|
|
|
${TAB}done
|
|
|
|
|
|
|
|
license:
|
|
|
|
${TAB}find . -name '*.[ch]' | while IFS= read -r src; do \\
|
|
|
|
${TAB} srcHeader=\$\$(grep -n -m 1 '^ \*/' "\$\$src" | cut -d ':' -f 1); \\
|
|
|
|
${TAB} head -n\$\$srcHeader \$\$src | \\
|
|
|
|
${TAB} diff -u -p - "LICENSE.txt" | \\
|
|
|
|
${TAB} patch "\$\$src" | grep -v "^Hmm"; \\
|
|
|
|
${TAB}done
|
|
|
|
|
2023-10-30 14:11:22 -04:00
|
|
|
${LIB_NAME}: ${OUT}/lib/lib${LIB_NAME}.a ${OUT}/lib/lib${LIB_NAME}.so
|
2023-09-28 19:27:50 -04:00
|
|
|
|
2023-10-02 16:02:35 -04:00
|
|
|
install: ${LIB_NAME}
|
2024-08-26 15:28:44 -04:00
|
|
|
${TAB}mkdir -p \$(PREFIX)/${OUT}/lib
|
|
|
|
${TAB}mkdir -p \$(PREFIX)/lib
|
2024-08-21 21:49:43 -04:00
|
|
|
${TAB}cp ${OUT}/lib/lib${LIB_NAME}.a \$(PREFIX)/lib/lib${LIB_NAME}.a
|
|
|
|
${TAB}cp ${OUT}/lib/lib${LIB_NAME}.so \$(PREFIX)/lib/lib${LIB_NAME}.so
|
2023-10-02 16:02:35 -04:00
|
|
|
$(collect ${INCLUDE}/ '' '' \$\(PREFIX\)/include/${LIB_NAME}/ install_out)
|
|
|
|
$(collect ${INCLUDE}/ .h .3 \$\(PREFIX\)/man/man3/${LIB_NAME}- install_man)
|
2023-10-30 12:21:14 -04:00
|
|
|
$(collect ${TOOLS}/ '.c' '' \$\(PREFIX\)/bin/ install_tool)
|
2023-10-02 16:02:35 -04:00
|
|
|
|
|
|
|
uninstall:
|
|
|
|
${TAB}rm -r \$(PREFIX)/lib/lib${LIB_NAME}.*
|
|
|
|
${TAB}rm -r \$(PREFIX)/include/${LIB_NAME}
|
|
|
|
${TAB}rm -r \$(PREFIX)/man/man3/${LIB_NAME}-*
|
2023-10-30 12:21:14 -04:00
|
|
|
$(collect ${TOOLS}/ '.c' '' \$\(PREFIX\)/bin/ uninstall_out)
|
2023-10-02 16:02:35 -04:00
|
|
|
|
2023-09-28 19:27:50 -04:00
|
|
|
clean:
|
2023-09-28 23:21:31 -04:00
|
|
|
${TAB}rm -r "${BUILD}" "${OUT}"
|
2023-09-28 19:27:50 -04:00
|
|
|
|
2023-09-28 23:21:31 -04:00
|
|
|
${OUT}/lib/lib${LIB_NAME}.a: ${OBJS}
|
|
|
|
${TAB}@mkdir -p ${OUT}/lib
|
|
|
|
${TAB}\$(AR) rcs ${OUT}/lib/lib${LIB_NAME}.a ${OBJS}
|
2023-09-28 19:27:50 -04:00
|
|
|
|
2023-09-28 23:21:31 -04:00
|
|
|
${OUT}/lib/lib${LIB_NAME}.so: ${OBJS}
|
|
|
|
${TAB}@mkdir -p ${OUT}/lib
|
|
|
|
${TAB}\$(CC) -shared -o ${OUT}/lib/lib${LIB_NAME}.so ${OBJS} ${LDFLAGS}
|
2023-09-28 19:27:50 -04:00
|
|
|
|
2023-09-28 23:21:31 -04:00
|
|
|
$(collect ${SRC}/ .c .o ${BUILD}/ compile_obj)
|
|
|
|
$(collect ${TOOLS}/ .c '' ${OUT}/bin/ compile_bin)
|
|
|
|
$(collect ${INCLUDE}/ .h .3 ${OUT}/man/man3/${LIB_NAME}- compile_doc)
|
2023-09-28 19:27:50 -04:00
|
|
|
|
2023-09-28 23:21:31 -04:00
|
|
|
EOF
|
2023-09-28 19:27:50 -04:00
|
|
|
|
|
|
|
echo "Done. Run 'make' to build ${LIB_NAME}."
|