-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathotp.build
executable file
·502 lines (440 loc) · 12.1 KB
/
otp.build
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#!/bin/bash -e
# ========================================================================
# Copyright (c) 2014-2023 T. R. Burghart.
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# ========================================================================
#
# Builds a relocatable OTP release optimized for the machine building it.
#
# * The latest suitable OpenSSL crypto library is statically linked.
#
# * Option: wxWidgets-dependent applications can be included, requiring
# a local installation of the wxWidgets libraries.
#
# * Option: HTML documentation can be generated, with an encompassing index.
#
# * Option: OpenSSL, OTP, and Rebar3 tests can be run.
#
# !!! This script outputs a LOT of text,
# !!! redirecting to a log file is strongly recommended.
#
# Usage: otp.build [options] <otp-release>
#
# Where:
#
# <otp-release> is a positive integer
#
# Options:
#
# -d generate documentation
# -k keep the build working directory
# -o directory in which to create artifacts, default $PWD
# -p portable, do not optimize for the specific machine CPU
# -t run OpenSSL, OTP, and Rebar3 tests
# -w with wxWdgets and dependant apps, default off
#
# Artifacts are overwritten without warning!
#
# directory otp-<otp-release>
# archive otp-<otp-release>.tgz
# test archives otp-<otp-release>-*-tests.tgz
#
readonly pwd="$(pwd)"
readonly scr="${0##*/}"
dest_dir="$pwd"
keep_bld='false'
portable='false'
run_tests='false'
with_docs='false'
with_wx='false'
usage()
{
echo "Usage: $scr [-dpw] [-c <cache-dir>] [-o <otp-base-dir>] <otp-release>" >&2
echo 'Where:' >&2
echo ' <otp-release> is a positive integer' >&2
echo 'Options:' >&2
echo ' -d generate documentation' >&2
echo ' -k keep the build working directory' >&2
echo ' -o directory in which to create artifacts, default $PWD' >&2
echo ' -p portable, do not optimize for the specific machine CPU' >&2
echo ' -t run OpenSSL, OTP, and Rebar3 tests' >&2
echo ' -w with wxWdgets and dependant apps, default off' >&2
exit 1
}
while getopts ':dko:ptw' flag
do
case "$flag" in
d)
with_docs='true'
;;
k)
keep_bld='true'
;;
o)
if ! [[ -n "$OPTARG" && -d "$OPTARG" ]]
then
echo "${scr}: invalid destination directory: '${OPTARG}'" >&2
exit 2
fi
dest_dir="$(cd "$OPTARG" && pwd)"
;;
p)
portable='true'
;;
t)
run_tests='true'
;;
w)
with_wx='true'
;;
'?')
[[ "$OPTARG" == h || "$OPTARG" == - ]] || \
echo "${scr}: illegal option '${OPTARG}'" >&2
usage
;;
esac
done
shift $(($OPTIND - 1))
case $# in
0)
echo "${scr}: Missing required OTP release" >&2
usage
;;
1)
if [[ "$1" =~ ^[1-9][0-9]*$ ]]
then
declare -i otp_rel="$1"
else
echo "${scr}: Non-integral OTP release '$1'" >&2
usage
fi
;;
*)
echo "${scr}: Invalid command parameters" >&2
usage
;;
esac
if [[ $otp_rel -lt 21 ]]
then
echo "${scr}: Unsupported OTP release '$1'" >&2
exit 1
elif [[ $otp_rel -gt 29 ]]
then
echo "${scr}: Script update required for OTP release '$1'" >&2
exit 1
fi
if $with_docs
then
if [[ -z "$JAVA_HOME" ]]
then
echo "${scr}: \$JAVA_HOME must be set to generate documentation" >&2
exit 5
elif [[ ! -d "$JAVA_HOME" ]]
then
echo "${scr}: Invalid \$JAVA_HOME: '${JAVA_HOME}'" >&2
exit 5
fi
fi
if $with_wx && ! type -P wx-config &>/dev/null
then
echo "${scr}: wx-config not found on \$PATH" >&2
exit 5
fi
if [[ $otp_rel -lt 25 ]]
then
declare -i ssl_rel=1
ssl_branch='OpenSSL_1_1_1-stable'
else
declare -i ssl_rel=3
ssl_branch='openssl-3.1'
fi
os_name="$(uname -s)"
os_vers="$(uname -r)"
os_arch="$(uname -m)"
git_exe="$(type -P git)"
run()
{
echo '==>' "$@" >&2
"$@"
}
git()
{
run "$git_exe" "$@"
}
repos="/tmp/${scr}.$(/bin/date '+%Y%m%d.%H%M%S')"
[[ -d "$repos" ]] || run /bin/mkdir "$repos"
otp_dir="$dest_dir/otp-$otp_rel"
echo
echo "Working directory '${repos}'"
if $keep_bld
then
echo 'This directory will be left intact'
else
echo 'On failure this directory will be left intact'
fi
echo
echo "Target OTP Release: $otp_rel"
echo "Platform: $os_name $os_vers $os_arch"
echo "Destination Directory: $otp_dir"
echo "With Documentation: $with_docs"
echo "With wxWidgets: $with_wx"
echo
# huge repo, and we don't need to search for a tag, so just take the tip of the branch
git clone --quiet --branch "maint-$otp_rel" --single-branch --depth 1 'http://github.com/erlang/otp.git' "$repos/otp"
# we'll be searching for the last release tag, so don't restrict depth
git clone --quiet 'http://github.com/erlang/rebar3.git' "$repos/rebar3"
git clone --quiet --branch "$ssl_branch" --single-branch 'http://github.com/openssl/openssl.git' "$repos/openssl"
#
# OTP maint-xx branch should always be at a release tag.
# Rebar3 tags don't contain '-', so we can easily find the last tagged release.
# OpenSSL tag patterns depend on version, takes a bit more work.
#
otp_vsn=$(< "$repos/otp/OTP_VERSION")
rebar_vsn=$("$git_exe" -C "$repos/rebar3" describe | cut -d- -f1)
git -C "$repos/rebar3" checkout --quiet "$rebar_vsn"
if [[ $ssl_rel -lt 2 ]]
then
ssl_vsn=$("$git_exe" -C "$repos/openssl" describe | cut -d- -f1)
git -C "$repos/openssl" checkout --quiet "$ssl_vsn"
ssl_vsn=${ssl_vsn#OpenSSL_}
ssl_vsn=${ssl_vsn//_/.}
else
ssl_vsn=$("$git_exe" -C "$repos/openssl" describe | cut -d- -f1,2)
git -C "$repos/openssl" checkout --quiet "$ssl_vsn"
ssl_vsn=${ssl_vsn#openssl-}
fi
#
# Figure out which app/switches to include/exclude
#
declare -a otp_apps
otp_apps+=( '--without-javac' )
otp_apps+=( '--without-jinterface' )
otp_apps+=( '--without-megaco' )
otp_apps+=( '--without-odbc' )
if [[ -d "$repos/otp/lib/hipe" ]]
then
otp_apps+=( '--disable-hipe' '--without-hipe' )
fi
# smoke tests use SNMP
if ! $run_tests
then
# if otp_mibs is present then os_mon requires it and hence snmp
if [[ ! -d "$repos/otp/lib/otp_mibs" ]]
then
otp_apps+=( '--without-snmp' )
fi
fi
if ! $with_wx
then
otp_apps+=( '--without-wx' )
otp_apps+=( '--without-debugger' )
otp_apps+=( '--without-et' )
otp_apps+=( '--without-observer' )
fi
echo
echo 'Building:'
echo " OTP version: $otp_vsn"
echo " OpenSSL version: $ssl_vsn"
echo " Rebar version: $rebar_vsn"
echo
#
# Purify the environment
#
unset AR ARFLAGS AS ASFLAGS CC CFLAGS CXX CXXFLAGS CPP CPPFLAGS \
CPPDEFINES CPPINCLUDES LD LDFLAGS LDLIBS MAKE MAKEFLAGS RANLIB RM
LANG=C
TERM=dumb
REBAR_COLOR=none
export LANG TERM REBAR_COLOR
case "$os_name" in
Darwin)
CC='/usr/bin/cc'
CXX='/usr/bin/c++'
cpx='cp -pX'
open_html='run open'
os_cflags="-mmacosx-version-min=$(
sw_vers -productVersion | cut -d. -f1).0"
os_makeflags="-j$(($(sysctl -n hw.ncpu) / 2))"
;;
Linux)
CC='/usr/bin/cc'
CXX='/usr/bin/c++'
cpx='cp -p'
open_html='true'
os_cflags=''
os_makeflags="-j$(($(nproc) - 1))"
;;
*)
CC='/usr/bin/cc'
CXX='/usr/bin/c++'
cpx='cp -p'
open_html='true'
os_cflags=''
os_makeflags="-j$(($(sysctl -n hw.ncpu) - 1))"
;;
esac
export CC CXX
#
# OpenSSL
#
run cd "$repos/openssl"
ssl_inst="$repos/openssl-inst"
export KERNEL_BITS=64
case $ssl_rel in
1)
run ./config "--prefix=$ssl_inst" "--openssldir=$ssl_inst/ssl" \
--release $os_cflags shared enable-ec_nistp_64_gcc_128
;;
3)
run ./Configure "--prefix=$ssl_inst" "--openssldir=$ssl_inst/ssl" \
--release $os_cflags no-makedepend no-module no-shared \
enable-ec_nistp_64_gcc_128
;;
*)
echo "${scr}: Unsupported OpenSSL release '${ssl_rel}'" >&2
exit 5
;;
esac
run make
if $run_tests ; then run make test ; fi
run make install_sw
unset KERNEL_BITS
#
# Erlang/OTP
#
run cd "$repos/otp"
declare -a otp_opts
otp_opts+=( '--enable-m64-build' )
otp_opts+=( "--with-ssl=$ssl_inst" )
otp_opts+=( '--disable-dynamic-ssl-lib' )
otp_opts+=( "${otp_apps[@]}" )
CFLAGS="-O3 -g0 -w $os_cflags"
$portable || CFLAGS+=" -march=native -mtune=native"
CXXFLAGS="$CFLAGS"
LDFLAGS="-O3 -g0 $os_cflags"
ERL_TOP="$repos/otp"
MAKE="$(type -P make)"
[[ "$($MAKE --version 2>/dev/null | head -1)" == GNU\ [Mm]ake\ * ]] \
|| MAKE="$(type -P gmake || echo gmake)"
MAKEFLAGS="$os_makeflags"
export CFLAGS CXXFLAGS LDFLAGS ERL_TOP MAKE MAKEFLAGS
[[ ! -d "$otp_dir" ]] || run /bin/rm -rf "$otp_dir"
run ./otp_build setup -a "--prefix=$otp_dir" "${otp_opts[@]}"
run $MAKE install
if $with_docs
then
export FOP_OPTS='-Xmx1024m'
# docs may fail if not building with wx
if run $MAKE docs install-docs DOC_TARGETS=html
then
run ln -s lib/erlang/doc/index.html "$otp_dir/index.html"
$open_html "$otp_dir/index.html" || true
fi
fi
#
# Rebar
#
run cd "$repos/rebar3"
path="$PATH"
export PATH="$ERL_TOP/bin:$path"
hash -r 2>/dev/null || true
run ./bootstrap
export PATH="$path"
hash -r 2>/dev/null || true
unset path
#
# Complete the install and archive it
#
run cd "$otp_dir"
# get the list of OTP executables *before* installing rebar
otp_exes=($(/bin/ls bin))
run install -m 0755 "$repos/rebar3/rebar3" bin
run install -m 0644 "$repos/otp/OTP_VERSION" .
cat <<EOF > manifest.mf
Erlang/OTP: $otp_vsn
OpenSSL: $ssl_vsn
Rebar: $rebar_vsn
EOF
chmod 0644 manifest.mf
cat <<EOF > relocate_here
#!/bin/bash -e
otp_dir="\$(cd "\$(dirname "\$0")" && pwd)"
"\$otp_dir/lib/erlang/Install" -minimal "\$otp_dir/lib/erlang" >/dev/null
bin_dir="\$otp_dir/bin"
cd "\$bin_dir"
for x in ${otp_exes[@]}
do
/bin/ln -sf "../lib/erlang/bin/\$x" .
done
# Make sure it works
./erl -noshell -eval 'erlang:halt(0).'
EOF
chmod 0755 relocate_here
cat <<EOF > activate
#!/usr/bin/false
_activate_dir="\$(cd "\$(dirname "\${BASH_SOURCE[0]}")" && pwd)"
otp_deactivate 2>/dev/null || true
[[ -z "\$_ACTIVE_OTP_PATH" ]] || PATH="\${PATH//\${_ACTIVE_OTP_PATH}:/}"
_ACTIVE_OTP_PATH="\$_activate_dir/bin"
PATH="\${_ACTIVE_OTP_PATH}:\${PATH//\${_ACTIVE_OTP_PATH}:/}"
export PATH _ACTIVE_OTP_PATH
otp_deactivate()
{
if [[ -n "\$_ACTIVE_OTP_PATH" ]]
then
export PATH="\${PATH//\${_ACTIVE_OTP_PATH}:/}"
unset _ACTIVE_OTP_PATH
hash -r 2>/dev/null || true
fi
unset -f otp_deactivate
}
unset _activate_dir
hash -r 2>/dev/null || true
EOF
chmod 0644 activate
run cd ..
run tar czf "${otp_dir}.tgz" "otp-${otp_rel}"
#
# Now we can test OTP and Rebar
#
if $run_tests
then
run cd "$ERL_TOP"
path="$PATH"
export PATH="$ERL_TOP/bin:$path"
hash -r 2>/dev/null || true
run $MAKE release_tests
run cd release/tests/test_server
run "$ERL_TOP/bin/erl" -s ts install -s ts smoke_test batch -s init stop
export PATH="$path"
hash -r 2>/dev/null || true
unset path
chmod -R +r .
run tar czf "${otp_dir}-smoke-tests.tgz" .
$open_html index.html
run cd "$repos/rebar3"
. "$otp_dir/activate"
REBAR3="$otp_dir/bin/rebar3"
REBAR="$REBAR3"
export REBAR REBAR3
run "$REBAR3" ct
run cd _build/test/logs
run tar czf "${otp_dir}-rebar-tests.tgz" .
$open_html index.html
fi
cd "$pwd"
$keep_bld || run /bin/rm -rf "$repos"
echo
echo done.
echo