forked from AdoptOpenJDK/openjdk-docker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
slim-java.ps1
377 lines (334 loc) · 10.3 KB
/
slim-java.ps1
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
#
# 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.
#
[CmdletBinding()]
Param(
[Parameter(Position=0, Mandatory=$true)]
[String]
$Src
)
$ErrorActionPreference = "Stop"
$env:PATH = "$(Join-Path $Src 'bin');${env:PATH}"
$pwd = Get-Location
# Which major java version
function Get-JavaVersion() {
$verstring = (Get-Command java.exe | Select-Object -ExpandProperty Version).ToString()
switch -wildcard ($verstring) {
"8.*" { return 8; break }
"9.*" { return 9; break }
"10*" { return 10; break }
"11*" { return 11; break }
"12*" { return 12; break }
"13*" { return 13; break }
"14*" { return 14; break }
"15*" { return 14; break }
"16*" { return 14; break }
default {
Write-Error "Unknown Java Version: $verstring"
exit 1
}
}
}
function Test-CommandExists($command) {
$oldPreference = $ErrorActionPreference
$ErrorActionPreference = 'stop'
$res = $false
try {
if(Get-Command $command) {
$res = $true
}
} catch {
$res = $false
} finally {
$ErrorActionPreference=$oldPreference
}
return $res
}
# Set the java major version that we are on right now.
$java_major_version = Get-JavaVersion
# Validate prerequisites(tools) necessary for making a slim build
$tools = @("jar", "jarsigner")
if($java_major_version -lt 14) {
$tools += "pack200"
}
foreach($tool in $tools) {
if(-not (Test-CommandExists $tool)) {
Write-Error "${tool} not found, please add ${tool} into PATH"
exit 1
}
}
# Store necessary directories paths
$basedir=Split-Path -Parent $Src
$target=Join-Path $basedir "slim"
# Files for Keep and Del list of classes in rt.jar
$keep_list=Join-Path $PSScriptRoot "slim-java_rtjar_keep.list"
$del_list=Join-Path $PSScriptRoot "slim-java_rtjar_del.list"
# jmod files to be deleted
$del_jmod_list=Join-Path $PSScriptRoot "slim-java_jmod_del.list"
# bin files to be deleted
$del_bin_list=Join-Path $PSScriptRoot "slim-java_bin_del.list"
# lib files to be deleted
$del_lib_list=Join-Path $PSScriptRoot "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-PlatformSpecific() {
return "amd64"
}
# Which vm implementation are we running on at the moment.
function Get-VMImplementation() {
$impl=& cmd /c "java -version 2>&1"
if($impl -match "OpenJ9") {
return "OpenJ9"
}
return "Hotspot"
}
# Strip debug symbols from the given jar file.
function Strip-DebugFromJar([string] $jar) {
$isSigned=(jarsigner -verify "${jar}" | Out-String).Contains('jar verified')
if(!$isSigned) {
Write-Host " Stripping debug info in ${jar}"
pack200 --repack --strip-debug -J-Xmx1024m "${jar}.new" "${jar}"
Move-Item -Path "${jar}.new" -Destination "${jar}" -Force
}
}
# Trim the files in jre/lib dir
function Trim-JRELibFiles() {
Write-Host -NoNewline "INFO: Trimming jre/lib dir..."
$path = Join-Path $target "jre/lib"
if(Test-Path $path) {
Push-Location $path
try {
@("applet", "boot", "ddr", "deploy", "destkop", "endorsed", "images/icons", "locale", "oblique-fonts", "security/javaws.policy", "aggressive.jar", "deploy.jar", "javaws.jar", "jexec", "jlm.src.jar", "plugin.jar") | ForEach-Object {
if(Test-Path $_) {
Remove-Item -Force -Recurse $_
}
}
Push-Location "ext"
try {
@("dnsns.jar", "dtfj*.jar", "nashorn.jar", "traceformat.jar") | ForEach-Object {
if($_ -match '\*') {
Remove-Item -Force -Recurse -Path .\* -Include $_
} elseif(Test-Path $_) {
Remove-Item -Force -Recurse -Path $_
}
}
} finally {
Pop-Location
}
# Derive arch from current platorm.
$lib_arch_dir=Parse-PlatformSpecific
if(Test-Path $lib_arch_dir) {
Push-Location $lib_arch_dir
try {
#rm -rf classic/ libdeploy.so libjavaplugin_* libjsoundalsa.so libnpjp2.so libsplashscreen.so
# Only remove the default dir for 64bit versions
if($proc_type -eq "64bit" -and (Test-Path "default")) {
Remove-Item -Recurse -Force "default"
}
} finally {
Pop-Location
}
}
} finally {
Pop-Location
}
}
Write-Host "done"
}
# Trim the files in the jre dir
function Trim-JREFiles() {
Write-Host -NoNewline "INFO: Trimming jre dir..."
$path = Join-Path $target "jre"
if(Test-Path $path) {
Push-Location $path
try {
@("ASSEMBLY_EXCEPTION", "LICENSE", "THIRD_PARTY_README") | ForEach-Object {
if(Test-Path $_) {
Remove-Item -Force -Recurse $_
}
}
# rm -rf bin
# ln -s ../bin bin
} finally {
Pop-Location
}
}
Write-Host "done"
}
# Trim the rt.jar classes. The classes deleted are as per slim-java_rtjar_del.list
function Trim-RTJarClasses() {
# 2.4 Remove classes in rt.jar
Write-Host -NoNewline "INFO: Trimming classes in rt.jar..."
$path = Join-Path $root "rt_class"
New-Item -ItemType Directory -Path $path -Force | Out-Null
Push-Location $path
try {
jar -xf (Join-Path $root "jre/lib/rt.jar")
New-Item -ItemType Directory -Path (Join-Path $root "rt_keep_class") -Force | Out-Null
if(Test-Path $keep_list) {
Get-Content $keep_list | Where-Object { $_ -notmatch '^#' -and ![string]::IsNullOrWhitespace($_) } | ForEach-Object {
if(Test-Path "${_}.class") {
Copy-Item -Path "${_}.class" -Recurse -Destination (Join-Path $root "rt_keep_class")
}
}
}
if(Test-Path $del_list) {
Get-Content $del_list | Where-Object { $_ -notmatch '^#' -and ![string]::IsNullOrWhitespace($_) } | ForEach-Object {
if(Test-Path $_) {
Remove-Item -Force -Recurse -Path $_
}
}
}
Copy-Item -Recurse -Force -Path "$(Join-Path $root "rt_keep_class")/*" -Destination .
Remove-Item -Force -Recurse -Path (Join-Path $root "rt_keep_class")
# 2.5. Restruct rt.jar
jar -cfm (Join-Path $root "jre\lib\rt.jar") META-INF/MANIFEST.MF ./*
} finally {
Pop-Location
}
if(Test-Path $path) {
Remove-Item -Force -Recurse $path
}
Write-Host "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) {
return
}
# Using pack200 to strip debug info in jars
Write-Host "INFO: Strip debug info from jar files"
Get-ChildItem -Recurse -Include *.jar -Path .\* | ForEach-Object {
Strip-DebugFromJar $_
}
}
# Remove all debuginfo files
function Remove-DebugInfoFiles() {
Write-Host -NoNewline "INFO: Removing all .debuginfo files..."
Get-ChildItem -Path .\* -Include *.debuginfo -Recurse | ForEach-Object {
Remove-Item -Force $_.FullName
}
Write-Host "done"
}
# Remove all src.zip files
function Remove-SrcZipFiles() {
Write-Host -NoNewline "INFO: Removing all src.zip files..."
Get-ChildItem -Path .\* -Include *src*zip -Recurse | ForEach-Object {
Remove-Item -Force $_.FullName
}
Write-Host "done"
}
# Remove unnecessary jmod files
function Remove-JmodFiles() {
$path = Join-Path $target "jmods"
if(Test-Path $path) {
Push-Location $path
try {
if(Test-Path $del_jmod_list) {
Get-Content $del_jmod_list | Where-Object { $_ -notmatch '^#' -and ![String]::IsNullOrWhitespace($_) } | ForEach-Object {
if(Test-Path $_) {
Remove-Item -Recurse -Force $_
}
}
}
} finally {
Pop-Location
}
}
}
# Remove unnecessary tools
function Remove-BinFiles() {
Write-Host -NoNewline "INFO: Trimming bin dir..."
$path = Join-Path $target "bin"
if(Test-Path $path) {
Push-Location $path
try {
Get-Content $del_bin_list | Where-Object { $_ -notmatch '^#' -and ![String]::IsNullOrWhitespace($_) } | ForEach-Object {
if(Test-Path $_) {
Remove-Item -Recurse -Force $_
}
}
} finally {
Pop-Location
}
}
Write-Host "done"
}
# Remove unnecessary tools and jars from lib dir
function Remove-LibFiles() {
Write-Host -NoNewline "INFO: Trimming lib dir..."
$path = Join-Path $target "lib"
if(Test-Path $path) {
Push-Location $path
try {
Get-Content $del_lib_list | Where-Object { $_ -notmatch '^#' -and ![String]::IsNullOrWhitespace($_) } | ForEach-Object {
if(Test-Path $_) {
Remove-Item -Recurse -Force $_
}
}
} finally {
Pop-Location
}
}
Write-Host "done"
}
# Create a new target directory and copy over the source contents.
Set-Location $basedir
New-Item -ItemType Directory $target -Force | Out-Null
Write-Host "Copying ${src} to ${target}..."
Copy-Item -Recurse -Force -Path $src\* -Destination $target
Push-Location $target
try{
$root=Get-Location
Write-Host "Trimming files..."
# Remove examples documentation and sources.
@("demo", "sample", "man") | ForEach-Object {
if(Test-Path $_) {
Remove-Item -Recurse -Force -Path $_
}
}
# jre dir may not be present on all builds.
if(Test-Path (Join-Path $target "jre")) {
# Trim file in jre dir.
Trim-JREFiles
# Trim file in jre/lib dir.
Trim-JRELibFiles
# Trim unneeded rt.jar classes.
Trim-RTJarClasses
}
# Strip all remaining jar files of debug info.
Strip-Jar
# Remove all debuginfo files
Remove-DebugInfoFiles
# Remove all src.zip files
Remove-SrcZipFiles
# Remove unnecessary jmod files
Remove-JmodFiles
# Remove unnecessary tools and jars from lib dir
Remove-LibFiles
# Remove unnecessary tools
Remove-BinFiles
# Remove temp folders
@((Join-Path $root "jre\lib\slim"), $src) | ForEach-Object {
if(Test-Path $_) {
Remove-Item -Force -Recurse $_
}
}
} finally {
Pop-Location
}
Move-Item -Path $target -Destination $src -Force
Set-Location $pwd
Write-Host "Done"