Continue (Sh|m)ake-iication
[llpp.git] / build.sh
blobf0745ddf7df29c9a0f7868f1615acc1927a887c9
1 #!/bin/sh
2 set -eu
4 now() { date +$dfmt; }
5 unameN=$(uname)
6 test "$unameN" = Darwin && {
7 darwin=true
8 wsi="wsi/osx"
9 } || {
10 darwin=false
11 wsi="wsi/x11"
14 dfmt="%s"
15 if $(expr >/dev/null "$(date --version 2>/dev/null)" : ".*GNU"); then
16 dfmt="%s.%N"
19 tstart=$(now)
20 alias vecho=${vecho-:}
21 command -v md5sum >/dev/null || true && alias sum=md5sum
22 digest() { sum "$@" 2>/dev/null | while read h _; do printf $h; done; }
24 partmsg() {
25 test $? -eq 0 && msg="ok" || msg="ko"
26 echo "$msg $(echo "scale=3; ($(now) - $tstart)/1" | bc -l) sec"
29 die() {
30 echo "$*" >&2
31 exit 111
34 trap 'partmsg' EXIT
36 test $(ocamlc -version | { IFS=. read a b _; echo $a$b; } ) -lt 406 && {
37 die OCaml version 4.06+ is required
40 test -n "${1-}" || die "usage: $0 build-directory"
42 outd=$1
43 srcd=$(dirname $0)
44 mudir=$srcd/mupdf
45 muinc="-I $mudir/include -I $mudir/thirdparty/freetype/include"
47 isfresh() {
48 test -e "$1" && test -r "$1.past" && {
49 . "$1.past"
50 test "$k" = "$2"
54 oflags() {
55 case "${1#$outd/}" in
56 main.cmo|utils.cmo|config.cmo|parser.cmo|wsi.cmi|$wsi/wsi.cmo)
57 f="-g -strict-sequence -strict-formats -warn-error a";;
58 *) f="-g";;
59 esac
60 echo "-I lablGL -I $outd/lablGL -I $wsi -I $outd/$wsi -I $outd $f"
63 cflags() {
64 case "${1#$outd/}" in
65 link.o)
66 f="-g -std=c99 -O2 $muinc -Wall -Werror -pedantic-errors"
67 $darwin && echo "$f -D__COCOA__" || echo $f;;
68 */ml_gl.o) echo "-g -Wno-pointer-sign -O2";;
69 *) echo "-g -O2";;
70 esac
73 mflags() { echo "-I $(ocamlc -where) -g -O2"; }
75 bocaml1() {
76 eval ocamlc -depend -bytecode -one-line $incs $s | {
77 read _ _ depl
78 for d in $(eval echo $depl); do
79 bocaml ${d#$srcd/} $((n+1))
80 done
82 cmd="ocamlc $(oflags $o) -c -o $o $s"
83 keycmd="digest $o $s"
84 grep -q "$o" $outd/ordered || {
85 echo "$o" >>"$outd/ordered"
86 isfresh "$o" "$cmd$(eval $keycmd)" || {
87 printf "%*.s%s -> %s\n" $n '' "${s#$srcd/}" "$o"
88 eval "$cmd"
89 echo "k='$cmd$(eval $keycmd)'" >"$o.past"
90 } && vecho "fresh '$o'"
94 bocaml() (
95 o="$1"
96 n="$2"
97 wocmi="${o%.cmi}"
98 test ${wocmi%help.cmo} != $wocmi && {
99 s=$outd/help.ml
100 o=$outd/help.cmo
101 } || {
102 test "$o" = "$wocmi" && s=$srcd/${o%.cmo}.ml || s=$srcd/$wocmi.mli
103 o=$outd/$o
105 incs="-I lablGL -I $outd/lablGL -I $wsi -I $outd/$wsi -I $outd"
106 bocaml1 "$s" "$o"
109 bocamlc() {
110 o=$outd/$1
111 s=$srcd/${1%.o}.c
112 cmd="ocamlc -ccopt \"$(cflags $o) -MMD -MF $o.dep -MT_ -o $o\" $s"
113 test -r $o.dep && read _ d <$o.dep || d=
114 keycmd='digest $o $d'
115 isfresh "$o" "$cmd$(eval $keycmd)" || {
116 printf "%s -> %s\n" "${s#$srcd/}" "$o"
117 eval "$cmd"
118 read _ d <$o.dep
119 echo "k='$cmd$(eval $keycmd)'" >"$o.past"
120 } && vecho "fresh $o"
123 bobjc() {
124 o=$outd/$1
125 s=$srcd/${1%.o}.m
126 cmd="$mcomp $(mflags $o) -MMD -MF $o.dep -MT_ -c -o $o $s"
127 test -r $o.dep && read _ d <$o.dep || d=
128 keycmd='digest $o $d'
129 isfresh "$o" "$cmd$(eval $keycmd)" || {
130 printf "%s -> %s\n" "${s#$srcd/}" "$o"
131 eval "$cmd"
132 read _ d <$o.dep
133 echo "k='$cmd$(eval $keycmd)'" >"$o.past"
134 } && vecho "fresh $o"
137 mkdir -p $outd/$wsi
138 mkdir -p $outd/lablGL
139 :>$outd/ordered
141 mkhelp() {
142 ocaml str.cma -stdin $srcd/KEYS <<EOF
143 let fixup = let open Str in
144 let dash = regexp {|\([^ ]*\) +- +\(.*\)|}
145 and head = regexp {|-----\(.*\)-----|} in fun s ->
146 String.escaped s |> global_replace dash {|\1\t\2|}
147 |> global_replace head {|\xc2\xb7\1|};;
148 let rec iter ic = match input_line ic with
149 | s -> Printf.printf "\"%s\";\\n" @@ fixup s; iter ic
150 | exception End_of_file -> ();;
151 Printf.printf "let keys = [\\n";
152 iter @@ open_in Sys.argv.(1);;
153 Printf.printf "] and version = \"$ver\";;"
157 ver=$(cd $srcd && git describe --tags --dirty) || echo unknown
158 cmd="mkhelp >$outd/help.ml"
159 keycmd="digest $srcd/KEYS; echo $ver"
160 isfresh "$outd/help.ml" '$cmd$(eval $keycmd)$ver' || {
161 eval $cmd
162 echo "k='$cmd$(eval $keycmd)$ver'" >"$outd/help.ml.past"
163 } && vecho "fresh $outd/help.ml"
166 case "${2-}" in
167 man)
168 md=$outd/man
169 mkdir -p $md
170 for m in llpp llppac llpphtml; do
171 man=$srcd/man/$m.man
172 xml=$md/$m.xml
173 out=$md/$m.1
174 ketcmd="digest $xml $man"
175 conf="$srcd/man/asciidoc.conf"
176 cmd="asciidoc -d manpage -b docbook -f $conf -o '$xml' '$man'"
177 isfresh "$xml" "$cmd$(eval $keycmd)" || {
178 eval $cmd
179 echo "k='$cmd$(eval $keycmd)'" >"$md/$m.past"
181 ketcmd="digest $out $xml"
182 isfresh "$out" "$cmd$(eval $keycmd)" || {
183 xmlto man -o $md $xml
185 done
186 shift;;
187 *) ;;
188 esac
190 # following is disgusting (from "generalize everything" perspective),
191 # but generic method of derviving .ml's location from .mli's is not
192 # immediately obvious
193 for m in lablGL/glMisc.cmo lablGL/glTex.cmo $wsi/wsi.cmo main.cmo; do
194 bocaml $m 0
195 done
196 bocamlc link.o
197 cobjs="$outd/link.o"
199 libs="str.cma unix.cma"
200 clibs="-L$mudir/build/native -lmupdf -lmupdfthird -lpthread"
201 if $darwin; then
202 mcomp=$(ocamlc -config | grep bytecomp_c_co | { read _ c; echo $c; })
203 clibs="$clibs -framework Cocoa -framework OpenGL"
204 bobjc main_osx.o
205 cobjs="$cobjs $outd/main_osx.o"
206 else
207 clibs="$clibs -lGL -lX11"
210 globjs=
211 for f in ml_gl ml_glarray ml_raw; do
212 bocamlc lablGL/$f.o
213 globjs="$globjs $outd/lablGL/$f.o"
214 done
216 ord=$(echo $(grep -v \.cmi $outd/ordered))
217 cmd="ocamlc -custom $libs -o $outd/llpp $cobjs $ord"
218 cmd="$cmd $globjs -cclib \"$clibs\""
219 keycmd="digest $outd/llpp $outd/link.o $ord"
220 isfresh "$outd/llpp" "$cmd$(eval $keycmd)" || {
221 echo linking $outd/llpp
222 eval $cmd || echo "$cmd failed"
223 echo "k='$cmd$(eval $keycmd)'" >"$outd/llpp.past"
224 } && vecho "fresh llpp"
226 if $darwin; then
227 out="$outd/llpp.app/Contents/Info.plist"
228 keycmd="digest $out $srcd/misc/Info.plist.sh"
229 isfresh $out "$(eval $keycmd)" || {
230 shortver=$(echo $ver | { IFS='-' read s _; echo ${s#v}; })
231 d=$(dirname $out)
232 test -d "$d" || mkdir -p "$d"
233 . $srcd/misc/Info.plist.sh >"$out"
234 echo "k=$(eval $keycmd)" >"$out.past"
235 } && vecho "fresh plist"
237 out=$outd/llpp.app/Contents/MacOS/llpp
238 keycmd="digest $out $outd/llpp"
239 isfresh $out "$(eval $keycmd)" || {
240 d=$(dirname $out)
241 mkdir -p "$d"
242 cp $outd/llpp $out
243 echo "k=$(eval $keycmd)" >"$out.past"
244 } && vecho "fresh bundle"