Quoting/progress
[llpp.git] / build.bash
blob8f004db7443896784fa7188eb458b7a8d16861c7
1 #!/bin/bash
2 set -eu
4 now() { date +%s; }
5 tstart=$(now)
6 vecho() { ${vecho-:} "$*"; }
7 digest() { cksum 2>/dev/null $* | while read h _; do printf $h; done; }
9 partmsg() {
10 test $? -eq 0 && msg="ok" || msg="ko"
11 echo "$msg $(($(now)-tstart)) sec"
14 die() {
15 echo "$*" >&2
16 exit 111
19 trap 'partmsg' EXIT
21 darwin=false
22 wsi="wsi/x11"
23 case "$(uname)" in
24 Darwin)
25 darwin=true
26 wsi="wsi/osx"
27 mjobs=$(getconf _NPROCESSORS_ONLN || echo 1)
29 Linux) mjobs=$(getconf _NPROCESSORS_ONLN || echo 1);;
30 OpenBSD) mjobs=$(getconf NPROCESSORS_ONLN || echo 1);;
31 *) die $(uname) is not supported;;
32 esac
34 test -n "${1-}" || die "usage: $0 build-directory"
36 outd="$1"
37 srcd="$(dirname $0)"
38 mudir=$outd/mupdf
39 muinc="-I $mudir/include -I $mudir/thirdparty/freetype/include"
41 mkdir -p $outd/$wsi
42 mkdir -p $outd/lablGL
43 :>$outd/ordered
45 isfresh() { test -r "$1.past" && . "$1.past" && test "$k" = "$2"; }
47 mulibs="$mudir/build/native/libmupdf.a" # $mudir/build/native/libmupdf-third.a
48 keycmd="(cd $mudir && git describe --tags --dirty); digest $mulibs"
49 isfresh "$mulibs" "$(eval $keycmd)" || (
50 make -C "$mudir" build=native -j $mjobs libs
51 echo "k='$(eval $keycmd)'" >$mudir/build/native/libmupdf.a.past
52 ) && vecho "fresh mupdf"
54 oflags() {
55 case "${1#$outd/}" in
56 config.cmo \
57 | glutils.cmo \
58 | help.cmo \
59 | keys.cmo \
60 | listview.cmo \
61 | main.cmo \
62 | parser.cmo \
63 | utf8syms.cmo \
64 | utils.cmo \
65 | $wsi/wsi.cmo) # XXX confstruct?
66 f="-g -strict-sequence -strict-formats -w @A";;
67 *) f="-g";;
68 esac
69 echo "$incs $f"
72 cflags() {
73 f="-g -std=c99 -O2 $muinc -Wall -Werror -Wextra -pedantic-errors"
74 case "${1#$outd/}" in
75 version.o) f='-DLLPP_VERSION="'$ver'"';;
76 link.o)
77 $darwin && f="$f -D__COCOA__ -D_GNU_SOURCE" \
78 || f="$f -D_POSIX_C_SOURCE" ;;
79 */keysym2ucs.o) f="-O2 -include inttypes.h -DKeySym=uint32_t";;
80 */ml_*.o) f="-g -Wno-pointer-sign -O2";;
81 *) f="-g -O2";;
82 esac
83 echo $f
86 mflags() { echo "-I $(ocamlc -where) -g -Wall -Werror -Wno-deprecated -O2"; }
88 incs="-I $srcd/lablGL -I $srcd/$wsi -I $srcd"
89 incs="$incs -I $outd/lablGL -I $outd/$wsi -I $outd"
91 overs="$(ocamlc -vnum 2>/dev/null)" || overs=""
92 test "$overs" = "4.06.1" || {
93 url=http://caml.inria.fr/pub/distrib/ocaml-4.06/ocaml-4.06.1.tar.xz
94 txz=$outd/$(basename $url)
95 isfresh $txz $url || {
96 executable_p() { command -v "$1" >/dev/null 2>&1; }
97 if executable_p wget; then dl() { wget -q "$1" -O "$2"; }
98 elif executable_p curl; then dl() { curl -L "$1" -o "$2"; }
99 else die "no program to fetch remote urls found"
101 dl $url $txz
102 echo "k=$url" >$txz.past
103 } && vecho "fresh $txz"
104 absprefix=$(cd $outd &>/dev/null; pwd -P)
105 export PATH=$absprefix/bin:$PATH
106 isfresh $absprefix/bin/ocamlc "$url" || (
107 tar xf $txz -C $outd
108 bn=$(basename $url)
109 cd $outd/${bn%.tar.xz}
110 ./configure -prefix $absprefix \
111 -no-graph -no-debugger -no-ocamldoc -no-native-compiler
112 make -j $mjobs world
113 make install
114 echo "k='$url'" >$absprefix/bin/ocamlc.past
115 ) && vecho "fresh ocamlc"
116 overs=$(ocamlc -vnum 2>/dev/null)
119 bocaml1() {
120 grep -q "$3" $outd/ordered || {
121 bocaml2 $*
122 echo "$3" >>"$outd/ordered"
126 bocaml2() {
127 local n=$1
128 local s="$2"
129 local o="$3"
130 local O=${4-}
131 local dd
133 local cmd="ocamlc -depend -bytecode -one-line $incs $s"
134 local keycmd="digest $o $s"
135 isfresh "$o.depl" "$overs$cmd$(eval $keycmd)" || {
136 eval "$cmd" | {
137 read _ _ depl
138 :>"$o.depl"
139 for d in $depl; do
140 local D=${d#$srcd/}
141 test "$O" = "$D" || {
142 bocaml "$D" $((n+1))
143 case $d in
144 $outd/*) dd=$d;;
145 *) dd=$outd/${d#$srcd/};;
146 esac
147 printf "$dd " >>"$o.depl"
149 done
150 } || die "$cmd failed"
151 echo "k='$overs$cmd$(eval $keycmd)'" >"$o.depl.past"
152 } && {
153 vecho "fresh $o.depl"
154 for d in $(< $o.depl); do
155 bocaml ${d#$outd/} $((n+1))
156 done
159 cmd="ocamlc $(oflags $o) -c -o $o $s"
160 keycmd="digest $o $s $(< $o.depl)"
161 isfresh "$o" "$overs$cmd$(eval $keycmd)" || {
162 printf "%*.s%s -> %s\n" $n '' "${s#$srcd/}" "${o#$outd/}"
163 eval "$cmd || die '$cmd failed'"
164 echo "k='$overs$cmd$(eval $keycmd)'" >"$o.past"
165 } && vecho "fresh '$o'"
168 bocaml() (
169 local o="$1"
170 local n="$2"
171 local wocmi="${o%.cmi}"
172 local s
173 case ${wocmi#$outd/} in
174 confstruct.cmo)
175 s=$outd/confstruct.ml
176 o=$outd/confstruct.cmo
179 test "$o" = "$wocmi" && s=$srcd/${o%.cmo}.ml || s=$srcd/$wocmi.mli
180 o=$outd/$o
182 esac
183 bocaml1 $n "$s" "$o"
184 case $wocmi in
185 wsi) s="$srcd/$wsi/wsi.ml";;
186 help) s="$srcd/help.ml";;
187 */glMisc) s="$srcd/lablGL/glMisc.ml";;
188 */glTex) s="$srcd/lablGL/glTex.ml";;
189 *) false;;
190 esac && {
191 local s1=${s#$srcd/}
192 bocaml1 $n "$s" "$outd/${s1%.ml}.cmo" "${o#$outd/}"
193 } || true
196 bocamlc() {
197 local o=$outd/$1
198 local s=$srcd/${1%.o}.c
199 local cmd="ocamlc -ccopt \"$(cflags $o) -MMD -MF $o.dep -MT_ -o $o\" $s"
200 test -r $o.dep && read _ d <$o.dep || d=
201 local keycmd='digest $o $d'
202 isfresh "$o" "$cmd$(eval $keycmd)" || {
203 printf "%s -> %s\n" "${s#$srcd/}" "${o#$outd/}"
204 eval "$cmd || die '$cmd failed'"
205 read _ d <$o.dep
206 echo "k='$cmd$(eval $keycmd)'" >"$o.past"
207 } && vecho "fresh $o"
210 bobjc() {
211 local o=$outd/$1
212 local s=$srcd/${1%.o}.m
213 local cmd="$mcomp $(mflags $o) -MMD -MF $o.dep -MT_ -c -o $o $s"
214 test -r $o.dep && read _ d <$o.dep || d=
215 local keycmd='digest $o $d'
216 isfresh "$o" "$cmd$(eval $keycmd)" || {
217 printf "%s -> %s\n" "${s#$srcd/}" "${o#$outd/}"
218 eval "$cmd || die '$cmd failed'"
219 read _ d <$o.dep
220 echo "k='$cmd$(eval $keycmd)'" >"$o.past"
221 } && vecho "fresh $o"
224 ver=$(cd $srcd && git describe --tags --dirty) || ver=unknown
226 cmd="(. $srcd/genconfstr.sh >$outd/confstruct.ml)"
227 keycmd="digest $srcd/genconfstr.sh $outd/confstruct.ml"
228 isfresh "$outd/confstruct.ml" "$cmd$(eval $keycmd)" || {
229 echo genconfstr
230 eval "$cmd || die genconfstr.sh failed"
231 echo "k='$cmd$(eval $keycmd)'" > "$outd/confstruct.ml.past"
232 } && vecho "fresh $outd/confstruct.ml"
234 shift 1
235 for target; do
236 case "$target" in
237 doc)
238 doct=${doct-manpage}
239 md=$outd/doc
240 mkdir -p $md
241 case $doct in
242 epub) suf=.epub;;
243 manpage) suf=.1;;
244 *) die "unknown doc type";;
245 esac
246 for m in llpp llppac llpphtml; do
247 src=$srcd/adoc/$m.adoc
248 out=$md/$m$suf
249 conf="$srcd/man/asciidoc.conf"
250 keycmd="digest $out $src $conf"
251 cmd="a2x -D $md -d manpage -f $doct $src"
252 isfresh "$out" "$cmd$(eval $keycmd)" || {
253 echo "$src -> $out"
254 eval "$cmd || die '$cmd failed'"
255 echo "k='$cmd$(eval $keycmd)'" >"$out.past"
256 } && vecho "fresh $out"
257 done;;
259 completions) die "not yet";;
261 *) die "no clue - '$target'";;
262 esac
263 done
265 bocaml main.cmo 0
267 cobjs=
268 for m in link cutils version; do
269 bocamlc $m.o
270 cobjs="$cobjs $outd/$m.o"
271 done
272 for m in ml_gl ml_glarray ml_raw; do
273 bocamlc lablGL/$m.o
274 cobjs="$cobjs $outd/lablGL/$m.o"
275 done
277 libs="str.cma unix.cma"
278 clibs="-L$mudir/build/native -lmupdf -lmupdf-third -lpthread"
279 if $darwin; then
280 mcomp=$(ocamlc -config | grep bytecomp_c_co | { read _ c; echo $c; })
281 clibs="$clibs -framework Cocoa -framework OpenGL"
282 bobjc wsi/osx/wsicocoa.o
283 cobjs="$cobjs $outd/wsi/osx/wsicocoa.o"
284 else
285 clibs="$clibs -lGL -lX11"
286 cobjs="$cobjs $outd/wsi/x11/keysym2ucs.o"
287 bocamlc wsi/x11/keysym2ucs.o
290 ord=$(grep -v \.cmi $outd/ordered)
291 cmd="ocamlc -custom $libs -o $outd/llpp $cobjs $(echo $ord) -cclib \"$clibs\""
292 keycmd="digest $outd/llpp $cobjs $ord $mulibs"
293 isfresh "$outd/llpp" "$cmd$(eval $keycmd)" || {
294 echo linking $outd/llpp
295 eval "$cmd || die '$cmd failed'"
296 echo "k='$cmd$(eval $keycmd)'" >"$outd/llpp.past"
297 } && vecho "fresh llpp"
299 if $darwin; then
300 out="$outd/llpp.app/Contents/Info.plist"
301 keycmd="digest $out $srcd/wsi/osx/genplist.sh"
302 isfresh $out "$(eval $keycmd)" || {
303 shortver=$(echo $ver | { IFS='-' read s _; echo ${s#v}; })
304 d=$(dirname $out)
305 mkdir -p "$d"
306 (. $srcd/wsi/osx/genplist.sh) >"$out"
307 echo "k='$(eval $keycmd)'" >"$out.past"
308 } && vecho "fresh plist"
310 out=$outd/llpp.app/Contents/MacOS/llpp
311 keycmd="digest $out $outd/llpp"
312 isfresh $out "$(eval $keycmd)" || {
313 echo "bundling llpp.app"
314 mkdir -p "$(dirname $out)"
315 cp $outd/llpp $out
316 echo "k='$(eval $keycmd)'" >"$out.past"
317 } && vecho "fresh bundle"