Skip to content

Commit

Permalink
Use argument file to call AOT tasks (#398)
Browse files Browse the repository at this point in the history
Once again, we're facing the fact that despite living in 2022,
Windows is still incapable of providing sensible defaults: invoking
a CLI tool with a long classpath will fail. As a consequence, we
have to invoke the AOT CLI tools with an argument file to
workaround the problem.

Fixes #397
  • Loading branch information
melix authored Mar 8, 2022
1 parent 113b98b commit eaa95ec
Showing 1 changed file with 42 additions and 24 deletions.
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

0 comments on commit eaa95ec

Please sign in to comment.