Update
[llpp.git] / build.bash
blob97ad1e94984de0ec103b5d7fd811868ccf057c27
1 #!/bin/bash
2 set -eu
4 now() { date +%s; }
5 S=$(now)
6 vecho() { ${vecho-:} "$*"; }
7 digest() { cksum 2>/dev/null $* | while read h _; do printf $h; done; }
8 die() { echo "$*" >&2; exit 111; }
9 partmsg() { echo "$(test $? -eq 0 || echo "fail ")$(($(now) - $S)) sec"; }
11 trap 'partmsg' EXIT
13 darwin=false
14 wsid="wsi/x11"
15 case "$(uname)" in
16 Darwin)
17 darwin=true
18 wsid="wsi/cocoa"
19 mjobs=$(getconf _NPROCESSORS_ONLN || echo 1);;
20 Linux) mjobs=$(getconf _NPROCESSORS_ONLN || echo 1);;
21 OpenBSD) mjobs=$(getconf NPROCESSORS_ONLN || echo 1);;
22 *) die $(uname) is not supported;;
23 esac
25 test -n "${1-}" || die "usage: $0 build-directory"
27 outd="$1"
28 srcd="$(dirname $0)"
29 mudir=$outd/mupdf
30 muinc="-I $mudir/include -I $mudir/thirdparty/freetype/include"
32 test -d "$mudir" || die muPDF not found, consult $(dirname $0)/BUILDING
34 mkdir -p $outd/{$wsid,lablGL}
35 :>$outd/ordered
37 isfresh() { test -r "$1.past" && . "$1.past" && test "$k" = "$2"; }
39 mbt=native
40 mulibs="$mudir/build/$mbt/libmupdf.a" # $mudir/build/$mbt/libmupdf-third.a
42 keycmd="(cd $mudir && git describe --tags --dirty); digest $mulibs"
43 isfresh "$mulibs" "$(eval $keycmd)" || (
44 make -C "$mudir" build=$mbt -j $mjobs libs
45 echo "k='$(eval $keycmd)'" >$mudir/build/$mbt/libmupdf.a.past
46 ) && vecho "fresh mupdf"
48 oincs() {
49 local i=
50 local incs1=
51 local incs=
52 case "${1#$outd/}" in
53 lablGL/*) incs1="$incs1 lablGL";;
54 wsi/$wsid/*) incs1="$incs1 $wsid";;
55 main.cmo) incs1="$incs1 $wsid lablGL";;
56 glutils.cmo|listview.cmo) incs1="$incs1 lablGL";;
57 *) ;;
58 esac
59 for i in $incs1; do
60 incs="$incs -I $srcd/$i -I $outd/$i"
61 done
62 echo "-I $srcd -I $outd $incs"
65 oflags() {
66 case "${1#$outd/}" in
67 lablGL/*) f="-g";;
68 *) f="-g -strict-sequence -strict-formats -w @A";;
69 esac
70 echo "$(oincs $1) $f"
73 cflags() {
74 case "${1#$outd/}" in
75 version.o) f='-DLLPP_VERSION="'$ver'"';;
76 link.o)
77 f="-g -std=c99 -O2 $muinc -Wall -Werror -Wextra -pedantic"
78 f="$f -DCACHE_PAGEREFS -DKeySym=uint32_t"
79 $darwin && f="$f -DCIDER -D_GNU_SOURCE" \
80 || f="$f -D_POSIX_C_SOURCE";;
81 */keysym2ucs.o) f="-O2 -include inttypes.h -DKeySym=uint32_t";;
82 */ml_*.o) f="-g -Wno-pointer-sign -O2";;
83 *) f="-g -O2";;
84 esac
85 echo $f
88 mflags() { echo "-I $(ocamlc -where) -g -Wall -Werror -O2"; }
90 overs="$(ocamlc -vnum 2>/dev/null)" || overs=""
91 test "$overs" = "4.07.0" || {
92 url=https://caml.inria.fr/pub/distrib/ocaml-4.07/ocaml-4.07.0+rc1.tar.xz
93 txz=$outd/$(basename $url)
94 isfresh $txz $url || {
95 executable_p() { command -v "$1" >/dev/null 2>&1; }
96 if executable_p wget; then dl() { wget -q "$1" -O "$2"; }
97 elif executable_p curl; then dl() { curl -L "$1" -o "$2"; }
98 else die "no program to fetch remote urls found"
100 dl $url $txz
101 echo "k=$url" >$txz.past
102 } && vecho "fresh $txz"
103 absprefix=$(cd $outd &>/dev/null; pwd -P)
104 export PATH=$absprefix/bin:$PATH
105 isfresh $absprefix/bin/ocamlc "$url" || (
106 tar xf $txz -C $outd
107 bn=$(basename $url)
108 cd $outd/${bn%.tar.xz}
109 ./configure -prefix $absprefix \
110 -no-graph -no-debugger -no-ocamldoc -no-native-compiler
111 make -j $mjobs world
112 make install
113 echo "k='$url'" >$absprefix/bin/ocamlc.past
114 ) && vecho "fresh ocamlc"
115 overs=$(ocamlc -vnum 2>/dev/null)
118 bocaml1() {
119 grep -q "$3" $outd/ordered || {
120 bocaml2 $*
121 echo "$3" >>"$outd/ordered"
125 bocaml2() {
126 local n=$1
127 local s="$2"
128 local o="$3"
129 local O=${4-}
130 local dd
132 local cmd="ocamlc -depend -bytecode -one-line $(oincs $o) $s"
133 local keycmd="digest $o $s"
134 isfresh "$o.depl" "$overs$cmd$(eval $keycmd)" || {
135 eval "$cmd || die '$cmd' failed" | {
136 read _ _ depl
137 :>"$o.depl"
138 for d in $depl; do
139 local D=${d#$srcd/}
140 test "$O" = "$D" || {
141 bocaml "$D" $((n+1))
142 case $d in
143 $outd/*) dd=$d;;
144 *) dd=$outd/${d#$srcd/};;
145 esac
146 printf "$dd " >>"$o.depl"
148 done
149 } || die "$cmd failed"
150 echo "k='$overs$cmd$(eval $keycmd)'" >"$o.depl.past"
151 } && {
152 vecho "fresh $o.depl"
153 for d in $(< $o.depl); do
154 bocaml ${d#$outd/} $((n+1))
155 done
158 cmd="ocamlc $(oflags $o) -c -o $o $s"
159 keycmd="digest $o $s $(< $o.depl)"
160 isfresh "$o" "$overs$cmd$(eval $keycmd)" || {
161 printf "%*.s%s -> %s\n" $n '' "${s#$srcd/}" "${o#$outd/}"
162 eval "$cmd || die '$cmd failed'"
163 echo "k='$overs$cmd$(eval $keycmd)'" >"$o.past"
164 } && vecho "fresh '$o'"
167 cycle=
168 bocaml() (
169 local o="$1"
170 local n="$2"
171 local wocmi="${o%.cmi}"
172 local s
173 local cycle1=$cycle
174 case ${wocmi#$outd/} in
175 confstruct.cmo)
176 s=$outd/confstruct.ml
177 o=$outd/confstruct.cmo;;
179 test "$o" = "$wocmi" && s=$srcd/${o%.cmo}.ml || s=$srcd/$wocmi.mli
180 o=$outd/$o;;
181 esac
182 expr >/dev/null "$cycle" : ".*$o" && die cycle $o || cycle="$cycle$o"
183 bocaml1 $n "$s" "$o"
184 case $wocmi in
185 wsi) s="$srcd/$wsid/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
194 cycle=$cycle1
197 bocamlc() {
198 local o=$outd/$1
199 local s=$srcd/${1%.o}.c
200 local cmd="ocamlc -ccopt \"$(cflags $o) -MMD -MF $o.dep -MT_ -o $o\" $s"
201 test -r $o.dep && read _ d <$o.dep || d=
202 local keycmd='digest $o $d'
203 isfresh "$o" "$cmd$(eval $keycmd)" || {
204 printf "%s -> %s\n" "${s#$srcd/}" "${o#$outd/}"
205 eval "$cmd || die '$cmd failed'"
206 read _ d <$o.dep
207 echo "k='$cmd$(eval $keycmd)'" >"$o.past"
208 } && vecho "fresh $o"
211 bobjc() {
212 local o=$outd/$1
213 local s=$srcd/${1%.o}.m
214 local cmd="$mcomp $(mflags $o) -MMD -MF $o.dep -MT_ -c -o $o $s"
215 test -r $o.dep && read _ d <$o.dep || d=
216 local keycmd='digest $o $d'
217 isfresh "$o" "$cmd$(eval $keycmd)" || {
218 printf "%s -> %s\n" "${s#$srcd/}" "${o#$outd/}"
219 eval "$cmd || die '$cmd failed'"
220 read _ d <$o.dep
221 echo "k='$cmd$(eval $keycmd)'" >"$o.past"
222 } && vecho "fresh $o"
225 ver=$(cd $srcd && git describe --tags --dirty) || ver=unknown
227 cmd="(. $srcd/genconfstr.sh >$outd/confstruct.ml)"
228 keycmd="digest $srcd/genconfstr.sh $outd/confstruct.ml"
229 isfresh "$outd/confstruct.ml" "$cmd$(eval $keycmd)" || {
230 echo "generating $outd/confstruct.ml"
231 eval "$cmd || die genconfstr.sh failed"
232 echo "k='$cmd$(eval $keycmd)'" > "$outd/confstruct.ml.past"
233 } && vecho "fresh $outd/confstruct.ml"
235 shift 1
236 for target; do
237 case "$target" in
238 doc)
239 doct=${doct-manpage}
240 md=$outd/doc
241 mkdir -p $md
242 case $doct in
243 epub) suf=.epub;;
244 manpage) suf=.1;;
245 *) die "unknown doc type";;
246 esac
247 for m in llpp llppac llpphtml; do
248 src=$srcd/adoc/$m.adoc
249 out=$md/$m$suf
250 conf="$srcd/man/asciidoc.conf"
251 keycmd="digest $out $src $conf"
252 cmd="a2x -D $md -d manpage -f $doct $src"
253 isfresh "$out" "$cmd$(eval $keycmd)" || {
254 echo "$src -> $out"
255 eval "$cmd || die '$cmd failed'"
256 echo "k='$cmd$(eval $keycmd)'" >"$out.past"
257 } && vecho "fresh $out"
258 done;;
260 completions) die "not yet";;
262 *) die "no clue - '$target'";;
263 esac
264 done
266 bocaml main.cmo 0
268 cobjs=
269 for m in link cutils version; do
270 bocamlc $m.o
271 cobjs="$cobjs $outd/$m.o"
272 done
273 for m in ml_gl ml_glarray ml_raw; do
274 bocamlc lablGL/$m.o
275 cobjs="$cobjs $outd/lablGL/$m.o"
276 done
278 libs="str.cma unix.cma"
279 clibs="-L$mudir/build/$mbt -lmupdf -lmupdf-third -lpthread"
280 if $darwin; then
281 mcomp=$(ocamlc -config | grep bytecomp_c_co | { read _ c; echo $c; })
282 clibs="$clibs -framework Cocoa -framework OpenGL"
283 cobjs="$cobjs $outd/wsi/cocoa/cocoa.o"
284 bobjc wsi/cocoa/cocoa.o
285 else
286 clibs="$clibs -lGL -lX11"
287 cobjs="$cobjs $outd/wsi/x11/keysym2ucs.o $outd/wsi/x11/xlib.o"
288 bocamlc wsi/x11/keysym2ucs.o
289 bocamlc wsi/x11/xlib.o
292 ord=$(grep -v \.cmi $outd/ordered)
293 cmd="ocamlc -custom $libs -o $outd/llpp $cobjs $(echo $ord) -cclib \"$clibs\""
294 keycmd="digest $outd/llpp $cobjs $ord $mulibs"
295 isfresh "$outd/llpp" "$cmd$(eval $keycmd)" || {
296 echo linking $outd/llpp
297 eval "$cmd || die '$cmd failed'"
298 echo "k='$cmd$(eval $keycmd)'" >"$outd/llpp.past"
299 } && vecho "fresh llpp"
301 if $darwin; then
302 out="$outd/llpp.app/Contents/Info.plist"
303 keycmd="digest $out $srcd/wsi/cocoa/genplist.sh; echo $ver"
304 isfresh $out "$(eval $keycmd)" || {
305 d=$(dirname $out)
306 mkdir -p "$d"
307 echo "generating $out"
308 (. $srcd/wsi/cocoa/genplist.sh) >"$out"
309 echo "k='$(eval $keycmd)'" >"$out.past"
310 } && vecho "fresh plist"
312 out=$outd/llpp.app/Contents/MacOS/llpp
313 keycmd="digest $out $outd/llpp"
314 isfresh $out "$(eval $keycmd)" || {
315 echo "bundling $out"
316 mkdir -p "$(dirname $out)"
317 cp $outd/llpp $out
318 echo "k='$(eval $keycmd)'" >"$out.past"
319 } && vecho "fresh bundle"