#!/bin/bash
# YES, THIS NEEDS BASH, NOT /bin/sh (e.g. for <<<).
+# Copyright (2013) Jann Horn <jann@thejh.net>
+
# -f for files with weird names
# -u for coding mistakes (or against, to be precise)
# -e and -o pipefail so that we don't have to spam the code with error handling
# flags for the build - adjust for your needs
# delete all the generated stuff afterwards (with `rm -r gen`)
CC='gcc'
-CFLAGS='-g -O0 -Wall -Werror -fPIC -std=c99'
+# -fstack-check isn't just hardening - we do unbounded stack allocations in TPRINTF!
+CFLAGS='-O3 -Wall -Werror -Wno-error=strict-aliasing -fPIC -std=c99 -march=native -fstack-check -fstack-protector-all -D_FORTIFY_SOURCE=2'
# create build environment if it doesn't exist yet
mkdir -p gen # contains all generated files
echo "going ahead with CFLAGS=\"$CFLAGS\"..." >&2
# generate header
+set +f
+# needs correct order, so list them here
+cat header.h > gen/jh.h
+set -f
for source_file in $(ls|grep '\.c$'); do
echo "extracting header data from $source_file..." >&2
source_name="$(sed 's|\.c$||' <<< "$source_file")"
sed 's|^PUBLIC_FN \(.*\){|KEEPLINE \1;|g' |
sed 's|^PUBLIC_CONST |KEEPLINE #define |g' |
sed 's|^HEADER |KEEPLINE |g' |
- grep '^KEEPLINE' |
+ (set +e +o pipefail; grep '^KEEPLINE'; exit 0) |
sed 's|^KEEPLINE ||g'
echo ''
echo ''
-done > gen/libjh.h
+done >> gen/jh.h
# preprocess all source files
for source_file in $(ls|grep '\.c$'); do
source_name="$(sed 's|\.c$||' <<< "$source_file")"
# do our own preprocessing
- echo '#include "../libjh.h"' > "gen/realc/$source_name.c"
+ echo '#include "../jh.h"' > "gen/realc/$source_name.c"
cat "$source_file" |
- grep -v '^PUBLIC_CONST ' |
+ (set +e +o pipefail; grep -v '^PUBLIC_CONST '; exit 0) |
sed 's|^PUBLIC_FN ||g' |
- grep -v '^HEADER ' |
+ (set +e +o pipefail; grep -v '^HEADER '; exit 0) |
+ sed 's| *JH_ATTR_[A-Z_]*||g' |
cat >> "gen/realc/$source_name.c"
if [ $? -ne 0 ]; then exit 1; fi
# ... and link!
cd gen/obj
$CC -shared -Wl,-soname,libjh.so -o ../libjh.so $(ls)
-cd ../..
\ No newline at end of file
+ar rcs ../libjh.a $(ls)
+cd ../..