Do not overdo it
[llpp.git] / build.bash
blob77a18278e5b389f807756c571a4af8675af1137e
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() { . 2>/dev/null "$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 | unisyms.cmo \
64 | utils.cmo \
65 | $wsi/wsi.cmo) # XXX confstruct?
66 f="-g -strict-sequence -strict-formats -warn-error @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 f="$f -D_POSIX_C_SOURCE"
78 ! $darwin || f="$f -D__COCOA__";;
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 -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 --version 2>/dev/null)" || overs="0.0.0"
92 oversnum="$(echo $overs | { IFS=. read a b _; echo $a$b; })"
94 test $oversnum -ge 406 || {
95 url=http://caml.inria.fr/pub/distrib/ocaml-4.06/ocaml-4.06.1.tar.xz
96 txz=$outd/$(basename $url)
97 isfresh $txz $url || {
98 executable_p() { command -v "$1" >/dev/null 2>&1; }
99 if executable_p wget; then dl() { wget -q "$1" -O "$2"; }
100 elif executable_p curl; then dl() { curl -L "$1" -o "$2"; }
101 else die "no program to fetch remote urls found"
103 dl $url $txz
104 echo "k=$url" >$txz.past
105 } && vecho "fresh $txz"
106 absprefix=$(cd $outd &>/dev/null; pwd -P)
107 export PATH=$absprefix/bin:$PATH
108 isfresh $absprefix/bin/ocamlc "$url" || (
109 tar xf $txz -C $outd
110 bn=$(basename $url)
111 cd $outd/${bn%.tar.xz}
112 ./configure -prefix $absprefix \
113 -no-graph -no-debugger -no-ocamldoc -no-native-compiler
114 make -j $mjobs world
115 make install
116 echo "k='$url'" >$absprefix/bin/ocamlc.past
117 ) && vecho "fresh ocamlc"
118 overs=$(ocamlc --version 2>/dev/null)
121 bocaml1() {
122 grep -q "$3" $outd/ordered || {
123 bocaml2 $*
124 echo "$3" >>"$outd/ordered"
128 bocaml2() {
129 local n=$1
130 local s="$2"
131 local o="$3"
132 local O=${4-}
133 local dd
135 local cmd="ocamlc -depend -bytecode -one-line $incs $s"
136 local keycmd="digest $o $s"
137 isfresh "$o.depl" "$overs$cmd$(eval $keycmd)" || {
138 eval "$cmd" | {
139 read _ _ depl
140 :>"$o.depl"
141 for d in $depl; do
142 local D=${d#$srcd/}
143 test "$O" = "$D" || {
144 bocaml "$D" $((n+1))
145 case $d in
146 $outd/*) dd=$d;;
147 *) dd=$outd/${d#$srcd/};;
148 esac
149 printf "$dd " >>"$o.depl"
151 done
152 } || die "$cmd failed"
153 echo "k='$overs$cmd$(eval $keycmd)'" >"$o.depl.past"
154 } && {
155 vecho "fresh $o.depl"
156 for d in $(< $o.depl); do
157 bocaml ${d#$outd/} $((n+1))
158 done
161 cmd="ocamlc $(oflags $o) -c -o $o $s"
162 keycmd="digest $o $s $(< $o.depl)"
163 isfresh "$o" "$overs$cmd$(eval $keycmd)" || {
164 printf "%*.s%s -> %s\n" $n '' "${s#$srcd/}" "${o#$outd/}"
165 eval "$cmd || die '$cmd failed'"
166 echo "k='$overs$cmd$(eval $keycmd)'" >"$o.past"
167 } && vecho "fresh '$o'"
170 bocaml() (
171 local o="$1"
172 local n="$2"
173 local wocmi="${o%.cmi}"
174 local s
175 case ${wocmi#$outd/} in
176 confstruct.cmo)
177 s=$outd/confstruct.ml
178 o=$outd/confstruct.cmo
181 test "$o" = "$wocmi" && s=$srcd/${o%.cmo}.ml || s=$srcd/$wocmi.mli
182 o=$outd/$o
184 esac
185 bocaml1 $n "$s" "$o"
186 case $wocmi in
187 wsi) s="$srcd/$wsi/wsi.ml";;
188 help) s="$srcd/help.ml";;
189 */glMisc) s="$srcd/lablGL/glMisc.ml";;
190 */glTex) s="$srcd/lablGL/glTex.ml";;
191 *) false;;
192 esac && {
193 local s1=${s#$srcd/}
194 bocaml1 $n "$s" "$outd/${s1%.ml}.cmo" "${o#$outd/}"
195 } || true
198 bocamlc() {
199 local o=$outd/$1
200 local s=$srcd/${1%.o}.c
201 local cmd="ocamlc -ccopt \"$(cflags $o) -MMD -MF $o.dep -MT_ -o $o\" $s"
202 test -r $o.dep && read _ d <$o.dep || d=
203 local keycmd='digest $o $d'
204 isfresh "$o" "$cmd$(eval $keycmd)" || {
205 printf "%s -> %s\n" "${s#$srcd/}" "${o#$outd/}"
206 eval "$cmd || die '$cmd failed'"
207 read _ d <$o.dep
208 echo "k='$cmd$(eval $keycmd)'" >"$o.past"
209 } && vecho "fresh $o"
212 bobjc() {
213 local o=$outd/$1
214 local s=$srcd/${1%.o}.m
215 local cmd="$mcomp $(mflags $o) -MMD -MF $o.dep -MT_ -c -o $o $s"
216 test -r $o.dep && read _ d <$o.dep || d=
217 local keycmd='digest $o $d'
218 isfresh "$o" "$cmd$(eval $keycmd)" || {
219 printf "%s -> %s\n" "${s#$srcd/}" "${o#$outd/}"
220 eval "$cmd || die '$cmd failed'"
221 read _ d <$o.dep
222 echo "k='$cmd$(eval $keycmd)'" >"$o.past"
223 } && vecho "fresh $o"
226 ver=$(cd $srcd && git describe --tags --dirty) || ver=unknown
228 cmd="zsh $srcd/genconfstr.sh >$outd/confstruct.ml"
229 keycmd="digest $srcd/genconfstr.sh $outd/confstruct.ml"
230 isfresh "$outd/confstruct.ml" "$cmd$(eval $keycmd)" || {
231 echo genconfstr
232 eval "$cmd || die genconfstr.sh failed"
233 echo "k='$cmd$(eval $keycmd)'" > "$outd/confstruct.ml.past"
234 } && vecho "fresh $outd/confstruct.ml"
236 shift 1
237 for target; do
238 case "$target" in
239 doc)
240 doct=${doct-manpage}
241 md=$outd/doc
242 mkdir -p $md
243 case $doct in
244 epub) suf=.epub;;
245 manpage) suf=.1;;
246 *) die "unknown doc type";;
247 esac
248 for m in llpp llppac llpphtml; do
249 src=$srcd/adoc/$m.adoc
250 out=$md/$m$suf
251 conf="$srcd/man/asciidoc.conf"
252 keycmd="digest $out $src $conf"
253 cmd="a2x -D $md -d manpage -f $doct $src"
254 isfresh "$out" "$cmd$(eval $keycmd)" || {
255 echo "$src -> $out"
256 eval "$cmd || die '$cmd failed'"
257 echo "k='$cmd$(eval $keycmd)'" >"$out.past"
258 } && vecho "fresh manual pages"
259 done;;
261 completions) die "not yet";;
263 *) die "no clue - '$target'";;
264 esac
265 done
267 bocaml main.cmo 0
269 cobjs=
270 for m in link cutils version; do
271 bocamlc $m.o
272 cobjs="$cobjs $outd/$m.o"
273 done
275 libs="str.cma unix.cma"
276 clibs="-L$mudir/build/native -lmupdf -lmupdf-third -lpthread"
277 if $darwin; then
278 mcomp=$(ocamlc -config | grep bytecomp_c_co | { read _ c; echo $c; })
279 clibs="$clibs -framework Cocoa -framework OpenGL"
280 bobjc wsi/osx/wsicocoa.o
281 cobjs="$cobjs $outd/wsi/osx/wsicocoa.o"
282 else
283 clibs="$clibs -lGL -lX11"
284 cobjs="$cobjs $outd/wsi/x11/keysym2ucs.o"
285 bocamlc wsi/x11/keysym2ucs.o
288 globjs=
289 for f in ml_gl ml_glarray ml_raw; do
290 bocamlc lablGL/$f.o
291 globjs="$globjs $outd/lablGL/$f.o"
292 done
294 ord=$(grep -v \.cmi $outd/ordered)
295 cmd="ocamlc -custom $libs -o $outd/llpp $cobjs $ord"
296 cmd=$(echo $cmd $globjs -cclib \"$clibs\")
297 keycmd="digest $outd/llpp $cobjs $ord $mulibs"
298 isfresh "$outd/llpp" "$cmd$(eval $keycmd)" || {
299 echo linking $outd/llpp
300 eval "$cmd || die '$cmd failed'"
301 echo "k='$cmd$(eval $keycmd)'" >"$outd/llpp.past"
302 } && vecho "fresh llpp"
304 if $darwin; then
305 out="$outd/llpp.app/Contents/Info.plist"
306 keycmd="digest $out $srcd/wsi/osx/genplist.sh"
307 isfresh $out "$(eval $keycmd)" || {
308 shortver=$(echo $ver | { IFS='-' read s _; echo ${s#v}; })
309 d=$(dirname $out)
310 mkdir -p "$d"
311 . $srcd/wsi/osx/genplist.sh >"$out"
312 echo "k='$(eval $keycmd)'" >"$out.past"
313 } && vecho "fresh plist"
315 out=$outd/llpp.app/Contents/MacOS/llpp
316 keycmd="digest $out $outd/llpp"
317 isfresh $out "$(eval $keycmd)" || {
318 mkdir -p "$(dirname $out)"
319 cp $outd/llpp $out
320 echo "k='$(eval $keycmd)'" >"$out.past"
321 } && vecho "fresh bundle"