Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Commit

Permalink
[GR-41440] Support for GraalVM jlink to produce a GraalVM.
Browse files Browse the repository at this point in the history
PullRequest: labsjdk-ce-11/313
  • Loading branch information
marwan-hallaoui committed Sep 29, 2022
2 parents 1ed9e98 + b2f8a87 commit ff283cd
Show file tree
Hide file tree
Showing 9 changed files with 630 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ci.jsonnet
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# https://github.com/graalvm/labs-openjdk-11/blob/master/doc/testing.md
local run_test_spec = "test/hotspot/jtreg/compiler/jvmci";
local run_test_spec = "test/hotspot/jtreg/compiler/jvmci test/jdk/tools/jlink/plugins";

local labsjdk_builder_version = "e9c60b5174490f2012c7c5d60a20aace93209a56";

Expand Down
166 changes: 166 additions & 0 deletions src/jdk.jlink/share/classes/jdk/tools/jlink/internal/CommandLine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package jdk.tools.jlink.internal;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;

/**
* This file was originally a copy of CommandLine.java in
* com.sun.tools.javac.main.
* It should track changes made to that file.
*/

/**
* Various utility methods for processing Java tool command line arguments.
*/
public class CommandLine {

static void loadCmdFile(InputStream in, List<String> args)
throws IOException {
try (Reader r = new InputStreamReader(in)) {
Tokenizer t = new Tokenizer(r);
String s;
while ((s = t.nextToken()) != null) {
args.add(s);
}
}
}
public static class Tokenizer {
private final Reader in;
private int ch;

public Tokenizer(Reader in) throws IOException {
this.in = in;
ch = in.read();
}

public String nextToken() throws IOException {
skipWhite();
if (ch == -1) {
return null;
}

StringBuilder sb = new StringBuilder();
char quoteChar = 0;

while (ch != -1) {
switch (ch) {
case ' ':
case '\t':
case '\f':
if (quoteChar == 0) {
return sb.toString();
}
sb.append((char) ch);
break;

case '\n':
case '\r':
return sb.toString();

case '\'':
case '"':
if (quoteChar == 0) {
quoteChar = (char) ch;
} else if (quoteChar == ch) {
quoteChar = 0;
} else {
sb.append((char) ch);
}
break;

case '\\':
if (quoteChar != 0) {
ch = in.read();
switch (ch) {
case '\n':
case '\r':
while (ch == ' ' || ch == '\n'
|| ch == '\r' || ch == '\t'
|| ch == '\f') {
ch = in.read();
}
continue;

case 'n':
ch = '\n';
break;
case 'r':
ch = '\r';
break;
case 't':
ch = '\t';
break;
case 'f':
ch = '\f';
break;
default:
break;
}
}
sb.append((char) ch);
break;

default:
sb.append((char) ch);
}

ch = in.read();
}

return sb.toString();
}

void skipWhite() throws IOException {
while (ch != -1) {
switch (ch) {
case ' ':
case '\t':
case '\n':
case '\r':
case '\f':
break;

case '#':
ch = in.read();
while (ch != '\n' && ch != '\r' && ch != -1) {
ch = in.read();
}
break;

default:
return;
}

ch = in.read();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.UncheckedIOException;
import java.lang.module.Configuration;
Expand Down Expand Up @@ -221,12 +222,26 @@ static class OptionsValues {
boolean suggestProviders = false;
}

public static final String OPTIONS_RESOURCE = "jdk/tools/jlink/internal/options";

int run(String[] args) {
if (log == null) {
setLog(new PrintWriter(System.out, true),
new PrintWriter(System.err, true));
}
try {
Module m = JlinkTask.class.getModule();
try (InputStream savedOptions = m.getResourceAsStream(OPTIONS_RESOURCE)) {
if (savedOptions != null) {
List<String> prependArgs = new ArrayList<>();
CommandLine.loadCmdFile(savedOptions, prependArgs);
if (!prependArgs.isEmpty()) {
prependArgs.addAll(Arrays.asList(args));
args = prependArgs.toArray(new String[prependArgs.size()]);
}
}
}

List<String> remaining = optionsHelper.handleOptions(this, args);
if (remaining.size() > 0 && !options.suggestProviders) {
throw taskHelper.newBadArgs("err.orphan.arguments",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.tools.jlink.internal.plugins;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import jdk.tools.jlink.plugin.*;
import jdk.tools.jlink.plugin.ResourcePoolEntry.Type;

/**
* Jlink plugin to copy files in the current runtime image into the output image.
* The files to copy are specified as {@link File#pathSeparatorChar} separated
* paths relative to the image root directory.
*/
public final class CopyFilesPlugin implements Plugin {

public static final String NAME = "copy-files";

/**
* List of relative path names for the files to copy.
*/
private final List<String> files = new ArrayList<>();

@Override
public String getName() {
return NAME;
}

@Override
public Category getType() {
return Category.ADDER;
}

@Override
public String getDescription() {
return PluginsResourceBundle.getDescription(NAME);
}

@Override
public String getArgumentsDescription() {
return PluginsResourceBundle.getArgument(NAME);
}

@Override
public boolean hasArguments() {
return true;
}

@Override
public boolean hasRawArgument() {
return true;
}

@Override
public void configure(Map<String, String> config) {
String arg = config.get(getName());
if (arg == null) {
throw new AssertionError();
}
for (String relativePath : arg.split(File.pathSeparator)) {
files.add(relativePath);
}
}

@Override
public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) {
if (!files.isEmpty()) {
in.transformAndCopy(Function.identity(), out);

String javaHome = System.getProperty("java.home");
for (String relativePath : files) {
Path file = Paths.get(javaHome, relativePath);
out.add(ResourcePoolEntry.create("/java.base/top/" + relativePath, Type.TOP, file));
}
}
return out.build();
}
}
Loading

0 comments on commit ff283cd

Please sign in to comment.