build.bash: update OCaml to 4.09 release url
[llpp.git] / build.bash
bloba4655725c26d05b7683d0c97508ace17eb0211d8
1 #!/bin/bash
2 set -eu
4 now() { date +%s; }
5 S=$(now)
6 vecho() { ${vecho-:} "$*"; }
7 digest() { cat $* | cksum; } 2>/dev/null
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=${mbt:-native}
40 mulibs="$mudir/build/$mbt/libmupdf.a" # $mudir/build/$mbt/libmupdf-third.a
42 keycmd="(cd $mudir && make -q build=$mbt libs && echo); 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 main.cmo) incs1="$incs1 $wsid lablGL";;
55 glutils.cmo|listview.cmo) incs1="$incs1 lablGL";;
56 *) ;;
57 esac
58 for i in $incs1; do
59 incs="$incs -I $srcd/$i -I $outd/$i"
60 done
61 echo "-I $srcd -I $outd $incs"
64 oflags() {
65 case "${1#$outd/}" in
66 lablGL/*) f="-g";;
67 *) f="-g -strict-sequence -strict-formats -w @A";;
68 esac
69 echo "$(oincs $1) $f"
72 cflags() {
73 case "${1#$outd/}" in
74 version.o) f=-DLLPP_VERSION=$ver;;
75 link.o)
76 f="-g -std=c99 -O2 $muinc -Wall -Werror -Wextra -pedantic"
77 f="$f -DCACHE_PAGEREFS -DKeySym=uint32_t"
78 $darwin && f="$f -DCIDER -D_GNU_SOURCE" \
79 || f="$f -D_POSIX_C_SOURCE";;
80 */keysym2ucs.o) f="-O2 -include inttypes.h -DKeySym=uint32_t";;
81 */ml_*.o) f="-g -Wno-pointer-sign -O2";;
82 *) f="-g -O2";;
83 esac
84 ! $darwin || f="$f -DGL_SILENCE_DEPRECATION"
85 echo $f
88 mflags() {
89 echo "-I $(ocamlc -where) -g -Wall -Werror -O2 -DGL_SILENCE_DEPRECATION"
92 overs="$(ocamlc -vnum 2>/dev/null)" || overs=""
93 test "$overs" = "4.09" || {
94 url=https://caml.inria.fr/pub/distrib/ocaml-4.09/ocaml-4.09.0.tar.xz
95 txz=$outd/$(basename $url)
96 isfresh $txz $url || {
97 executable_p() { command -v "$1" >/dev/null 2>&1; }
98 if executable_p wget; then dl() { wget -q "$1" -O "$2"; }
99 elif executable_p curl; then dl() { curl -L "$1" -o "$2"; }
100 else die "no program to fetch remote urls found"
102 dl $url $txz
103 echo "k=$url" >$txz.past
104 } && vecho "fresh $txz"
105 absprefix=$(cd $outd &>/dev/null; pwd -P)
106 export PATH=$absprefix/bin:$PATH
107 isfresh $absprefix/bin/ocamlc "$url" || (
108 tar xf $txz -C $outd
109 bn=$(basename $url)
110 cd $outd/${bn%.tar.xz}
111 ./configure --disable-ocamldoc --enable-debugger=no --prefix=$absprefix
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 $(oincs $o) $s"
134 local keycmd="digest $o $s"
135 isfresh "$o.depl" "$overs$cmd$(eval $keycmd)" || {
136 eval "$cmd || die '$cmd' failed" | {
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 cycle=
169 bocaml() (
170 local o="$1"
171 local n="$2"
172 local wocmi="${o%.cmi}"
173 local s
174 local cycle1=$cycle
175 case ${wocmi#$outd/} in
176 confstruct.cmo)
177 s=$outd/confstruct.ml
178 o=$outd/confstruct.cmo;;
180 test "$o" = "$wocmi" && s=$srcd/${o%.cmo}.ml || s=$srcd/$wocmi.mli
181 o=$outd/$o;;
182 esac
183 expr >/dev/null "$cycle" : ".*$o" && die cycle $o || cycle="$cycle$o"
184 bocaml1 $n "$s" "$o"
185 case $wocmi in
186 wsi) s="$srcd/$wsid/wsi.ml";;
187 help) s="$srcd/help.ml";;
188 */glMisc) s="$srcd/lablGL/glMisc.ml";;
189 */glTex) s="$srcd/lablGL/glTex.ml";;
190 *) false;;
191 esac && {
192 local s1=${s#$srcd/}
193 bocaml1 $n "$s" "$outd/${s1%.ml}.cmo" "${o#$outd/}"
194 } || true
195 cycle=$cycle1
198 bocamlc() {
199 local o=$outd/$1
200 local s=$srcd/${1%.o}.c
201 local cc=${LLPP_CC:+-cc $LLPP_CC }
202 local cmd="ocamlc $cc-ccopt \"$(cflags $o) -MMD -MF $o.dep -MT_ -o $o\" $s"
203 test -r $o.dep && read _ d <$o.dep || d=
204 local keycmd='digest $o $d'
205 isfresh "$o" "$cmd$(eval $keycmd)" || {
206 printf "%s -> %s\n" "${s#$srcd/}" "${o#$outd/}"
207 eval "$cmd || die '$cmd failed'"
208 read _ d <$o.dep
209 echo "k='$cmd$(eval $keycmd)'" >"$o.past"
210 } && vecho "fresh $o"
213 bobjc() {
214 local o=$outd/$1
215 local s=$srcd/${1%.o}.m
216 local cmd="$mcomp $(mflags $o) -MD -MF $o.dep -MT_ -c -o $o $s"
217 test -r $o.dep && read _ d <$o.dep || d=
218 local keycmd='digest $o $d'
219 isfresh "$o" "$cmd$(eval $keycmd)" || {
220 printf "%s -> %s\n" "${s#$srcd/}" "${o#$outd/}"
221 eval "$cmd || die '$cmd failed'"
222 read _ d <$o.dep
223 echo "k='$cmd$(eval $keycmd)'" >"$o.past"
224 } && vecho "fresh $o"
227 ver=$(cd $srcd && git describe --tags --dirty) || ver=unknown
229 cmd="(. $srcd/genconfstr.sh >$outd/confstruct.ml)"
230 keycmd="digest $srcd/genconfstr.sh $outd/confstruct.ml"
231 isfresh "$outd/confstruct.ml" "$cmd$(eval $keycmd)" || {
232 echo "generating $outd/confstruct.ml"
233 eval "$cmd || die genconfstr.sh failed"
234 echo "k='$cmd$(eval $keycmd)'" > "$outd/confstruct.ml.past"
235 } && vecho "fresh $outd/confstruct.ml"
237 shift 1
238 for target; do
239 case "$target" in
240 doc)
241 doct=${doct-manpage}
242 md=$outd/doc
243 mkdir -p $md
244 case $doct in
245 epub) suf=.epub;;
246 manpage) suf=.1;;
247 *) die "unknown doc type";;
248 esac
249 for m in llpp llppac llpphtml; do
250 src=$srcd/adoc/$m.adoc
251 out=$md/$m$suf
252 conf="$srcd/man/asciidoc.conf"
253 keycmd="digest $out $src $conf"
254 cmd="a2x -D $md -d manpage -f $doct $src"
255 isfresh "$out" "$cmd$(eval $keycmd)" || {
256 echo "$src -> $out"
257 eval "$cmd || die '$cmd failed'"
258 echo "k='$cmd$(eval $keycmd)'" >"$out.past"
259 } && vecho "fresh $out"
260 done;;
262 completions) die "not yet";;
264 *) die "no clue - '$target'";;
265 esac
266 done
268 bocaml main.cmo 0
270 cobjs=
271 for m in link cutils version; do
272 bocamlc $m.o
273 cobjs="$cobjs $outd/$m.o"
274 done
275 for m in ml_gl ml_glarray ml_raw; do
276 bocamlc lablGL/$m.o
277 cobjs="$cobjs $outd/lablGL/$m.o"
278 done
280 libs="str.cma unix.cma"
281 clibs="-L$mudir/build/$mbt -lmupdf -lmupdf-third -lpthread"
282 if $darwin; then
283 mcomp=$(ocamlc -config | grep bytecomp_c_co | { read _ c; echo $c; })
284 clibs="$clibs -framework Cocoa -framework OpenGL"
285 cobjs="$cobjs $outd/wsi/cocoa/cocoa.o"
286 bobjc wsi/cocoa/cocoa.o
287 else
288 clibs="$clibs -lGL -lX11"
289 cobjs="$cobjs $outd/wsi/x11/keysym2ucs.o $outd/wsi/x11/xlib.o"
290 bocamlc wsi/x11/keysym2ucs.o
291 bocamlc wsi/x11/xlib.o
294 ord=$(grep -v \.cmi $outd/ordered)
295 cmd="ocamlc -custom $libs -o $outd/llpp $cobjs $(echo $ord) -cclib \"$clibs\""
296 keycmd="digest $outd/llpp $cobjs $ord $mulibs"
297 isfresh "$outd/llpp" "$cmd$(eval $keycmd)" || {
298 echo linking $outd/llpp
299 eval "$cmd || die '$cmd failed'"
300 echo "k='$cmd$(eval $keycmd)'" >"$outd/llpp.past"
301 } && vecho "fresh llpp"
303 if $darwin; then
304 out="$outd/llpp.app/Contents/Info.plist"
305 keycmd="digest $out $srcd/wsi/cocoa/genplist.sh; echo $ver"
306 isfresh $out "$(eval $keycmd)" || {
307 d=$(dirname $out)
308 mkdir -p "$d"
309 echo "generating $out"
310 (. $srcd/wsi/cocoa/genplist.sh) >"$out"
311 echo "k='$(eval $keycmd)'" >"$out.past"
312 } && vecho "fresh plist"
314 out=$outd/llpp.app/Contents/MacOS/llpp
315 keycmd="digest $out $outd/llpp"
316 isfresh $out "$(eval $keycmd)" || {
317 echo "bundling $out"
318 mkdir -p "$(dirname $out)"
319 cp $outd/llpp $out
320 echo "k='$(eval $keycmd)'" >"$out.past"
321 } && vecho "fresh bundle"