Fail iff r32s is actually used
[llpp.git] / build.bash
blobd5c98f792d026e2b971859836eae0429490f3978
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() { test -r "$1.past" && . "$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 lablGL/*) f="-g";;
57 $wsi/wsi.cmo|*) f="-g -strict-sequence -strict-formats -w @A";;
58 esac
59 echo "$incs $f"
62 cflags() {
63 case "${1#$outd/}" in
64 version.o) f='-DLLPP_VERSION="'$ver'"';;
65 link.o)
66 f="-g -std=c99 -O2 $muinc -Wall -Werror -Wextra -pedantic"
67 f="$f -DCACHE_PAGEREFS"
68 $darwin && f="$f -D__COCOA__ -D_GNU_SOURCE" \
69 || f="$f -D_POSIX_C_SOURCE" ;;
70 */keysym2ucs.o) f="-O2 -include inttypes.h -DKeySym=uint32_t";;
71 */ml_*.o) f="-g -Wno-pointer-sign -O2";;
72 *) f="-g -O2";;
73 esac
74 echo $f
77 mflags() { echo "-I $(ocamlc -where) -g -Wall -Werror -O2"; }
79 incs="-I $srcd/lablGL -I $srcd/$wsi -I $srcd"
80 incs="$incs -I $outd/lablGL -I $outd/$wsi -I $outd"
82 overs="$(ocamlc -vnum 2>/dev/null)" || overs=""
83 test "$overs" = "4.06.1" || {
84 url=http://caml.inria.fr/pub/distrib/ocaml-4.06/ocaml-4.06.1.tar.xz
85 txz=$outd/$(basename $url)
86 isfresh $txz $url || {
87 executable_p() { command -v "$1" >/dev/null 2>&1; }
88 if executable_p wget; then dl() { wget -q "$1" -O "$2"; }
89 elif executable_p curl; then dl() { curl -L "$1" -o "$2"; }
90 else die "no program to fetch remote urls found"
92 dl $url $txz
93 echo "k=$url" >$txz.past
94 } && vecho "fresh $txz"
95 absprefix=$(cd $outd &>/dev/null; pwd -P)
96 export PATH=$absprefix/bin:$PATH
97 isfresh $absprefix/bin/ocamlc "$url" || (
98 tar xf $txz -C $outd
99 bn=$(basename $url)
100 cd $outd/${bn%.tar.xz}
101 ./configure -prefix $absprefix \
102 -no-graph -no-debugger -no-ocamldoc -no-native-compiler
103 make -j $mjobs world
104 make install
105 echo "k='$url'" >$absprefix/bin/ocamlc.past
106 ) && vecho "fresh ocamlc"
107 overs=$(ocamlc -vnum 2>/dev/null)
110 bocaml1() {
111 grep -q "$3" $outd/ordered || {
112 bocaml2 $*
113 echo "$3" >>"$outd/ordered"
117 bocaml2() {
118 local n=$1
119 local s="$2"
120 local o="$3"
121 local O=${4-}
122 local dd
124 local cmd="ocamlc -depend -bytecode -one-line $incs $s"
125 local keycmd="digest $o $s"
126 isfresh "$o.depl" "$overs$cmd$(eval $keycmd)" || {
127 eval "$cmd" | {
128 read _ _ depl
129 :>"$o.depl"
130 for d in $depl; do
131 local D=${d#$srcd/}
132 test "$O" = "$D" || {
133 bocaml "$D" $((n+1))
134 case $d in
135 $outd/*) dd=$d;;
136 *) dd=$outd/${d#$srcd/};;
137 esac
138 printf "$dd " >>"$o.depl"
140 done
141 } || die "$cmd failed"
142 echo "k='$overs$cmd$(eval $keycmd)'" >"$o.depl.past"
143 } && {
144 vecho "fresh $o.depl"
145 for d in $(< $o.depl); do
146 bocaml ${d#$outd/} $((n+1))
147 done
150 cmd="ocamlc $(oflags $o) -c -o $o $s"
151 keycmd="digest $o $s $(< $o.depl)"
152 isfresh "$o" "$overs$cmd$(eval $keycmd)" || {
153 printf "%*.s%s -> %s\n" $n '' "${s#$srcd/}" "${o#$outd/}"
154 eval "$cmd || die '$cmd failed'"
155 echo "k='$overs$cmd$(eval $keycmd)'" >"$o.past"
156 } && vecho "fresh '$o'"
159 bocaml() (
160 local o="$1"
161 local n="$2"
162 local wocmi="${o%.cmi}"
163 local s
164 case ${wocmi#$outd/} in
165 confstruct.cmo)
166 s=$outd/confstruct.ml
167 o=$outd/confstruct.cmo
170 test "$o" = "$wocmi" && s=$srcd/${o%.cmo}.ml || s=$srcd/$wocmi.mli
171 o=$outd/$o
173 esac
174 bocaml1 $n "$s" "$o"
175 case $wocmi in
176 wsi) s="$srcd/$wsi/wsi.ml";;
177 help) s="$srcd/help.ml";;
178 */glMisc) s="$srcd/lablGL/glMisc.ml";;
179 */glTex) s="$srcd/lablGL/glTex.ml";;
180 *) false;;
181 esac && {
182 local s1=${s#$srcd/}
183 bocaml1 $n "$s" "$outd/${s1%.ml}.cmo" "${o#$outd/}"
184 } || true
187 bocamlc() {
188 local o=$outd/$1
189 local s=$srcd/${1%.o}.c
190 local cmd="ocamlc -ccopt \"$(cflags $o) -MMD -MF $o.dep -MT_ -o $o\" $s"
191 test -r $o.dep && read _ d <$o.dep || d=
192 local keycmd='digest $o $d'
193 isfresh "$o" "$cmd$(eval $keycmd)" || {
194 printf "%s -> %s\n" "${s#$srcd/}" "${o#$outd/}"
195 eval "$cmd || die '$cmd failed'"
196 read _ d <$o.dep
197 echo "k='$cmd$(eval $keycmd)'" >"$o.past"
198 } && vecho "fresh $o"
201 bobjc() {
202 local o=$outd/$1
203 local s=$srcd/${1%.o}.m
204 local cmd="$mcomp $(mflags $o) -MMD -MF $o.dep -MT_ -c -o $o $s"
205 test -r $o.dep && read _ d <$o.dep || d=
206 local keycmd='digest $o $d'
207 isfresh "$o" "$cmd$(eval $keycmd)" || {
208 printf "%s -> %s\n" "${s#$srcd/}" "${o#$outd/}"
209 eval "$cmd || die '$cmd failed'"
210 read _ d <$o.dep
211 echo "k='$cmd$(eval $keycmd)'" >"$o.past"
212 } && vecho "fresh $o"
215 ver=$(cd $srcd && git describe --tags --dirty) || ver=unknown
217 cmd="(. $srcd/genconfstr.sh >$outd/confstruct.ml)"
218 keycmd="digest $srcd/genconfstr.sh $outd/confstruct.ml"
219 isfresh "$outd/confstruct.ml" "$cmd$(eval $keycmd)" || {
220 echo genconfstr
221 eval "$cmd || die genconfstr.sh failed"
222 echo "k='$cmd$(eval $keycmd)'" > "$outd/confstruct.ml.past"
223 } && vecho "fresh $outd/confstruct.ml"
225 shift 1
226 for target; do
227 case "$target" in
228 doc)
229 doct=${doct-manpage}
230 md=$outd/doc
231 mkdir -p $md
232 case $doct in
233 epub) suf=.epub;;
234 manpage) suf=.1;;
235 *) die "unknown doc type";;
236 esac
237 for m in llpp llppac llpphtml; do
238 src=$srcd/adoc/$m.adoc
239 out=$md/$m$suf
240 conf="$srcd/man/asciidoc.conf"
241 keycmd="digest $out $src $conf"
242 cmd="a2x -D $md -d manpage -f $doct $src"
243 isfresh "$out" "$cmd$(eval $keycmd)" || {
244 echo "$src -> $out"
245 eval "$cmd || die '$cmd failed'"
246 echo "k='$cmd$(eval $keycmd)'" >"$out.past"
247 } && vecho "fresh $out"
248 done;;
250 completions) die "not yet";;
252 *) die "no clue - '$target'";;
253 esac
254 done
256 bocaml main.cmo 0
258 cobjs=
259 for m in link cutils version; do
260 bocamlc $m.o
261 cobjs="$cobjs $outd/$m.o"
262 done
263 for m in ml_gl ml_glarray ml_raw; do
264 bocamlc lablGL/$m.o
265 cobjs="$cobjs $outd/lablGL/$m.o"
266 done
268 libs="str.cma unix.cma"
269 clibs="-L$mudir/build/native -lmupdf -lmupdf-third -lpthread"
270 if $darwin; then
271 mcomp=$(ocamlc -config | grep bytecomp_c_co | { read _ c; echo $c; })
272 clibs="$clibs -framework Cocoa -framework OpenGL"
273 bobjc wsi/osx/wsicocoa.o
274 cobjs="$cobjs $outd/wsi/osx/wsicocoa.o"
275 else
276 clibs="$clibs -lGL -lX11"
277 cobjs="$cobjs $outd/wsi/x11/keysym2ucs.o"
278 bocamlc wsi/x11/keysym2ucs.o
281 ord=$(grep -v \.cmi $outd/ordered)
282 cmd="ocamlc -custom $libs -o $outd/llpp $cobjs $(echo $ord) -cclib \"$clibs\""
283 keycmd="digest $outd/llpp $cobjs $ord $mulibs"
284 isfresh "$outd/llpp" "$cmd$(eval $keycmd)" || {
285 echo linking $outd/llpp
286 eval "$cmd || die '$cmd failed'"
287 echo "k='$cmd$(eval $keycmd)'" >"$outd/llpp.past"
288 } && vecho "fresh llpp"
290 if $darwin; then
291 out="$outd/llpp.app/Contents/Info.plist"
292 keycmd="digest $out $srcd/wsi/osx/genplist.sh"
293 isfresh $out "$(eval $keycmd)" || {
294 shortver=$(echo $ver | { IFS='-' read s _; echo ${s#v}; })
295 d=$(dirname $out)
296 mkdir -p "$d"
297 (. $srcd/wsi/osx/genplist.sh) >"$out"
298 echo "k='$(eval $keycmd)'" >"$out.past"
299 } && vecho "fresh plist"
301 out=$outd/llpp.app/Contents/MacOS/llpp
302 keycmd="digest $out $outd/llpp"
303 isfresh $out "$(eval $keycmd)" || {
304 echo "bundling $out"
305 mkdir -p "$(dirname $out)"
306 cp $outd/llpp $out
307 echo "k='$(eval $keycmd)'" >"$out.past"
308 } && vecho "fresh bundle"