2022-07-23 00:19:12 +00:00
|
|
|
#!/usr/bin/env sh
|
2022-07-29 21:09:15 +00:00
|
|
|
#
|
|
|
|
# td: "Telodendria Developer"
|
|
|
|
#
|
|
|
|
# A helper script that operates sort of like Make, except it is
|
|
|
|
# a lot easier to read and write than a Makefile, and it works on
|
|
|
|
# any POSIX system.
|
|
|
|
#
|
|
|
|
# I chose to use a custom build script instead of using Make because
|
|
|
|
# it is much more portable and flexible. This script doesn't only
|
|
|
|
# handle building the code, it also handles formatting it, as well
|
|
|
|
# as generating patch files.
|
|
|
|
|
2022-07-23 17:44:55 +00:00
|
|
|
#
|
|
|
|
# Set variables
|
|
|
|
#
|
|
|
|
# This syntax may look odd, but as far as I can tell, it is POSIX,
|
|
|
|
# and it allows the values to be overridden by the environment,
|
|
|
|
# such that these serve more as sane defaults than hard requirements.
|
|
|
|
#
|
2022-07-23 00:19:12 +00:00
|
|
|
|
2023-03-08 03:46:38 +00:00
|
|
|
: "${TELODENDRIA_VERSION:=0.3.0}"
|
2022-07-23 17:44:55 +00:00
|
|
|
: "${CVS_TAG:=Telodendria-$(echo $TELODENDRIA_VERSION | sed 's/\./_/g')}"
|
2022-07-23 00:19:12 +00:00
|
|
|
|
2023-03-01 03:19:21 +00:00
|
|
|
: "${DEFINES:=-D_DEFAULT_SOURCE -DTELODENDRIA_VERSION=\"${TELODENDRIA_VERSION}-$(uname)\"}"
|
2022-07-23 17:44:55 +00:00
|
|
|
|
|
|
|
: "${CC:=cc}"
|
2023-03-01 01:26:34 +00:00
|
|
|
: "${CFLAGS:=-Wall -Wextra -pedantic -ansi -O3 -pipe}"
|
2023-02-05 14:46:20 +00:00
|
|
|
: "${STATIC:=-static -Wl,-static}"
|
|
|
|
: "${LDFLAGS:=-lm -pthread -flto -fdata-sections -ffunction-sections -s -Wl,-gc-sections}"
|
2022-07-23 17:44:55 +00:00
|
|
|
: "${PROG:=telodendria}"
|
|
|
|
|
2022-11-11 01:07:49 +00:00
|
|
|
. "$(pwd)/tools/lib/common.sh"
|
2022-08-01 20:09:18 +00:00
|
|
|
|
2023-03-22 01:17:42 +00:00
|
|
|
if [ -n "$TLS_IMPL" ]; then
|
|
|
|
case "$TLS_IMPL" in
|
2023-03-24 00:25:44 +00:00
|
|
|
"LIBRESSL")
|
2023-03-22 01:17:42 +00:00
|
|
|
TLS_LIBS="-ltls -lcrypto -lssl"
|
2023-03-24 02:41:01 +00:00
|
|
|
;;
|
|
|
|
"OPENSSL")
|
|
|
|
TLS_LIBS="-lcrypto -lssl"
|
2023-03-22 01:17:42 +00:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Unrecognized TLS implementation: ${TLS_IMPL}"
|
|
|
|
echo "Consult src/include/Tls.h for supported implementations."
|
2023-03-24 00:25:44 +00:00
|
|
|
echo "Note that the TLS_ prefix is omitted in TLS_IMPL."
|
2023-03-22 01:17:42 +00:00
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2023-03-24 00:25:44 +00:00
|
|
|
DEFINES="${DEFINES} -DTLS_IMPL=TLS_${TLS_IMPL}"
|
2023-03-22 01:17:42 +00:00
|
|
|
LDFLAGS="${LDFLAGS} ${TLS_LIBS}"
|
|
|
|
fi
|
|
|
|
|
2022-11-22 22:31:53 +00:00
|
|
|
CFLAGS="${CFLAGS} ${DEFINES} ${INCLUDES}"
|
2023-02-05 14:46:20 +00:00
|
|
|
LDFLAGS="${LDFLAGS} ${STATIC}"
|
2022-11-22 22:31:53 +00:00
|
|
|
|
2023-03-09 02:46:04 +00:00
|
|
|
MAIN="Main"
|
2023-03-08 03:30:36 +00:00
|
|
|
|
2022-08-10 14:49:17 +00:00
|
|
|
if [ "$DEBUG" = "1" ]; then
|
2023-03-22 00:13:27 +00:00
|
|
|
CFLAGS="$CFLAGS -O0 -g"
|
2023-03-23 16:39:15 +00:00
|
|
|
LDFLAGS="-lm -pthread ${TLS_LIBS}"
|
2022-12-14 01:00:52 +00:00
|
|
|
PROG="$PROG-debug"
|
2022-08-10 14:49:17 +00:00
|
|
|
fi
|
|
|
|
|
2022-07-29 21:09:15 +00:00
|
|
|
# Check the modificiation time of a file. This is used to do
|
|
|
|
# incremental builds; we only want to rebuild files that have
|
|
|
|
# have changed.
|
2022-07-23 00:19:12 +00:00
|
|
|
mod_time() {
|
|
|
|
if [ -n "$1" ] && [ -f "$1" ]; then
|
|
|
|
case "$(uname)" in
|
2023-03-08 22:47:40 +00:00
|
|
|
Linux | CYGWIN_NT* | Haiku)
|
|
|
|
stat -c %Y "$1"
|
2022-07-23 00:19:12 +00:00
|
|
|
;;
|
2023-03-08 22:47:40 +00:00
|
|
|
*BSD | DragonFly | Minix)
|
|
|
|
stat -f %m "$1"
|
2022-07-23 00:19:12 +00:00
|
|
|
;;
|
|
|
|
*)
|
2022-12-14 01:00:52 +00:00
|
|
|
# Platform unknown, force rebuilding the whole
|
|
|
|
# project every time.
|
2023-03-08 22:47:40 +00:00
|
|
|
echo "0"
|
2022-07-23 00:19:12 +00:00
|
|
|
;;
|
2023-03-08 22:47:40 +00:00
|
|
|
esac
|
2022-07-23 00:19:12 +00:00
|
|
|
else
|
2023-03-08 22:47:40 +00:00
|
|
|
echo "0"
|
|
|
|
fi
|
2022-07-23 00:19:12 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 00:54:52 +00:00
|
|
|
# Substitute shell variables in a stream with their actual value
|
|
|
|
# in this shell.
|
|
|
|
setsubst() {
|
|
|
|
SED="/tmp/sed-$RANDOM.txt"
|
|
|
|
|
|
|
|
(
|
|
|
|
set | while IFS='=' read -r var val; do
|
2023-02-15 03:30:52 +00:00
|
|
|
val=$(echo "$val" | cut -d "'" -f 2- | rev | cut -d "'" -f 2- | rev)
|
2022-12-14 00:54:52 +00:00
|
|
|
echo "s|\\\${$var}|$val|g"
|
|
|
|
done
|
|
|
|
|
|
|
|
echo "s|\\\${[a-zA-Z_]*}||g"
|
2023-02-15 16:00:36 +00:00
|
|
|
echo "s|'''|'|g"
|
2023-03-08 22:47:40 +00:00
|
|
|
) >"$SED"
|
2022-12-14 00:54:52 +00:00
|
|
|
|
|
|
|
sed -f "$SED" $@
|
|
|
|
rm "$SED"
|
|
|
|
}
|
|
|
|
|
2022-07-29 21:09:15 +00:00
|
|
|
# Build the source code, and generate the 'build/telodendria'
|
|
|
|
# binary.
|
2022-07-23 05:05:55 +00:00
|
|
|
recipe_build() {
|
2023-01-06 23:00:18 +00:00
|
|
|
cd src
|
|
|
|
mkdir -p ../build
|
2022-07-23 00:19:12 +00:00
|
|
|
|
2023-02-05 14:46:20 +00:00
|
|
|
echo "CC = ${CC}"
|
|
|
|
echo "CFLAGS = ${CFLAGS}"
|
|
|
|
echo "LDFLAGS = ${LDFLAGS}"
|
|
|
|
echo
|
|
|
|
|
2022-12-14 01:00:52 +00:00
|
|
|
do_rebuild=0
|
|
|
|
objs=""
|
2023-01-06 23:00:18 +00:00
|
|
|
for src in $(find . -name '*.c' | cut -d '/' -f 2-); do
|
2023-03-08 03:30:36 +00:00
|
|
|
obj=$(echo "build/$src" | sed -e 's/\.c$/\.o/')
|
|
|
|
|
|
|
|
if [ $(basename "$obj" .o) != "$MAIN" ]; then
|
|
|
|
objs="$objs $obj"
|
|
|
|
fi
|
2023-03-08 22:47:40 +00:00
|
|
|
|
2023-03-08 03:30:36 +00:00
|
|
|
if [ $(mod_time "$src") -ge $(mod_time "../$obj") ]; then
|
2023-02-11 00:07:18 +00:00
|
|
|
echo "CC $(basename $obj)"
|
2023-03-08 03:30:36 +00:00
|
|
|
obj_dir=$(dirname "../$obj")
|
2022-12-14 01:00:52 +00:00
|
|
|
mkdir -p "$obj_dir"
|
2023-03-08 03:30:36 +00:00
|
|
|
if ! $CC $CFLAGS -Iinclude -c -o "../$obj" "$src"; then
|
2022-12-14 01:00:52 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
do_rebuild=1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2023-03-08 03:30:36 +00:00
|
|
|
cd ..
|
|
|
|
if [ $do_rebuild -eq 1 ] || [ ! -f "build/$PROG" ]; then
|
2023-02-11 00:07:18 +00:00
|
|
|
echo "LD $PROG"
|
2023-03-22 00:13:27 +00:00
|
|
|
$CC -o "build/$PROG" $objs ${LDFLAGS} "build/$MAIN.o"
|
2022-12-14 01:00:52 +00:00
|
|
|
fi
|
2023-03-08 03:30:36 +00:00
|
|
|
|
|
|
|
for src in $(find tools/src -name '*.c'); do
|
|
|
|
out=$(basename "$src" .c)
|
|
|
|
out="build/tools/$out"
|
|
|
|
|
2023-03-10 18:46:03 +00:00
|
|
|
if [ $(mod_time "$src") -ge $(mod_time "$out") ] || [ $do_rebuild -eq 1 ]; then
|
2023-03-08 03:30:36 +00:00
|
|
|
echo "CC $(basename $out)"
|
|
|
|
mkdir -p "$(dirname $out)"
|
2023-03-22 00:13:27 +00:00
|
|
|
if ! $CC $CFLAGS -Isrc/include -o "$out" $objs ${LDFLAGS} "$src"; then
|
2023-03-08 03:30:36 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
2022-07-23 05:05:55 +00:00
|
|
|
}
|
|
|
|
|
2022-08-13 02:00:36 +00:00
|
|
|
recipe_run() {
|
2022-12-14 01:00:52 +00:00
|
|
|
if [ -f "build/$PROG" ]; then
|
|
|
|
"build/$PROG" -f contrib/development.conf
|
2023-03-22 14:57:57 +00:00
|
|
|
|
|
|
|
dataDir=$(< contrib/development.conf json -s "dataDir->@decode")
|
|
|
|
if [ -f "$dataDir/Memory.txt" ]; then
|
|
|
|
echo "WARNING: Memory.txt exists in the data directory; this means"
|
|
|
|
echo "Telodendria is leaking memory. Please fix memory leaks."
|
|
|
|
fi
|
2022-12-14 01:00:52 +00:00
|
|
|
else
|
|
|
|
echo "build/$PROG does not exist; build it first."
|
|
|
|
fi
|
2022-08-13 02:00:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-29 21:09:15 +00:00
|
|
|
# Remove all build files, which can be regenerated by re-running the
|
|
|
|
# build recipe.
|
2022-07-23 05:05:55 +00:00
|
|
|
recipe_clean() {
|
2022-12-14 01:00:52 +00:00
|
|
|
rm -r build
|
2022-07-23 05:05:55 +00:00
|
|
|
}
|
|
|
|
|
2022-07-29 21:09:15 +00:00
|
|
|
# Format the source code by updating the copyright headers and
|
|
|
|
# then running indent(1) on each source code file.
|
2022-07-25 19:18:25 +00:00
|
|
|
recipe_format() {
|
2023-03-08 03:30:36 +00:00
|
|
|
find . -name '*.c' -or -name '*.h' | while IFS= read -r src; do
|
2023-01-10 01:21:35 +00:00
|
|
|
if [ -t 1 ]; then
|
|
|
|
printf "FMT %s%*c\r" $(basename "$src") "16" " "
|
|
|
|
fi
|
2022-12-14 01:00:52 +00:00
|
|
|
# Update the headers
|
|
|
|
srcHeader=$(grep -n -m 1 '^ \*/' "$src" | cut -d ':' -f 1)
|
|
|
|
head "-n$srcHeader" "$src" |
|
|
|
|
diff -u -p - LICENSE.txt |
|
|
|
|
patch "$src" | grep -v "^Hmm"
|
|
|
|
|
|
|
|
# Format the source code
|
|
|
|
if indent "$src"; then
|
|
|
|
rm $(basename "$src").BAK
|
|
|
|
fi
|
|
|
|
done
|
2023-01-10 01:21:35 +00:00
|
|
|
if [ -t 1 ]; then
|
|
|
|
printf "%*c\n" "50" " "
|
|
|
|
fi
|
2022-07-25 19:18:25 +00:00
|
|
|
}
|
|
|
|
|
2022-07-29 21:09:15 +00:00
|
|
|
# Deploy the Telodendria website by copying the required files to
|
|
|
|
# a web root defined by TELODENDRIA_PUB.
|
2022-07-23 17:44:55 +00:00
|
|
|
recipe_site() {
|
2022-12-14 01:00:52 +00:00
|
|
|
if [ -z "$TELODENDRIA_PUB" ]; then
|
|
|
|
echo "No public root directory specified."
|
|
|
|
echo "Set TELODENDRIA_PUB."
|
|
|
|
exit 1
|
|
|
|
fi
|
2022-07-23 17:44:55 +00:00
|
|
|
|
2023-02-15 03:30:52 +00:00
|
|
|
# Set some variables that may be replaced in the files.
|
2022-12-14 00:54:52 +00:00
|
|
|
DATE=$(date)
|
2023-02-15 03:30:52 +00:00
|
|
|
USER_DOCS=$(man-table user)
|
|
|
|
DEV_DOCS=$(man-table dev)
|
2022-12-14 00:54:52 +00:00
|
|
|
|
|
|
|
cd site/
|
2023-02-24 02:02:47 +00:00
|
|
|
find . -type f | grep -v CVS | while IFS= read -r file; do
|
2022-12-14 00:54:52 +00:00
|
|
|
dest="$TELODENDRIA_PUB/$file"
|
|
|
|
dir=$(dirname "$dest")
|
|
|
|
|
|
|
|
echo "$dest"
|
|
|
|
|
|
|
|
mkdir -p "$dir"
|
2023-03-08 22:47:40 +00:00
|
|
|
setsubst "$file" >"$dest"
|
2022-12-14 00:54:52 +00:00
|
|
|
done
|
2023-03-08 22:47:40 +00:00
|
|
|
cd - >/dev/null
|
2022-09-22 01:17:00 +00:00
|
|
|
|
2022-12-14 01:00:52 +00:00
|
|
|
find man/ -name '*.[1-9]' | while IFS= read -r man; do
|
|
|
|
dir=$(dirname "$man")
|
|
|
|
html=$(basename "$man")
|
2022-09-30 23:11:44 +00:00
|
|
|
|
2022-12-14 01:00:52 +00:00
|
|
|
mkdir -p "$TELODENDRIA_PUB/$dir/"
|
|
|
|
mandoc -Thtml \
|
2023-01-17 13:53:31 +00:00
|
|
|
-O style=/style.css,man=/man/man%S/%N.%S.html "$man" \
|
2023-03-08 22:47:40 +00:00
|
|
|
>"$TELODENDRIA_PUB/$dir/$html.html"
|
2022-12-14 01:00:52 +00:00
|
|
|
echo "$TELODENDRIA_PUB/$dir/$html.html"
|
|
|
|
done
|
2022-07-23 17:44:55 +00:00
|
|
|
}
|
|
|
|
|
2022-07-29 21:09:15 +00:00
|
|
|
# Generate a release tarball, checksum and sign it, and push it to
|
|
|
|
# the web root.
|
2022-07-23 17:44:55 +00:00
|
|
|
recipe_release() {
|
2022-12-14 01:00:52 +00:00
|
|
|
if [ -z "$TELODENDRIA_PUB" ]; then
|
|
|
|
echo "No public root directory specified."
|
|
|
|
echo "Set TELODENDRIA_PUB."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Tag the release at this point in time.
|
|
|
|
cvs tag "$CVS_TAG"
|
|
|
|
|
|
|
|
mkdir -p "$TELODENDRIA_PUB/pub/v$TELODENDRIA_VERSION"
|
|
|
|
cd "$TELODENDRIA_PUB/pub/v$TELODENDRIA_VERSION"
|
|
|
|
|
|
|
|
# Generate the release tarball.
|
|
|
|
cvs export "-r$CVS_TAG" "Telodendria"
|
|
|
|
mv "Telodendria" "Telodendria-v$TELODENDRIA_VERSION"
|
|
|
|
tar -czf "Telodendria-v$TELODENDRIA_VERSION.tar.gz" \
|
|
|
|
"Telodendria-v$TELODENDRIA_VERSION"
|
|
|
|
rm -r "Telodendria-v$TELODENDRIA_VERSION"
|
|
|
|
|
|
|
|
# Checksum the release tarball.
|
|
|
|
sha256 "Telodendria-v$TELODENDRIA_VERSION.tar.gz" \
|
2023-03-08 22:47:40 +00:00
|
|
|
>"Telodendria-v$TELODENDRIA_VERSION.tar.gz.sha256"
|
2022-12-14 01:00:52 +00:00
|
|
|
|
|
|
|
# Sign the release tarball.
|
|
|
|
if [ ! -z "$TELODENDRIA_SIGNIFY_SECRET" ]; then
|
|
|
|
signify -S -s "$TELODENDRIA_SIGNIFY_SECRET" \
|
|
|
|
-m "Telodendria-v$TELODENDRIA_VERSION.tar.gz" \
|
|
|
|
-x "Telodendria-v$TELODENDRIA_VERSION.tar.gz.sig"
|
|
|
|
else
|
|
|
|
echo "Warning: TELODENDRIA_SIGNIFY_SECRET not net."
|
|
|
|
echo "The built tarball will not be signed."
|
|
|
|
fi
|
2022-07-23 17:44:55 +00:00
|
|
|
}
|
|
|
|
|
2022-07-29 21:09:15 +00:00
|
|
|
# Generate a formatted patch file. The Telodendria project isn't
|
|
|
|
# really picky about how patches look, but this is how we like them
|
|
|
|
# best. Makes them easy to read.
|
2022-07-29 18:42:23 +00:00
|
|
|
recipe_patch() {
|
2022-07-29 21:09:15 +00:00
|
|
|
|
2022-12-14 01:00:52 +00:00
|
|
|
# If the user has not set their MXID, try to deduce one from
|
|
|
|
# their system.
|
|
|
|
if [ -z "$MXID" ]; then
|
|
|
|
MXID="@${USER}:$(hostname)"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# If the user has not set their EDITOR, use a safe default.
|
|
|
|
# (vi should be available on any POSIX system)
|
|
|
|
if [ -z "$EDITOR" ]; then
|
|
|
|
EDITOR=vi
|
|
|
|
fi
|
|
|
|
|
|
|
|
NORMALIZED_MXID=$(echo "$MXID" | sed -e 's/@//g' -e 's/:/-/g')
|
|
|
|
PATCH_FILE="${NORMALIZED_MXID}_$(date +%s).patch"
|
|
|
|
|
|
|
|
# Generate the actual patch file
|
|
|
|
# Here we write some nice headers, and then do a cvs diff.
|
|
|
|
(
|
|
|
|
printf 'From: "%s" <%s>\n' "$DISPLAY_NAME" "$MXID"
|
|
|
|
echo "Date: $(date)"
|
|
|
|
echo "Subject: "
|
|
|
|
echo
|
|
|
|
echo "[ ] I have read the Telodendria Project developer certificate"
|
|
|
|
echo " of origin, and certify that I have permission to submit"
|
|
|
|
echo " this patch under the conditions specified in it."
|
|
|
|
echo
|
|
|
|
cvs -q diff -uNp $PATCHSET | grep -v '^\? '
|
2023-03-08 22:47:40 +00:00
|
|
|
) >"$PATCH_FILE"
|
2022-12-14 01:00:52 +00:00
|
|
|
|
|
|
|
"$EDITOR" "$PATCH_FILE"
|
|
|
|
echo "$PATCH_FILE"
|
2022-07-29 18:42:23 +00:00
|
|
|
}
|
|
|
|
|
2022-07-29 21:09:15 +00:00
|
|
|
recipe_diff() {
|
2022-12-14 01:00:52 +00:00
|
|
|
tmp_patch="/tmp/telodendria-$(date +%s).patch"
|
2023-03-08 22:47:40 +00:00
|
|
|
cvs -q diff -uNp $PATCHSET >"$tmp_patch"
|
2022-12-14 01:00:52 +00:00
|
|
|
if [ -z "$PAGER" ]; then
|
|
|
|
PAGER="less -F"
|
|
|
|
fi
|
|
|
|
|
|
|
|
$PAGER "$tmp_patch"
|
|
|
|
rm "$tmp_patch"
|
2022-07-29 21:09:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Execute the user-specified recipes.
|
2022-07-23 05:05:55 +00:00
|
|
|
for recipe in $@; do
|
2022-12-14 01:00:52 +00:00
|
|
|
recipe_$recipe
|
2022-07-23 00:19:12 +00:00
|
|
|
done
|
|
|
|
|
2022-07-29 21:09:15 +00:00
|
|
|
# If no recipe was provided, run a build.
|
2022-07-23 05:05:55 +00:00
|
|
|
if [ -z "$1" ]; then
|
2022-12-14 01:00:52 +00:00
|
|
|
recipe_build
|
2022-07-23 00:19:12 +00:00
|
|
|
fi
|