Skip to content

Commit

Permalink
Add a single-file library bundler
Browse files Browse the repository at this point in the history
  • Loading branch information
rootmos committed Jul 30, 2021
1 parent 6a61fd0 commit d0c25cc
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
90 changes: 90 additions & 0 deletions bundle.sh
@@ -0,0 +1,90 @@
#!/bin/bash

# single-file library bundler
# https://github.com/nothings/stb
# https://github.com/nothings/stb/blob/master/docs/stb_howto.txt

set -o nounset -o pipefail -o errexit

URL=${URL-https://github.com/rootmos/libr}
VERSION=${VERSION-$(git rev-parse HEAD)}
OUTPUT=/dev/stdout
while getopts "o:-" OPT; do
case $OPT in
o) OUTPUT=$OPTARG ;;
-) break ;;
?) exit 2 ;;
esac
done
shift $((OPTIND-1))

SCRIPT_DIR=$(readlink -f "$0" | xargs dirname)
BASE_DIR=${BASE_DIR-$SCRIPT_DIR}
SRC=${SRC-$BASE_DIR/src}
INC=${INC-$BASE_DIR/include/r}

TMP=$(mktemp -d)
trap 'rm -rf $TMP' EXIT

readarray -t ALL_MODULES < <(find "$SRC" -name "*.c" -exec basename --suffix=.c {} \;)

if [ $# -gt 0 ]; then
for i in "$@"; do
echo "$i" >> "$TMP/choices"
done
else
DIALOG_ARGS=()
for m in "${ALL_MODULES[@]}"; do
DIALOG_ARGS+=("$m" "$m" "off")
done
dialog --separate-output --no-tags --checklist "select modules" 0 0 0 \
"${DIALOG_ARGS[@]}" 2>"$TMP/choices"
fi

is_selected() {
grep -cFxq "$@" "$TMP/choices"
}

depends() {
if is_selected "$1" && ! is_selected "$2"; then
echo "$2" >> "$TMP/choices"
fi
}

depends "stopwatch" "fail"
depends "stopwatch" "logging"
depends "xorshift" "fail"
depends "mark" "fail"
depends "mark" "logging"
depends "test" "util"
depends "test" "fail"
depends "logging" "fail"
depends "fail" "logging"
depends "logging" "util"

readarray -t MODULES < "$TMP/choices"

echo 1>&2 "included modules: ${MODULES[*]}"

echo >"$OUTPUT" "// libr ($VERSION) ($URL) ($(date -Is))"
echo >>"$OUTPUT" "// modules: ${MODULES[*]}"
echo >>"$OUTPUT" ""

echo >>"$OUTPUT" "#ifndef LIBR_HEADER"
echo >>"$OUTPUT" "#define LIBR_HEADER"
for m in "${MODULES[@]}"; do
echo >>"$OUTPUT" ""
echo >>"$OUTPUT" "// libr: $m.h"
tail -n+3 "$INC/$m.h" >>"$OUTPUT" # HACK: remove pragma once directive
done
echo >>"$OUTPUT" ""
echo >>"$OUTPUT" "#endif // LIBR_HEADER"

echo >>"$OUTPUT" "#ifdef LIBR_IMPLEMENTATION"
for m in "${MODULES[@]}"; do
echo >>"$OUTPUT" ""
echo >>"$OUTPUT" "// libr: $m.c"
grep -vx '#include "r/\w\+.h"' "$SRC/$m.c" >>"$OUTPUT"
done
echo >>"$OUTPUT" ""
echo >>"$OUTPUT" "#endif // LIBR_IMPLEMENTATION"
2 changes: 1 addition & 1 deletion src/util.c
@@ -1,4 +1,4 @@
#include <r.h>
#include "r/util.h"

#include <assert.h>
#include <fcntl.h>
Expand Down

0 comments on commit d0c25cc

Please sign in to comment.