#!/bin/bash srcdir=$1 outdir=$2 # 1 when the library was configured with a threading backend, 0 for # -DAKERR_THREADS=none. Stamped into the header so a consumer cannot disagree # with the library about whether it locks and whether its per-thread state is # thread local. Defaults to 1 for a hand-run of this script. thread_safe=${3:-1} # 1 for a normal (libc-linked) build, 0 for -DAKERR_USE_STDLIB=OFF. The # freestanding build cannot shell out to `errno --list` (moreutils) or # #include , so it skips the errno scrape entirely and stamps # AKERR_LAST_ERRNO_VALUE from a fixed fallback instead. Defaults to 1 for a # hand-run of this script. use_stdlib=${4:-1} # AKERR_LAST_ERRNO_VALUE to stamp when use_stdlib is 0. Defaults to 133 # (Linux's EHWPOISON) so the reserved band assertion in akerror.tmpl.h still # holds. last_errno_fallback=${5:-133} # Bytes reserved for the fname/function fields of akerr_ErrorContext. Defaults # to 4096 (PATH_MAX on Linux/glibc) so sizeof(akerr_ErrorContext) and the # soname stay unchanged from before this became a build option. max_error_fname_length=${6:-4096} if [ "${thread_safe}" != "0" ] && [ "${thread_safe}" != "1" ]; then echo "$0: thread-safe argument must be 0 or 1, got '${thread_safe}'" >&2 exit 1 fi if [ "${use_stdlib}" != "0" ] && [ "${use_stdlib}" != "1" ]; then echo "$0: use-stdlib argument must be 0 or 1, got '${use_stdlib}'" >&2 exit 1 fi mkdir -p ${outdir}/src mkdir -p ${outdir}/include rm -f ${outdir}/src/errno.c if [ "${use_stdlib}" = "1" ]; then echo "#include " >> ${outdir}/src/errno.c echo "#include " >> ${outdir}/src/errno.c cat >> ${outdir}/src/errno.c <<'EOF' /* * These names belong to the library's own reserved band, and this runs from * akerr_init(), which has no caller to raise into -- so it goes through * __akerr_name_library_status(), which reports a refusal and terminates rather * than leaving every later stack trace to print "Unknown Error" for an errno. * Keeping the branch in src/error.c also keeps this generated file free of * control flow no test can reach. */ EOF echo "void akerr_init_errno(void) {" >> ${outdir}/src/errno.c maxval=$(errno --list | cut -d ' ' -f 2 | sort -g | tail -n 1) errno --list | while read LINE; do define=$(echo "$LINE" | cut -d ' ' -f 1); value=$(echo "$LINE" | cut -d ' ' -f 2); desc=$(echo "$LINE" | cut -d ' ' -f 3-); echo " __akerr_name_library_status(${define}, \"${desc}\");" >> ${outdir}/src/errno.c ; done; echo "}" >> ${outdir}/src/errno.c else # Freestanding: no `errno --list` shellout (requires moreutils and # , neither freestanding-safe), and no errno.c at all -- CMake # does not compile it into the library under AKERR_USE_STDLIB=OFF. Still # write a stub so the OUTPUT this rule promises always exists. echo "/* AKERR_USE_STDLIB=OFF: no errno table generated. */" >> ${outdir}/src/errno.c maxval=${last_errno_fallback} fi sed -e "s/#define AKERR_LAST_ERRNO_VALUE .*/#define AKERR_LAST_ERRNO_VALUE ${maxval}/" \ -e "s/#define AKERR_THREAD_SAFE .*/#define AKERR_THREAD_SAFE ${thread_safe}/" \ -e "s/#define AKERR_MAX_ERROR_FNAME_LENGTH .*/#define AKERR_MAX_ERROR_FNAME_LENGTH ${max_error_fname_length}/" \ ${srcdir}/include/akerror.tmpl.h > ${outdir}/include/akerror.h