Search in sources :

Example 6 with NativeImageOptions

use of org.graalvm.buildtools.gradle.dsl.NativeImageOptions in project native-build-tools by graalvm.

the class NativeImagePlugin method createMainOptions.

private static NativeImageOptions createMainOptions(GraalVMExtension graalExtension, Project project) {
    NativeImageOptions buildExtension = graalExtension.getBinaries().create(NATIVE_MAIN_EXTENSION);
    NativeConfigurations configs = createNativeConfigurations(project, "main", JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME);
    buildExtension.getClasspath().from(configs.getImageClasspathConfiguration());
    return buildExtension;
}
Also used : NativeConfigurations(org.graalvm.buildtools.gradle.internal.NativeConfigurations) NativeImageOptions(org.graalvm.buildtools.gradle.dsl.NativeImageOptions) BaseNativeImageOptions(org.graalvm.buildtools.gradle.internal.BaseNativeImageOptions) DeprecatedNativeImageOptions(org.graalvm.buildtools.gradle.internal.DeprecatedNativeImageOptions)

Example 7 with NativeImageOptions

use of org.graalvm.buildtools.gradle.dsl.NativeImageOptions in project micronaut-gradle-plugin by micronaut-projects.

the class MicronautAotPlugin method registerOptimizedBinary.

private void registerOptimizedBinary(Project project, TaskProvider<MicronautAotOptimizerTask> prepareNative) {
    GraalVMExtension graalVMExtension = project.getExtensions().getByType(GraalVMExtension.class);
    NamedDomainObjectContainer<NativeImageOptions> binaries = graalVMExtension.getBinaries();
    binaries.create(OPTIMIZED_BINARY_NAME, binary -> {
        NativeImageOptions main = binaries.getByName(MAIN_BINARY_NAME);
        binary.getMainClass().set(main.getMainClass());
        binary.getClasspath().from(main.getClasspath());
        binary.getClasspath().from(prepareNative.map(MicronautAotOptimizerTask::getGeneratedClassesDirectory));
        project.getPlugins().withId("java-library", p -> binary.getSharedLibrary().convention(true));
    });
}
Also used : GraalVMExtension(org.graalvm.buildtools.gradle.dsl.GraalVMExtension) NativeImageOptions(org.graalvm.buildtools.gradle.dsl.NativeImageOptions)

Example 8 with NativeImageOptions

use of org.graalvm.buildtools.gradle.dsl.NativeImageOptions in project native-build-tools by graalvm.

the class NativeImagePlugin method registerTestBinary.

public void registerTestBinary(Project project, DefaultGraalVmExtension graalExtension, DefaultTestBinaryConfig config) {
    NativeImageOptions mainOptions = graalExtension.getBinaries().getByName("main");
    String name = config.getName();
    boolean isPrimaryTest = "test".equals(name);
    TaskContainer tasks = project.getTasks();
    // Testing part begins here. -------------------------------------------
    // In future Gradle releases this becomes a proper DirectoryProperty
    File testResultsDir = GradleUtils.getJavaPluginConvention(project).getTestResultsDir();
    DirectoryProperty testListDirectory = project.getObjects().directoryProperty();
    // Add DSL extension for testing
    NativeImageOptions testOptions = createTestOptions(graalExtension, name, project, mainOptions, testListDirectory, config.getSourceSet());
    if (isPrimaryTest) {
        deprecateExtension(project, testOptions, DEPRECATED_NATIVE_TEST_EXTENSION, "test");
    }
    TaskProvider<Test> testTask = config.validate().getTestTask();
    testOptions.getAgent().getInstrumentedTask().set(testTask);
    testTask.configure(test -> {
        File testList = new File(testResultsDir, test.getName() + "/testlist");
        testListDirectory.set(testList);
        test.getOutputs().dir(testList);
        // Set system property read by the UniqueIdTrackingListener.
        test.systemProperty(JUNIT_PLATFORM_LISTENERS_UID_TRACKING_ENABLED, true);
        TrackingDirectorySystemPropertyProvider directoryProvider = project.getObjects().newInstance(TrackingDirectorySystemPropertyProvider.class);
        directoryProvider.getDirectory().set(testListDirectory);
        test.getJvmArgumentProviders().add(directoryProvider);
        test.doFirst("cleanup test ids", new CleanupTestIdsDirectory(testListDirectory));
    });
    // Following ensures that required feature jar is on classpath for every project
    injectTestPluginDependencies(project, graalExtension.getTestSupport());
    TaskProvider<BuildNativeImageTask> testImageBuilder = tasks.named(deriveTaskName(name, "native", "Compile"), BuildNativeImageTask.class, task -> {
        task.setOnlyIf(t -> graalExtension.getTestSupport().get());
        task.getTestListDirectory().set(testListDirectory);
        testTask.get();
        ConfigurableFileCollection testList = project.getObjects().fileCollection();
        // Later this will be replaced by a dedicated task not requiring execution of tests
        testList.from(testListDirectory).builtBy(testTask);
        testOptions.getClasspath().from(testList);
    });
    if (isPrimaryTest) {
        tasks.register(DEPRECATED_NATIVE_TEST_BUILD_TASK, t -> {
            t.dependsOn(testImageBuilder);
            t.doFirst("Warn about deprecation", task -> task.getLogger().warn("Task " + DEPRECATED_NATIVE_TEST_BUILD_TASK + " is deprecated. Use " + NATIVE_TEST_COMPILE_TASK_NAME + " instead."));
        });
    }
    tasks.named(isPrimaryTest ? NATIVE_TEST_TASK_NAME : "native" + capitalize(name), NativeRunTask.class, task -> {
        task.setGroup(LifecycleBasePlugin.VERIFICATION_GROUP);
        task.setOnlyIf(t -> graalExtension.getTestSupport().get());
    });
}
Also used : ConfigurableFileCollection(org.gradle.api.file.ConfigurableFileCollection) TaskContainer(org.gradle.api.tasks.TaskContainer) DirectoryProperty(org.gradle.api.file.DirectoryProperty) NativeImageOptions(org.graalvm.buildtools.gradle.dsl.NativeImageOptions) BaseNativeImageOptions(org.graalvm.buildtools.gradle.internal.BaseNativeImageOptions) DeprecatedNativeImageOptions(org.graalvm.buildtools.gradle.internal.DeprecatedNativeImageOptions) BuildNativeImageTask(org.graalvm.buildtools.gradle.tasks.BuildNativeImageTask) Test(org.gradle.api.tasks.testing.Test) GenerateResourcesConfigFile(org.graalvm.buildtools.gradle.tasks.GenerateResourcesConfigFile) File(java.io.File)

Example 9 with NativeImageOptions

use of org.graalvm.buildtools.gradle.dsl.NativeImageOptions in project native-build-tools by graalvm.

the class NativeImagePlugin method createTestOptions.

private static NativeImageOptions createTestOptions(GraalVMExtension graalExtension, String binaryName, Project project, NativeImageOptions mainExtension, DirectoryProperty testListDirectory, SourceSet sourceSet) {
    NativeImageOptions testExtension = graalExtension.getBinaries().create(binaryName);
    NativeConfigurations configs = createNativeConfigurations(project, binaryName, JavaPlugin.TEST_RUNTIME_CLASSPATH_CONFIGURATION_NAME);
    testExtension.getMainClass().set("org.graalvm.junit.platform.NativeImageJUnitLauncher");
    testExtension.getMainClass().finalizeValue();
    testExtension.getImageName().convention(mainExtension.getImageName().map(name -> name + SharedConstants.NATIVE_TESTS_SUFFIX));
    ListProperty<String> runtimeArgs = testExtension.getRuntimeArgs();
    runtimeArgs.add("--xml-output-dir");
    runtimeArgs.add(project.getLayout().getBuildDirectory().dir("test-results/" + binaryName + "-native").map(d -> d.getAsFile().getAbsolutePath()));
    testExtension.buildArgs("--features=org.graalvm.junit.platform.JUnitPlatformFeature");
    ConfigurableFileCollection classpath = testExtension.getClasspath();
    classpath.from(configs.getImageClasspathConfiguration());
    classpath.from(sourceSet.getOutput().getClassesDirs());
    classpath.from(sourceSet.getOutput().getResourcesDir());
    return testExtension;
}
Also used : NativeConfigurations(org.graalvm.buildtools.gradle.internal.NativeConfigurations) GradleUtils(org.graalvm.buildtools.gradle.internal.GradleUtils) ConfigurableFileCollection(org.gradle.api.file.ConfigurableFileCollection) Arrays(java.util.Arrays) NativeImageExecutableLocator.graalvmHomeProvider(org.graalvm.buildtools.gradle.internal.NativeImageExecutableLocator.graalvmHomeProvider) BuildNativeImageTask(org.graalvm.buildtools.gradle.tasks.BuildNativeImageTask) URISyntaxException(java.net.URISyntaxException) ListProperty(org.gradle.api.provider.ListProperty) JavaPlugin(org.gradle.api.plugins.JavaPlugin) NamedDomainObjectContainer(org.gradle.api.NamedDomainObjectContainer) NativeConfigurations(org.graalvm.buildtools.gradle.internal.NativeConfigurations) ApplicationPlugin(org.gradle.api.plugins.ApplicationPlugin) OutputDirectory(org.gradle.api.tasks.OutputDirectory) Task(org.gradle.api.Task) Provider(org.gradle.api.provider.Provider) ModuleVersionIdentifier(org.gradle.api.artifacts.ModuleVersionIdentifier) ConfigurationContainer(org.gradle.api.artifacts.ConfigurationContainer) TaskProvider(org.gradle.api.tasks.TaskProvider) NativeImageOptions(org.graalvm.buildtools.gradle.dsl.NativeImageOptions) Locale(java.util.Locale) Map(java.util.Map) DuplicatesStrategy(org.gradle.api.file.DuplicatesStrategy) ExtensionAware(org.gradle.api.plugins.ExtensionAware) GraalVMExtension(org.graalvm.buildtools.gradle.dsl.GraalVMExtension) URI(java.net.URI) Path(java.nio.file.Path) LogLevel(org.gradle.api.logging.LogLevel) Project(org.gradle.api.Project) SharedConstants(org.graalvm.buildtools.utils.SharedConstants) GenerateResourcesConfigFile(org.graalvm.buildtools.gradle.tasks.GenerateResourcesConfigFile) GraalVMLogger(org.graalvm.buildtools.gradle.internal.GraalVMLogger) Set(java.util.Set) JavaForkOptions(org.gradle.process.JavaForkOptions) GFileUtils(org.gradle.util.GFileUtils) Collectors(java.util.stream.Collectors) TaskContainer(org.gradle.api.tasks.TaskContainer) ArchiveOperations(org.gradle.api.file.ArchiveOperations) DefaultTestBinaryConfig(org.graalvm.buildtools.gradle.internal.DefaultTestBinaryConfig) AGENT_OUTPUT_FOLDER(org.graalvm.buildtools.utils.SharedConstants.AGENT_OUTPUT_FOLDER) JavaToolchainService(org.gradle.jvm.toolchain.JavaToolchainService) AgentConfiguration(org.graalvm.buildtools.gradle.dsl.AgentConfiguration) Callable(java.util.concurrent.Callable) AbstractArchiveTask(org.gradle.api.tasks.bundling.AbstractArchiveTask) FileCollection(org.gradle.api.file.FileCollection) CommandLineArgumentProvider(org.gradle.process.CommandLineArgumentProvider) SourceSet(org.gradle.api.tasks.SourceSet) Inject(javax.inject.Inject) Configuration(org.gradle.api.artifacts.Configuration) LifecycleBasePlugin(org.gradle.language.base.plugins.LifecycleBasePlugin) VersionInfo(org.graalvm.buildtools.VersionInfo) AttributeContainer(org.gradle.api.attributes.AttributeContainer) Attribute(org.gradle.api.attributes.Attribute) DirectoryProperty(org.gradle.api.file.DirectoryProperty) SourceSetContainer(org.gradle.api.tasks.SourceSetContainer) ProcessGeneratedGraalResourceFiles(org.graalvm.buildtools.gradle.internal.ProcessGeneratedGraalResourceFiles) Directory(org.gradle.api.file.Directory) JvmReachabilityMetadataRepositoryExtension(org.graalvm.buildtools.gradle.dsl.JvmReachabilityMetadataRepositoryExtension) Test(org.gradle.api.tasks.testing.Test) FileSystemLocation(org.gradle.api.file.FileSystemLocation) FileSystemOperations(org.gradle.api.file.FileSystemOperations) BaseNativeImageOptions(org.graalvm.buildtools.gradle.internal.BaseNativeImageOptions) Jar(org.gradle.api.tasks.bundling.Jar) AGENT_PROPERTY(org.graalvm.buildtools.utils.SharedConstants.AGENT_PROPERTY) Action(org.gradle.api.Action) JavaPluginConvention(org.gradle.api.plugins.JavaPluginConvention) AgentCommandLineProvider(org.graalvm.buildtools.gradle.internal.AgentCommandLineProvider) NativeRunTask(org.graalvm.buildtools.gradle.tasks.NativeRunTask) ExecOperations(org.gradle.process.ExecOperations) DeprecatedNativeImageOptions(org.graalvm.buildtools.gradle.internal.DeprecatedNativeImageOptions) JavaApplication(org.gradle.api.plugins.JavaApplication) File(java.io.File) Consumer(java.util.function.Consumer) ObjectFactory(org.gradle.api.model.ObjectFactory) GradleUtils.transitiveProjectArtifacts(org.graalvm.buildtools.gradle.internal.GradleUtils.transitiveProjectArtifacts) Property(org.gradle.api.provider.Property) DefaultGraalVmExtension(org.graalvm.buildtools.gradle.internal.DefaultGraalVmExtension) JavaExec(org.gradle.api.tasks.JavaExec) JvmReachabilityMetadataService(org.graalvm.buildtools.gradle.internal.JvmReachabilityMetadataService) Collections(java.util.Collections) Plugin(org.gradle.api.Plugin) NativeImageOptions(org.graalvm.buildtools.gradle.dsl.NativeImageOptions) BaseNativeImageOptions(org.graalvm.buildtools.gradle.internal.BaseNativeImageOptions) DeprecatedNativeImageOptions(org.graalvm.buildtools.gradle.internal.DeprecatedNativeImageOptions) ConfigurableFileCollection(org.gradle.api.file.ConfigurableFileCollection)

Example 10 with NativeImageOptions

use of org.graalvm.buildtools.gradle.dsl.NativeImageOptions in project native-build-tools by graalvm.

the class NativeImagePlugin method configureJavaProject.

private void configureJavaProject(Project project, Provider<NativeImageService> nativeImageServiceProvider, DefaultGraalVmExtension graalExtension) {
    logger.log("====================");
    logger.log("Initializing project: " + project.getName());
    logger.log("====================");
    Map<String, Provider<Boolean>> agents = graalExtension.getAgentProperties();
    // Add DSL extensions for building and testing
    NativeImageOptions mainOptions = createMainOptions(graalExtension, project);
    deprecateExtension(project, mainOptions, DEPRECATED_NATIVE_BUILD_EXTENSION, "main");
    project.getPlugins().withId("application", p -> mainOptions.getMainClass().convention(project.getExtensions().findByType(JavaApplication.class).getMainClass()));
    project.getPlugins().withId("java-library", p -> mainOptions.getSharedLibrary().convention(true));
    registerServiceProvider(project, nativeImageServiceProvider);
    // Register Native Image tasks
    TaskContainer tasks = project.getTasks();
    JavaPluginConvention javaConvention = project.getConvention().getPlugin(JavaPluginConvention.class);
    configureAutomaticTaskCreation(project, graalExtension, agents, tasks, javaConvention.getSourceSets());
    TaskProvider<BuildNativeImageTask> imageBuilder = tasks.named(NATIVE_COMPILE_TASK_NAME, BuildNativeImageTask.class);
    TaskProvider<Task> deprecatedTask = tasks.register(DEPRECATED_NATIVE_BUILD_TASK, t -> {
        t.dependsOn(imageBuilder);
        t.doFirst("Warn about deprecation", task -> task.getLogger().warn("Task " + DEPRECATED_NATIVE_BUILD_TASK + " is deprecated. Use " + NATIVE_COMPILE_TASK_NAME + " instead."));
    });
    // We want to add agent invocation to "run" task, but it is only available when
    // Application Plugin is initialized.
    project.getPlugins().withType(ApplicationPlugin.class, applicationPlugin -> {
        TaskProvider<? extends JavaForkOptions> runTask = tasks.withType(JavaExec.class).named(ApplicationPlugin.TASK_RUN_NAME);
        mainOptions.getAgent().getInstrumentedTask().convention(runTask);
    });
    graalExtension.registerTestBinary("test", config -> {
        config.forTestTask(tasks.named("test", Test.class));
        config.usingSourceSet(GradleUtils.findSourceSet(project, SourceSet.TEST_SOURCE_SET_NAME));
    });
}
Also used : BuildNativeImageTask(org.graalvm.buildtools.gradle.tasks.BuildNativeImageTask) Task(org.gradle.api.Task) AbstractArchiveTask(org.gradle.api.tasks.bundling.AbstractArchiveTask) NativeRunTask(org.graalvm.buildtools.gradle.tasks.NativeRunTask) NativeImageExecutableLocator.graalvmHomeProvider(org.graalvm.buildtools.gradle.internal.NativeImageExecutableLocator.graalvmHomeProvider) Provider(org.gradle.api.provider.Provider) TaskProvider(org.gradle.api.tasks.TaskProvider) CommandLineArgumentProvider(org.gradle.process.CommandLineArgumentProvider) AgentCommandLineProvider(org.graalvm.buildtools.gradle.internal.AgentCommandLineProvider) TaskContainer(org.gradle.api.tasks.TaskContainer) NativeImageOptions(org.graalvm.buildtools.gradle.dsl.NativeImageOptions) BaseNativeImageOptions(org.graalvm.buildtools.gradle.internal.BaseNativeImageOptions) DeprecatedNativeImageOptions(org.graalvm.buildtools.gradle.internal.DeprecatedNativeImageOptions) JavaPluginConvention(org.gradle.api.plugins.JavaPluginConvention) BuildNativeImageTask(org.graalvm.buildtools.gradle.tasks.BuildNativeImageTask) JavaExec(org.gradle.api.tasks.JavaExec) Test(org.gradle.api.tasks.testing.Test) JavaApplication(org.gradle.api.plugins.JavaApplication)

Aggregations

NativeImageOptions (org.graalvm.buildtools.gradle.dsl.NativeImageOptions)11 BaseNativeImageOptions (org.graalvm.buildtools.gradle.internal.BaseNativeImageOptions)8 DeprecatedNativeImageOptions (org.graalvm.buildtools.gradle.internal.DeprecatedNativeImageOptions)7 File (java.io.File)5 GraalVMExtension (org.graalvm.buildtools.gradle.dsl.GraalVMExtension)5 BuildNativeImageTask (org.graalvm.buildtools.gradle.tasks.BuildNativeImageTask)5 JavaApplication (org.gradle.api.plugins.JavaApplication)5 AgentCommandLineProvider (org.graalvm.buildtools.gradle.internal.AgentCommandLineProvider)4 GraalVMLogger (org.graalvm.buildtools.gradle.internal.GraalVMLogger)4 NativeImageExecutableLocator.graalvmHomeProvider (org.graalvm.buildtools.gradle.internal.NativeImageExecutableLocator.graalvmHomeProvider)4 GenerateResourcesConfigFile (org.graalvm.buildtools.gradle.tasks.GenerateResourcesConfigFile)4 NativeRunTask (org.graalvm.buildtools.gradle.tasks.NativeRunTask)4 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3 Path (java.nio.file.Path)3 Arrays (java.util.Arrays)3 Collections (java.util.Collections)3 Locale (java.util.Locale)3 Map (java.util.Map)3 Set (java.util.Set)3