-
Notifications
You must be signed in to change notification settings - Fork 7
/
build.bash
executable file
·319 lines (284 loc) · 9.31 KB
/
build.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/bin/bash
set -e
vecho() { ${vecho-:} "$@"; }
executable_p() { command -v "$1" >/dev/null 2>&1; }
dgst='cksum "$@" | while read d _; do printf $d; done'
! executable_p b3sum || dgst='b3sum --no-names "$@"'
executable_p realpath || realpath() (cd "$1" &>/dev/null; pwd -P)
eval "digest() { $dgst; } 2>/dev/null"
die() { echo "$@" >&2; exit 111; }
trap 'test $? -eq 0 || echo "build failed"' EXIT
darwin=false
wsid="wsi/x11"
clip="LC_CTYPE=UTF-8 xclip -i"
paste="LC_CTYPE=UTF-8 xclip -o"
uopen="echo 'Open "%s"' >&2"
print="echo 'Print "%s"' >&2"
mjobs=$(getconf _NPROCESSORS_ONLN || echo 1)
case "$(uname)" in
Darwin)
darwin=true
wsid="wsi/cocoa"
clip="LC_CTYPE=UTF-8 pbcopy"
paste="LC_CTYPE=UTF-8 pbaste"
uopen='open "%s"';;
Linux) ;;
*) die $(uname) is not supported;;
esac
test -n "${1-}" || die "usage: $0 build-directory"
outd=$1
srcd=$(dirname $0)
mudir=$outd/mupdf
mudeps=('freetype2' 'gumbo' 'harfbuzz' 'libjpeg' 'libopenjp2' 'x11' 'zlib')
mkdir -p $outd/{$wsid,lablGL}
isfresh() {
test -f "$1.past" || return 1
test "$(<$1.past)" = "$2"
} 2>/dev/null
mbt=${mbt:-release}
test -n "${gmk:-}" && gmk=false || gmk=true
mulibs="$mudir/build/$mbt/libmupdf.a $mudir/build/$mbt/libmupdf-third.a"
oincs() {
local b=$1 incs
case "${2#$outd/}" in
$wsid/wsi.cm[oi]|confstruct.cmo|help.cmo) incs="-I $b -I $b/$wsid";;
glutils.cmo) incs="-I $b -I $b/lablGL";;
uiutils.cmo|main.cmo) incs="-I $b -I $b/$wsid -I $b/lablGL";;
ffi.cmo|help.cmi|parser.cmo) incs="-I $b";;
config.cmo)
incs="-I $b -I $b/$wsid"
test "$b" = $outd || incs="$incs -I $outd"
;;
lablGL/*) incs="-I $b/lablGL";;
main.cmo|keys.cmo|utils.cmo|utf8syms.cmo) incs="-I $b";;
config.cmi) incs="-I $outd -I $b -I $b/$wsid";;
uiutils.cmi|ffi.cmi) incs="-I $b";;
glutils.cmi) incs="-I $b/lablGL";;
main.cmi|keys.cmi|utils.cmi|utf8syms.cmi|parser.cmi) ;;
*) die "ocaml include paths for '$2' aren't set";;
esac
test -z "${incs-}" || echo $incs
}
oflags() {
case "${1#$outd/}" in
lablGL/*) f="-g";;
utf8syms.cmo|confstruct.cmo|config.cmo|ffi.cmo|wsi/cocoa/wsi.cmo)
f="-g -strict-sequence -strict-formats -alert @all-missing-mli";;
*) f="-g -strict-sequence -strict-formats -alert @all -warn-error @A";;
esac
echo $(oincs $outd $1) -I +unix -I +str $f
}
cflags() {
case "${1#$outd/}" in
version.o) f=-DLLPP_VERSION=$ver;;
lablGL/*.o) f="-g -Wno-pointer-sign -Werror -O2";;
link.o)
f="$CFLAGS -g -std=c11 $(pkg-config --cflags "${mudeps[@]}") -Wall -Wextra -pedantic "
test "${mbt-}" = "debug" || f+="-O2 "
$darwin && f+="-DMACOS -D_GNU_SOURCE -DGL_H='<OpenGL/gl.h>'" \
|| f+="-D_POSIX_C_SOURCE -DGL_H='<GL/gl.h>'"
f+=" -DTEXT_TYPE=GL_TEXTURE_RECTANGLE_ARB"
#f+=" -DLLPARANOIDP"
#f+=" -DTEXT_TYPE=GL_TEXTURE_2D"
;;
*) f="-g -O2 -Wall -Werror";;
esac
! $darwin || f+=" -DGL_SILENCE_DEPRECATION"
echo $f
}
mflags() {
echo "-I $(ocamlc -where) -g -Wall -Werror -O2 -DGL_SILENCE_DEPRECATION"
}
overs=$(ocamlc -vnum 2>/dev/null) || overs=""
while read k v; do
case "$k" in
"bytecomp_c_compiler:") ccomp=${CAML_CC-$v};;
"word_size:") ! test "$darwin$v" = "true32" || die "need 64bit ocaml";;
esac
done < <(ocamlc -config)
read cvers < <($ccomp --version)
seen=
ord=
$gmk || :>$outd/Makefile
bocaml1() {
local n=$1 s=$2 o=$3 deps= cmd d
local keycmd="digest $s $o.depl"
cmd="ocamlc -depend -bytecode -one-line $(oincs $srcd $o) $s"
isfresh "$o.depl" "$overs$cmd$(eval $keycmd)" || {
read _ _ depl < <(eval $cmd) || die "$cmd failed"
for d in $depl; do
if test "$d" = "$outd/confstruct.cmo";
then d=confstruct.cmo; else d=${d#$srcd/}; fi
deps+="$d\n"
done
printf "$deps" >$o.depl
deps=
echo "$overs$cmd$(eval $keycmd)" >"$o.depl.past"
} && vecho "fresh $o.depl"
# this saves time but is overly optimistic as interface (dis)
# appearance will result in an invalid (stale) .depl (cache). not
# using a cache is correct but slow(er (much)) way to handle this.
while read d; do
bocaml $d $((n+1))
deps+=" $outd/$d"
done <$o.depl
cmd="ocamlc $(oflags $o) -c -o $o $s"
keycmd="digest $o $s $deps"
isfresh "$o" "$overs$cmd$(eval $keycmd)" || {
printf "%*.s%s\n" $n '' "${o#$outd/}"
eval "$cmd" || die "$cmd failed"
echo "$overs$cmd$(eval $keycmd)" >"$o.past"
} && vecho "fresh $o"
$gmk || printf "$o: $deps\n\t%s\n" "$cmd" >>$outd/Makefile
seen+=$o
ord+=" $o"
}
cycle=
bocaml() {
[[ ! $seen =~ $1 ]] || return 0
local s o=$1 n=$2 cycle1=$cycle
case $o in
confstruct.cmo) s=$outd/confstruct.ml;;
*.cmo) s=$srcd/${o%.cmo}.ml;;
*.cmi) s=$srcd/${o%.cmi}.mli;;
esac
o=$outd/$o
[[ "$cycle" =~ "$o" ]] && die cycle $o || cycle=$cycle$o
bocaml1 $n $s $o
cycle=$cycle1
}
baux() {
local o=$1 cmd=$2
read 2>/dev/null _ d <$o.dep || d=
local keycmd='digest $o $d'
isfresh "$o" "$cvers$cmd$(eval $keycmd)" || {
echo "${o#$outd/}"
eval "$cmd" || die "$cmd failed"
read _ d <$o.dep
echo "$cvers$cmd$(eval $keycmd)" >"$o.past"
} && vecho "fresh $o"
$gmk || printf "$o: $d\n\t$cmd\n" >>$outd/Makefile
}
bocamlc() {
local o=$outd/$1 s=$srcd/${1%.o}.c cc=${CAML_CC:+-cc "'$CAML_CC'" }
baux $o "ocamlc $cc-ccopt \"$(cflags $o) -MMD -MF $o.dep -MT_\" -o $o -c $s"
}
bobjc() {
local o=$outd/$1
baux $o "$mcomp $(mflags $o) -MD -MF $o.dep -MT_ -c -o $o $srcd/${1%.o}.m"
}
ver=$(cd $srcd && git describe --tags --dirty) || ver="'built on $(date)'"
gen=$srcd/genconfstruct.sh
out=$outd/confstruct.ml
cmd="(export print paste clip uopen; . $gen >$out)"
keycmd="{ echo '$print $paste $clip $uopen'; digest $gen $out; }"
isfresh "$out" "$cmd$(eval $keycmd)" || {
echo "generating $out"
eval "$cmd" || die $gen failed
echo "$cmd$(eval $keycmd)" > "${out}.past"
} && vecho "fresh $out"
shift 1
for target; do
case "$target" in
doc)
md=$outd/doc
mkdir -p $md
for m in llpp llppac; do
src=$srcd/adoc/$m.adoc
o=$md/$m.1
conf=$srcd/man/asciidoc.conf
keycmd="digest $o $src $conf"
cmd="a2x -f manpage -D $md $src"
isfresh "$o" "$cmd$(eval $keycmd)" || {
echo "${o#$outd/}"
eval "$cmd" || die "$cmd failed"
echo "$cmd$(eval $keycmd)" >"$o.past"
} && vecho "fresh $o"
done;
exit;;
*) die "no such target - '$target'";;
esac
done
flatten() {
local o
[[ ! "$seen" =~ "$1" ]] || return 0
bocaml $1 0
for o in $ord; do
local wooutd=${o#$outd/}
case $o in
*.cmi) flatten ${wooutd%.cmi}.cmo;;
*.cmo) flatten $wooutd;;
esac
done
}
flatten main.cmo
modules=
collectmodules() {
# it might appear that following can be done inside bocaml* but
# alas due to the early cmi->cmo descent this ought to be done
# here (at least the solution inside bocaml* eludes me)
local dep cmo this=$1
while read dep; do
case $dep in
*.cmi)
cmo=${dep%.cmi}.cmo
test $cmo = $this || collectmodules $cmo
;;
*.cmo)
collectmodules $dep
cmo=$dep
;;
esac
[[ $modules =~ $cmo ]] || modules+=" $outd/$cmo"
done <$outd/$1.depl
}
collectmodules main.cmo
cobjs=
for m in link cutils version; do
bocamlc $m.o
cobjs+=" $outd/$m.o"
done
for m in ml_gl ml_glarray ml_raw; do
bocamlc lablGL/$m.o
cobjs+=" $outd/lablGL/$m.o"
done
libs="str.cma unix.cma"
clibs="-ljbig2dec $(pkg-config --libs "${mudeps[@]}") -lmupdf -lpthread"
if $darwin; then
mcomp=$ccomp
clibs+=" -framework Cocoa -framework OpenGL"
cobjs+=" $outd/wsi/cocoa/cocoa.o"
bobjc wsi/cocoa/cocoa.o
else
clibs+=" -lGL -lX11"
cobjs+=" $outd/wsi/x11/keysym2ucs.o $outd/wsi/x11/xlib.o"
bocamlc wsi/x11/keysym2ucs.o
bocamlc wsi/x11/xlib.o
fi
cmd="ocamlc -custom $libs -o $outd/llpp $cobjs $modules -cclib \"$clibs\" -I +unix -I +str"
keycmd="digest $outd/llpp $cobjs $modules $mulibs"
isfresh "$outd/llpp" "$cmd$(eval $keycmd)" || {
echo linking $outd/llpp
eval "$cmd" || die "$cmd failed"
echo "$cmd$(eval $keycmd)" >"$outd/llpp.past"
} && vecho "fresh llpp"
$gmk || printf "$outd/llpp: $cobjs $modules $mulibs\n\t$cmd\n" >>$outd/Makefile
if $darwin; then
out="$outd/llpp.app/Contents/Info.plist"
keycmd="digest $out $srcd/wsi/cocoa/genplist.sh; echo $ver"
isfresh $out "$(eval $keycmd)" || {
d=$(dirname $out)
mkdir -p "$d"
echo "generating $out"
(. $srcd/wsi/cocoa/genplist.sh) >"$out"
eval $keycmd>"$out.past"
} && vecho "fresh plist"
out=$outd/llpp.app/Contents/MacOS/llpp
keycmd="digest $out $outd/llpp"
isfresh $out "$(eval $keycmd)" || {
echo "bundling $out"
mkdir -p "$(dirname $out)"
cp $outd/llpp $out
eval $keycmd>"$out.past"
} && vecho "fresh bundle"
fi