Skip to content

Commit

Permalink
Merge pull request #614 from graalvm/vj/adopt-reachability-metadata
Browse files Browse the repository at this point in the history
Adjust native-build tools to reachability-metadata.json
  • Loading branch information
dnestoro authored Sep 3, 2024
2 parents fb962b9 + ef4b536 commit fae3e47
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 199 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,21 @@
*/
package org.graalvm.buildtools.agent;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class AgentConfiguration implements Serializable {

private static final String DEFAULT_ACCESS_FILTER_FILE = "/access-filter.json";
private final Collection<String> callerFilterFiles;
private final Collection<String> accessFilterFiles;
private final Boolean builtinCallerFilter;
Expand Down Expand Up @@ -88,6 +96,7 @@ public AgentConfiguration(Collection<String> callerFilterFiles,
}

public List<String> getAgentCommandLine() {
addDefaultAccessFilter();
List<String> cmdLine = new ArrayList<>(agentMode.getAgentCommandLine());
appendOptionToValues("caller-filter-file=", callerFilterFiles, cmdLine);
appendOptionToValues("access-filter-file=", accessFilterFiles, cmdLine);
Expand Down Expand Up @@ -127,4 +136,34 @@ private void addToCmd(String option, Boolean value, List<String> cmdLine) {
}
}

private void addDefaultAccessFilter() {
if (accessFilterFiles == null) {
// this could only happen if we instantiated disabled agent configuration
return;
}

String tempDir = System.getProperty("java.io.tmpdir");
Path agentDir = Path.of(tempDir).resolve("agent-config");
Path accessFilterFile = agentDir.resolve("access-filter.json");
if (Files.exists(accessFilterFile)) {
accessFilterFiles.add(accessFilterFile.toString());
return;
}

try(InputStream accessFilter = AgentConfiguration.class.getResourceAsStream(DEFAULT_ACCESS_FILTER_FILE)) {
if (accessFilter != null) {
if (!Files.exists(agentDir)) {
Files.createDirectory(agentDir);
}

Files.copy(accessFilter, accessFilterFile, StandardCopyOption.REPLACE_EXISTING);
accessFilterFiles.add(accessFilterFile.toString());
} else {
throw new IOException("Cannot find access-filter.json on default location: " + DEFAULT_ACCESS_FILTER_FILE);
}
} catch (IOException e) {
throw new RuntimeException("Cannot add default access-filter.json" ,e);
}
}

}
13 changes: 13 additions & 0 deletions common/utils/src/main/resources/access-filter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"rules": [
{
"includeClasses": "**"
},
{
"excludeClasses": "org.gradle.**"
},
{
"excludeClasses": "org.junit.**"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,32 @@
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.Unroll

class JavaApplicationWithAgentFunctionalTest extends AbstractFunctionalTest {

def getCurrentJDKVersion() {
return NativeImageUtils.getMajorJDKVersion(GraalVMSupport.getGraalVMHomeVersionString())
}

def metadataInSingleConfigFile() {
return getCurrentJDKVersion() >= 23
}

def metadataExistsAt(String path) {
if (metadataInSingleConfigFile()) {
return file("${path}/reachability-metadata.json").exists()
}

boolean allFilesExist = ['jni', 'proxy', 'reflect', 'resource', 'serialization'].every { name ->
file("${path}/${name}-config.json").exists()
}

return allFilesExist
}

@Unroll("agent is not passed and the application fails with JUnit Platform #junitVersion")
def "agent is not passed"() {
given:
Expand Down Expand Up @@ -94,18 +116,13 @@ class JavaApplicationWithAgentFunctionalTest extends AbstractFunctionalTest {
""".trim()

and:
['jni', 'proxy', 'reflect', 'resource', 'serialization'].each { name ->
assert file("build/native/agent-output/test/${name}-config.json").exists()
}
assert metadataExistsAt("build/native/agent-output/test")

when:
run 'metadataCopy'

then:
['jni', 'proxy', 'reflect', 'resource', 'serialization'].each { name ->
assert file("build/native/metadataCopyTest/${name}-config.json").exists()
}

assert metadataExistsAt("build/native/metadataCopyTest")

where:
junitVersion = System.getProperty('versions.junit')
Expand All @@ -125,7 +142,11 @@ class JavaApplicationWithAgentFunctionalTest extends AbstractFunctionalTest {
}

and:
assert file("build/native/agent-output/test/reflect-config.json").text.contains("\"condition\"")
if (metadataInSingleConfigFile()) {
assert file("build/native/agent-output/test/reachability-metadata.json").text.contains("\"condition\"")
} else {
assert file("build/native/agent-output/test/reflect-config.json").text.contains("\"condition\"")
}

where:
junitVersion = System.getProperty('versions.junit')
Expand All @@ -148,22 +169,26 @@ class JavaApplicationWithAgentFunctionalTest extends AbstractFunctionalTest {
}

and:
['jni', 'proxy', 'reflect', 'resource', 'serialization'].each { name ->
assert file("build/native/agent-output/run/${name}-config.json").exists()
}
assert metadataExistsAt("build/native/agent-output/run")

when:
run'metadataCopy', '--task', 'run', '--dir', metadata_dir
run 'metadataCopy', '--task', 'run', '--dir', metadata_dir

then:
['jni', 'proxy', 'reflect', 'resource', 'serialization'].each { name ->
assert file("${metadata_dir}/${name}-config.json").exists()
}
assert metadataExistsAt(metadata_dir)

and:
var reflect_config = file("${metadata_dir}/reflect-config.json")
var reflect_config_contents = reflect_config.text
assert reflect_config_contents.contains("DummyClass") && reflect_config_contents.contains("org.graalvm.demo.Message")
if (metadataInSingleConfigFile()) {
var reachabilityMetadata = file("${metadata_dir}/reachability-metadata.json")
var reachabilityMetadataContents = reachabilityMetadata.text
println reachabilityMetadataContents
assert reachabilityMetadataContents.contains("DummyClass"), reachabilityMetadataContents
assert reachabilityMetadataContents.contains("org.graalvm.demo.Message"), reachabilityMetadataContents
} else {
var reflect_config = file("${metadata_dir}/reflect-config.json")
var reflect_config_contents = reflect_config.text
assert reflect_config_contents.contains("DummyClass") && reflect_config_contents.contains("org.graalvm.demo.Message")
}

when:
run 'nativeRun'
Expand All @@ -190,9 +215,7 @@ class JavaApplicationWithAgentFunctionalTest extends AbstractFunctionalTest {
}

and:
['jni', 'proxy', 'reflect', 'resource', 'serialization'].each { name ->
assert file("build/native/agent-output/run/${name}-config.json").exists()
}
assert metadataExistsAt("build/native/agent-output/run")

when:
run'run', '-Pagent', '--configuration-cache', '--rerun-tasks'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
import org.graalvm.buildtools.gradle.tasks.NativeRunTask;
import org.graalvm.buildtools.gradle.tasks.actions.CleanupAgentFilesAction;
import org.graalvm.buildtools.gradle.tasks.actions.MergeAgentFilesAction;
import org.graalvm.buildtools.gradle.tasks.actions.ProcessGeneratedGraalResourceFilesAction;
import org.graalvm.buildtools.utils.SharedConstants;
import org.graalvm.reachability.DirectoryConfiguration;
import org.gradle.api.Action;
Expand Down Expand Up @@ -871,11 +870,6 @@ public void execute(@Nonnull Task task) {
execOperations));

taskToInstrument.doLast(new CleanupAgentFilesAction(mergeInputDirs, fileOperations));

taskToInstrument.doLast(new ProcessGeneratedGraalResourceFilesAction(
outputDir,
graalExtension.getAgent().getFilterableEntries()
));
}

private static void injectTestPluginDependencies(Project project, Property<Boolean> testSupportEnabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
import org.gradle.jvm.toolchain.JavaToolchainService;

import javax.inject.Inject;
import java.util.Arrays;

public abstract class DefaultGraalVmExtension implements GraalVMExtension {
private final transient NamedDomainObjectContainer<NativeImageOptions> nativeImages;
Expand All @@ -79,7 +78,6 @@ public DefaultGraalVmExtension(NamedDomainObjectContainer<NativeImageOptions> na
agentOpts.getEnabled().convention(false);
agentOpts.getModes().getConditional().getParallel().convention(true);
agentOpts.getMetadataCopy().getMergeWithExisting().convention(false);
agentOpts.getFilterableEntries().convention(Arrays.asList("org.gradle.", "org.junit."));
agentOpts.getBuiltinHeuristicFilter().convention(true);
agentOpts.getBuiltinCallerFilter().convention(true);
agentOpts.getEnableExperimentalPredefinedClasses().convention(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;

import static org.graalvm.buildtools.gradle.internal.NativeImageExecutableLocator.findNativeImageExecutable;
Expand Down Expand Up @@ -94,8 +95,11 @@ public MergeAgentFilesAction(Provider<Boolean> isMergingEnabled,
this.noLauncherProperty = objectFactory.property(JavaLauncher.class);
}

private static final Set<String> METADATA_FILES = Set.of("reflect-config.json", "jni-config.json", "proxy-config.json", "resource-config.json", "reachability-metadata.json");

private static boolean isConfigDir(String dir) {
return Arrays.stream(new File(dir).listFiles()).anyMatch(file -> file.getName().equals("reflect-config.json"));
return Arrays.stream(new File(dir).listFiles())
.anyMatch(file -> METADATA_FILES.contains(file.getName()));
}

@Override
Expand Down
Loading

0 comments on commit fae3e47

Please sign in to comment.