summaryrefslogtreecommitdiff
path: root/otp_patch_apply
blob: 0127b96a73a9ec1015eab158ab2068a18e0f3ea2 (plain)
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
#!/bin/sh
#
# %CopyrightBegin%
#
# Copyright Ericsson AB 2014. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# %CopyrightEnd%
#

version="1.0.1"

force=
lib_path=
orig_dir=
sdir=
idir="/broken/path/here"
cleanup=no
install_docs=yes

invalid_src="does not seem to be a valid OTP source tree"
not_built="Source in has not been built"
doc_not_built="Documentation has not been built. Either build the
documentation and re-run 'otp_patch_apply', or re-run 'otp_patch_apply'
with the '-n' switch."

print_usage()
{
    cat <<EOF
otp_patch_apply -s <Dir> -i <Dir> [-l <Dir>] [-c] [-f] [-h] [-n] [-v] \\
                <App1> [... <AppN>]

  -s <Dir>  -- OTP source directory that contain build results.
  -i <Dir>  -- OTP installation directory to patch.
  -l <Dir>  -- Alternative OTP source library directory path(s) containing
               build results of OTP applications. Multiple paths should be
               colon separated.
  -c        -- Cleanup (remove) old versions of applications patched
               in the installation.
  -f        -- Force patch of application(s) even though dependencies are
               not fullfilled.
  -h        -- Print this help then exit.
  -n        -- Do not install documentation.
  -v        -- Print version then exit.
  <AppX>    -- Application to patch.

Environment Variable:
  ERL_LIBS  -- Alternative OTP source library directory path(s) containing
               build results of OTP applications. Multiple paths should be
               colon separated.

NOTE:
  * Complete build environment is required while running otp_patch_apply.
  * Before applying a patch you need to build all of OTP in the source
    directory.
  * All source directories identified by -s and -l should contain build
    results of OTP applications.

Version: $version

EOF

}

error()
{
    echo "ERROR:" "$@" 1>&2
    exit 1
}

usage_error()
{
    echo "ERROR:" "$@" 1>&2
    echo "" 1>&2
    print_usage 1>&2
    exit 1
}

usage()
{
    print_usage
    exit 0
}

alt_lib_path()
{
    app=$1
    save_ifs=$IFS
    IFS=:

    cd "$orig_dir" || error "Cannot change directory to $orig_dir"

    for lib in $lib_path; do
	# Want absolute path
	case "$lib" in
	    /*)
		;;
	    *)
		cd "$lib" || error "Cannot change directory to $lib"
		lib=`pwd`
		cd "$orig_dir" || error "Cannot change directory to $orig_dir"
	esac
	if [ -d "$lib/$app" ]; then
	    echo "$lib/$app"
	    IFS=$save_ifs
	    return 0
	fi
    done

    IFS=$save_ifs

    return 1
}

prog_in_mod_path()
{
    chk_path="/bin:$PATH"
    PROG=$1
    save_ifs=$IFS
    IFS=:
    if [ "X$TARGET" = "Xwin32" ]; then
	for p in $chk_path; do
	    if [ -f "$p/$PROG.exe" ]; then
		IFS=$save_ifs
		echo "$p/$PROG.exe"
		return 0
	    fi
	done
    else
	for p in $chk_path; do
	    if [ -x "$p/$PROG" ]; then
		IFS=$save_ifs
		echo "$p/$PROG"
		return 0
	    fi
	done
    fi
    IFS=$save_ifs
    return 1
}

find_prog()
{
    prog_in_mod_path "$1"
    if [ $? -ne 0 ]; then
	echo "$1"
    fi
    return 0
}

# Parse arguments

while [ $# -gt 0 ]; do
    case "$1" in
    	"-s")
	    shift
	    if [ ! $# -gt 0 ]; then
		usage_error "Missing OTP source directory"
	    fi
	    sdir="$1";;
    	"-i")
	    shift
	    if [ ! $# -gt 0 ]; then
		usage_error "Missing OTP install directory"
	    fi
	    idir="$1";;
    	"-l")
	    shift
	    if [ ! $# -gt 0 ]; then
		usage_error "Missing OTP library directory"
	    fi
	    if [ "x$lib_path" = "x" ]; then
		lib_path="$1"
	    else
		lib_path="$lib_path:$1"
	    fi;;
    	"-f")
	    force="-force";;
    	"-c")
	    cleanup=yes;;
	"-h")
	    usage;;
	"-n")
	    install_docs=no;;
	"-v")
	    echo "otp_patch_apply version $version"
	    exit 0;;
	*)
	    app="$1"
	    applications="$applications $app";;
    esac
    shift
done

# Check that we got mandatory arguments
test "x$sdir" != "x" || usage_error "Missing OTP source directory"
test "x$idir" != "x" || usage_error "Missing OTP install directory"
test "x$applications" != "x" || usage_error "Missing applications"

orig_dir=`pwd`

# Check that the source directory seems sane
cd "$sdir" 2>/dev/null || error "Cannot change directory to $sdir"

# Want absolute path
case "$sdir" in
    /*) ;;
    *) sdir=`pwd`;;
esac

export ERL_TOP="$sdir"
test -f "$sdir/otp_build" || error "$ERL_TOP" $invalid_src
test -f "$sdir/OTP_VERSION" || error "$ERL_TOP" $invalid_src
test -f "$sdir/otp_versions.table" || error "$ERL_TOP" $invalid_src
test -f "$sdir/erts/autoconf/config.guess" || error "$ERL_TOP" $invalid_src
test -f "$sdir/make/verify_runtime_dependencies" || error "$ERL_TOP" $invalid_src
test -x "$sdir/bootstrap/bin/erl" || error $not_built
test -x "$sdir/bootstrap/bin/erlc" || error $not_built
test -x "$sdir/bootstrap/bin/escript" || error $not_built
test -f "$sdir/make/otp_built" || error $not_built

if [ $install_docs = yes ]; then
    test -f "$sdir/make/otp_doc_built" || usage_error $doc_not_built
fi

otp_rel=`sed 's|\([0-9]*\).*|\1|' < $ERL_TOP/OTP_VERSION` || \
    error "Failed to read $ERL_TOP/OTP_VERSION"

case "$otp_rel" in
    1[7-9]|[2-9][0-9]) ;; # ok; release 17-99
    *) error "Invalid OTP release: $otp_rel";;
esac

export PATH="$ERL_TOP/bootstrap/bin:$PATH"
erlc="$ERL_TOP/bootstrap/bin/erlc"
erl="$ERL_TOP/bootstrap/bin/erl"

erl_otp_rel=`$erl -noshell -noinput -eval "io:format(\"~s~n\", [erlang:system_info(otp_release)]), erlang:halt(0)"` || \
    error "Failed to execute: $erl"

test "$otp_rel" = "$erl_otp_rel" || error "Inconsistent source: $sdir"

app_dirs=
for app in $applications; do
    case "$app" in
	"erts")
	    dir="$ERL_TOP/erts";;
	*)
	    dir="$ERL_TOP/lib/$app";;
    esac
    if [ ! -d "$dir" ]; then
	dir=`alt_lib_path "$app"`
	if [ $? -ne 0 ]; then
	    error "Application missing in source: $app"
	fi
    fi
    app_dirs="$app_dirs $dir"
done

cd "$orig_dir" 2>/dev/null || error "Cannot change directory to $orig_dir"

# Check that the install directory seems sane
cd "$idir" 2>/dev/null || error "Cannot change directory to $idir"

# Want absolute path
case "$idir" in
    /*) ;;
    *) idir=`pwd`;;
esac

test -d "$idir/releases/$otp_rel" || \
    error "No OTP-$otp_rel installation present in $idir"

cd "$ERL_TOP" 2>/dev/null || error "Cannot change directory to $ERL_TOP"

# Some tools we use
rm=`find_prog rm`
rmdir=`find_prog rmdir`
cp=`find_prog cp`
mv=`find_prog mv`
mkdir=`find_prog mkdir`

# Setup build stuff
if [ "x$TARGET" = "x" ]; then
    TARGET=`$ERL_TOP/erts/autoconf/config.guess`
fi
BUILDSYS=$TARGET
if [ -z "$MAKE" ]; then
    case $TARGET in
	win32)
	    MAKE=make;;
	*)
	    prog_in_mod_path gmake >/dev/null
	    if [ $? -eq 0 ]; then
		MAKE=gmake
	    else
		MAKE=make
	    fi;;
    esac
fi
if [ X`$MAKE is_cross_configured` = Xyes ]; then
    TARGET=`$MAKE target_configured`
elif [ "x$OVERRIDE_TARGET" != "x" -a "x$OVERRIDE_TARGET" != "xwin32" ]; then
    TARGET=$OVERRIDE_TARGET
fi

# Check for cleanup
inst_app_vers="$idir/releases/$otp_rel/installed_application_versions"
rm_app_vers=
if [ $cleanup = yes ]; then
    $mv "$inst_app_vers" "${inst_app_vers}.save" || \
	error "Failed to save $inst_app_vers"
    for app in $applications; do
	tmp=`grep "$app-*" "${inst_app_vers}.save"`
	rm_app_vers="$rm_app_vers $tmp"
    done
    $cp "${inst_app_vers}.save" "$inst_app_vers"
    for rm_app_ver in $rm_app_vers; do
	$cp "$inst_app_vers" "${inst_app_vers}.tmp"
	grep -v $rm_app_ver "${inst_app_vers}.tmp" > "$inst_app_vers"
    done
    $rm -f "${inst_app_vers}.tmp"
fi

# Verify runtime dependencies
$ERL_TOP/make/verify_runtime_dependencies -release "$otp_rel" \
    -source "$ERL_TOP" -target "$idir" $force $applications || {
    test ! -f  "${inst_app_vers}.save" || \
	$mv "${inst_app_vers}.save" "$inst_app_vers"
    exit 1
}

# Update OTP_VERSION in installation
otp_version=`cat "$idir/releases/$otp_rel/OTP_VERSION"` || {
    test ! -f  "${inst_app_vers}.save" || \
	$mv "${inst_app_vers}.save" "$inst_app_vers"
    error "Not able to read $idir/releases/$otp_rel/OTP_VERSION"
}

{
    echo "$otp_version" | sed "s|^\([^\*]*\)\**|\1\*\*|g" > \
	"$idir/releases/$otp_rel/OTP_VERSION"
} 2>/dev/null || {
    test ! -f  "${inst_app_vers}.save" || \
	$mv "${inst_app_vers}.save" "$inst_app_vers"
    error "Not able to update $idir/releases/$otp_rel/OTP_VERSION"
}

# Do actual cleanup
if [ "x$rm_app_vers" != "x" ]; then
    for app_ver in $rm_app_vers; do
	case x"$app_ver" in
	    x)
		;;
	    xerts-*)
		$rm -rf "$idir/$app_ver" ;;
	    x*)
		$rm -rf "$idir/lib/$app_ver" ;;
	esac
    done
    $rm -f "${inst_app_vers}.save"
fi

# Install application from built source
for app_dir in $app_dirs; do
    (cd "$app_dir" && \
	$MAKE MAKE="$MAKE" TARGET=$TARGET RELEASE_ROOT="$idir" \
	RELEASE_PATH="$idir" TESTROOT="$idir" release) || exit 1
done

if [ $install_docs = yes ]; then
# Documentation have been built and should be installed

    for app_dir in $app_dirs; do
	(cd "$app_dir" && \
	    $MAKE MAKE="$MAKE" RELEASE_ROOT="$idir" RELEASE_PATH="$idir" \
	    TESTROOT="$idir" release_docs) || exit 1
    done

    (cd "$sdir/system/doc/top" && $MAKE clean)

    (cd "$sdir/system/doc/top" && \
	$MAKE MAKE="$MAKE" RELEASE_ROOT="$idir" RELEASE_PATH="$idir" \
	TESTROOT="$idir" release_docs) || exit 1

    echo ""
    echo "*"
    echo "* NOTE! In order to update pre-formatted man pages you"
    echo "*       need to run the 'Install' script located in:"
    echo "*       $idir"
    echo "*"
fi 

# If erts, kernel, stdlib or sasl is included, find versions
for app in $applications; do
    case "$app" in
	erts)
	    erts_vsn=`grep '^VSN' erts/vsn.mk | sed "s|^VSN.*=[^0-9]*\([0-9].*\)$|\1|g"`
	    update_rel=true;;
	kernel)
	    kernel_vsn=`sed "s|^KERNEL_VSN[^=]*=[^0-9]*\([0-9].*\)$|\1|g" lib/kernel/vsn.mk`
	    update_rel=true;;
	stdlib)
	    stdlib_vsn=`sed "s|^STDLIB_VSN[^=]*=[^0-9]*\([0-9].*\)$|\1|g" lib/stdlib/vsn.mk`
	    update_rel=true;;
	sasl)
	    sasl_vsn=`sed "s|^SASL_VSN[^=]*=[^0-9]*\([0-9].*\)$|\1|g" lib/sasl/vsn.mk`
	    update_rel=true;;
	*)
	    ;;
    esac
done

# and find the old versions for those not included
if [ "X$update_rel" != "X" ]; then
    if [ "X$erts_vsn" = "X" ]; then
	erts_vsns=`ls -d "$idir"/erts-* | sed "s|$idir/erts-\([0-9\.].*\)|\1|g"`
	erts_vsn=`echo "$erts_vsns" | sort -t '.' -g | tail -n 1`
    fi
    if [ "X$kernel_vsn" = "X" ]; then
	kernel_vsns=`ls -d "$idir"/lib/kernel-* | sed "s|$idir/lib/kernel-\([0-9\.].*\)|\1|g"`
	kernel_vsn=`echo "$kernel_vsns" | sort -t '.' -g | tail -n 1`
    fi
    if [ "X$stdlib_vsn" = "X" ]; then
	stdlib_vsns=`ls -d "$idir"/lib/stdlib-* | sed "s|$idir/lib/stdlib-\([0-9\.].*\)|\1|g"`
	stdlib_vsn=`echo "$stdlib_vsns" | sort -t '.' -g | tail -n 1`
    fi
    if [ "X$sasl_vsn" = "X" ]; then
	sasl_vsns=`ls -d "$idir"/lib/sasl-* | sed "s|$idir/lib/sasl-\([0-9\.].*\)|\1|g"`
	sasl_vsn=`echo "$sasl_vsns" | sort -t '.' -g | tail -n 1`
    fi

    # Generate .rel, .script and .boot - to tmp dir
    start_clean="{release, {\"Erlang/OTP\",\"$otp_rel\"}, {erts, \"$erts_vsn\"}, [{kernel,\"$kernel_vsn\"},  {stdlib,\"$stdlib_vsn\"}]}."
    start_sasl="{release, {\"Erlang/OTP\",\"$otp_rel\"}, {erts, \"$erts_vsn\"}, [{kernel,\"$kernel_vsn\"},  {stdlib,\"$stdlib_vsn\"},  {sasl,\"$sasl_vsn\"}]}."

    tmp_dir="$idir/tmp";
    if [ ! -d "$tmp_dir" ]; then
	$mkdir "$tmp_dir"
    fi
    echo "$start_sasl" > "$tmp_dir/start_sasl.rel"
    echo "$start_clean" > "$tmp_dir/start_clean.rel"
    echo "$start_clean" > "$tmp_dir/no_dot_erlang.rel"

    $erlc -I"$idir"/lib/*/ebin -o"$tmp_dir" "$tmp_dir/start_sasl.rel" || exit 1
    $erlc -I"$idir"/lib/*/ebin -o"$tmp_dir" +no_warn_sasl "$tmp_dir/start_clean.rel" || exit 1
    $erlc -I"$idir"/lib/*/ebin -o"$tmp_dir" +no_warn_sasl +no_dot_erlang "$tmp_dir/no_dot_erlang.rel" || exit 1

    # Generate RELEASES file
    "$erl" -noinput +B -eval "release_handler:create_RELEASES(\"%ERL_ROOT%\", \"$tmp_dir\", \"$tmp_dir/start_sasl.rel\", []), halt()" || exit 1

    # If all good so far, move generated files into target area
    $mv "$tmp_dir/RELEASES" "$idir/releases/RELEASES.src"
    $mv "$tmp_dir"/* "$idir/releases/$otp_rel"
    $rmdir "$tmp_dir"

    # Remove old start scripts (forces a new run of Install)
    $rm -f "$idir"/releases/RELEASES
    $rm -f "$idir"/bin/*.script
    $rm -f "$idir"/bin/*.boot
    $rm -f "$idir"/bin/erl

    echo ""
    echo "*"
    echo "* NOTE! In order to get a runnable OTP system again you"
    echo "*       need to run the 'Install' script located in:"
    echo "*       $idir"
    echo "*"
fi