Gradle: report build progress from build tasks

This commit is contained in:
Alexey Andreev 2023-10-10 19:01:15 +02:00
parent bcc2c0ff35
commit d33c491a33
2 changed files with 64 additions and 3 deletions

View File

@ -364,14 +364,14 @@ public class TeaVM implements TeaVMHost, ServiceRepository {
*/
public void build(BuildTarget buildTarget, String outputName) {
// Check dependencies
reportPhase(TeaVMPhase.DEPENDENCY_ANALYSIS, lastKnownClasses > 0 ? lastKnownClasses : 1);
reportPhase(TeaVMPhase.DEPENDENCY_ANALYSIS, lastKnownClasses);
if (wasCancelled()) {
return;
}
dependencyAnalyzer.setAsyncSupported(target.isAsyncSupported());
dependencyAnalyzer.setInterruptor(() -> {
int progress = lastKnownClasses > 0 ? dependencyAnalyzer.getReachableClasses().size() : 0;
int progress = dependencyAnalyzer.getReachableClasses().size();
cancelled |= progressListener.progressReached(progress) != TeaVMProgressFeedback.CONTINUE;
return !cancelled;
});
@ -417,7 +417,7 @@ public class TeaVM implements TeaVMHost, ServiceRepository {
// Render
try {
if (!isLazy) {
compileProgressReportStart = 500;
compileProgressReportStart = 0;
compileProgressReportLimit = 1000;
}
target.emit(classSet, buildTarget, outputName);

View File

@ -21,6 +21,7 @@ import java.rmi.NotBoundException;
import java.rmi.registry.LocateRegistry;
import java.util.ArrayList;
import java.util.Properties;
import javax.inject.Inject;
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.file.ConfigurableFileCollection;
@ -33,6 +34,8 @@ import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.TaskAction;
import org.gradle.internal.logging.progress.ProgressLogger;
import org.gradle.internal.logging.progress.ProgressLoggerFactory;
import org.teavm.gradle.api.OptimizationLevel;
import org.teavm.tooling.TeaVMProblemRenderer;
import org.teavm.tooling.builder.BuildException;
@ -43,6 +46,9 @@ import org.teavm.tooling.daemon.BuildDaemon;
import org.teavm.tooling.daemon.DaemonLog;
import org.teavm.tooling.daemon.RemoteBuildService;
import org.teavm.vm.TeaVMOptimizationLevel;
import org.teavm.vm.TeaVMPhase;
import org.teavm.vm.TeaVMProgressFeedback;
import org.teavm.vm.TeaVMProgressListener;
public abstract class TeaVMTask extends DefaultTask {
public TeaVMTask() {
@ -102,6 +108,9 @@ public abstract class TeaVMTask extends DefaultTask {
@Internal
public abstract Property<Integer> getDaemonDebugPort();
@Inject
protected abstract ProgressLoggerFactory getProgressLoggerFactory();
@TaskAction
public void execute() throws BuildException, IOException, NotBoundException {
if (getOutOfProcess().get()) {
@ -149,6 +158,7 @@ public abstract class TeaVMTask extends DefaultTask {
}
builder.setProperties(properties);
}
builder.setProgressListener(createProgressListener());
setupBuilder(builder);
var result = builder.build();
TeaVMProblemRenderer.describeProblems(result.getCallGraph(), result.getProblems(), toolLog);
@ -157,6 +167,57 @@ public abstract class TeaVMTask extends DefaultTask {
}
}
private TeaVMProgressListener createProgressListener() {
return new TeaVMProgressListener() {
private ProgressLogger currentLogger;
private TeaVMPhase phase;
private int phaseLimit;
@Override
public TeaVMProgressFeedback phaseStarted(TeaVMPhase phase, int count) {
this.phase = phase;
this.phaseLimit = count;
if (currentLogger != null) {
currentLogger.completed();
currentLogger = null;
}
switch (phase) {
case DEPENDENCY_ANALYSIS:
currentLogger = getProgressLoggerFactory().newOperation(getClass());
currentLogger.start("Dependency analysis", getName());
break;
case COMPILING:
currentLogger = getProgressLoggerFactory().newOperation(getClass());
currentLogger.start("Compilation", getName());
break;
}
return TeaVMProgressFeedback.CONTINUE;
}
@Override
public TeaVMProgressFeedback progressReached(int progress) {
switch (phase) {
case DEPENDENCY_ANALYSIS:
if (phaseLimit == 0) {
currentLogger.progress(progress + " classes reached");
} else {
currentLogger.progress(showPercent(progress) + " %");
}
break;
case COMPILING:
currentLogger.progress(showPercent(progress) + " %");
break;
}
return TeaVMProgressFeedback.CONTINUE;
}
private int showPercent(int value) {
return Math.min(100, (value * 1000 / phaseLimit + 5) / 10);
}
};
}
private static TeaVMOptimizationLevel map(OptimizationLevel level) {
switch (level) {
case AGGRESSIVE: