forked from AdoptOpenJDK/openjdk-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slim-java.sh
executable file
·345 lines (299 loc) · 9.26 KB
/
slim-java.sh
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
#!/usr/bin/env bash
#
# 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
#
# https://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.
#
set -o pipefail
# Parse arguments
argc=$#
if [ ${argc} != 1 ]; then
message=$(basename "$0")
echo " Usage: ${message} Full-JDK-path"
exit 1
fi
# Which major java version
function get_java_version() {
# Only version 8 has the 1.8.0 format, all others have x.y.z-nnn where x is the major version
jver_string="$(java -version 2>&1 | grep "^openjdk version" | awk '{ print $3 }')"
ver_string="$(echo "${jver_string}" | awk -F'.' '{ print $1 }' | awk -F'"' '{ print $2 }')"
case "${ver_string}" in
1)
java_major_version="8";
;;
*)
java_major_version="${ver_string}";
;;
esac
}
# Set the java major version that we are on right now.
get_java_version
# Validate prerequisites(tools) necessary for making a slim build
if [ "${java_major_version}" -ge 14 ]; then
tools="jar jarsigner strip"
else
tools="jar jarsigner pack200 strip"
fi
for tool in ${tools};
do
command -v "${tool}" >/dev/null 2>&1 || { echo >&2 "${tool} not found, please add ${tool} into PATH"; exit 1; }
done
# Set input of this script
src="$1"
# Store necessary directories paths
basedir=$(dirname "${src}")
scriptdir=$(dirname "$0")
target="${basedir}"/slim
# Files for Keep and Del list of classes in rt.jar
keep_list="${scriptdir}/slim-java_rtjar_keep.list"
del_list="${scriptdir}/slim-java_rtjar_del.list"
# jmod files to be deleted
del_jmod_list="${scriptdir}/slim-java_jmod_del.list"
# bin files to be deleted
del_bin_list="${scriptdir}/slim-java_bin_del.list"
# lib files to be deleted
del_lib_list="${scriptdir}/slim-java_lib_del.list"
# We only support 64 bit builds now
proc_type="64bit"
# Find the arch specific dir in jre/lib based on current arch
function parse_platform_specific() {
arch_info=$(uname -m)
case "${arch_info}" in
aarch64)
echo "aarch64";
;;
ppc64el|ppc64le)
echo "ppc64le";
;;
s390x)
echo "s390x";
;;
amd64|x86_64)
echo "amd64";
;;
*)
echo "ERROR: Unknown platform";
exit 1;
;;
esac
}
# Which vm implementation are we running on at the moment.
function get_vm_impl() {
impl="$(java -version 2>&1 | grep "OpenJ9")";
if [ -n "${impl}" ]; then
echo "OpenJ9";
else
echo "Hotspot";
fi
}
# Strip debug symbols from the given jar file.
function strip_debug_from_jar() {
jar=$1
isSigned=$(jarsigner -verify "${jar}" | grep 'jar verified')
if [ "${isSigned}" == "" ]; then
echo " Stripping debug info in ${jar}"
pack200 --repack --strip-debug -J-Xmx1024m "${jar}".new "${jar}"
mv "${jar}".new "${jar}"
fi
}
# Trim the files in jre/lib dir
function jre_lib_files() {
local vm_impl=$(get_vm_impl)
echo -n "INFO: Trimming jre/lib dir..."
pushd "${target}"/jre/lib >/dev/null || return
rm -rf applet/ boot/ ddr/ deploy desktop/ endorsed/
rm -rf images/icons/ locale/ oblique-fonts/ security/javaws.policy aggressive.jar deploy.jar javaws.jar jexec jlm.src.jar plugin.jar
pushd ext/ >/dev/null || return
rm -f dnsns.jar dtfj*.jar nashorn.jar traceformat.jar
popd >/dev/null || return
# Derive arch from current platorm.
lib_arch_dir=$(parse_platform_specific)
if [ -d "${lib_arch_dir}" ]; then
pushd "${lib_arch_dir}" >/dev/null || return
rm -rf classic/ libdeploy.so libjavaplugin_* libjsoundalsa.so libnpjp2.so libsplashscreen.so
# Only remove the default dir for 64bit versions and for hotspot
if [[ "${proc_type}" == "64bit" && "${vm_impl}" != "OpenJ9" ]]; then
rm -rf default/
fi
popd >/dev/null || return
fi
popd >/dev/null || return
echo "done"
}
# Trim the files in the jre dir
function jre_files() {
echo -n "INFO: Trimming jre dir..."
pushd "${target}"/jre >/dev/null || return
rm -f ASSEMBLY_EXCEPTION LICENSE THIRD_PARTY_README
rm -rf bin
ln -s ../bin bin
popd >/dev/null || return
echo "done"
}
# Exclude the zOS specific charsets
function charset_files() {
# 2.3 Special treat for removing ZOS specific charsets
echo -n "INFO: Trimming charsets..."
mkdir -p "${root}"/charsets_class
pushd "${root}"/charsets_class >/dev/null || return
jar -xf "${root}"/jre/lib/charsets.jar
ibmEbcdic="290 300 833 834 838 918 930 933 935 937 939 1025 1026 1046 1047 1097 1112 1122 1123 1364"
# Generate sfj-excludes-charsets list as well. (OpenJ9 expects the file to be named sfj-excludes-charsets).
[ ! -e "${root}"/jre/lib/slim/sun/nio/cs/ext/sfj-excludes-charsets ] || rm -rf "${root}"/jre/lib/sfj/sun/nio/cs/ext/sfj-excludes-charsets
exclude_charsets=""
for charset in ${ibmEbcdic};
do
rm -f sun/nio/cs/ext/IBM"${charset}".class
rm -f sun/nio/cs/ext/IBM"${charset}"\$*.class
exclude_charsets="${exclude_charsets} IBM${charset}"
done
mkdir -p "${root}"/jre/lib/slim/sun/nio/cs/ext
echo "${exclude_charsets}" > "${root}"/jre/lib/slim/sun/nio/cs/ext/sfj-excludes-charsets
cp "${root}"/jre/lib/slim/sun/nio/cs/ext/sfj-excludes-charsets sun/nio/cs/ext/
jar -cfm "${root}"/jre/lib/charsets.jar META-INF/MANIFEST.MF ./*
popd >/dev/null || return
rm -rf "${root}"/charsets_class
echo "done"
}
# Trim the rt.jar classes. The classes deleted are as per slim-java_rtjar_del.list
function rt_jar_classes() {
# 2.4 Remove classes in rt.jar
echo -n "INFO: Trimming classes in rt.jar..."
mkdir -p "${root}"/rt_class
pushd "${root}"/rt_class >/dev/null || return
jar -xf "${root}"/jre/lib/rt.jar
mkdir -p "${root}"/rt_keep_class
grep -v '^#' < "${keep_list}" | while IFS= read -r class
do
cp --parents "${class}".class "${root}"/rt_keep_class/ >null 2>&1
cp --parents "${class}"\$*.class "${root}"/rt_keep_class/ >null 2>&1
done
grep -v '^#' < "${del_list}" | while IFS= read -r class
do
rm -rf "${class}"
done
cp -rf "${root}"/rt_keep_class/* ./
rm -rf "${root}"/rt_keep_class
# 2.5. Restruct rt.jar
jar -cfm "${root}"/jre/lib/rt.jar META-INF/MANIFEST.MF ./*
popd >/dev/null || return
rm -rf rt_class
echo "done"
}
# Strip the debug info from all jar files
function strip_jar() {
# pack200 is not available from Java 14 onwards
if [ "${java_major_version}" -ge 14 ]; then
return;
fi
# Using pack200 to strip debug info in jars
echo "INFO: Strip debug info from jar files"
list=$(find . -name "*.jar")
for jar in ${list};
do
strip_debug_from_jar "${jar}"
done
}
# Strip debug information from share libraries
function strip_bin() {
echo -n "INFO: Stripping debug info in object files..."
find bin -type f ! -path "./*"/java-rmi.cgi -exec strip -s {} \;
find . -name "*.so*" -exec strip -s {} \;
find . -name jexec -exec strip -s {} \;
echo "done"
}
# Remove all debuginfo files
function debuginfo_files() {
echo -n "INFO: Removing all .debuginfo files..."
find . -name "*.debuginfo" -exec rm -f {} \;
echo "done"
}
# Remove all src.zip files
function srczip_files() {
echo -n "INFO: Removing all src.zip files..."
find . -name "*src*zip" -exec rm -f {} \;
echo "done"
}
# Remove unnecessary jmod files
function jmod_files() {
if [ ! -d "${target}"/jmods ]; then
return;
fi
pushd "${target}"/jmods >/dev/null || return
grep -v '^#' < "${del_jmod_list}" | while IFS= read -r jfile
do
rm -rf "${jfile}"
done
popd >/dev/null || return
}
# Remove unnecessary tools
function bin_files() {
echo -n "INFO: Trimming bin dir..."
pushd "${target}"/bin >/dev/null || return
grep -v '^#' < "${del_bin_list}" | while IFS= read -r binfile
do
rm -rf "${binfile}"
done
popd >/dev/null || return
}
# Remove unnecessary tools and jars from lib dir
function lib_files() {
echo -n "INFO: Trimming bin dir..."
pushd "${target}"/lib >/dev/null || return
grep -v '^#' < "${del_lib_list}" | while IFS= read -r libfile
do
rm -rf "${libfile}"
done
popd >/dev/null || return
}
# Create a new target directory and copy over the source contents.
cd "${basedir}" || exit
mkdir -p "${target}"
echo "Copying ${src} to ${target}..."
cp -rf "${src}"/* "${target}"/
pushd "${target}" >/dev/null || exit
root=$(pwd)
echo "Trimming files..."
# Remove examples documentation and sources.
rm -rf demo/ sample/ man/
# jre dir may not be present on all builds.
if [ -d "${target}"/jre ]; then
# Trim file in jre dir.
jre_files
# Trim file in jre/lib dir.
jre_lib_files
# Remove IBM zOS charset files.
# This needs extra code in sun/nio/cs/ext/ExtendedCharsets.class to
# ignore the charset files that are removed. Disabling for now until
# this gets added in the upstream openjdk project.
# charset_files
# Trim unneeded rt.jar classes.
rt_jar_classes
fi
# Strip all remaining jar files of debug info.
strip_jar
# Strip object files of debug info.
strip_bin
# Remove all debuginfo files
debuginfo_files
# Remove all src.zip files
srczip_files
# Remove unnecessary jmod files
jmod_files
# Remove unnecessary tools and jars from lib dir
lib_files
# Remove unnecessary tools
bin_files
# Remove temp folders
rm -rf "${root}"/jre/lib/slim "${src}"
popd >/dev/null || exit
mv "${target}" "${src}"
echo "Done"