shasum is apparently a perl thing
[llpp.git] / build.sh
blob742d88dcce50d4e26887e0f481f174aac28ab422
1 #!/bin/sh
2 set -e
4 date --version | grep -q "GNU" && dfmt="%s.%N" || dfmt="%s"
5 now() { date +$dfmt; }
7 tstart=$(now)
8 vecho=${vecho-:}
9 command -v md5sum >/dev/null && alias sum=md5sum
11 partmsg() {
12 test $? -eq 0 && msg="ok" || msg="ko"
13 echo "$msg $(echo "scale=3; ($(now) - $tstart)/1" | bc -l) sec"
16 die() {
17 echo "$*" >&2
18 exit 111
21 trap 'partmsg' EXIT
23 test -n "$1" || die "usage: $0 build-directory"
25 outd=$1
26 srcd=$PWD
28 isfresh() {
29 test -e "$1" && test -r "$1.past" && {
30 . "$1.past"
31 true || {
32 echo "$1"
33 echo "$k" | md5sum
34 echo "$2" | md5sum
36 test "$k" = "$2"
40 bocaml1() {
41 eval ocamlc -depend -bytecode -one-line $incs $s | {
42 read _ _ depl
43 test -z "$depl" || {
44 for d in $(eval echo $depl); do
45 d=${d#$srcd/}
46 bocaml $d $((n+1))
47 done
50 cmd="ocamlc $incs -c -o $o $s"
51 keycmd="sum $o $s"
52 grep -q "$o" $outd/ordered || {
53 echo "$o" >>$outd/ordered
54 isfresh "$o" "$cmd$(eval $keycmd)" || {
55 printf "%*.s%s -> %s\n" $n '' "${s#$srcd/}" "$o"
56 eval "$cmd"
57 echo "k='$cmd$(eval $keycmd)'" >$o.past
58 } && $vecho "fresh '$o'"
62 bocaml() (
63 o="$1"
64 n="$2"
65 wocmi="${o%.cmi}"
66 test ${wocmi%help.cmo} != $wocmi && {
67 s=$outd/help.ml
68 o=$outd/help.cmo
69 } || {
70 test "$o" = "$wocmi" && s=${o%.cmo}.ml || s=$wocmi.mli
71 s=$srcd/$s
72 o=$outd/$o
74 incs="-I lablGL -I $outd/lablGL -I wsi/x11 -I $outd/wsi/x11 -I $outd"
75 bocaml1 "$s" "$o"
78 bocamlc() {
79 o=$outd/$1
80 s=$srcd/${1%.o}.c
81 mudir=$srcd/mupdf
82 muinc="-I $mudir/include -I $mudir/thirdparty/freetype/include"
83 cmd="ocamlc -ccopt \"-O2 $muinc -MMD -MF $o.dep -MT_ -o $o\" $s"
84 test -r $o.dep && read _ d <$o.dep
85 keycmd='sum $o $d 2>/dev/null'
86 isfresh "$o" "$cmd$(eval $keycmd)" || {
87 printf "%s -> %s\n" "${s#$srcd/}" "$o"
88 eval "$cmd"
89 read _ d <$o.dep
90 echo "k='$cmd$(eval $keycmd)'" >$o.past
94 mkdir -p $outd/wsi/x11
95 mkdir -p $outd/lablGL
96 :>$outd/ordered
98 mkhelp() {
99 ocaml str.cma -stdin $srcd/KEYS <<EOF
100 let fixup = let open Str in
101 let dash = regexp {|\([^ ]*\) +- +\(.*\)|}
102 and head = regexp {|-----\(.*\)-----|} in fun s ->
103 String.escaped s |> global_replace dash {|\1\t\2|}
104 |> global_replace head {|\xc2\xb7\1|};;
105 let rec iter ic = match input_line ic with
106 | s -> Printf.printf "\"%s\";\\n" @@ fixup s; iter ic
107 | exception End_of_file -> ();;
108 Printf.printf "let keys = [\\n";
109 iter @@ open_in Sys.argv.(1);;
110 Printf.printf "] and version = \"$ver\";;"
114 ver=$(cd $srcd && git describe --tags) || echo unknown
115 cmd="mkhelp >$outd/help.ml"
116 keycmd="sum $srcd/KEYS; echo $ver"
117 isfresh "$outd/help.ml" '$cmd$(eval keycmd)$ver' || {
118 eval $cmd
119 echo "k='$cmd$(eval $keycmd)$ver'" >$outd/help.ml.past
122 for m in lablGL/glMisc.cmo lablGL/glTex.cmo wsi/x11/wsi.cmo main.cmo; do
123 bocaml $m 0
124 done
125 bocamlc link.o
127 libs="str.cma unix.cma"
128 clibs="-lGL -lX11 -L$mudir/build/native -lmupdf -lmupdfthird -lpthread"
129 globjs=
130 for f in ml_gl ml_glarray ml_raw; do
131 bocamlc lablGL/$f.o
132 globjs="$globjs $outd/lablGL/$f.o"
133 done
135 ord=$(echo $(eval grep -v \.cmi $outd/ordered))
136 cmd="ocamlc -custom $libs -o $outd/llpp $ord"
137 cmd="$cmd $globjs $outd/link.o -cclib \"$clibs\""
138 keycmd="sum $outd/llpp $ord"
139 isfresh "$outd/llpp" "$cmd$(eval $keycmd)" || {
140 echo linking $outd/llpp
141 eval $cmd
142 echo "k='$cmd$(eval $keycmd)'" >$outd/llpp.past