Update
[llpp.git] / build.bash
blobc387973e075423cbb121a2da86fa34d02fecfb19
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 wsid="wsi/x11"
23 case "$(uname)" in
24 Darwin)
25 darwin=true
26 wsid="wsi/cocoa"
27 mjobs=$(getconf _NPROCESSORS_ONLN || echo 1);;
28 Linux) mjobs=$(getconf _NPROCESSORS_ONLN || echo 1);;
29 OpenBSD) mjobs=$(getconf NPROCESSORS_ONLN || echo 1);;
30 *) die $(uname) is not supported;;
31 esac
33 test -n "${1-}" || die "usage: $0 build-directory"
35 outd="$1"
36 srcd="$(dirname $0)"
37 mudir=$outd/mupdf
38 muinc="-I $mudir/include -I $mudir/thirdparty/freetype/include"
40 test -d "$mudir" || die muPDF not found, consult $(dirname $0)/BUILDING
42 mkdir -p $outd/$wsid
43 mkdir -p $outd/lablGL
44 :>$outd/ordered
46 isfresh() { test -r "$1.past" && . "$1.past" && test "$k" = "$2"; }
48 mulibs="$mudir/build/native/libmupdf.a" # $mudir/build/native/libmupdf-third.a
49 keycmd="(cd $mudir && git describe --tags --dirty); digest $mulibs"
50 isfresh "$mulibs" "$(eval $keycmd)" || (
51 make -C "$mudir" build=native -j $mjobs libs
52 echo "k='$(eval $keycmd)'" >$mudir/build/native/libmupdf.a.past
53 ) && vecho "fresh mupdf"
55 oincs() {
56 local i=
57 local incs1=
58 local incs=
59 case "${1#$outd/}" in
60 lablGL/*) incs1="$incs1 lablGL";;
61 wsi/$wsid/*) incs1="$incs1 $wsid";;
62 main.cmo) incs1="$incs1 $wsid lablGL";;
63 glutils.cmo|listview.cmo) incs1="$incs1 lablGL";;
64 *) ;;
65 esac
66 for i in $incs1; do
67 incs="$incs -I $srcd/$i -I $outd/$i"
68 done
69 echo "-I $srcd -I $outd $incs"
72 oflags() {
73 case "${1#$outd/}" in
74 lablGL/*) f="-g";;
75 *) f="-g -strict-sequence -strict-formats -w @A";;
76 esac
77 echo "$(oincs $1) $f"
80 cflags() {
81 case "${1#$outd/}" in
82 version.o) f='-DLLPP_VERSION="'$ver'"';;
83 link.o)
84 f="-g -std=c99 -O2 $muinc -Wall -Werror -Wextra -pedantic"
85 f="$f -DCACHE_PAGEREFS"
86 $darwin && f="$f -D__COCOA__ -D_GNU_SOURCE" \
87 || f="$f -D_POSIX_C_SOURCE";;
88 */keysym2ucs.o) f="-O2 -include inttypes.h -DKeySym=uint32_t";;
89 */ml_*.o) f="-g -Wno-pointer-sign -O2";;
90 *) f="-g -O2";;
91 esac
92 echo $f
95 mflags() { echo "-I $(ocamlc -where) -g -Wall -Werror -O2"; }
97 overs="$(ocamlc -vnum 2>/dev/null)" || overs=""
98 test "$overs" = "4.06.1" || {
99 url=http://caml.inria.fr/pub/distrib/ocaml-4.06/ocaml-4.06.1.tar.xz
100 txz=$outd/$(basename $url)
101 isfresh $txz $url || {
102 executable_p() { command -v "$1" >/dev/null 2>&1; }
103 if executable_p wget; then dl() { wget -q "$1" -O "$2"; }
104 elif executable_p curl; then dl() { curl -L "$1" -o "$2"; }
105 else die "no program to fetch remote urls found"
107 dl $url $txz
108 echo "k=$url" >$txz.past
109 } && vecho "fresh $txz"
110 absprefix=$(cd $outd &>/dev/null; pwd -P)
111 export PATH=$absprefix/bin:$PATH
112 isfresh $absprefix/bin/ocamlc "$url" || (
113 tar xf $txz -C $outd
114 bn=$(basename $url)
115 cd $outd/${bn%.tar.xz}
116 ./configure -prefix $absprefix \
117 -no-graph -no-debugger -no-ocamldoc -no-native-compiler
118 make -j $mjobs world
119 make install
120 echo "k='$url'" >$absprefix/bin/ocamlc.past
121 ) && vecho "fresh ocamlc"
122 overs=$(ocamlc -vnum 2>/dev/null)
125 bocaml1() {
126 grep -q "$3" $outd/ordered || {
127 bocaml2 $*
128 echo "$3" >>"$outd/ordered"
132 bocaml2() {
133 local n=$1
134 local s="$2"
135 local o="$3"
136 local O=${4-}
137 local dd
139 local cmd="ocamlc -depend -bytecode -one-line $(oincs $o) $s"
140 local keycmd="digest $o $s"
141 isfresh "$o.depl" "$overs$cmd$(eval $keycmd)" || {
142 eval "$cmd || die '$cmd' failed" | {
143 read _ _ depl
144 :>"$o.depl"
145 for d in $depl; do
146 local D=${d#$srcd/}
147 test "$O" = "$D" || {
148 bocaml "$D" $((n+1))
149 case $d in
150 $outd/*) dd=$d;;
151 *) dd=$outd/${d#$srcd/};;
152 esac
153 printf "$dd " >>"$o.depl"
155 done
156 } || die "$cmd failed"
157 echo "k='$overs$cmd$(eval $keycmd)'" >"$o.depl.past"
158 } && {
159 vecho "fresh $o.depl"
160 for d in $(< $o.depl); do
161 bocaml ${d#$outd/} $((n+1))
162 done
165 cmd="ocamlc $(oflags $o) -c -o $o $s"
166 keycmd="digest $o $s $(< $o.depl)"
167 isfresh "$o" "$overs$cmd$(eval $keycmd)" || {
168 printf "%*.s%s -> %s\n" $n '' "${s#$srcd/}" "${o#$outd/}"
169 eval "$cmd || die '$cmd failed'"
170 echo "k='$overs$cmd$(eval $keycmd)'" >"$o.past"
171 } && vecho "fresh '$o'"
174 bocaml() (
175 local o="$1"
176 local n="$2"
177 local wocmi="${o%.cmi}"
178 local s
179 case ${wocmi#$outd/} in
180 confstruct.cmo)
181 s=$outd/confstruct.ml
182 o=$outd/confstruct.cmo;;
184 test "$o" = "$wocmi" && s=$srcd/${o%.cmo}.ml || s=$srcd/$wocmi.mli
185 o=$outd/$o;;
186 esac
187 bocaml1 $n "$s" "$o"
188 case $wocmi in
189 wsi) s="$srcd/$wsid/wsi.ml";;
190 help) s="$srcd/help.ml";;
191 */glMisc) s="$srcd/lablGL/glMisc.ml";;
192 */glTex) s="$srcd/lablGL/glTex.ml";;
193 *) false;;
194 esac && {
195 local s1=${s#$srcd/}
196 bocaml1 $n "$s" "$outd/${s1%.ml}.cmo" "${o#$outd/}"
197 } || true
200 bocamlc() {
201 local o=$outd/$1
202 local s=$srcd/${1%.o}.c
203 local cmd="ocamlc -ccopt \"$(cflags $o) -MMD -MF $o.dep -MT_ -o $o\" $s"
204 test -r $o.dep && read _ d <$o.dep || d=
205 local keycmd='digest $o $d'
206 isfresh "$o" "$cmd$(eval $keycmd)" || {
207 printf "%s -> %s\n" "${s#$srcd/}" "${o#$outd/}"
208 eval "$cmd || die '$cmd failed'"
209 read _ d <$o.dep
210 echo "k='$cmd$(eval $keycmd)'" >"$o.past"
211 } && vecho "fresh $o"
214 bobjc() {
215 local o=$outd/$1
216 local s=$srcd/${1%.o}.m
217 local cmd="$mcomp $(mflags $o) -MMD -MF $o.dep -MT_ -c -o $o $s"
218 test -r $o.dep && read _ d <$o.dep || d=
219 local keycmd='digest $o $d'
220 isfresh "$o" "$cmd$(eval $keycmd)" || {
221 printf "%s -> %s\n" "${s#$srcd/}" "${o#$outd/}"
222 eval "$cmd || die '$cmd failed'"
223 read _ d <$o.dep
224 echo "k='$cmd$(eval $keycmd)'" >"$o.past"
225 } && vecho "fresh $o"
228 ver=$(cd $srcd && git describe --tags --dirty) || ver=unknown
230 cmd="(. $srcd/genconfstr.sh >$outd/confstruct.ml)"
231 keycmd="digest $srcd/genconfstr.sh $outd/confstruct.ml"
232 isfresh "$outd/confstruct.ml" "$cmd$(eval $keycmd)" || {
233 echo genconfstr
234 eval "$cmd || die genconfstr.sh failed"
235 echo "k='$cmd$(eval $keycmd)'" > "$outd/confstruct.ml.past"
236 } && vecho "fresh $outd/confstruct.ml"
238 shift 1
239 for target; do
240 case "$target" in
241 doc)
242 doct=${doct-manpage}
243 md=$outd/doc
244 mkdir -p $md
245 case $doct in
246 epub) suf=.epub;;
247 manpage) suf=.1;;
248 *) die "unknown doc type";;
249 esac
250 for m in llpp llppac llpphtml; do
251 src=$srcd/adoc/$m.adoc
252 out=$md/$m$suf
253 conf="$srcd/man/asciidoc.conf"
254 keycmd="digest $out $src $conf"
255 cmd="a2x -D $md -d manpage -f $doct $src"
256 isfresh "$out" "$cmd$(eval $keycmd)" || {
257 echo "$src -> $out"
258 eval "$cmd || die '$cmd failed'"
259 echo "k='$cmd$(eval $keycmd)'" >"$out.past"
260 } && vecho "fresh $out"
261 done;;
263 completions) die "not yet";;
265 *) die "no clue - '$target'";;
266 esac
267 done
269 bocaml main.cmo 0
271 cobjs=
272 for m in link cutils version; do
273 bocamlc $m.o
274 cobjs="$cobjs $outd/$m.o"
275 done
276 for m in ml_gl ml_glarray ml_raw; do
277 bocamlc lablGL/$m.o
278 cobjs="$cobjs $outd/lablGL/$m.o"
279 done
281 libs="str.cma unix.cma"
282 clibs="-L$mudir/build/native -lmupdf -lmupdf-third -lpthread"
283 if $darwin; then
284 mcomp=$(ocamlc -config | grep bytecomp_c_co | { read _ c; echo $c; })
285 clibs="$clibs -framework Cocoa -framework OpenGL"
286 bobjc wsi/cocoa/wsicocoa.o
287 cobjs="$cobjs $outd/wsi/cocoa/wsicocoa.o"
288 else
289 clibs="$clibs -lGL -lX11"
290 cobjs="$cobjs $outd/wsi/x11/keysym2ucs.o"
291 bocamlc wsi/x11/keysym2ucs.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"
306 isfresh $out "$(eval $keycmd)" || {
307 shortver=$(echo $ver | { IFS='-' read s _; echo ${s#v}; })
308 d=$(dirname $out)
309 mkdir -p "$d"
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"