use of org.graalvm.buildtools.gradle.dsl.NativeImageOptions in project micronaut-gradle-plugin by micronaut-projects.
the class NativeImageDockerfile method buildActualCommandLine.
protected List<String> buildActualCommandLine(Provider<String> executable, DockerBuildStrategy buildStrategy, BaseImageForBuildStrategyResolver imageResolver) {
NativeImageOptions options = newNativeImageOptions("actualDockerOptions");
prepareNativeImageOptions(options);
if (buildStrategy == DockerBuildStrategy.ORACLE_FUNCTION) {
options.getMainClass().set("com.fnproject.fn.runtime.EntryPoint");
options.getBuildArgs().add("--report-unsupported-elements-at-runtime");
} else if (buildStrategy == DockerBuildStrategy.LAMBDA) {
JavaApplication javaApplication = getProject().getExtensions().getByType(JavaApplication.class);
if (!javaApplication.getMainClass().isPresent()) {
options.getMainClass().set("io.micronaut.function.aws.runtime.MicronautLambdaRuntime");
}
if (!options.getMainClass().isPresent()) {
options.getMainClass().set("io.micronaut.function.aws.runtime.MicronautLambdaRuntime");
}
}
List<String> commandLine = new ArrayList<>();
commandLine.add("native-image");
List<String> args = buildNativeImageCommandLineArgs(executable, options);
commandLine.addAll(args);
String baseImage = imageResolver.get();
// add --static if image is scratch
if (baseImage.equalsIgnoreCase("scratch") && !commandLine.contains("--static")) {
commandLine.add("--static");
}
// to build a "mostly" static native-image if image when using distroless
if (baseImage.contains("distroless") && !commandLine.contains("-H:+StaticExecutableWithDynamicLibC")) {
commandLine.add("-H:+StaticExecutableWithDynamicLibC");
}
return commandLine;
}
use of org.graalvm.buildtools.gradle.dsl.NativeImageOptions in project native-build-tools by graalvm.
the class NativeImagePlugin method configureAgent.
private static void configureAgent(Project project, Map<String, Provider<Boolean>> agents, Provider<Boolean> disableToolchainDetection, NativeImageOptions nativeImageOptions, ExecOperations execOperations, FileSystemOperations fileOperations) {
String postProcessTaskName = PROCESS_AGENT_RESOURCES_TASK_NAME_PREFIX + capitalize(nativeImageOptions.getName()) + PROCESS_AGENT_RESOURCES_TASK_NAME_SUFFIX;
TaskProvider<ProcessGeneratedGraalResourceFiles> postProcessingTask = registerProcessAgentFilesTask(project, postProcessTaskName);
TaskProvider<? extends JavaForkOptions> instrumentedTask = nativeImageOptions.getAgent().getInstrumentedTask().get();
AgentCommandLineProvider cliProvider = project.getObjects().newInstance(AgentCommandLineProvider.class);
Provider<Boolean> agent = agents.get(nativeImageOptions.getName());
cliProvider.getEnabled().set(agent);
Provider<Directory> outputDir = project.getLayout().getBuildDirectory().dir(AGENT_OUTPUT_FOLDER + "/" + instrumentedTask.getName());
cliProvider.getOutputDirectory().set(outputDir);
cliProvider.getAgentOptions().set(nativeImageOptions.getAgent().getOptions());
instrumentedTask.get().getJvmArgumentProviders().add(cliProvider);
instrumentedTask.configure(task -> task.doLast(new MergeAgentFiles(agent, graalvmHomeProvider(project.getProviders()), outputDir, disableToolchainDetection, nativeImageOptions, execOperations, fileOperations, project.getLogger())));
// Gradle won't let us configure from configure so we have to eagerly create the post-processing task :(
postProcessingTask.get().getGeneratedFilesDir().set(instrumentedTask.map(t -> outputDir.get()));
// We can't set from(postProcessingTask) directly, otherwise a task
// dependency would be introduced even if the agent is not enabled.
// We should be able to write this:
// nativeImageOptions.getConfigurationFileDirectories().from(
// agent.map(enabled -> enabled ? postProcessingTask : project.files())
// )
// but Gradle won't track the postProcessingTask dependency so we have to write this:
ConfigurableFileCollection files = project.getObjects().fileCollection();
files.from(agent.map(enabled -> enabled ? postProcessingTask : project.files()));
files.builtBy((Callable<Task>) () -> agent.get() ? postProcessingTask.get() : null);
nativeImageOptions.getConfigurationFileDirectories().from(files);
}
use of org.graalvm.buildtools.gradle.dsl.NativeImageOptions in project native-build-tools by graalvm.
the class NativeImageCommandLineProvider method asArguments.
@Override
public List<String> asArguments() {
NativeImageOptions options = getOptions().get();
List<String> cliArgs = new ArrayList<>(20);
cliArgs.add("-cp");
String classpathString = buildClasspathString(options);
cliArgs.add(classpathString);
appendBooleanOption(cliArgs, options.getDebug(), "-H:GenerateDebugInfo=1");
appendBooleanOption(cliArgs, options.getFallback().map(NEGATE), "--no-fallback");
appendBooleanOption(cliArgs, options.getVerbose(), "--verbose");
appendBooleanOption(cliArgs, options.getSharedLibrary(), "--shared");
if (getOutputDirectory().isPresent()) {
cliArgs.add("-H:Path=" + getOutputDirectory().get());
}
cliArgs.add("-H:Name=" + getExecutableName().get());
options.getSystemProperties().get().forEach((n, v) -> {
if (v != null) {
cliArgs.add("-D" + n + "=\"" + v + "\"");
}
});
options.getJvmArgs().get().forEach(jvmArg -> cliArgs.add("-J" + jvmArg));
String configFiles = options.getConfigurationFileDirectories().getElements().get().stream().map(FileSystemLocation::getAsFile).map(File::getAbsolutePath).collect(Collectors.joining(","));
if (!configFiles.isEmpty()) {
cliArgs.add("-H:ConfigurationFileDirectories=" + configFiles);
}
if (getAgentEnabled().get()) {
cliArgs.add("--allow-incomplete-classpath");
}
if (options.getMainClass().isPresent()) {
cliArgs.add("-H:Class=" + options.getMainClass().get());
}
cliArgs.addAll(options.getBuildArgs().get());
if (useArgFile.getOrElse(true)) {
return NativeImageUtils.convertToArgsFile(cliArgs);
}
return Collections.unmodifiableList(cliArgs);
}
use of org.graalvm.buildtools.gradle.dsl.NativeImageOptions in project native-build-tools by graalvm.
the class BuildNativeImageTask method exec.
@TaskAction
@SuppressWarnings("ConstantConditions")
public void exec() {
List<String> args = buildActualCommandLineArgs();
NativeImageOptions options = getOptions().get();
GraalVMLogger logger = GraalVMLogger.of(getLogger());
if (options.getVerbose().get()) {
logger.lifecycle("Args are: " + args);
}
File executablePath = NativeImageExecutableLocator.findNativeImageExecutable(options, getDisableToolchainDetection(), getGraalVMHome(), getExecOperations(), logger);
logger.lifecycle("Using executable path: " + executablePath);
String executable = executablePath.getAbsolutePath();
File outputDir = getOutputDirectory().getAsFile().get();
if (outputDir.isDirectory() || outputDir.mkdirs()) {
getExecOperations().exec(spec -> {
spec.setWorkingDir(getWorkingDirectory());
if (getTestListDirectory().isPresent()) {
NativeImagePlugin.TrackingDirectorySystemPropertyProvider directoryProvider = getObjects().newInstance(NativeImagePlugin.TrackingDirectorySystemPropertyProvider.class);
directoryProvider.getDirectory().set(getTestListDirectory());
spec.getArgumentProviders().add(directoryProvider);
}
spec.args(args);
getService().get();
spec.setExecutable(executable);
});
logger.lifecycle("Native Image written to: " + outputDir);
}
}
use of org.graalvm.buildtools.gradle.dsl.NativeImageOptions in project native-build-tools by graalvm.
the class NativeImagePlugin method configureJvmReachabilityConfigurationDirectories.
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);
}));
}
Aggregations