Skip to content
Open
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ WORKDIR /app
COPY --chown=gradle:gradle . /app
RUN gradle -i --stacktrace clean build

FROM ghcr.io/graalvm/native-image-community:25.0.2@sha256:0d936f32bb8acb5bc60c41b33e05f064d7a6aaf36b726538296c54949bd4a3c0 AS native-javac

COPY --from=build /app/build/libs/java-test-runner.jar /tmp/java-test-runner.jar
COPY aot-training /tmp/aot-training
COPY bin/build-native-javac.sh /tmp/build-native-javac.sh
RUN sh /tmp/build-native-javac.sh

FROM eclipse-temurin:27_35-jdk@sha256:473d02d477a8152a05dc42fde10b965754f1863d45e586a97763ef298965c48b

WORKDIR /opt/test-runner
Expand All @@ -13,4 +20,8 @@ COPY bin/create-aot-cache.sh bin/create-aot-cache.sh
COPY aot-training aot-training
RUN sh bin/create-aot-cache.sh

# The native javac is added after the AOT cache is created. That way the training run
# compiles inside the JVM, and the cache holds javac's classes for runs that fall back to it.
COPY --from=native-javac /native-javac native-javac

ENTRYPOINT ["sh", "/opt/test-runner/bin/run.sh"]
43 changes: 43 additions & 0 deletions bin/build-native-javac.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env sh

# Synopsis:
# Build javac as a native executable with GraalVM Native Image.
# This runs once, in the native-javac stage of the Docker build.

# Output:
# Writes /native-javac/javac and /native-javac/lib/ct.sym.
# The Dockerfile copies them to /opt/test-runner/native-javac.

# A native javac starts in a few milliseconds and runs as compiled machine code from
# the start. The javac inside the JVM has to be loaded and warmed up by the JIT on
# every run, which takes most of a second.
#
# A native executable can't load JDK classes from lib/modules, so the native javac
# compiles against the class signatures in lib/ct.sym, selected with --release.
# ExerciseCompiler passes the same --release when it runs the native javac.

set -e

release=25
config_folder="/tmp/native-javac-config"
classes_folder="/tmp/native-javac-classes"

# javac looks up some classes and resources by name. GraalVM's tracing agent records
# them while javac compiles the sample exercise, so the native executable includes them.
mkdir -p "${classes_folder}" /native-javac
java -agentlib:native-image-agent=config-output-dir="${config_folder}" \
-m jdk.compiler/com.sun.tools.javac.Main \
--release "${release}" \
-classpath /tmp/java-test-runner.jar \
-d "${classes_folder}" \
$(find /tmp/aot-training -name '*.java')

native-image --no-fallback \
-H:+UnlockExperimentalVMOptions \
-H:ConfigurationFileDirectories="${config_folder}" \
-H:IncludeResourceBundles=com.sun.tools.javac.resources.javac,com.sun.tools.javac.resources.compiler,com.sun.tools.javac.resources.ct \
-m jdk.compiler/com.sun.tools.javac.Main \
-o /native-javac/javac

mkdir -p /native-javac/lib
cp "${JAVA_HOME}/lib/ct.sym" /native-javac/lib/ct.sym
12 changes: 6 additions & 6 deletions src/main/java/com/exercism/TestRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ private void run() throws IOException {
var sourceFiles = resolveSourceFiles();
var testFiles = resolveTestFiles();

for (File testFile : testFiles) {
testParser.parse(testFile);
}

var filesToCompile = Stream.concat(sourceFiles.stream(), testFiles.stream()).toList();

Report report;
try (var compiler = new ExerciseCompiler(slug)) {
compiler.compile(filesToCompile);
try (var compiler = new ExerciseCompiler(slug, filesToCompile)) {
for (File testFile : testFiles) {
testParser.parse(testFile);
}

compiler.compile();
testRunner.test(compiler.getClassLoader(), compiler.getClasspathRoots());
report = ReportGenerator.generate(testRunner.getTestDetails(), testParser.buildTestCodeMap());
} catch (ExerciseCompilationException e) {
Expand Down
66 changes: 62 additions & 4 deletions src/main/java/com/exercism/compiler/ExerciseCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,45 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

public class ExerciseCompiler implements Closeable, Flushable {
// The native javac is built when the Docker image is built (see bin/build-native-javac.sh).
// It compiles against the Java API of the release given here, read from lib/ct.sym.
private static final Path NATIVE_JAVAC_HOME = Path.of("/opt/test-runner/native-javac");
private static final Path NATIVE_JAVAC = NATIVE_JAVAC_HOME.resolve("javac");
private static final String NATIVE_JAVAC_RELEASE = "25";

private final JavaCompiler compiler;
private final StandardJavaFileManager fileManager;
private final DiagnosticCollector<JavaFileObject> diagnosticCollector;
private final Path outputDir;
private final Collection<File> filesToCompile;
private final Process nativeCompilation;

public ExerciseCompiler(String exerciseSlug, Collection<File> filesToCompile) throws IOException {
this.filesToCompile = filesToCompile;
this.outputDir = Files.createTempDirectory(String.format("java-test-runner-%s-", exerciseSlug));
this.nativeCompilation = startNativeCompilation();

public ExerciseCompiler(String exerciseSlug) throws IOException {
this.compiler = ToolProvider.getSystemJavaCompiler();
this.diagnosticCollector = new DiagnosticCollector<>();
this.fileManager = compiler.getStandardFileManager(this.diagnosticCollector, null, null);

var outputDir = Files.createTempDirectory(String.format("java-test-runner-%s-", exerciseSlug));
this.fileManager.setLocation(StandardLocation.CLASS_OUTPUT, List.of(outputDir.toFile()));
}

public void compile(final Collection<File> filesToCompile) throws ExerciseCompilationException {
// The native javac starts compiling straight away, in its own process, while the
// test runner parses the test files. If it fails for any reason, compile() will
// compile the files again with the JVM's own javac. That keeps compile errors
// exactly as before, and handles code that needs a newer Java release.
public void compile() throws ExerciseCompilationException {
if (nativeCompilationSucceeded()) {
return;
}

var sources = fileManager.getJavaFileObjectsFromFiles(filesToCompile);
var task = compiler.getTask(null, fileManager, diagnosticCollector, null, null, sources);
if (task.call()) {
Expand All @@ -41,6 +61,44 @@ public void compile(final Collection<File> filesToCompile) throws ExerciseCompil
throw new ExerciseCompilationException(messageBuilder.toString());
}

private Process startNativeCompilation() {
if (!Files.isExecutable(NATIVE_JAVAC)) {
return null;
}

var command = new ArrayList<>(List.of(
NATIVE_JAVAC.toString(),
"-Djava.home=" + NATIVE_JAVAC_HOME,
"--release", NATIVE_JAVAC_RELEASE,
"-classpath", System.getProperty("java.class.path"),
"-d", outputDir.toString()));
for (var file : filesToCompile) {
command.add(file.getPath());
}

try {
return new ProcessBuilder(command)
.redirectErrorStream(true)
.redirectOutput(ProcessBuilder.Redirect.DISCARD)
.start();
} catch (IOException e) {
return null;
}
}

private boolean nativeCompilationSucceeded() {
if (nativeCompilation == null) {
return false;
}

try {
return nativeCompilation.waitFor() == 0;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return false;
}
}

public ClassLoader getClassLoader() {
return this.fileManager.getClassLoader(StandardLocation.CLASS_OUTPUT);
}
Expand Down