-
Notifications
You must be signed in to change notification settings - Fork 76
Experimental support for layered images #719
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright (c) 2021, 2021 Oracle and/or its affiliates. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* The Universal Permissive License (UPL), Version 1.0 | ||
* | ||
* Subject to the condition set forth below, permission is hereby granted to any | ||
* person obtaining a copy of this software, associated documentation and/or | ||
* data (collectively the "Software"), free of charge and under any and all | ||
* copyright rights in the Software, and any and all patent rights owned or | ||
* freely licensable by each licensor hereunder covering either (i) the | ||
* unmodified Software as contributed to or provided by such licensor, or (ii) | ||
* the Larger Works (as defined below), to deal in both | ||
* | ||
* (a) the Software, and | ||
* | ||
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if | ||
* one is included with the Software each a "Larger Work" to which the Software | ||
* is contributed by such licensors), | ||
* | ||
* without restriction, including without limitation the rights to copy, create | ||
* derivative works of, display, perform, and distribute the Software and make, | ||
* use, sell, offer for sale, import, export, have made, and have sold the | ||
* Software and the Larger Work(s), and to sublicense the foregoing rights on | ||
* either these or other terms. | ||
* | ||
* This license is subject to the following condition: | ||
* | ||
* The above copyright notice and either this complete permission notice or at a | ||
* minimum a reference to the UPL must be included in all copies or substantial | ||
* portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package org.graalvm.buildtools.model.resources; | ||
|
||
public abstract class NativeImageFlags { | ||
public static final String BUILD_OUTPUT_COLORFUL = "-H:+BuildOutputColorful"; | ||
public static final String COLOR = "--color"; | ||
public static final String CONFIGURATION_FILE_DIRECTORIES = "-H:ConfigurationFileDirectories"; | ||
public static final String LAYER_CREATE = "-H:LayerCreate"; | ||
public static final String LAYER_USE = "-H:LayerUse"; | ||
public static final String NO_FALLBACK = "--no-fallback"; | ||
public static final String PGO_INSTRUMENT = "--pgo-instrument"; | ||
public static final String QUICK_BUILD = "-Ob"; | ||
public static final String SHARED = "--shared"; | ||
public static final String UNLOCK_EXPERIMENTAL_VMOPTIONS = "-H:+UnlockExperimentalVMOptions"; | ||
public static final String VERBOSE = "--verbose"; | ||
|
||
private NativeImageFlags() { | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2003-2021 the original author or authors. | ||
* | ||
* 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. | ||
*/ | ||
package org.graalvm.buildtools.utils; | ||
|
||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Properties; | ||
|
||
public class JarMetadata { | ||
private final List<String> packageList; | ||
|
||
public JarMetadata(List<String> packageList) { | ||
this.packageList = packageList; | ||
} | ||
|
||
public List<String> getPackageList() { | ||
return packageList; | ||
} | ||
|
||
public static JarMetadata readFrom(Path propertiesFile) { | ||
Properties props = new Properties(); | ||
try (InputStream is = Files.newInputStream(propertiesFile)) { | ||
props.load(is); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Unable to read metadata from properties file " + propertiesFile, e); | ||
} | ||
String packages = (String) props.get("packages"); | ||
List<String> packageList = packages == null ? List.of() : Arrays.asList(packages.split(",")); | ||
return new JarMetadata(packageList); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2003-2021 the original author or authors. | ||
* | ||
* 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. | ||
*/ | ||
package org.graalvm.buildtools.utils; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.io.Writer; | ||
import java.nio.file.FileSystem; | ||
import java.nio.file.FileSystems; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Set; | ||
import java.util.TreeSet; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* Performs scanning of a JAR file and extracts some metadata in the | ||
* form of a properties file. For now this type only extracts the list | ||
* of packages from a jar. | ||
*/ | ||
public class JarScanner { | ||
/** | ||
* Scans a jar and creates a properties file with metadata about the jar contents. | ||
* @param inputJar the input jar | ||
* @param outputFile the output file | ||
* @throws IOException | ||
*/ | ||
public static void scanJar(Path inputJar, Path outputFile) throws IOException { | ||
try (Writer fileWriter = Files.newBufferedWriter(outputFile); PrintWriter writer = new PrintWriter(fileWriter)) { | ||
Set<String> packageList = new TreeSet<>(); | ||
try (FileSystem jarFileSystem = FileSystems.newFileSystem(inputJar, (ClassLoader) null)) { | ||
Path root = jarFileSystem.getPath("/"); | ||
try (Stream<Path> files = Files.walk(root)) { | ||
files.forEach(path -> { | ||
if (path.toString().endsWith(".class") && !path.toString().contains("META-INF")) { | ||
Path relativePath = root.relativize(path); | ||
String className = relativePath.toString() | ||
.replace('/', '.') | ||
.replace('\\', '.') | ||
.replaceAll("[.]class$", ""); | ||
var lastDot = className.lastIndexOf("."); | ||
if (lastDot > 0) { | ||
var packageName = className.substring(0, lastDot); | ||
packageList.add(packageName); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
writer.println("packages=" + String.join(",", packageList)); | ||
} catch (IOException ex) { | ||
throw new RuntimeException("Unable to write JAR analysis", ex); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/* | ||
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* The Universal Permissive License (UPL), Version 1.0 | ||
* | ||
* Subject to the condition set forth below, permission is hereby granted to any | ||
* person obtaining a copy of this software, associated documentation and/or | ||
* data (collectively the "Software"), free of charge and under any and all | ||
* copyright rights in the Software, and any and all patent rights owned or | ||
* freely licensable by each licensor hereunder covering either (i) the | ||
* unmodified Software as contributed to or provided by such licensor, or (ii) | ||
* the Larger Works (as defined below), to deal in both | ||
* | ||
* (a) the Software, and | ||
* | ||
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if | ||
* one is included with the Software each a "Larger Work" to which the Software | ||
* is contributed by such licensors), | ||
* | ||
* without restriction, including without limitation the rights to copy, create | ||
* derivative works of, display, perform, and distribute the Software and make, | ||
* use, sell, offer for sale, import, export, have made, and have sold the | ||
* Software and the Larger Work(s), and to sublicense the foregoing rights on | ||
* either these or other terms. | ||
* | ||
* This license is subject to the following condition: | ||
* | ||
* The above copyright notice and either this complete permission notice or at a | ||
* minimum a reference to the UPL must be included in all copies or substantial | ||
* portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package org.graalvm.buildtools.gradle | ||
|
||
import org.graalvm.buildtools.gradle.fixtures.AbstractFunctionalTest | ||
import org.graalvm.buildtools.gradle.fixtures.GraalVMSupport | ||
import org.graalvm.buildtools.utils.NativeImageUtils | ||
import spock.lang.Requires | ||
import spock.util.concurrent.PollingConditions | ||
|
||
import java.net.http.HttpClient | ||
import java.net.http.HttpRequest | ||
import java.net.http.HttpResponse | ||
import java.nio.charset.StandardCharsets | ||
|
||
@Requires( | ||
{ NativeImageUtils.getMajorJDKVersion(GraalVMSupport.getGraalVMHomeVersionString()) >= 25 } | ||
) | ||
class LayeredApplicationFunctionalTest extends AbstractFunctionalTest { | ||
def "can build a native image using layers"() { | ||
def nativeApp = getExecutableFile("build/native/nativeCompile/layered-java-application") | ||
|
||
given: | ||
withSample("layered-java-application") | ||
|
||
when: | ||
run 'nativeLibdependenciesCompile' | ||
|
||
then: | ||
tasks { | ||
succeeded ':nativeLibdependenciesCompile' | ||
} | ||
outputContains "'-H:LayerCreate' (origin(s): command line)" | ||
|
||
when: | ||
run 'nativeRun', '-Pmessage="Hello, layered images!"' | ||
|
||
then: | ||
tasks { | ||
upToDate ':nativeLibdependenciesCompile' | ||
succeeded ':nativeCompile' | ||
} | ||
nativeApp.exists() | ||
|
||
and: | ||
outputContains "- '-H:LayerUse' (origin(s): command line)" | ||
outputContains "Hello, layered images!" | ||
|
||
when: "Updating the application without changing the dependencies" | ||
file("src/main/java/org/graalvm/demo/Application.java").text = """ | ||
package org.graalvm.demo; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class Application { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(Application.class); | ||
|
||
public static void main(String[] args) { | ||
LOGGER.info("App started with args {}", String.join(", ", args)); | ||
} | ||
|
||
} | ||
|
||
""" | ||
run 'nativeRun', '-Pmessage="Hello, layered images!"' | ||
|
||
then: | ||
tasks { | ||
// Base layer is not rebuilt | ||
upToDate ':nativeLibdependenciesCompile' | ||
// Application layer is recompiled | ||
succeeded ':nativeCompile' | ||
} | ||
|
||
outputContains "- '-H:LayerUse' (origin(s): command line)" | ||
outputContains "Hello, layered images!" | ||
} | ||
|
||
def "can build a layered Micronaut application"() { | ||
given: | ||
withSample("layered-mn-application") | ||
|
||
when: | ||
run 'nativeCompile' | ||
|
||
then: | ||
tasks { | ||
succeeded ':nativeLibdependenciesCompile', ':nativeCompile' | ||
} | ||
|
||
when: | ||
def builder = new ProcessBuilder() | ||
.directory(testDirectory.toFile()) | ||
.inheritIO() | ||
.command("build/native/nativeCompile/layered-mn-app${IS_WINDOWS?".exe":""}") | ||
def env = builder.environment() | ||
env["LD_LIBRARY_PATH"] = testDirectory.resolve("build/native/nativeLibdependenciesCompile").toString() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of setting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had tried this without success. That said I'm not sure it's a good idea: this is only used for running the app, it is not required when building. So if we change this to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's correct, if you set |
||
def process = builder.start() | ||
def client = HttpClient.newHttpClient() | ||
def request = HttpRequest.newBuilder() | ||
.GET() | ||
.uri(new URI("http://localhost:8080/")) | ||
.build() | ||
def conditions = new PollingConditions() | ||
|
||
then: | ||
conditions.within(10) { | ||
def response = client.send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8)).body() | ||
response == "Hello, layered images!" | ||
} | ||
|
||
cleanup: | ||
process.destroy() | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a leftover?