-
Notifications
You must be signed in to change notification settings - Fork 51
/
NativeImagePlugin.java
719 lines (651 loc) · 38.1 KB
/
NativeImagePlugin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
/*
* 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.VersionInfo;
import org.graalvm.buildtools.agent.AgentConfiguration;
import org.graalvm.buildtools.agent.AgentMode;
import org.graalvm.buildtools.agent.StandardAgentMode;
import org.graalvm.buildtools.gradle.dsl.GraalVMExtension;
import org.graalvm.buildtools.gradle.dsl.JvmReachabilityMetadataRepositoryExtension;
import org.graalvm.buildtools.gradle.dsl.NativeImageOptions;
import org.graalvm.buildtools.gradle.dsl.agent.AgentOptions;
import org.graalvm.buildtools.gradle.internal.AgentCommandLineProvider;
import org.graalvm.buildtools.gradle.internal.BaseNativeImageOptions;
import org.graalvm.buildtools.gradle.internal.DefaultGraalVmExtension;
import org.graalvm.buildtools.gradle.internal.DefaultTestBinaryConfig;
import org.graalvm.buildtools.gradle.internal.DeprecatedNativeImageOptions;
import org.graalvm.buildtools.gradle.internal.GraalVMLogger;
import org.graalvm.buildtools.gradle.internal.GradleUtils;
import org.graalvm.buildtools.gradle.internal.JvmReachabilityMetadataService;
import org.graalvm.buildtools.gradle.internal.NativeConfigurations;
import org.graalvm.buildtools.gradle.tasks.actions.CleanupAgentFilesAction;
import org.graalvm.buildtools.gradle.tasks.actions.ProcessGeneratedGraalResourceFilesAction;
import org.graalvm.buildtools.gradle.internal.agent.AgentConfigurationFactory;
import org.graalvm.buildtools.gradle.tasks.BuildNativeImageTask;
import org.graalvm.buildtools.gradle.tasks.CopyMetadataTask;
import org.graalvm.buildtools.gradle.tasks.GenerateResourcesConfigFile;
import org.graalvm.buildtools.gradle.tasks.NativeRunTask;
import org.graalvm.buildtools.gradle.tasks.actions.MergeAgentFilesAction;
import org.graalvm.buildtools.utils.SharedConstants;
import org.gradle.api.Action;
import org.gradle.api.NamedDomainObjectContainer;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ConfigurationContainer;
import org.gradle.api.artifacts.ModuleVersionIdentifier;
import org.gradle.api.attributes.Attribute;
import org.gradle.api.attributes.AttributeContainer;
import org.gradle.api.file.ArchiveOperations;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.Directory;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.DuplicatesStrategy;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.FileSystemLocation;
import org.gradle.api.file.FileSystemOperations;
import org.gradle.api.logging.LogLevel;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.plugins.ExtensionAware;
import org.gradle.api.plugins.JavaApplication;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.TaskContainer;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.api.tasks.bundling.AbstractArchiveTask;
import org.gradle.api.tasks.bundling.Jar;
import org.gradle.api.tasks.testing.Test;
import org.gradle.jvm.toolchain.JavaToolchainService;
import org.gradle.language.base.plugins.LifecycleBasePlugin;
import org.gradle.process.CommandLineArgumentProvider;
import org.gradle.process.ExecOperations;
import org.gradle.process.JavaForkOptions;
import org.gradle.util.GFileUtils;
import javax.inject.Inject;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import static org.graalvm.buildtools.gradle.internal.GradleUtils.transitiveProjectArtifacts;
import static org.graalvm.buildtools.gradle.internal.NativeImageExecutableLocator.graalvmHomeProvider;
import static org.graalvm.buildtools.utils.SharedConstants.AGENT_PROPERTY;
/**
* Gradle plugin for GraalVM Native Image.
*/
@SuppressWarnings("unused")
public class NativeImagePlugin implements Plugin<Project> {
public static final String NATIVE_COMPILE_TASK_NAME = "nativeCompile";
public static final String NATIVE_TEST_COMPILE_TASK_NAME = "nativeTestCompile";
public static final String NATIVE_TEST_TASK_NAME = "nativeTest";
public static final String NATIVE_TEST_EXTENSION = "test";
public static final String NATIVE_MAIN_EXTENSION = "main";
public static final String PROCESS_AGENT_RESOURCES_TASK_NAME_PREFIX = "filterAgent";
public static final String PROCESS_AGENT_RESOURCES_TASK_NAME_SUFFIX = "Resources";
public static final String GENERATE_RESOURCES_CONFIG_FILE_TASK_NAME = "generateResourcesConfigFile";
public static final String GENERATE_TEST_RESOURCES_CONFIG_FILE_TASK_NAME = "generateTestResourcesConfigFile";
public static final String DEPRECATED_NATIVE_BUILD_EXTENSION = "nativeBuild";
public static final String DEPRECATED_NATIVE_TEST_EXTENSION = "nativeTest";
public static final String DEPRECATED_NATIVE_BUILD_TASK = "nativeBuild";
public static final String DEPRECATED_NATIVE_TEST_BUILD_TASK = "nativeTestBuild";
public static final String CONFIG_REPO_LOGLEVEL = "org.graalvm.internal.gradle.configrepo.logging";
/**
* This looks strange, but it is used to force the configuration of a dependent
* task during the configuration of another one. This is a workaround for a bug
* when applying the Kotlin plugin, where the test task is configured too late
* for some reason.
*/
private static final Consumer<Object> FORCE_CONFIG = t -> {
};
private static final String JUNIT_PLATFORM_LISTENERS_UID_TRACKING_ENABLED = "junit.platform.listeners.uid.tracking.enabled";
private static final String JUNIT_PLATFORM_LISTENERS_UID_TRACKING_OUTPUT_DIR = "junit.platform.listeners.uid.tracking.output.dir";
private GraalVMLogger logger;
@Inject
public ArchiveOperations getArchiveOperations() {
throw new UnsupportedOperationException();
}
@Inject
public ExecOperations getExecOperations() {
throw new UnsupportedOperationException();
}
@Inject
public FileSystemOperations getFileOperations() {
throw new UnsupportedOperationException();
}
@Override
public void apply(Project project) {
Provider<NativeImageService> nativeImageServiceProvider = NativeImageService.registerOn(project);
logger = GraalVMLogger.of(project.getLogger());
DefaultGraalVmExtension graalExtension = (DefaultGraalVmExtension) registerGraalVMExtension(project);
graalExtension.getUseArgFile().convention(true);
project.getPlugins()
.withType(JavaPlugin.class, javaPlugin -> configureJavaProject(project, nativeImageServiceProvider, graalExtension));
project.afterEvaluate(p -> {
instrumentTasksWithAgent(project, graalExtension);
});
}
private void instrumentTasksWithAgent(Project project, DefaultGraalVmExtension graalExtension) {
Provider<String> agentMode = agentProperty(project, graalExtension.getAgent());
Provider<AgentConfiguration> agentConfiguration = AgentConfigurationFactory.getAgentConfiguration(agentMode, graalExtension.getAgent());
Predicate<? super Task> taskPredicate = graalExtension.getAgent().getTasksToInstrumentPredicate().get();
project.getTasks().configureEach(t -> {
if (isTaskInstrumentableByAgent(t) && taskPredicate.test(t)) {
configureAgent(project, agentConfiguration, graalExtension.getToolchainDetection(), getExecOperations(), getFileOperations(), t, (JavaForkOptions) t);
} else {
logger.lifecycle("Skipping task: " + t.getName());
}
});
}
private static boolean isTaskInstrumentableByAgent(Task task) {
return task instanceof JavaForkOptions;
}
private static String deriveTaskName(String name, String prefix, String suffix) {
if ("main".equals(name)) {
return prefix + suffix;
}
return prefix + capitalize(name) + suffix;
}
private void configureJavaProject(Project project, Provider<NativeImageService> nativeImageServiceProvider, DefaultGraalVmExtension graalExtension) {
logger.log("====================");
logger.log("Initializing project: " + project.getName());
logger.log("====================");
// 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, 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."));
});
graalExtension.registerTestBinary("test", config -> {
config.forTestTask(tasks.named("test", Test.class));
config.usingSourceSet(GradleUtils.findSourceSet(project, SourceSet.TEST_SOURCE_SET_NAME));
});
TaskProvider<CopyMetadataTask> copyMetadataTask = project.getTasks().register("copyMetadata", CopyMetadataTask.class, task -> {
task.setGroup(LifecycleBasePlugin.BUILD_GROUP);
task.setDescription("Copies metadata collected from tasks instrumented with the agent into target directories.");
task.getInputTaskNames().set(graalExtension.getCopyMetadata().getInputTaskNames());
task.getOutputDirectories().set(graalExtension.getCopyMetadata().getOutputDirectories());
task.getMergeWithExisting().set(graalExtension.getCopyMetadata().getMergeWithExisting());
Provider<Boolean> isMergeEnabled = project.provider(() -> true);
Provider<AgentMode> agentModeProvider = project.provider(StandardAgentMode::new);
Provider<List<String>> inputDirectories = task.getInputTaskNames().map(list ->
list.stream()
.map(taskName -> AgentConfigurationFactory.getAgentOutputDirectoryForTask(project, taskName))
.map(dir -> dir.get().getAsFile().getAbsolutePath())
.collect(Collectors.toList()));
task.doLast(new MergeAgentFilesAction(
isMergeEnabled,
agentModeProvider,
task.getMergeWithExisting(),
project,
graalvmHomeProvider(project.getProviders()),
inputDirectories,
task.getOutputDirectories(),
graalExtension.getToolchainDetection(),
getExecOperations(),
task.getLogger()));
});
}
private void configureAutomaticTaskCreation(Project project,
GraalVMExtension graalExtension,
TaskContainer tasks,
SourceSetContainer sourceSets) {
graalExtension.getBinaries().configureEach(options -> {
String binaryName = options.getName();
String compileTaskName = deriveTaskName(binaryName, "native", "Compile");
if ("main".equals(binaryName)) {
compileTaskName = NATIVE_COMPILE_TASK_NAME;
}
TaskProvider<BuildNativeImageTask> imageBuilder = tasks.register(compileTaskName,
BuildNativeImageTask.class, builder -> {
builder.setDescription("Compiles a native image for the " + options.getName() + " binary");
builder.setGroup(LifecycleBasePlugin.BUILD_GROUP);
builder.getOptions().convention(options);
builder.getUseArgFile().convention(graalExtension.getUseArgFile());
});
String runTaskName = deriveTaskName(binaryName, "native", "Run");
if ("main".equals(binaryName)) {
runTaskName = NativeRunTask.TASK_NAME;
} else if (binaryName.toLowerCase(Locale.US).endsWith("test")) {
runTaskName = "native" + capitalize(binaryName);
}
tasks.register(runTaskName, NativeRunTask.class, task -> {
task.setGroup(LifecycleBasePlugin.BUILD_GROUP);
task.setDescription("Executes the " + options.getName() + " native binary");
task.getImage().convention(imageBuilder.map(t -> t.getOutputFile().get()));
task.getRuntimeArgs().convention(options.getRuntimeArgs());
});
configureClasspathJarFor(tasks, options, imageBuilder);
SourceSet sourceSet = "test".equals(binaryName) ? sourceSets.getByName(SourceSet.TEST_SOURCE_SET_NAME) : sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME);
TaskProvider<GenerateResourcesConfigFile> generateResourcesConfig = registerResourcesConfigTask(
graalExtension.getGeneratedResourcesDirectory(),
options,
tasks,
transitiveProjectArtifacts(project, sourceSet.getRuntimeClasspathConfigurationName()),
deriveTaskName(binaryName, "generate", "ResourcesConfigFile"));
options.getConfigurationFileDirectories().from(generateResourcesConfig.map(t ->
t.getOutputFile().map(f -> f.getAsFile().getParentFile())
));
configureJvmReachabilityConfigurationDirectories(project, graalExtension, options, sourceSet);
});
}
private void configureJvmReachabilityConfigurationDirectories(Project project, GraalVMExtension graalExtension, NativeImageOptions options, SourceSet sourceSet) {
JvmReachabilityMetadataRepositoryExtension repositoryExtension = reachabilityExtensionOn(graalExtension);
Provider<JvmReachabilityMetadataService> serviceProvider = project.getGradle()
.getSharedServices()
.registerIfAbsent("nativeConfigurationService", JvmReachabilityMetadataService.class, spec -> {
LogLevel logLevel = determineLogLevel();
spec.getParameters().getLogLevel().set(logLevel);
spec.getParameters().getUri().set(repositoryExtension.getUri());
spec.getParameters().getCacheDir().set(new File(project.getGradle().getGradleUserHomeDir(), "native-build-tools/repositories"));
});
options.getConfigurationFileDirectories().from(repositoryExtension.getEnabled().flatMap(enabled -> {
if (enabled) {
if (repositoryExtension.getUri().isPresent()) {
Configuration classpath = project.getConfigurations().getByName(sourceSet.getRuntimeClasspathConfigurationName());
Set<String> excludedModules = repositoryExtension.getExcludedModules().getOrElse(Collections.emptySet());
Map<String, String> forcedVersions = repositoryExtension.getModuleToConfigVersion().getOrElse(Collections.emptyMap());
return serviceProvider.map(repo -> repo.findConfigurationDirectoriesFor(query -> classpath.getIncoming().getResolutionResult().allComponents(component -> {
ModuleVersionIdentifier moduleVersion = component.getModuleVersion();
String module = moduleVersion.getGroup() + ":" + moduleVersion.getName();
if (!excludedModules.contains(module)) {
query.forArtifact(artifact -> {
artifact.gav(module + ":" + moduleVersion.getVersion());
if (forcedVersions.containsKey(module)) {
artifact.forceConfigVersion(forcedVersions.get(module));
}
});
}
query.useLatestConfigWhenVersionIsUntested();
})).stream()
.map(Path::toAbsolutePath)
.map(Path::toFile)
.collect(Collectors.toList()));
}
}
return project.getProviders().provider(Collections::emptySet);
}));
}
private static LogLevel determineLogLevel() {
LogLevel logLevel = LogLevel.DEBUG;
String loggingProperty = System.getProperty(CONFIG_REPO_LOGLEVEL);
if (loggingProperty != null) {
logLevel = LogLevel.valueOf(loggingProperty.toUpperCase(Locale.US));
}
return logLevel;
}
private static JvmReachabilityMetadataRepositoryExtension reachabilityExtensionOn(GraalVMExtension graalExtension) {
return ((ExtensionAware) graalExtension).getExtensions().getByType(JvmReachabilityMetadataRepositoryExtension.class);
}
private void deprecateExtension(Project project,
NativeImageOptions delegate,
String name,
String substitute) {
JavaToolchainService toolchains = project.getExtensions().findByType(JavaToolchainService.class);
ObjectFactory objects = project.getObjects();
project.getExtensions().add(name, objects.newInstance(DeprecatedNativeImageOptions.class,
name,
delegate,
substitute,
project.getLogger()));
}
private void configureClasspathJarFor(TaskContainer tasks, NativeImageOptions options, TaskProvider<BuildNativeImageTask> imageBuilder) {
String baseName = imageBuilder.getName();
TaskProvider<Jar> classpathJar = tasks.register(baseName + "ClasspathJar", Jar.class, jar -> {
jar.setDescription("Builds a pathing jar for the " + options.getName() + " native binary");
jar.from(
options.getClasspath()
.getElements()
.map(elems -> elems.stream()
.map(e -> {
if (isJar(e)) {
return getArchiveOperations().zipTree(e);
}
return e;
})
.collect(Collectors.toList()))
);
jar.setDuplicatesStrategy(DuplicatesStrategy.WARN);
jar.getArchiveBaseName().set(baseName.toLowerCase(Locale.ENGLISH) + "-classpath");
});
imageBuilder.configure(nit -> {
if (options.getUseFatJar().get()) {
nit.getClasspathJar().set(classpathJar.flatMap(AbstractArchiveTask::getArchiveFile));
}
});
}
private static boolean isJar(FileSystemLocation location) {
return location.getAsFile().getName().toLowerCase(Locale.US).endsWith(".jar");
}
private GraalVMExtension registerGraalVMExtension(Project project) {
NamedDomainObjectContainer<NativeImageOptions> nativeImages = project.getObjects()
.domainObjectContainer(NativeImageOptions.class, name ->
project.getObjects().newInstance(BaseNativeImageOptions.class,
name,
project.getObjects(),
project.getProviders(),
project.getExtensions().findByType(JavaToolchainService.class),
project.getName())
);
GraalVMExtension graalvmNative = project.getExtensions().create(GraalVMExtension.class, "graalvmNative",
DefaultGraalVmExtension.class, nativeImages, this, project);
graalvmNative.getGeneratedResourcesDirectory().set(project.getLayout()
.getBuildDirectory()
.dir("native/generated/"));
configureNativeConfigurationRepo((ExtensionAware) graalvmNative);
return graalvmNative;
}
private void configureNativeConfigurationRepo(ExtensionAware graalvmNative) {
JvmReachabilityMetadataRepositoryExtension configurationRepository = graalvmNative.getExtensions().create("metadataRepository", JvmReachabilityMetadataRepositoryExtension.class);
configurationRepository.getEnabled().convention(false);
configurationRepository.getUri().convention(configurationRepository.getVersion().map(v -> {
try {
return new URI("https://github.com/graalvm/jvm-reachability-metadata/releases/download/" + v + "/jvm-reachability-metadata-" + v + ".zip");
} catch (URISyntaxException e) {
return null;
}
}));
configurationRepository.getExcludedModules().convention(Collections.emptySet());
configurationRepository.getModuleToConfigVersion().convention(Collections.emptyMap());
}
private TaskProvider<GenerateResourcesConfigFile> registerResourcesConfigTask(Provider<Directory> generatedDir,
NativeImageOptions options,
TaskContainer tasks,
FileCollection transitiveProjectArtifacts,
String name) {
return tasks.register(name, GenerateResourcesConfigFile.class, task -> {
task.setDescription("Generates a GraalVM resource-config.json file");
task.getOptions().convention(options.getResources());
task.getClasspath().from(options.getClasspath());
task.getTransitiveProjectArtifacts().from(transitiveProjectArtifacts);
task.getOutputFile().convention(generatedDir.map(d -> d.file(name + "/resource-config.json")));
});
}
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();
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();
if (!agentProperty(project, graalExtension.getAgent()).get().equals("disabled")) {
testOptions.getConfigurationFileDirectories().from(getProcessedAgentOutputFilesDirectory(project, 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());
});
}
/**
* Returns a provider which prefers the CLI arguments over the configured
* extension value.
*/
private static Provider<String> agentProperty(Project project, AgentOptions options) {
return project.getProviders()
.gradleProperty(AGENT_PROPERTY)
.forUseAtConfigurationTime()
.map(v -> {
if (!v.isEmpty()) {
return v;
}
return options.getDefaultMode().get();
})
.orElse(project.provider(() -> "disabled"));
}
@SuppressWarnings("UnstableApiUsage")
private static void registerServiceProvider(Project project, Provider<NativeImageService> nativeImageServiceProvider) {
project.getTasks()
.withType(BuildNativeImageTask.class)
.configureEach(task -> {
task.usesService(nativeImageServiceProvider);
task.getService().set(nativeImageServiceProvider);
});
}
private static String capitalize(String name) {
if (name.length() > 0) {
return name.substring(0, 1).toUpperCase(Locale.US) + name.substring(1);
}
return name;
}
@SuppressWarnings("unchecked")
private static NativeConfigurations createNativeConfigurations(Project project, String binaryName, String baseClasspathConfigurationName) {
ConfigurationContainer configurations = project.getConfigurations();
String prefix = "main".equals(binaryName) ? "" : capitalize(binaryName);
Configuration baseClasspath = configurations.getByName(baseClasspathConfigurationName);
Configuration compileOnly = configurations.create("nativeImage" + prefix + "CompileOnly", c -> {
c.setCanBeResolved(false);
c.setCanBeConsumed(false);
});
Configuration classpath = configurations.create("nativeImage" + prefix + "Classpath", c -> {
c.setCanBeConsumed(false);
c.setCanBeResolved(true);
c.extendsFrom(compileOnly);
baseClasspath.getExtendsFrom().forEach(c::extendsFrom);
c.attributes(attrs -> {
AttributeContainer baseAttributes = baseClasspath.getAttributes();
for (Attribute<?> attribute : baseAttributes.keySet()) {
Attribute<Object> attr = (Attribute<Object>) attribute;
Object value = baseAttributes.getAttribute(attr);
attrs.attribute(attr, value);
}
});
});
compileOnly.getDependencies().add(project.getDependencies().create(project));
return new NativeConfigurations(compileOnly, classpath);
}
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;
}
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;
}
private static String postProcessTaskName(String taskName) {
return PROCESS_AGENT_RESOURCES_TASK_NAME_PREFIX + capitalize(taskName) + PROCESS_AGENT_RESOURCES_TASK_NAME_SUFFIX;
}
private static List<String> agentSessionDirectories(Directory outputDirectory) {
return Arrays.stream(outputDirectory.getAsFile().listFiles(file -> file.isDirectory() && file.getName().startsWith("session-"))).map(File::getAbsolutePath).collect(Collectors.toList());
}
private void configureAgent(Project project,
Provider<AgentConfiguration> agentConfiguration,
Provider<Boolean> disableToolchainDetection,
ExecOperations execOperations,
FileSystemOperations fileOperations,
Task taskToInstrument,
JavaForkOptions javaForkOptions) {
logger.lifecycle("Instrumenting task: " + taskToInstrument.getName());
AgentCommandLineProvider cliProvider = project.getObjects().newInstance(AgentCommandLineProvider.class);
cliProvider.getInputFiles().from(agentConfiguration.map(AgentConfiguration::getAgentFiles));
cliProvider.getEnabled().set(agentConfiguration.map(AgentConfiguration::isEnabled));
Provider<Directory> outputDir = AgentConfigurationFactory.getAgentOutputDirectoryForTask(project, taskToInstrument.getName());
Provider<Boolean> isMergingEnabled = agentConfiguration.map(AgentConfiguration::isEnabled);
Provider<AgentMode> agentModeProvider = agentConfiguration.map(AgentConfiguration::getAgentMode);
Provider<List<String>> mergeOutputDirs = outputDir.map(dir -> Collections.singletonList(dir.getAsFile().getAbsolutePath()));
Provider<List<String>> mergeInputDirs = outputDir.map(NativeImagePlugin::agentSessionDirectories);
cliProvider.getOutputDirectory().set(outputDir);
cliProvider.getAgentOptions().set(agentConfiguration.map(AgentConfiguration::getAgentCommandLine));
javaForkOptions.getJvmArgumentProviders().add(cliProvider);
taskToInstrument.doLast(new MergeAgentFilesAction(
isMergingEnabled,
agentModeProvider,
project.provider(() -> false),
project,
graalvmHomeProvider(project.getProviders()),
mergeInputDirs,
mergeOutputDirs,
disableToolchainDetection,
execOperations,
project.getLogger()));
taskToInstrument.doLast(new CleanupAgentFilesAction(mergeInputDirs, fileOperations));
Provider<Directory> processedOutputDirectory = getProcessedAgentOutputFilesDirectory(project, taskToInstrument);
taskToInstrument.doLast(new ProcessGeneratedGraalResourceFilesAction(
outputDir,
processedOutputDirectory,
Arrays.asList("org.gradle.", "java.", "org.junit.")));
}
private static Provider<Directory> getProcessedAgentOutputFilesDirectory(Project project, Task taskToInstrument) {
String name = postProcessTaskName(taskToInstrument.getName());
return project.getLayout().getBuildDirectory().dir("native/processed/agent/" + name);
}
private static void injectTestPluginDependencies(Project project, Property<Boolean> testSupportEnabled) {
project.afterEvaluate(p -> {
if (testSupportEnabled.get()) {
project.getDependencies().add("testImplementation", "org.graalvm.buildtools:junit-platform-native:"
+ VersionInfo.JUNIT_PLATFORM_NATIVE_VERSION);
}
});
}
private static final class CleanupTestIdsDirectory implements Action<Task> {
private final DirectoryProperty directory;
private CleanupTestIdsDirectory(DirectoryProperty directory) {
this.directory = directory;
}
@Override
public void execute(Task task) {
File dir = directory.getAsFile().get();
if (dir.exists()) {
GFileUtils.deleteDirectory(dir);
}
}
}
public abstract static class TrackingDirectorySystemPropertyProvider implements CommandLineArgumentProvider {
@OutputDirectory
public abstract DirectoryProperty getDirectory();
/**
* The arguments which will be provided to the process.
*/
@Override
public Iterable<String> asArguments() {
return Collections.singleton("-D" + JUNIT_PLATFORM_LISTENERS_UID_TRACKING_OUTPUT_DIR + "=" + getDirectory().getAsFile().get().getAbsolutePath());
}
}
}