Consistency
[llpp.git] / build.bash
blobbb09e6df1a5e0f36f27595457cedc525d72bb8e9
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
35 mkdir -p $outd/lablGL
36 :>$outd/ordered
38 isfresh() { test -r "$1.past" && . "$1.past" && test "$k" = "$2"; }
40 mulibs="$mudir/build/native/libmupdf.a" # $mudir/build/native/libmupdf-third.a
41 keycmd="(cd $mudir && git describe --tags --dirty); digest $mulibs"
42 isfresh "$mulibs" "$(eval $keycmd)" || (
43 make -C "$mudir" build=native -j $mjobs libs
44 echo "k='$(eval $keycmd)'" >$mudir/build/native/libmupdf.a.past
45 ) && vecho "fresh mupdf"
47 oincs() {
48 local i=
49 local incs1=
50 local incs=
51 case "${1#$outd/}" in
52 lablGL/*) incs1="$incs1 lablGL";;
53 wsi/$wsid/*) incs1="$incs1 $wsid";;
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 echo $f
87 mflags() { echo "-I $(ocamlc -where) -g -Wall -Werror -O2"; }
89 overs="$(ocamlc -vnum 2>/dev/null)" || overs=""
90 test "$overs" = "4.06.1" || {
91 url=http://caml.inria.fr/pub/distrib/ocaml-4.06/ocaml-4.06.1.tar.xz
92 txz=$outd/$(basename $url)
93 isfresh $txz $url || {
94 executable_p() { command -v "$1" >/dev/null 2>&1; }
95 if executable_p wget; then dl() { wget -q "$1" -O "$2"; }
96 elif executable_p curl; then dl() { curl -L "$1" -o "$2"; }
97 else die "no program to fetch remote urls found"
99 dl $url $txz
100 echo "k=$url" >$txz.past
101 } && vecho "fresh $txz"
102 absprefix=$(cd $outd &>/dev/null; pwd -P)
103 export PATH=$absprefix/bin:$PATH
104 isfresh $absprefix/bin/ocamlc "$url" || (
105 tar xf $txz -C $outd
106 bn=$(basename $url)
107 cd $outd/${bn%.tar.xz}
108 ./configure -prefix $absprefix \
109 -no-graph -no-debugger -no-ocamldoc -no-native-compiler
110 make -j $mjobs world
111 make install
112 echo "k='$url'" >$absprefix/bin/ocamlc.past
113 ) && vecho "fresh ocamlc"
114 overs=$(ocamlc -vnum 2>/dev/null)
117 bocaml1() {
118 grep -q "$3" $outd/ordered || {
119 bocaml2 $*
120 echo "$3" >>"$outd/ordered"
124 bocaml2() {
125 local n=$1
126 local s="$2"
127 local o="$3"
128 local O=${4-}
129 local dd
131 local cmd="ocamlc -depend -bytecode -one-line $(oincs $o) $s"
132 local keycmd="digest $o $s"
133 isfresh "$o.depl" "$overs$cmd$(eval $keycmd)" || {
134 eval "$cmd || die '$cmd' failed" | {
135 read _ _ depl
136 :>"$o.depl"
137 for d in $depl; do
138 local D=${d#$srcd/}
139 test "$O" = "$D" || {
140 bocaml "$D" $((n+1))
141 case $d in
142 $outd/*) dd=$d;;
143 *) dd=$outd/${d#$srcd/};;
144 esac
145 printf "$dd " >>"$o.depl"
147 done
148 } || die "$cmd failed"
149 echo "k='$overs$cmd$(eval $keycmd)'" >"$o.depl.past"
150 } && {
151 vecho "fresh $o.depl"
152 for d in $(< $o.depl); do
153 bocaml ${d#$outd/} $((n+1))
154 done
157 cmd="ocamlc $(oflags $o) -c -o $o $s"
158 keycmd="digest $o $s $(< $o.depl)"
159 isfresh "$o" "$overs$cmd$(eval $keycmd)" || {
160 printf "%*.s%s -> %s\n" $n '' "${s#$srcd/}" "${o#$outd/}"
161 eval "$cmd || die '$cmd failed'"
162 echo "k='$overs$cmd$(eval $keycmd)'" >"$o.past"
163 } && vecho "fresh '$o'"
166 bocaml() (
167 local o="$1"
168 local n="$2"
169 local wocmi="${o%.cmi}"
170 local s
171 case ${wocmi#$outd/} in
172 confstruct.cmo)
173 s=$outd/confstruct.ml
174 o=$outd/confstruct.cmo;;
176 test "$o" = "$wocmi" && s=$srcd/${o%.cmo}.ml || s=$srcd/$wocmi.mli
177 o=$outd/$o;;
178 esac
179 bocaml1 $n "$s" "$o"
180 case $wocmi in
181 wsi) s="$srcd/$wsid/wsi.ml";;
182 help) s="$srcd/help.ml";;
183 */glMisc) s="$srcd/lablGL/glMisc.ml";;
184 */glTex) s="$srcd/lablGL/glTex.ml";;
185 *) false;;
186 esac && {
187 local s1=${s#$srcd/}
188 bocaml1 $n "$s" "$outd/${s1%.ml}.cmo" "${o#$outd/}"
189 } || true
192 bocamlc() {
193 local o=$outd/$1
194 local s=$srcd/${1%.o}.c
195 local cmd="ocamlc -ccopt \"$(cflags $o) -MMD -MF $o.dep -MT_ -o $o\" $s"
196 test -r $o.dep && read _ d <$o.dep || d=
197 local keycmd='digest $o $d'
198 isfresh "$o" "$cmd$(eval $keycmd)" || {
199 printf "%s -> %s\n" "${s#$srcd/}" "${o#$outd/}"
200 eval "$cmd || die '$cmd failed'"
201 read _ d <$o.dep
202 echo "k='$cmd$(eval $keycmd)'" >"$o.past"
203 } && vecho "fresh $o"
206 bobjc() {
207 local o=$outd/$1
208 local s=$srcd/${1%.o}.m
209 local cmd="$mcomp $(mflags $o) -MMD -MF $o.dep -MT_ -c -o $o $s"
210 test -r $o.dep && read _ d <$o.dep || d=
211 local keycmd='digest $o $d'
212 isfresh "$o" "$cmd$(eval $keycmd)" || {
213 printf "%s -> %s\n" "${s#$srcd/}" "${o#$outd/}"
214 eval "$cmd || die '$cmd failed'"
215 read _ d <$o.dep
216 echo "k='$cmd$(eval $keycmd)'" >"$o.past"
217 } && vecho "fresh $o"
220 ver=$(cd $srcd && git describe --tags --dirty) || ver=unknown
222 cmd="(. $srcd/genconfstr.sh >$outd/confstruct.ml)"
223 keycmd="digest $srcd/genconfstr.sh $outd/confstruct.ml"
224 isfresh "$outd/confstruct.ml" "$cmd$(eval $keycmd)" || {
225 echo "generating $outd/confstruct.ml"
226 eval "$cmd || die genconfstr.sh failed"
227 echo "k='$cmd$(eval $keycmd)'" > "$outd/confstruct.ml.past"
228 } && vecho "fresh $outd/confstruct.ml"
230 shift 1
231 for target; do
232 case "$target" in
233 doc)
234 doct=${doct-manpage}
235 md=$outd/doc
236 mkdir -p $md
237 case $doct in
238 epub) suf=.epub;;
239 manpage) suf=.1;;
240 *) die "unknown doc type";;
241 esac
242 for m in llpp llppac llpphtml; do
243 src=$srcd/adoc/$m.adoc
244 out=$md/$m$suf
245 conf="$srcd/man/asciidoc.conf"
246 keycmd="digest $out $src $conf"
247 cmd="a2x -D $md -d manpage -f $doct $src"
248 isfresh "$out" "$cmd$(eval $keycmd)" || {
249 echo "$src -> $out"
250 eval "$cmd || die '$cmd failed'"
251 echo "k='$cmd$(eval $keycmd)'" >"$out.past"
252 } && vecho "fresh $out"
253 done;;
255 completions) die "not yet";;
257 *) die "no clue - '$target'";;
258 esac
259 done
261 bocaml main.cmo 0
263 cobjs=
264 for m in link cutils version; do
265 bocamlc $m.o
266 cobjs="$cobjs $outd/$m.o"
267 done
268 for m in ml_gl ml_glarray ml_raw; do
269 bocamlc lablGL/$m.o
270 cobjs="$cobjs $outd/lablGL/$m.o"
271 done
273 libs="str.cma unix.cma"
274 clibs="-L$mudir/build/native -lmupdf -lmupdf-third -lpthread"
275 if $darwin; then
276 mcomp=$(ocamlc -config | grep bytecomp_c_co | { read _ c; echo $c; })
277 clibs="$clibs -framework Cocoa -framework OpenGL"
278 cobjs="$cobjs $outd/wsi/cocoa/cocoa.o"
279 bobjc wsi/cocoa/cocoa.o
280 else
281 clibs="$clibs -lGL -lX11"
282 cobjs="$cobjs $outd/wsi/x11/keysym2ucs.o $outd/wsi/x11/xlib.o"
283 bocamlc wsi/x11/keysym2ucs.o
284 bocamlc wsi/x11/xlib.o
287 ord=$(grep -v \.cmi $outd/ordered)
288 cmd="ocamlc -custom $libs -o $outd/llpp $cobjs $(echo $ord) -cclib \"$clibs\""
289 keycmd="digest $outd/llpp $cobjs $ord $mulibs"
290 isfresh "$outd/llpp" "$cmd$(eval $keycmd)" || {
291 echo linking $outd/llpp
292 eval "$cmd || die '$cmd failed'"
293 echo "k='$cmd$(eval $keycmd)'" >"$outd/llpp.past"
294 } && vecho "fresh llpp"
296 if $darwin; then
297 out="$outd/llpp.app/Contents/Info.plist"
298 keycmd="digest $out $srcd/wsi/cocoa/genplist.sh; echo $ver"
299 isfresh $out "$(eval $keycmd)" || {
300 d=$(dirname $out)
301 mkdir -p "$d"
302 echo "generating $out"
303 (. $srcd/wsi/cocoa/genplist.sh) >"$out"
304 echo "k='$(eval $keycmd)'" >"$out.past"
305 } && vecho "fresh plist"
307 out=$outd/llpp.app/Contents/MacOS/llpp
308 keycmd="digest $out $outd/llpp"
309 isfresh $out "$(eval $keycmd)" || {
310 echo "bundling $out"
311 mkdir -p "$(dirname $out)"
312 cp $outd/llpp $out
313 echo "k='$(eval $keycmd)'" >"$out.past"
314 } && vecho "fresh bundle"