Skip to content

Commit df0165b

Browse files
ammbrajaikiran
authored andcommitted
8321139: jlink's compression plugin doesn't handle -c option correctly
Reviewed-by: jpai, alanb
1 parent 16699a3 commit df0165b

File tree

6 files changed

+106
-25
lines changed

6 files changed

+106
-25
lines changed

src/jdk.jlink/share/classes/jdk/tools/jlink/internal/TaskHelper.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,13 @@ private void addOrderedPluginOptions(Plugin plugin,
362362

363363
if (plugin instanceof DefaultCompressPlugin) {
364364
plugOption
365-
= new PluginOption(false,
365+
= new PluginOption(true,
366366
(task, opt, arg) -> {
367367
Map<String, String> m = addArgumentMap(plugin);
368-
m.put(plugin.getName(), DefaultCompressPlugin.LEVEL_2);
368+
String level = (arg != null && !arg.isEmpty())
369+
? arg
370+
:"zip-6";
371+
m.put(plugin.getName(), level);
369372
}, false, "--compress", "-c");
370373
mainOptions.add(plugOption);
371374
} else if (plugin instanceof DefaultStripDebugPlugin) {

src/jdk.jlink/share/classes/jdk/tools/jlink/resources/plugins.properties

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ Class optimization: convert Class.forName calls to constant loads.
6161
class-for-name.usage=\
6262
\ --class-for-name Class optimization: convert Class.forName calls to constant loads.
6363

64-
compress.argument=<zip-[0-9]>[:filter=<pattern-list>]
64+
compress.argument=<zip-'{0-9}'>[:filter=<pattern-list>]
6565

6666
compress.description= Compression to use in compressing resources.
6767

6868
compress.usage=\
6969
\ --compress <compress> Compression to use in compressing resources:\n\
7070
\ Accepted values are:\n\
71-
\ zip-[0-9], where zip-0 provides no compression,\n\
71+
\ zip-'{0-9}', where zip-0 provides no compression,\n\
7272
\ and zip-9 provides the best compression.\n\
7373
\ Default is zip-6.
7474

@@ -307,15 +307,15 @@ plugin.opt.disable-plugin=\
307307
\ --disable-plugin <pluginname> Disable the plugin mentioned
308308

309309
plugin.opt.compress=\
310-
\ --compress <compress> Compression to use in compressing resources:\n\
310+
\ --compress <compress> Compress all resources in the output image:\n\
311311
\ Accepted values are:\n\
312-
\ zip-[0-9], where zip-0 provides no compression,\n\
312+
\ zip-'{0-9}', where zip-0 provides no compression,\n\
313313
\ and zip-9 provides the best compression.\n\
314314
\ Default is zip-6.\n\
315315
\ Deprecated values to be removed in a future release:\n\
316-
\ 0: No compression. Equivalent to zip-0.\n\
316+
\ 0: No compression. Use zip-0 instead.\n\
317317
\ 1: Constant String Sharing\n\
318-
\ 2: Equivalent to zip-6.
318+
\ 2: ZIP. Use zip-6 instead.
319319

320320
plugin.opt.strip-debug=\
321321
\ -G, --strip-debug Strip debug information

src/jdk.jlink/share/man/jlink.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,15 @@ Developers are responsible for updating their custom runtime images.
6464
`--bind-services`
6565
: Link service provider modules and their dependencies.
6666

67-
`-c ={0|1|2}` or `--compress={0|1|2}`
68-
: Enable compression of resources:
67+
`-c zip-{0-9}` or `--compress=zip-{0-9}`
68+
: Enable compression of resources. The accepted values are:
69+
zip-{0-9}, where zip-0 provides no compression,
70+
and zip-9 provides the best compression. Default is zip-6.
6971

70-
- `0`: No compression
72+
: Deprecated values to be removed in a future release:
73+
- `0`: No compression. Use zip-0 instead.
7174
- `1`: Constant string sharing
72-
- `2`: ZIP
75+
- `2`: ZIP. Use zip-6 instead.
7376

7477
`--disable-plugin` *pluginname*
7578
: Disables the specified plug-in. See [jlink Plug-ins] for the list of
@@ -170,14 +173,18 @@ For a complete list of all available plug-ins, run the command
170173
### Plugin `compress`
171174

172175
Options
173-
: `--compress=`{`0`\|`1`\|`2`}\[`:filter=`*pattern-list*\]
176+
: `--compress=zip-`{`0`-`9`}\[`:filter=`*pattern-list*\]
174177

175178
Description
176179
: Compresses all resources in the output image.
180+
Accepted values are:
181+
zip-{0-9}, where zip-0 provides no compression,
182+
and zip-9 provides the best compression. Default is zip-6.
177183

178-
- Level 0: No compression
184+
: Deprecated values to be removed in a future release:
185+
- Level 0: No compression. Use zip-0 instead.
179186
- Level 1: Constant string sharing
180-
- Level 2: ZIP
187+
- Level 2: ZIP. Use zip-6 instead.
181188

182189
An optional *pattern-list* filter can be specified to list the pattern of
183190
files to include.

test/jdk/tools/jlink/JLinkTest.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@
4242
/*
4343
* @test
4444
* @summary Test image creation
45-
* @bug 8189777
46-
* @bug 8194922
47-
* @bug 8206962
48-
* @bug 8240349
45+
* @bug 8189777 8194922 8206962 8240349 8163382 8165735 8166810 8173717 8321139
4946
* @author Jean-Francois Denise
5047
* @requires (vm.compMode != "Xcomp" & os.maxMemory >= 2g)
5148
* @library ../lib
@@ -358,6 +355,39 @@ public static void main(String[] args) throws Exception {
358355
helper.generateDefaultImage(userOptions, moduleName).assertFailure("Error: Invalid compression level invalid");
359356
}
360357

358+
// short command without argument
359+
{
360+
String[] userOptions = {"-c"};
361+
String moduleName = "invalidCompressLevelEmpty";
362+
helper.generateDefaultJModule(moduleName, "composite2");
363+
helper.generateDefaultImage(userOptions, moduleName).assertFailure("Error: no value given for -c");
364+
}
365+
366+
// invalid short command
367+
{
368+
String[] userOptions = {"-c", "3", "--output", "image"};
369+
String moduleName = "invalidCompressLevel3";
370+
helper.generateDefaultJModule(moduleName, "composite2");
371+
helper.generateDefaultImage(userOptions, moduleName).assertFailure("Error: Invalid compression level 3");
372+
}
373+
374+
375+
// invalid argument value
376+
{
377+
String[] userOptions = {"--compress", "42", "--output", "image"};
378+
String moduleName = "invalidCompressLevel42";
379+
helper.generateDefaultJModule(moduleName, "composite2");
380+
helper.generateDefaultImage(userOptions, moduleName).assertFailure("Error: Invalid compression level 42");
381+
}
382+
383+
// invalid argument value
384+
{
385+
String[] userOptions = {"--compress", "zip-", "--output", "image"};
386+
String moduleName = "invalidCompressLevelZip";
387+
helper.generateDefaultJModule(moduleName, "composite2");
388+
helper.generateDefaultImage(userOptions, moduleName).assertFailure("Error: Invalid compression level zip-");
389+
}
390+
361391
// orphan argument - JDK-8166810
362392
{
363393
String[] userOptions = {"--compress", "2", "foo" };

test/jdk/tools/jlink/TaskHelperTest.java

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
/*
4949
* @test
5050
* @summary Test TaskHelper option parsing
51-
* @bug 8303884
51+
* @bug 8303884 8321139
5252
* @modules jdk.jlink/jdk.tools.jlink.internal
5353
* jdk.jlink/jdk.tools.jlink.plugin
5454
* @run junit TaskHelperTest
@@ -59,19 +59,22 @@ public class TaskHelperTest {
5959

6060
private static final List<Option<TaskHelperTest>> OPTIONS = List.of(
6161
new Option<>(true, (task, opt, arg) -> {
62-
System.out.println(arg);
6362
mainArgValue = arg;
6463
}, true, "--main-expecting"),
6564
new Option<>(false, (task, opt, arg) -> {
6665
mainFlag = true;
67-
}, true, "--main-no-arg")
66+
}, true, "--main-no-arg"),
67+
new Option<>(true, (task, opt, arg) -> {
68+
compressArgValue = (arg != null && !arg.isEmpty()) ? arg : "zip-6";
69+
}, "--compress", "-c")
6870
);
6971

7072
private static String argValue;
7173
private static String mainArgValue;
7274
private static boolean mainFlag = false;
75+
private static String compressArgValue;
7376

74-
public record ArgTestCase(String cmdLine, String[] tokens, String pluginArgValue, String mainArgValue, boolean mainFlagSet) {};
77+
public record ArgTestCase(String cmdLine, String[] tokens, String pluginArgValue, String mainArgValue, boolean mainFlagSet) {}
7578

7679
public static class TestPluginWithRawOption implements Plugin {
7780
@Override
@@ -118,6 +121,7 @@ public void reset() {
118121
argValue = null;
119122
mainArgValue = null;
120123
mainFlag = false;
124+
compressArgValue= null;
121125
}
122126

123127
public static Stream<ArgTestCase> gnuStyleUsages() {
@@ -217,4 +221,41 @@ public void testRemaining() throws BadArgs {
217221
var remaining = optionsHelper.handleOptions(this, args);
218222
assertEquals(2, remaining.size());
219223
}
220-
}
224+
225+
record CompressTestCase(String[] tokens, String expectedCompressValue) {}
226+
227+
public static Stream<CompressTestCase> compressUsages() {
228+
return Stream.of(
229+
230+
new CompressTestCase(new String[] {"-c", "0"}, "0"),
231+
new CompressTestCase(new String[] {"--compress=zip-0"}, "zip-0"),
232+
233+
new CompressTestCase(new String[] {"-c", "1"}, "1"),
234+
new CompressTestCase(new String[] {"--compress=zip-1"}, "zip-1"),
235+
236+
new CompressTestCase(new String[] {"-c", "2"}, "2"),
237+
new CompressTestCase(new String[] {"--compress=zip-2"}, "zip-2"),
238+
239+
new CompressTestCase(new String[] {"--compress=zip-3"}, "zip-3"),
240+
new CompressTestCase(new String[] {"--compress=zip-4"}, "zip-4"),
241+
new CompressTestCase(new String[] {"--compress=zip-5"}, "zip-5"),
242+
new CompressTestCase(new String[] {"--compress=zip-6"}, "zip-6"),
243+
new CompressTestCase(new String[] {"--compress=zip-7"}, "zip-7"),
244+
new CompressTestCase(new String[] {"--compress=zip-8"}, "zip-8"),
245+
new CompressTestCase(new String[] {"--compress=zip-9"}, "zip-9")
246+
);
247+
}
248+
249+
@ParameterizedTest
250+
@MethodSource("compressUsages")
251+
public void testCompressOptionArg(CompressTestCase testCase) throws TaskHelper.BadArgs, IOException {
252+
var remaining = optionsHelper.handleOptions(this, testCase.tokens);
253+
254+
// trigger Plugin::configure
255+
taskHelper.getPluginsConfig(null, null, null);
256+
257+
assertTrue(remaining.isEmpty());
258+
assertEquals(testCase.expectedCompressValue, compressArgValue);
259+
}
260+
}
261+

test/setup_aot/TestSetupAOT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ static void runJDKTools(String[] args) throws Throwable {
137137
String jlinkOutput = tmpDir + File.separator + "jlinkOutput";
138138

139139
execTool("jlink", "--help")
140-
.shouldContain("Compression to use in compressing resources");
140+
.shouldContain("Compress all resources in the output image");
141141
execTool("jlink", "--list-plugins")
142142
.shouldContain("List of available plugins",
143143
"--generate-cds-archive ");

0 commit comments

Comments
 (0)