Skip to content
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

Use argument file to call AOT tasks #398

Merged
merged 1 commit into from
Mar 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@

import javax.inject.Inject;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -69,34 +72,49 @@ protected void onSuccess(File outputDir) {
}

@TaskAction
public final void execute() {
public final void execute() throws IOException {
File outputDir = getOutputDirectory().getAsFile().get();
getFileOperations().delete(outputDir);
ExecResult javaexec = getExecOperations().javaexec(spec -> {
FileCollection classpath = getOptimizerClasspath().plus(getClasspath());
spec.setClasspath(classpath);
spec.getMainClass().set("io.micronaut.aot.cli.Main");
List<String> args = new ArrayList<>(Arrays.asList(
"--classpath", getClasspath().getAsPath(),
"--runtime", getTargetRuntime().get().name().toUpperCase(),
"--package", getTargetPackage().get()
));
maybeAddOptimizerClasspath(args, getClasspath());
configureExtraArguments(args);
spec.args(args);
getLogger().info("Running AOT optimizer with parameters: {}", args);
if (getDebug().get()) {
getLogger().info("Running with debug enabled");
spec.jvmArgs("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005");
File argFile = File.createTempFile("aot", "args");
try {
ExecResult javaexec = getExecOperations().javaexec(spec -> {
FileCollection classpath = getOptimizerClasspath().plus(getClasspath());
spec.setClasspath(classpath);
spec.getMainClass().set("io.micronaut.aot.cli.Main");
List<String> args = new ArrayList<>(Arrays.asList(
"--classpath", getClasspath().getAsPath(),
"--runtime", getTargetRuntime().get().name().toUpperCase(),
"--package", getTargetPackage().get()
));
maybeAddOptimizerClasspath(args, getClasspath());
configureExtraArguments(args);
boolean useArgFile = true;
try (PrintWriter wrt = new PrintWriter(new FileWriter(argFile))) {
args.forEach(wrt::println);
} catch (IOException e) {
useArgFile = false;
}
if (useArgFile) {
spec.args("@" + argFile.getAbsolutePath());
} else {
spec.args(args);
}
getLogger().info("Running AOT optimizer {} with parameters: {}", useArgFile ? "using arg file" : "directly", args);
if (getDebug().get()) {
getLogger().info("Running with debug enabled");
spec.jvmArgs("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005");
}
if (getEnvironmentVariables().isPresent()) {
spec.environment(getEnvironmentVariables().get());
}
});
if (javaexec.getExitValue() != 0) {
throw new GradleException("AOT analysis failed");
}
if (getEnvironmentVariables().isPresent()) {
spec.environment(getEnvironmentVariables().get());
}
});
if (javaexec.getExitValue() != 0) {
throw new GradleException("AOT analysis failed");
} finally {
onSuccess(outputDir);
argFile.delete();
}
onSuccess(outputDir);
}

private void maybeAddOptimizerClasspath(List<String> args, ConfigurableFileCollection classpath) {
Expand Down