b8e658e7aa8671734bd38db02957e45054ccb4db
[libjh.git] / compile.sh
1 #!/bin/bash
2 # YES, THIS NEEDS BASH, NOT /bin/sh (e.g. for <<<).
3
4 # Copyright (2013) Jann Horn <jann@thejh.net>
5
6 # -f                  for files with weird names
7 # -u                  for coding mistakes (or against, to be precise)
8 # -e and -o pipefail  so that we don't have to spam the code with error handling
9 set -f -u -e -o pipefail
10
11 # flags for the build - adjust for your needs
12 # delete all the generated stuff afterwards (with `rm -r gen`)
13 CC='gcc'
14 CFLAGS='-O3 -Wall -Werror -Wno-error=strict-aliasing -fPIC -std=c99 -march=native'
15
16 # create build environment if it doesn't exist yet
17 mkdir -p gen # contains all generated files
18 mkdir -p gen/realc # source files, with our preprocessing applied
19 mkdir -p gen/realc_preprocessed # source files, preprocessed by the C compiler
20 mkdir -p gen/chash # hashes of the preprocessed C files used to generate files in gen/obj
21 mkdir -p gen/obj # object files
22
23 echo "welcome. your friendly compiler will be \"$CC\" today." >&2
24 echo "going ahead with CFLAGS=\"$CFLAGS\"..." >&2
25
26 # generate header
27 set +f
28 # needs correct order, so list them here
29 cat header.h bufio.h > gen/jh.h
30 set -f
31 for source_file in $(ls|grep '\.c$'); do
32   echo "extracting header data from $source_file..." >&2
33   source_name="$(sed 's|\.c$||' <<< "$source_file")"
34   echo "/* ----========   $source_name   ========---- */"
35   
36   cat "$source_file" |
37   sed 's|^PUBLIC_FN \(.*\){|KEEPLINE \1;|g' |
38   sed 's|^PUBLIC_CONST |KEEPLINE #define |g' |
39   sed 's|^HEADER |KEEPLINE |g' |
40   (set +e +o pipefail; grep '^KEEPLINE'; exit 0) |
41   sed 's|^KEEPLINE ||g'
42   
43   echo ''
44   echo ''
45 done >> gen/jh.h
46
47 # preprocess all source files
48 for source_file in $(ls|grep '\.c$'); do
49   echo "handling source file $source_file..." >&2
50   source_name="$(sed 's|\.c$||' <<< "$source_file")"
51   
52   # do our own preprocessing
53   echo '#include "../jh.h"' > "gen/realc/$source_name.c"
54   
55   cat "$source_file" |
56   (set +e +o pipefail; grep -v '^PUBLIC_CONST '; exit 0) |
57   sed 's|^PUBLIC_FN ||g' |
58   (set +e +o pipefail; grep -v '^HEADER '; exit 0) |
59   sed 's| *JH_ATTR_[A-Z_]*||g' |
60   cat >> "gen/realc/$source_name.c"
61   if [ $? -ne 0 ]; then exit 1; fi
62   
63   # do the normal C preprocessing
64   echo 'preprocessing...' >&2
65   $CC -E "gen/realc/$source_name.c" > "gen/realc_preprocessed/$source_name.i"
66   
67   # compile if there have been changes since the last time
68   echo -n 'checking sha checksum... ' >&2
69   file_hash="$(shasum "gen/realc_preprocessed/$source_name.i" | cut -d' ' -f1)"
70   # drop errors: that file might well be missing
71   set +e
72   last_file_hash="$(cat "gen/chash/$source_name" 2>/dev/null)"
73   set -e
74   if [ "$file_hash" = "$last_file_hash" ]; then
75     echo 'unchanged, will not compile again' >&2
76   else
77     echo 'changed – recompiling.' >&2
78     $CC $CFLAGS -c -o "gen/obj/$source_name.o" "gen/realc_preprocessed/$source_name.i"
79     echo "$file_hash" > "gen/chash/$source_name"
80   fi
81 done
82
83 # ... and link!
84 cd gen/obj
85 $CC -shared -Wl,-soname,libjh.so -o ../libjh.so $(ls)
86 ar rcs ../libjh.a $(ls)
87 cd ../..