From c0e5feea37dbc95675face03bc4707ad09db20b8 Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Sun, 8 Oct 2017 13:19:25 +1300 Subject: Add GL/EGL extension loader generator --- glgen.sh | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100755 glgen.sh (limited to 'glgen.sh') diff --git a/glgen.sh b/glgen.sh new file mode 100755 index 00000000..d9de6f9f --- /dev/null +++ b/glgen.sh @@ -0,0 +1,91 @@ +#!/bin/sh + +# Generates a simple GL/EGL extension function loader. +# +# The input is a .txt file, with each function to load on its own line. +# If a line starts with a -, it is optional, and will not cause the loader +# to fail if it can't load the function. You'll need to check if that function +# is NULL before using it. + +if [ $# -ne 2 ]; then + exit 1 +fi + +SPEC=$1 +OUT=$2 + +BASE=$(basename "$SPEC" .txt) +INCLUDE_GUARD=$(printf %s "$SPEC" | tr -c [:alnum:] _ | tr [:lower:] [:upper:]) + +DECL="" +DEFN="" +LOADER="" + +DECL_FMT='extern %s %s;' +DEFN_FMT='%s %s;' +LOADER_FMT='%s = (%s)eglGetProcAddress("%s");' +CHECK_FMT='if (!%s) { + wlr_log(L_ERROR, "Unable to load %s"); + return false; +}' + +while read -r COMMAND; do + [ ${COMMAND::1} = "-" ] + OPTIONAL=$? + + COMMAND=${COMMAND#-} + if [ ${COMMAND: -2} = "WL" ]; then + FUNC_PTR_FMT='PFN%s' + else + FUNC_PTR_FMT='PFN%sPROC' + fi + + FUNC_PTR=$(printf "$FUNC_PTR_FMT" "$COMMAND" | tr [:lower:] [:upper:]) + + DECL="$DECL$(printf "\n$DECL_FMT" "$FUNC_PTR" "$COMMAND")" + DEFN="$DEFN$(printf "\n$DEFN_FMT" "$FUNC_PTR" "$COMMAND")" + LOADER="$LOADER$(printf "\n$LOADER_FMT" "$COMMAND" "$FUNC_PTR" "$COMMAND")" + + if [ $OPTIONAL -ne 0 ]; then + LOADER="$LOADER$(printf "\n$CHECK_FMT" "$COMMAND" "$COMMAND")" + fi +done < $SPEC + +if [ ${OUT: -2} = '.h' ]; then + cat > $OUT << EOF + #ifndef $INCLUDE_GUARD + #define $INCLUDE_GUARD + + #include + + #include + #include + #include + #include + #include + + bool load_$BASE(void); + $DECL + + #endif +EOF +elif [ ${OUT: -2} = '.c' ]; then + cat > $OUT << EOF + #include + #include "$BASE.h" + $DEFN + + bool load_$BASE(void) { + static bool done = false; + if (done) { + return true; + } + $LOADER + + done = true; + return true; + } +EOF +else + exit 1 +fi -- cgit v1.2.3